2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "ac_shader_util.h"
32 #include "aco_builder.h"
33 #include "aco_interface.h"
34 #include "aco_instruction_selection_setup.cpp"
35 #include "util/fast_idiv_by_const.h"
40 class loop_info_RAII
{
42 unsigned header_idx_old
;
44 bool divergent_cont_old
;
45 bool divergent_branch_old
;
46 bool divergent_if_old
;
49 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
51 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
52 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
53 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
54 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
56 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
57 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
58 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
59 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
60 ctx
->cf_info
.parent_if
.is_divergent
= false;
61 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
66 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
67 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
68 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
70 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
71 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
72 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
73 ctx
->cf_info
.exec_potentially_empty
= false;
81 bool exec_potentially_empty_old
;
85 bool then_branch_divergent
;
90 static void visit_cf_list(struct isel_context
*ctx
,
91 struct exec_list
*list
);
93 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
95 succ
->logical_preds
.emplace_back(pred_idx
);
99 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
101 succ
->linear_preds
.emplace_back(pred_idx
);
104 static void add_edge(unsigned pred_idx
, Block
*succ
)
106 add_logical_edge(pred_idx
, succ
);
107 add_linear_edge(pred_idx
, succ
);
110 static void append_logical_start(Block
*b
)
112 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
115 static void append_logical_end(Block
*b
)
117 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
120 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
122 assert(ctx
->allocated
[def
->index
].id());
123 return ctx
->allocated
[def
->index
];
126 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
128 Builder
bld(ctx
->program
, ctx
->block
);
131 dst
= bld
.tmp(src
.regClass());
133 assert(src
.size() == dst
.size());
135 if (ctx
->stage
!= fragment_fs
) {
139 bld
.copy(Definition(dst
), src
);
143 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
144 ctx
->program
->needs_wqm
|= program_needs_wqm
;
148 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
150 if (index
.regClass() == s1
)
151 return bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), data
, index
);
153 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
155 /* Currently not implemented on GFX6-7 */
156 assert(ctx
->options
->chip_class
>= GFX8
);
158 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->program
->wave_size
== 32) {
159 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
162 /* GFX10, wave64 mode:
163 * The bpermute instruction is limited to half-wave operation, which means that it can't
164 * properly support subgroup shuffle like older generations (or wave32 mode), so we
167 if (!ctx
->has_gfx10_wave64_bpermute
) {
168 ctx
->has_gfx10_wave64_bpermute
= true;
169 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
170 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
173 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u));
174 lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), lane_id
);
175 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
176 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
177 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
, vcc
), lane_is_hi
, index_is_hi
);
179 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
180 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
183 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
185 if (val
.type() == RegType::sgpr
) {
186 Builder
bld(ctx
->program
, ctx
->block
);
187 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
189 assert(val
.type() == RegType::vgpr
);
193 //assumes a != 0xffffffff
194 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
197 Builder
bld(ctx
->program
, ctx
->block
);
199 if (util_is_power_of_two_or_zero(b
)) {
200 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
204 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
206 assert(info
.multiplier
<= 0xffffffff);
208 bool pre_shift
= info
.pre_shift
!= 0;
209 bool increment
= info
.increment
!= 0;
210 bool multiply
= true;
211 bool post_shift
= info
.post_shift
!= 0;
213 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
214 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
218 Temp pre_shift_dst
= a
;
220 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
221 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
224 Temp increment_dst
= pre_shift_dst
;
226 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
227 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
230 Temp multiply_dst
= increment_dst
;
232 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
233 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
234 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
238 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
242 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
244 Builder
bld(ctx
->program
, ctx
->block
);
245 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
249 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
251 /* no need to extract the whole vector */
252 if (src
.regClass() == dst_rc
) {
256 assert(src
.size() > idx
);
257 Builder
bld(ctx
->program
, ctx
->block
);
258 auto it
= ctx
->allocated_vec
.find(src
.id());
259 /* the size check needs to be early because elements other than 0 may be garbage */
260 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
261 if (it
->second
[idx
].regClass() == dst_rc
) {
262 return it
->second
[idx
];
264 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
265 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
266 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
270 if (src
.size() == dst_rc
.size()) {
272 return bld
.copy(bld
.def(dst_rc
), src
);
274 Temp dst
= bld
.tmp(dst_rc
);
275 emit_extract_vector(ctx
, src
, idx
, dst
);
280 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
282 if (num_components
== 1)
284 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
286 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
287 split
->operands
[0] = Operand(vec_src
);
288 std::array
<Temp
,4> elems
;
289 for (unsigned i
= 0; i
< num_components
; i
++) {
290 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
291 split
->definitions
[i
] = Definition(elems
[i
]);
293 ctx
->block
->instructions
.emplace_back(std::move(split
));
294 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
297 /* This vector expansion uses a mask to determine which elements in the new vector
298 * come from the original vector. The other elements are undefined. */
299 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
301 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
306 Builder
bld(ctx
->program
, ctx
->block
);
307 if (num_components
== 1) {
308 if (dst
.type() == RegType::sgpr
)
309 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
311 bld
.copy(Definition(dst
), vec_src
);
315 unsigned component_size
= dst
.size() / num_components
;
316 std::array
<Temp
,4> elems
;
318 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
319 vec
->definitions
[0] = Definition(dst
);
321 for (unsigned i
= 0; i
< num_components
; i
++) {
322 if (mask
& (1 << i
)) {
323 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
324 if (dst
.type() == RegType::sgpr
)
325 src
= bld
.as_uniform(src
);
326 vec
->operands
[i
] = Operand(src
);
328 vec
->operands
[i
] = Operand(0u);
330 elems
[i
] = vec
->operands
[i
].getTemp();
332 ctx
->block
->instructions
.emplace_back(std::move(vec
));
333 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
336 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
338 Builder
bld(ctx
->program
, ctx
->block
);
342 assert(val
.regClass() == s1
);
343 assert(dst
.regClass() == s2
);
345 return bld
.sop2(aco_opcode::s_cselect_b64
, bld
.hint_vcc(Definition(dst
)), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
348 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
350 Builder
bld(ctx
->program
, ctx
->block
);
354 assert(val
.regClass() == s2
);
355 assert(dst
.regClass() == s1
);
357 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
358 Temp tmp
= bld
.tmp(s1
);
359 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, s2
));
360 return emit_wqm(ctx
, tmp
, dst
);
363 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
365 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
366 return get_ssa_temp(ctx
, src
.src
.ssa
);
368 if (src
.src
.ssa
->num_components
== size
) {
369 bool identity_swizzle
= true;
370 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
371 if (src
.swizzle
[i
] != i
)
372 identity_swizzle
= false;
374 if (identity_swizzle
)
375 return get_ssa_temp(ctx
, src
.src
.ssa
);
378 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
379 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
380 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
381 assert(vec
.size() % elem_size
== 0);
383 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
385 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
388 std::array
<Temp
,4> elems
;
389 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
390 for (unsigned i
= 0; i
< size
; ++i
) {
391 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
392 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
394 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
395 vec_instr
->definitions
[0] = Definition(dst
);
396 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
397 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
402 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
406 Builder
bld(ctx
->program
, ctx
->block
);
407 if (ptr
.type() == RegType::vgpr
)
408 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
409 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
410 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
413 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
415 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
416 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
417 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
418 sop2
->definitions
[0] = Definition(dst
);
420 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
421 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
424 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
426 Builder
bld(ctx
->program
, ctx
->block
);
427 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
428 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
429 if (src1
.type() == RegType::sgpr
) {
430 if (commutative
&& src0
.type() == RegType::vgpr
) {
434 } else if (src0
.type() == RegType::vgpr
&&
435 op
!= aco_opcode::v_madmk_f32
&&
436 op
!= aco_opcode::v_madak_f32
&&
437 op
!= aco_opcode::v_madmk_f16
&&
438 op
!= aco_opcode::v_madak_f16
) {
439 /* If the instruction is not commutative, we emit a VOP3A instruction */
440 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
443 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
446 bld
.vop2(op
, Definition(dst
), src0
, src1
);
449 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
451 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
452 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
453 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
455 /* ensure that the instruction has at most 1 sgpr operand
456 * The optimizer will inline constants for us */
457 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
458 src0
= as_vgpr(ctx
, src0
);
459 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
460 src1
= as_vgpr(ctx
, src1
);
461 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
462 src2
= as_vgpr(ctx
, src2
);
464 Builder
bld(ctx
->program
, ctx
->block
);
465 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
468 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
470 Builder
bld(ctx
->program
, ctx
->block
);
471 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
474 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
476 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
477 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
478 aco_ptr
<Instruction
> vopc
;
479 if (src1
.type() == RegType::sgpr
) {
480 if (src0
.type() == RegType::vgpr
) {
481 /* to swap the operands, we might also have to change the opcode */
483 case aco_opcode::v_cmp_lt_f32
:
484 op
= aco_opcode::v_cmp_gt_f32
;
486 case aco_opcode::v_cmp_ge_f32
:
487 op
= aco_opcode::v_cmp_le_f32
;
489 case aco_opcode::v_cmp_lt_i32
:
490 op
= aco_opcode::v_cmp_gt_i32
;
492 case aco_opcode::v_cmp_ge_i32
:
493 op
= aco_opcode::v_cmp_le_i32
;
495 case aco_opcode::v_cmp_lt_u32
:
496 op
= aco_opcode::v_cmp_gt_u32
;
498 case aco_opcode::v_cmp_ge_u32
:
499 op
= aco_opcode::v_cmp_le_u32
;
501 case aco_opcode::v_cmp_lt_f64
:
502 op
= aco_opcode::v_cmp_gt_f64
;
504 case aco_opcode::v_cmp_ge_f64
:
505 op
= aco_opcode::v_cmp_le_f64
;
507 case aco_opcode::v_cmp_lt_i64
:
508 op
= aco_opcode::v_cmp_gt_i64
;
510 case aco_opcode::v_cmp_ge_i64
:
511 op
= aco_opcode::v_cmp_le_i64
;
513 case aco_opcode::v_cmp_lt_u64
:
514 op
= aco_opcode::v_cmp_gt_u64
;
516 case aco_opcode::v_cmp_ge_u64
:
517 op
= aco_opcode::v_cmp_le_u64
;
519 default: /* eq and ne are commutative */
526 src1
= as_vgpr(ctx
, src1
);
530 Builder
bld(ctx
->program
, ctx
->block
);
531 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
534 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
536 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
537 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
539 assert(dst
.regClass() == s2
);
540 assert(src0
.type() == RegType::sgpr
);
541 assert(src1
.type() == RegType::sgpr
);
543 Builder
bld(ctx
->program
, ctx
->block
);
544 /* Emit the SALU comparison instruction */
545 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
546 /* Turn the result into a per-lane bool */
547 bool_to_vector_condition(ctx
, cmp
, dst
);
550 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
551 aco_opcode v32_op
, aco_opcode v64_op
, aco_opcode s32_op
= aco_opcode::last_opcode
, aco_opcode s64_op
= aco_opcode::last_opcode
)
553 aco_opcode s_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? s64_op
: s32_op
;
554 aco_opcode v_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? v64_op
: v32_op
;
555 bool divergent_vals
= ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
];
556 bool use_valu
= s_op
== aco_opcode::last_opcode
||
558 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
559 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
560 aco_opcode op
= use_valu
? v_op
: s_op
;
561 assert(op
!= aco_opcode::last_opcode
);
564 emit_vopc_instruction(ctx
, instr
, op
, dst
);
566 emit_sopc_instruction(ctx
, instr
, op
, dst
);
569 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
571 Builder
bld(ctx
->program
, ctx
->block
);
572 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
573 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
575 assert(dst
.regClass() == s2
);
576 assert(src0
.regClass() == s2
);
577 assert(src1
.regClass() == s2
);
579 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
582 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
584 Builder
bld(ctx
->program
, ctx
->block
);
585 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
586 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
587 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
589 assert(cond
.regClass() == s2
);
591 if (dst
.type() == RegType::vgpr
) {
592 aco_ptr
<Instruction
> bcsel
;
593 if (dst
.size() == 1) {
594 then
= as_vgpr(ctx
, then
);
595 els
= as_vgpr(ctx
, els
);
597 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
598 } else if (dst
.size() == 2) {
599 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
600 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
601 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
602 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
604 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
605 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
607 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
609 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
610 nir_print_instr(&instr
->instr
, stderr
);
611 fprintf(stderr
, "\n");
616 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
617 assert(dst
.regClass() == s2
);
618 assert(then
.regClass() == s2
);
619 assert(els
.regClass() == s2
);
622 if (!ctx
->divergent_vals
[instr
->src
[0].src
.ssa
->index
]) { /* uniform condition and values in sgpr */
623 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
624 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
625 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
626 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
628 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
629 nir_print_instr(&instr
->instr
, stderr
);
630 fprintf(stderr
, "\n");
635 /* divergent boolean bcsel
636 * this implements bcsel on bools: dst = s0 ? s1 : s2
637 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
638 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
640 if (cond
.id() != then
.id())
641 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
643 if (cond
.id() == els
.id())
644 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
646 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
647 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
650 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
651 aco_opcode op
, uint32_t undo
)
653 /* multiply by 16777216 to handle denormals */
654 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(s2
)),
655 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
656 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
657 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
658 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
660 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
662 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
665 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
667 if (ctx
->block
->fp_mode
.denorm32
== 0) {
668 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
672 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
675 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
677 if (ctx
->block
->fp_mode
.denorm32
== 0) {
678 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
682 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
685 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
687 if (ctx
->block
->fp_mode
.denorm32
== 0) {
688 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
692 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
695 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
697 if (ctx
->block
->fp_mode
.denorm32
== 0) {
698 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
702 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
705 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
707 if (!instr
->dest
.dest
.is_ssa
) {
708 fprintf(stderr
, "nir alu dst not in ssa: ");
709 nir_print_instr(&instr
->instr
, stderr
);
710 fprintf(stderr
, "\n");
713 Builder
bld(ctx
->program
, ctx
->block
);
714 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
719 std::array
<Temp
,4> elems
;
720 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
721 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
722 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
723 vec
->operands
[i
] = Operand
{elems
[i
]};
725 vec
->definitions
[0] = Definition(dst
);
726 ctx
->block
->instructions
.emplace_back(std::move(vec
));
727 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
731 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
732 aco_ptr
<Instruction
> mov
;
733 if (dst
.type() == RegType::sgpr
) {
734 if (src
.type() == RegType::vgpr
)
735 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
736 else if (src
.regClass() == s1
)
737 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
738 else if (src
.regClass() == s2
)
739 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
741 unreachable("wrong src register class for nir_op_imov");
742 } else if (dst
.regClass() == v1
) {
743 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
744 } else if (dst
.regClass() == v2
) {
745 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
747 nir_print_instr(&instr
->instr
, stderr
);
748 unreachable("Should have been lowered to scalar.");
753 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
754 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
755 assert(src
.regClass() == s2
);
756 assert(dst
.regClass() == s2
);
757 bld
.sop2(aco_opcode::s_andn2_b64
, Definition(dst
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
758 } else if (dst
.regClass() == v1
) {
759 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
760 } else if (dst
.type() == RegType::sgpr
) {
761 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
762 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
764 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
765 nir_print_instr(&instr
->instr
, stderr
);
766 fprintf(stderr
, "\n");
771 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
772 if (dst
.regClass() == v1
) {
773 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
774 } else if (dst
.regClass() == s1
) {
775 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
776 } else if (dst
.size() == 2) {
777 Temp src0
= bld
.tmp(dst
.type(), 1);
778 Temp src1
= bld
.tmp(dst
.type(), 1);
779 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
781 if (dst
.regClass() == s2
) {
782 Temp carry
= bld
.tmp(s1
);
783 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
784 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
785 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
787 Temp lower
= bld
.tmp(v1
);
788 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
789 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
790 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
793 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
794 nir_print_instr(&instr
->instr
, stderr
);
795 fprintf(stderr
, "\n");
800 if (dst
.regClass() == s1
) {
801 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
802 } else if (dst
.regClass() == v1
) {
803 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
804 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
806 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
807 nir_print_instr(&instr
->instr
, stderr
);
808 fprintf(stderr
, "\n");
813 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
814 if (dst
.regClass() == s1
) {
815 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
816 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
817 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
818 } else if (dst
.regClass() == s2
) {
819 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
820 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
821 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
822 } else if (dst
.regClass() == v1
) {
823 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
824 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
825 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
826 } else if (dst
.regClass() == v2
) {
827 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
828 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
829 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
830 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
831 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
832 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
834 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
835 nir_print_instr(&instr
->instr
, stderr
);
836 fprintf(stderr
, "\n");
841 if (dst
.regClass() == v1
) {
842 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
843 } else if (dst
.regClass() == s1
) {
844 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
846 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
847 nir_print_instr(&instr
->instr
, stderr
);
848 fprintf(stderr
, "\n");
853 if (dst
.regClass() == v1
) {
854 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
855 } else if (dst
.regClass() == s1
) {
856 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
858 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
859 nir_print_instr(&instr
->instr
, stderr
);
860 fprintf(stderr
, "\n");
865 if (dst
.regClass() == v1
) {
866 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
867 } else if (dst
.regClass() == s1
) {
868 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
870 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
871 nir_print_instr(&instr
->instr
, stderr
);
872 fprintf(stderr
, "\n");
877 if (dst
.regClass() == v1
) {
878 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
879 } else if (dst
.regClass() == s1
) {
880 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
882 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
883 nir_print_instr(&instr
->instr
, stderr
);
884 fprintf(stderr
, "\n");
889 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
890 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
891 } else if (dst
.regClass() == v1
) {
892 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
893 } else if (dst
.regClass() == s1
) {
894 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
895 } else if (dst
.regClass() == s2
) {
896 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
898 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
899 nir_print_instr(&instr
->instr
, stderr
);
900 fprintf(stderr
, "\n");
905 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
906 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
907 } else if (dst
.regClass() == v1
) {
908 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
909 } else if (dst
.regClass() == s1
) {
910 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
911 } else if (dst
.regClass() == s2
) {
912 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
914 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
915 nir_print_instr(&instr
->instr
, stderr
);
916 fprintf(stderr
, "\n");
921 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
922 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
923 } else if (dst
.regClass() == v1
) {
924 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
925 } else if (dst
.regClass() == s1
) {
926 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
927 } else if (dst
.regClass() == s2
) {
928 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
930 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
931 nir_print_instr(&instr
->instr
, stderr
);
932 fprintf(stderr
, "\n");
937 if (dst
.regClass() == v1
) {
938 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
939 } else if (dst
.regClass() == v2
) {
940 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
941 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
942 } else if (dst
.regClass() == s2
) {
943 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
944 } else if (dst
.regClass() == s1
) {
945 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
947 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
948 nir_print_instr(&instr
->instr
, stderr
);
949 fprintf(stderr
, "\n");
954 if (dst
.regClass() == v1
) {
955 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
956 } else if (dst
.regClass() == v2
) {
957 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
958 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
959 } else if (dst
.regClass() == s1
) {
960 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
961 } else if (dst
.regClass() == s2
) {
962 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
964 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
965 nir_print_instr(&instr
->instr
, stderr
);
966 fprintf(stderr
, "\n");
971 if (dst
.regClass() == v1
) {
972 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
973 } else if (dst
.regClass() == v2
) {
974 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
975 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
976 } else if (dst
.regClass() == s1
) {
977 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
978 } else if (dst
.regClass() == s2
) {
979 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
981 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
982 nir_print_instr(&instr
->instr
, stderr
);
983 fprintf(stderr
, "\n");
987 case nir_op_find_lsb
: {
988 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
989 if (src
.regClass() == s1
) {
990 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
991 } else if (src
.regClass() == v1
) {
992 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
993 } else if (src
.regClass() == s2
) {
994 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
996 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
997 nir_print_instr(&instr
->instr
, stderr
);
998 fprintf(stderr
, "\n");
1002 case nir_op_ufind_msb
:
1003 case nir_op_ifind_msb
: {
1004 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1005 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1006 aco_opcode op
= src
.regClass() == s2
?
1007 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1008 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1009 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1011 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1012 Operand(src
.size() * 32u - 1u), msb_rev
);
1013 Temp msb
= sub
.def(0).getTemp();
1014 Temp carry
= sub
.def(1).getTemp();
1016 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
1017 } else if (src
.regClass() == v1
) {
1018 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1019 Temp msb_rev
= bld
.tmp(v1
);
1020 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1021 Temp msb
= bld
.tmp(v1
);
1022 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1023 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1025 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1026 nir_print_instr(&instr
->instr
, stderr
);
1027 fprintf(stderr
, "\n");
1031 case nir_op_bitfield_reverse
: {
1032 if (dst
.regClass() == s1
) {
1033 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1034 } else if (dst
.regClass() == v1
) {
1035 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1037 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1038 nir_print_instr(&instr
->instr
, stderr
);
1039 fprintf(stderr
, "\n");
1044 if (dst
.regClass() == s1
) {
1045 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1049 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1050 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1051 if (dst
.regClass() == v1
) {
1052 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1056 assert(src0
.size() == 2 && src1
.size() == 2);
1057 Temp src00
= bld
.tmp(src0
.type(), 1);
1058 Temp src01
= bld
.tmp(dst
.type(), 1);
1059 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1060 Temp src10
= bld
.tmp(src1
.type(), 1);
1061 Temp src11
= bld
.tmp(dst
.type(), 1);
1062 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1064 if (dst
.regClass() == s2
) {
1065 Temp carry
= bld
.tmp(s1
);
1066 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1067 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1068 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1069 } else if (dst
.regClass() == v2
) {
1070 Temp dst0
= bld
.tmp(v1
);
1071 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1072 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1073 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1075 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1076 nir_print_instr(&instr
->instr
, stderr
);
1077 fprintf(stderr
, "\n");
1081 case nir_op_uadd_sat
: {
1082 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1083 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1084 if (dst
.regClass() == s1
) {
1085 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1086 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1088 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1089 } else if (dst
.regClass() == v1
) {
1090 if (ctx
->options
->chip_class
>= GFX9
) {
1091 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1092 add
->operands
[0] = Operand(src0
);
1093 add
->operands
[1] = Operand(src1
);
1094 add
->definitions
[0] = Definition(dst
);
1096 ctx
->block
->instructions
.emplace_back(std::move(add
));
1098 if (src1
.regClass() != v1
)
1099 std::swap(src0
, src1
);
1100 assert(src1
.regClass() == v1
);
1101 Temp tmp
= bld
.tmp(v1
);
1102 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1103 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1106 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1107 nir_print_instr(&instr
->instr
, stderr
);
1108 fprintf(stderr
, "\n");
1112 case nir_op_uadd_carry
: {
1113 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1114 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1115 if (dst
.regClass() == s1
) {
1116 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1119 if (dst
.regClass() == v1
) {
1120 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1121 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1125 Temp src00
= bld
.tmp(src0
.type(), 1);
1126 Temp src01
= bld
.tmp(dst
.type(), 1);
1127 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1128 Temp src10
= bld
.tmp(src1
.type(), 1);
1129 Temp src11
= bld
.tmp(dst
.type(), 1);
1130 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1131 if (dst
.regClass() == s2
) {
1132 Temp carry
= bld
.tmp(s1
);
1133 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1134 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1135 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1136 } else if (dst
.regClass() == v2
) {
1137 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1138 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1139 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1140 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1142 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1143 nir_print_instr(&instr
->instr
, stderr
);
1144 fprintf(stderr
, "\n");
1149 if (dst
.regClass() == s1
) {
1150 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1154 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1155 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1156 if (dst
.regClass() == v1
) {
1157 bld
.vsub32(Definition(dst
), src0
, src1
);
1161 Temp src00
= bld
.tmp(src0
.type(), 1);
1162 Temp src01
= bld
.tmp(dst
.type(), 1);
1163 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1164 Temp src10
= bld
.tmp(src1
.type(), 1);
1165 Temp src11
= bld
.tmp(dst
.type(), 1);
1166 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1167 if (dst
.regClass() == s2
) {
1168 Temp carry
= bld
.tmp(s1
);
1169 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1170 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1171 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1172 } else if (dst
.regClass() == v2
) {
1173 Temp lower
= bld
.tmp(v1
);
1174 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1175 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1176 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1178 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1179 nir_print_instr(&instr
->instr
, stderr
);
1180 fprintf(stderr
, "\n");
1184 case nir_op_usub_borrow
: {
1185 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1186 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1187 if (dst
.regClass() == s1
) {
1188 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1190 } else if (dst
.regClass() == v1
) {
1191 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1192 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1196 Temp src00
= bld
.tmp(src0
.type(), 1);
1197 Temp src01
= bld
.tmp(dst
.type(), 1);
1198 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1199 Temp src10
= bld
.tmp(src1
.type(), 1);
1200 Temp src11
= bld
.tmp(dst
.type(), 1);
1201 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1202 if (dst
.regClass() == s2
) {
1203 Temp borrow
= bld
.tmp(s1
);
1204 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1205 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1206 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1207 } else if (dst
.regClass() == v2
) {
1208 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1209 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1210 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1211 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1213 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1214 nir_print_instr(&instr
->instr
, stderr
);
1215 fprintf(stderr
, "\n");
1220 if (dst
.regClass() == v1
) {
1221 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1222 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1223 } else if (dst
.regClass() == s1
) {
1224 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1226 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr
->instr
, stderr
);
1228 fprintf(stderr
, "\n");
1232 case nir_op_umul_high
: {
1233 if (dst
.regClass() == v1
) {
1234 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1235 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1236 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1237 } else if (dst
.regClass() == s1
) {
1238 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1239 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1240 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1242 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1243 nir_print_instr(&instr
->instr
, stderr
);
1244 fprintf(stderr
, "\n");
1248 case nir_op_imul_high
: {
1249 if (dst
.regClass() == v1
) {
1250 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1251 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1252 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1253 } else if (dst
.regClass() == s1
) {
1254 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1255 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1256 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1258 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1259 nir_print_instr(&instr
->instr
, stderr
);
1260 fprintf(stderr
, "\n");
1265 if (dst
.size() == 1) {
1266 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1267 } else if (dst
.size() == 2) {
1268 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1269 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1271 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1272 nir_print_instr(&instr
->instr
, stderr
);
1273 fprintf(stderr
, "\n");
1278 if (dst
.size() == 1) {
1279 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1280 } else if (dst
.size() == 2) {
1281 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1282 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1284 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1285 nir_print_instr(&instr
->instr
, stderr
);
1286 fprintf(stderr
, "\n");
1291 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1292 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1293 if (dst
.size() == 1) {
1294 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1295 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1297 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1298 } else if (dst
.size() == 2) {
1299 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1300 get_alu_src(ctx
, instr
->src
[0]),
1301 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1302 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1305 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr
->instr
, stderr
);
1307 fprintf(stderr
, "\n");
1312 if (dst
.size() == 1) {
1313 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1314 } else if (dst
.size() == 2) {
1315 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1316 get_alu_src(ctx
, instr
->src
[0]),
1317 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1319 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1320 nir_print_instr(&instr
->instr
, stderr
);
1321 fprintf(stderr
, "\n");
1326 if (dst
.size() == 1) {
1327 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1328 } else if (dst
.size() == 2) {
1329 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1330 get_alu_src(ctx
, instr
->src
[0]),
1331 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1333 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1334 nir_print_instr(&instr
->instr
, stderr
);
1335 fprintf(stderr
, "\n");
1339 case nir_op_fmax3
: {
1340 if (dst
.size() == 1) {
1341 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1343 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1344 nir_print_instr(&instr
->instr
, stderr
);
1345 fprintf(stderr
, "\n");
1349 case nir_op_fmin3
: {
1350 if (dst
.size() == 1) {
1351 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1353 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1354 nir_print_instr(&instr
->instr
, stderr
);
1355 fprintf(stderr
, "\n");
1359 case nir_op_fmed3
: {
1360 if (dst
.size() == 1) {
1361 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1363 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1364 nir_print_instr(&instr
->instr
, stderr
);
1365 fprintf(stderr
, "\n");
1369 case nir_op_umax3
: {
1370 if (dst
.size() == 1) {
1371 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1373 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1374 nir_print_instr(&instr
->instr
, stderr
);
1375 fprintf(stderr
, "\n");
1379 case nir_op_umin3
: {
1380 if (dst
.size() == 1) {
1381 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1383 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1384 nir_print_instr(&instr
->instr
, stderr
);
1385 fprintf(stderr
, "\n");
1389 case nir_op_umed3
: {
1390 if (dst
.size() == 1) {
1391 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1393 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1394 nir_print_instr(&instr
->instr
, stderr
);
1395 fprintf(stderr
, "\n");
1399 case nir_op_imax3
: {
1400 if (dst
.size() == 1) {
1401 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1403 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1404 nir_print_instr(&instr
->instr
, stderr
);
1405 fprintf(stderr
, "\n");
1409 case nir_op_imin3
: {
1410 if (dst
.size() == 1) {
1411 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1413 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1414 nir_print_instr(&instr
->instr
, stderr
);
1415 fprintf(stderr
, "\n");
1419 case nir_op_imed3
: {
1420 if (dst
.size() == 1) {
1421 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1423 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1424 nir_print_instr(&instr
->instr
, stderr
);
1425 fprintf(stderr
, "\n");
1429 case nir_op_cube_face_coord
: {
1430 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1431 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1432 emit_extract_vector(ctx
, in
, 1, v1
),
1433 emit_extract_vector(ctx
, in
, 2, v1
) };
1434 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1435 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1436 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1437 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1438 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1439 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1440 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1443 case nir_op_cube_face_index
: {
1444 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1445 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1446 emit_extract_vector(ctx
, in
, 1, v1
),
1447 emit_extract_vector(ctx
, in
, 2, v1
) };
1448 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1451 case nir_op_bcsel
: {
1452 emit_bcsel(ctx
, instr
, dst
);
1456 if (dst
.size() == 1) {
1457 emit_rsq(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1458 } else if (dst
.size() == 2) {
1459 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1461 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1462 nir_print_instr(&instr
->instr
, stderr
);
1463 fprintf(stderr
, "\n");
1468 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1469 if (dst
.size() == 1) {
1470 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1471 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1472 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1473 } else if (dst
.size() == 2) {
1474 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1475 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1476 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1477 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1478 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1479 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1481 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1482 nir_print_instr(&instr
->instr
, stderr
);
1483 fprintf(stderr
, "\n");
1488 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1489 if (dst
.size() == 1) {
1490 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1491 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1492 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1493 } else if (dst
.size() == 2) {
1494 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1495 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1496 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1497 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1498 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1499 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1501 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1502 nir_print_instr(&instr
->instr
, stderr
);
1503 fprintf(stderr
, "\n");
1508 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1509 if (dst
.size() == 1) {
1510 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1511 } else if (dst
.size() == 2) {
1512 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1513 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1516 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1517 nir_print_instr(&instr
->instr
, stderr
);
1518 fprintf(stderr
, "\n");
1522 case nir_op_flog2
: {
1523 if (dst
.size() == 1) {
1524 emit_log2(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1526 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1527 nir_print_instr(&instr
->instr
, stderr
);
1528 fprintf(stderr
, "\n");
1533 if (dst
.size() == 1) {
1534 emit_rcp(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1535 } else if (dst
.size() == 2) {
1536 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1538 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1539 nir_print_instr(&instr
->instr
, stderr
);
1540 fprintf(stderr
, "\n");
1544 case nir_op_fexp2
: {
1545 if (dst
.size() == 1) {
1546 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1548 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1549 nir_print_instr(&instr
->instr
, stderr
);
1550 fprintf(stderr
, "\n");
1554 case nir_op_fsqrt
: {
1555 if (dst
.size() == 1) {
1556 emit_sqrt(ctx
, bld
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1557 } else if (dst
.size() == 2) {
1558 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1560 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1561 nir_print_instr(&instr
->instr
, stderr
);
1562 fprintf(stderr
, "\n");
1566 case nir_op_ffract
: {
1567 if (dst
.size() == 1) {
1568 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1569 } else if (dst
.size() == 2) {
1570 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1572 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1573 nir_print_instr(&instr
->instr
, stderr
);
1574 fprintf(stderr
, "\n");
1578 case nir_op_ffloor
: {
1579 if (dst
.size() == 1) {
1580 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1581 } else if (dst
.size() == 2) {
1582 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1584 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1585 nir_print_instr(&instr
->instr
, stderr
);
1586 fprintf(stderr
, "\n");
1590 case nir_op_fceil
: {
1591 if (dst
.size() == 1) {
1592 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1593 } else if (dst
.size() == 2) {
1594 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1596 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1597 nir_print_instr(&instr
->instr
, stderr
);
1598 fprintf(stderr
, "\n");
1602 case nir_op_ftrunc
: {
1603 if (dst
.size() == 1) {
1604 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1605 } else if (dst
.size() == 2) {
1606 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1608 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1609 nir_print_instr(&instr
->instr
, stderr
);
1610 fprintf(stderr
, "\n");
1614 case nir_op_fround_even
: {
1615 if (dst
.size() == 1) {
1616 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1617 } else if (dst
.size() == 2) {
1618 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1620 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1621 nir_print_instr(&instr
->instr
, stderr
);
1622 fprintf(stderr
, "\n");
1628 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1629 aco_ptr
<Instruction
> norm
;
1630 if (dst
.size() == 1) {
1632 Operand
half_pi(0x3e22f983u
);
1633 if (src
.type() == RegType::sgpr
)
1634 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1636 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1638 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1639 if (ctx
->options
->chip_class
< GFX9
)
1640 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1642 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1643 bld
.vop1(opcode
, Definition(dst
), tmp
);
1645 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1646 nir_print_instr(&instr
->instr
, stderr
);
1647 fprintf(stderr
, "\n");
1651 case nir_op_ldexp
: {
1652 if (dst
.size() == 1) {
1653 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1654 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1655 get_alu_src(ctx
, instr
->src
[1]));
1656 } else if (dst
.size() == 2) {
1657 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1658 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1659 get_alu_src(ctx
, instr
->src
[1]));
1661 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1662 nir_print_instr(&instr
->instr
, stderr
);
1663 fprintf(stderr
, "\n");
1667 case nir_op_frexp_sig
: {
1668 if (dst
.size() == 1) {
1669 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1670 get_alu_src(ctx
, instr
->src
[0]));
1671 } else if (dst
.size() == 2) {
1672 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1673 get_alu_src(ctx
, instr
->src
[0]));
1675 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1676 nir_print_instr(&instr
->instr
, stderr
);
1677 fprintf(stderr
, "\n");
1681 case nir_op_frexp_exp
: {
1682 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1683 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1684 get_alu_src(ctx
, instr
->src
[0]));
1685 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1686 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1687 get_alu_src(ctx
, instr
->src
[0]));
1689 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1690 nir_print_instr(&instr
->instr
, stderr
);
1691 fprintf(stderr
, "\n");
1695 case nir_op_fsign
: {
1696 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1697 if (dst
.size() == 1) {
1698 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1699 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1700 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1701 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1702 } else if (dst
.size() == 2) {
1703 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1704 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1705 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
1707 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1708 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1709 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1711 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1713 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1714 nir_print_instr(&instr
->instr
, stderr
);
1715 fprintf(stderr
, "\n");
1719 case nir_op_f2f32
: {
1720 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1721 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1723 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1724 nir_print_instr(&instr
->instr
, stderr
);
1725 fprintf(stderr
, "\n");
1729 case nir_op_f2f64
: {
1730 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1731 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1733 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1734 nir_print_instr(&instr
->instr
, stderr
);
1735 fprintf(stderr
, "\n");
1739 case nir_op_i2f32
: {
1740 assert(dst
.size() == 1);
1741 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1744 case nir_op_i2f64
: {
1745 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1746 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1747 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1748 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1749 RegClass rc
= RegClass(src
.type(), 1);
1750 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1751 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1752 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1753 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1754 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1755 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1758 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1759 nir_print_instr(&instr
->instr
, stderr
);
1760 fprintf(stderr
, "\n");
1764 case nir_op_u2f32
: {
1765 assert(dst
.size() == 1);
1766 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1769 case nir_op_u2f64
: {
1770 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1771 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1772 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1773 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1774 RegClass rc
= RegClass(src
.type(), 1);
1775 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1776 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1777 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1778 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1779 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1780 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1782 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1783 nir_print_instr(&instr
->instr
, stderr
);
1784 fprintf(stderr
, "\n");
1788 case nir_op_f2i32
: {
1789 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1790 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1791 if (dst
.type() == RegType::vgpr
)
1792 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1794 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1795 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1797 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1798 if (dst
.type() == RegType::vgpr
)
1799 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1801 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1802 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1805 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1806 nir_print_instr(&instr
->instr
, stderr
);
1807 fprintf(stderr
, "\n");
1811 case nir_op_f2u32
: {
1812 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1813 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1814 if (dst
.type() == RegType::vgpr
)
1815 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1817 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1818 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1820 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1821 if (dst
.type() == RegType::vgpr
)
1822 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1824 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1825 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1828 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1829 nir_print_instr(&instr
->instr
, stderr
);
1830 fprintf(stderr
, "\n");
1834 case nir_op_f2i64
: {
1835 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1836 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1837 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1838 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1839 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1840 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1841 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1842 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1843 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1844 Temp new_exponent
= bld
.tmp(v1
);
1845 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1846 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1847 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1848 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1849 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1850 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1851 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1852 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1853 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1854 Temp new_lower
= bld
.tmp(v1
);
1855 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1856 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1857 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1859 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1860 if (src
.type() == RegType::vgpr
)
1861 src
= bld
.as_uniform(src
);
1862 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1863 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1864 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1865 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1866 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1867 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1868 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1869 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1870 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1871 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1872 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1873 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1874 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1875 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1876 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1877 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1878 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1879 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1880 Temp borrow
= bld
.tmp(s1
);
1881 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1882 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1883 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1885 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1886 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1887 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1888 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1889 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1890 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1891 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1892 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1893 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1894 if (dst
.type() == RegType::sgpr
) {
1895 lower
= bld
.as_uniform(lower
);
1896 upper
= bld
.as_uniform(upper
);
1898 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1901 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1902 nir_print_instr(&instr
->instr
, stderr
);
1903 fprintf(stderr
, "\n");
1907 case nir_op_f2u64
: {
1908 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1909 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1910 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1911 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1912 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1913 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1914 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1915 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1916 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1917 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1918 Temp new_exponent
= bld
.tmp(v1
);
1919 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1920 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1921 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1922 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1923 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1924 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1925 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1926 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1927 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1929 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1930 if (src
.type() == RegType::vgpr
)
1931 src
= bld
.as_uniform(src
);
1932 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1933 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1934 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1935 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1936 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1937 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1938 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1939 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1940 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1941 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1942 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1943 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1944 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1945 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1946 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1947 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1948 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1949 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1951 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1952 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1953 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1954 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1955 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1956 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1957 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1958 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1959 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1960 if (dst
.type() == RegType::sgpr
) {
1961 lower
= bld
.as_uniform(lower
);
1962 upper
= bld
.as_uniform(upper
);
1964 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1967 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1968 nir_print_instr(&instr
->instr
, stderr
);
1969 fprintf(stderr
, "\n");
1973 case nir_op_b2f32
: {
1974 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1975 assert(src
.regClass() == s2
);
1977 if (dst
.regClass() == s1
) {
1978 src
= bool_to_scalar_condition(ctx
, src
);
1979 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1980 } else if (dst
.regClass() == v1
) {
1981 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1983 unreachable("Wrong destination register class for nir_op_b2f32.");
1987 case nir_op_b2f64
: {
1988 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1989 assert(src
.regClass() == s2
);
1991 if (dst
.regClass() == s2
) {
1992 src
= bool_to_scalar_condition(ctx
, src
);
1993 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1994 } else if (dst
.regClass() == v2
) {
1995 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1996 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
1997 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1999 unreachable("Wrong destination register class for nir_op_b2f64.");
2003 case nir_op_i2i32
: {
2004 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2005 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2006 /* we can actually just say dst = src, as it would map the lower register */
2007 emit_extract_vector(ctx
, src
, 0, dst
);
2009 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2010 nir_print_instr(&instr
->instr
, stderr
);
2011 fprintf(stderr
, "\n");
2015 case nir_op_u2u32
: {
2016 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2017 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2018 if (dst
.regClass() == s1
) {
2019 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
2021 // TODO: do better with SDWA
2022 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
2024 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2025 /* we can actually just say dst = src, as it would map the lower register */
2026 emit_extract_vector(ctx
, src
, 0, dst
);
2028 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2029 nir_print_instr(&instr
->instr
, stderr
);
2030 fprintf(stderr
, "\n");
2034 case nir_op_i2i64
: {
2035 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2036 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2037 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2039 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2040 nir_print_instr(&instr
->instr
, stderr
);
2041 fprintf(stderr
, "\n");
2045 case nir_op_u2u64
: {
2046 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2047 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2048 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
2050 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2051 nir_print_instr(&instr
->instr
, stderr
);
2052 fprintf(stderr
, "\n");
2056 case nir_op_b2i32
: {
2057 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2058 assert(src
.regClass() == s2
);
2060 if (dst
.regClass() == s1
) {
2061 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2062 bool_to_scalar_condition(ctx
, src
, dst
);
2063 } else if (dst
.regClass() == v1
) {
2064 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2066 unreachable("Invalid register class for b2i32");
2071 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2072 assert(dst
.regClass() == s2
);
2074 if (src
.type() == RegType::vgpr
) {
2075 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2076 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2077 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2079 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2080 Temp tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2081 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2082 bool_to_vector_condition(ctx
, tmp
, dst
);
2086 case nir_op_pack_64_2x32_split
: {
2087 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2088 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2090 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2093 case nir_op_unpack_64_2x32_split_x
:
2094 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2096 case nir_op_unpack_64_2x32_split_y
:
2097 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2099 case nir_op_pack_half_2x16
: {
2100 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2102 if (dst
.regClass() == v1
) {
2103 Temp src0
= bld
.tmp(v1
);
2104 Temp src1
= bld
.tmp(v1
);
2105 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2106 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2107 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2109 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2110 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2111 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2113 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2114 nir_print_instr(&instr
->instr
, stderr
);
2115 fprintf(stderr
, "\n");
2119 case nir_op_unpack_half_2x16_split_x
: {
2120 if (dst
.regClass() == v1
) {
2121 Builder
bld(ctx
->program
, ctx
->block
);
2122 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2124 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2125 nir_print_instr(&instr
->instr
, stderr
);
2126 fprintf(stderr
, "\n");
2130 case nir_op_unpack_half_2x16_split_y
: {
2131 if (dst
.regClass() == v1
) {
2132 Builder
bld(ctx
->program
, ctx
->block
);
2133 /* TODO: use SDWA here */
2134 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2135 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2137 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2138 nir_print_instr(&instr
->instr
, stderr
);
2139 fprintf(stderr
, "\n");
2143 case nir_op_fquantize2f16
: {
2144 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2145 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2147 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2149 Temp cmp_res
= bld
.tmp(s2
);
2150 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2152 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2154 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
) {
2155 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2156 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2158 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2163 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2164 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2166 if (dst
.regClass() == s1
) {
2167 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2168 } else if (dst
.regClass() == v1
) {
2169 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2171 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2172 nir_print_instr(&instr
->instr
, stderr
);
2173 fprintf(stderr
, "\n");
2177 case nir_op_bitfield_select
: {
2178 /* (mask & insert) | (~mask & base) */
2179 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2180 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2181 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2183 /* dst = (insert & bitmask) | (base & ~bitmask) */
2184 if (dst
.regClass() == s1
) {
2185 aco_ptr
<Instruction
> sop2
;
2186 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2187 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2189 if (const_insert
&& const_bitmask
) {
2190 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2192 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2193 lhs
= Operand(insert
);
2197 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2198 if (const_base
&& const_bitmask
) {
2199 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2201 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2202 rhs
= Operand(base
);
2205 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2207 } else if (dst
.regClass() == v1
) {
2208 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2209 base
= as_vgpr(ctx
, base
);
2210 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2211 insert
= as_vgpr(ctx
, insert
);
2213 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2216 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2217 nir_print_instr(&instr
->instr
, stderr
);
2218 fprintf(stderr
, "\n");
2224 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2225 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2226 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2228 if (dst
.type() == RegType::sgpr
) {
2230 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2231 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2232 if (const_offset
&& const_bits
) {
2233 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2234 extract
= Operand(const_extract
);
2238 width
= Operand(const_bits
->u32
<< 16);
2240 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2242 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2246 if (dst
.regClass() == s1
) {
2247 if (instr
->op
== nir_op_ubfe
)
2248 opcode
= aco_opcode::s_bfe_u32
;
2250 opcode
= aco_opcode::s_bfe_i32
;
2251 } else if (dst
.regClass() == s2
) {
2252 if (instr
->op
== nir_op_ubfe
)
2253 opcode
= aco_opcode::s_bfe_u64
;
2255 opcode
= aco_opcode::s_bfe_i64
;
2257 unreachable("Unsupported BFE bit size");
2260 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2264 if (dst
.regClass() == v1
) {
2265 if (instr
->op
== nir_op_ubfe
)
2266 opcode
= aco_opcode::v_bfe_u32
;
2268 opcode
= aco_opcode::v_bfe_i32
;
2270 unreachable("Unsupported BFE bit size");
2273 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2277 case nir_op_bit_count
: {
2278 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2279 if (src
.regClass() == s1
) {
2280 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2281 } else if (src
.regClass() == v1
) {
2282 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2283 } else if (src
.regClass() == v2
) {
2284 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2285 emit_extract_vector(ctx
, src
, 1, v1
),
2286 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2287 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2288 } else if (src
.regClass() == s2
) {
2289 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2291 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2292 nir_print_instr(&instr
->instr
, stderr
);
2293 fprintf(stderr
, "\n");
2298 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2302 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2306 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2310 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2314 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_i32
, aco_opcode::v_cmp_lt_i64
, aco_opcode::s_cmp_lt_i32
);
2318 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_i32
, aco_opcode::v_cmp_ge_i64
, aco_opcode::s_cmp_ge_i32
);
2322 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2323 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xnor_b32
, aco_opcode::s_xnor_b64
, dst
);
2325 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_i32
, aco_opcode::v_cmp_eq_i64
, aco_opcode::s_cmp_eq_i32
, aco_opcode::s_cmp_eq_u64
);
2329 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2330 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
2332 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lg_i32
, aco_opcode::v_cmp_lg_i64
, aco_opcode::s_cmp_lg_i32
, aco_opcode::s_cmp_lg_u64
);
2336 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_u32
, aco_opcode::v_cmp_lt_u64
, aco_opcode::s_cmp_lt_u32
);
2340 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_u32
, aco_opcode::v_cmp_ge_u64
, aco_opcode::s_cmp_ge_u32
);
2345 case nir_op_fddx_fine
:
2346 case nir_op_fddy_fine
:
2347 case nir_op_fddx_coarse
:
2348 case nir_op_fddy_coarse
: {
2349 Definition tl
= bld
.def(v1
);
2351 if (instr
->op
== nir_op_fddx_fine
) {
2352 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2353 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2354 } else if (instr
->op
== nir_op_fddy_fine
) {
2355 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2356 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2358 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2359 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2360 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2362 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2365 Definition tmp
= bld
.def(v1
);
2366 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2367 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2371 fprintf(stderr
, "Unknown NIR ALU instr: ");
2372 nir_print_instr(&instr
->instr
, stderr
);
2373 fprintf(stderr
, "\n");
2377 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2379 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2381 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2382 // which get truncated the lsb if double and msb if int
2383 // for now, we only use s_mov_b64 with 64bit inline constants
2384 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2385 assert(dst
.type() == RegType::sgpr
);
2387 Builder
bld(ctx
->program
, ctx
->block
);
2389 if (instr
->def
.bit_size
== 1) {
2390 assert(dst
.regClass() == s2
);
2391 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), Operand((uint64_t)(instr
->value
[0].b
? -1 : 0)));
2392 } else if (dst
.size() == 1) {
2393 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2395 assert(dst
.size() != 1);
2396 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2397 if (instr
->def
.bit_size
== 64)
2398 for (unsigned i
= 0; i
< dst
.size(); i
++)
2399 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2401 for (unsigned i
= 0; i
< dst
.size(); i
++)
2402 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2404 vec
->definitions
[0] = Definition(dst
);
2405 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2409 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2411 uint32_t new_mask
= 0;
2412 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2413 if (mask
& (1u << i
))
2414 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2418 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2420 /* This wouldn't work inside control flow or with indirect offsets but
2421 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2423 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2424 unsigned component
= nir_intrinsic_component(instr
);
2425 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2426 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2428 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2429 if (off_instr
->type
!= nir_instr_type_load_const
) {
2430 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2431 nir_print_instr(off_instr
, stderr
);
2432 fprintf(stderr
, "\n");
2434 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2436 if (instr
->src
[0].ssa
->bit_size
== 64)
2437 write_mask
= widen_mask(write_mask
, 2);
2439 for (unsigned i
= 0; i
< 8; ++i
) {
2440 if (write_mask
& (1 << i
)) {
2441 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2442 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2448 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2450 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2452 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2453 for (unsigned i
= 0; i
< 4; ++i
) {
2454 if (write_mask
& (1 << i
)) {
2455 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2456 values
[i
] = Operand(tmp
);
2458 values
[i
] = Operand(v1
);
2462 unsigned index
= nir_intrinsic_base(instr
) / 4;
2463 unsigned target
, col_format
;
2464 unsigned enabled_channels
= 0xF;
2465 aco_opcode compr_op
= (aco_opcode
)0;
2467 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2468 assert(offset
&& "Non-const offsets on exports not yet supported");
2469 index
+= offset
->u32
;
2471 assert(index
!= FRAG_RESULT_COLOR
);
2473 /* Unlike vertex shader exports, it's fine to use multiple exports to
2474 * export separate channels of one target. So shaders which export both
2475 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2476 * TODO: combine the exports in those cases and create better code
2479 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2481 if (ctx
->program
->info
->ps
.writes_z
) {
2482 target
= V_008DFC_SQ_EXP_MRTZ
;
2483 enabled_channels
= 0x4;
2484 col_format
= (unsigned) -1;
2486 values
[2] = values
[0];
2487 values
[0] = Operand(v1
);
2489 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2490 exp
->valid_mask
= false;
2492 exp
->compressed
= true;
2493 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2494 exp
->enabled_mask
= 0xc;
2495 for (int i
= 0; i
< 4; i
++)
2496 exp
->operands
[i
] = Operand(v1
);
2497 exp
->operands
[1] = Operand(values
[0]);
2498 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2502 } else if (index
== FRAG_RESULT_DEPTH
) {
2504 target
= V_008DFC_SQ_EXP_MRTZ
;
2505 enabled_channels
= 0x1;
2506 col_format
= (unsigned) -1;
2508 } else if (index
== FRAG_RESULT_STENCIL
) {
2510 if (ctx
->program
->info
->ps
.writes_z
) {
2511 target
= V_008DFC_SQ_EXP_MRTZ
;
2512 enabled_channels
= 0x2;
2513 col_format
= (unsigned) -1;
2515 values
[1] = values
[0];
2516 values
[0] = Operand(v1
);
2518 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2519 shift
->operands
[0] = Operand((uint32_t) 16);
2520 shift
->operands
[1] = values
[0];
2521 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2522 shift
->definitions
[0] = Definition(tmp
);
2523 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2525 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2526 exp
->valid_mask
= false;
2528 exp
->compressed
= true;
2529 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2530 exp
->enabled_mask
= 0x3;
2531 exp
->operands
[0] = Operand(tmp
);
2532 for (int i
= 1; i
< 4; i
++)
2533 exp
->operands
[i
] = Operand(v1
);
2534 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2539 index
-= FRAG_RESULT_DATA0
;
2540 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2541 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2543 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2544 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2545 assert(!is_int8
&& !is_int10
);
2549 case V_028714_SPI_SHADER_ZERO
:
2550 enabled_channels
= 0; /* writemask */
2551 target
= V_008DFC_SQ_EXP_NULL
;
2554 case V_028714_SPI_SHADER_32_R
:
2555 enabled_channels
= 1;
2558 case V_028714_SPI_SHADER_32_GR
:
2559 enabled_channels
= 0x3;
2562 case V_028714_SPI_SHADER_32_AR
:
2563 if (ctx
->options
->chip_class
>= GFX10
) {
2564 /* Special case: on GFX10, the outputs are different for 32_AR */
2565 enabled_channels
= 0x3;
2566 values
[1] = values
[3];
2568 enabled_channels
= 0x9;
2572 case V_028714_SPI_SHADER_FP16_ABGR
:
2573 enabled_channels
= 0x5;
2574 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2577 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2578 enabled_channels
= 0x5;
2579 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2582 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2583 enabled_channels
= 0x5;
2584 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2587 case V_028714_SPI_SHADER_UINT16_ABGR
:
2588 enabled_channels
= 0x5;
2589 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2592 case V_028714_SPI_SHADER_SINT16_ABGR
:
2593 enabled_channels
= 0x5;
2594 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2597 case V_028714_SPI_SHADER_32_ABGR
:
2598 enabled_channels
= 0xF;
2605 if (target
== V_008DFC_SQ_EXP_NULL
)
2610 for (int i
= 0; i
< 2; i
++)
2612 /* check if at least one of the values to be compressed is enabled */
2613 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2615 enabled_channels
|= enabled
<< (i
*2);
2616 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2617 Temp tmp
{ctx
->program
->allocateId(), v1
};
2618 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2619 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2620 compr
->definitions
[0] = Definition(tmp
);
2621 values
[i
] = Operand(tmp
);
2622 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2624 values
[i
] = Operand(v1
);
2629 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2630 exp
->valid_mask
= false;
2632 exp
->compressed
= (bool) compr_op
;
2634 exp
->enabled_mask
= enabled_channels
;
2635 if ((bool) compr_op
) {
2636 for (int i
= 0; i
< 2; i
++)
2637 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2638 exp
->operands
[2] = Operand(v1
);
2639 exp
->operands
[3] = Operand(v1
);
2641 for (int i
= 0; i
< 4; i
++)
2642 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2645 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2648 Operand
load_lds_size_m0(isel_context
*ctx
)
2650 /* TODO: m0 does not need to be initialized on GFX9+ */
2651 Builder
bld(ctx
->program
, ctx
->block
);
2652 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2655 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2656 Temp address
, unsigned base_offset
, unsigned align
)
2658 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2660 Builder
bld(ctx
->program
, ctx
->block
);
2662 Operand m
= load_lds_size_m0(ctx
);
2664 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2665 unsigned bytes_read
= 0;
2666 unsigned result_size
= 0;
2667 unsigned total_bytes
= num_components
* elem_size_bytes
;
2668 std::array
<Temp
, 4> result
;
2670 while (bytes_read
< total_bytes
) {
2671 unsigned todo
= total_bytes
- bytes_read
;
2672 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2673 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2675 aco_opcode op
= aco_opcode::last_opcode
;
2677 if (todo
>= 16 && aligned16
) {
2678 op
= aco_opcode::ds_read_b128
;
2680 } else if (todo
>= 16 && aligned8
) {
2681 op
= aco_opcode::ds_read2_b64
;
2684 } else if (todo
>= 12 && aligned16
) {
2685 op
= aco_opcode::ds_read_b96
;
2687 } else if (todo
>= 8 && aligned8
) {
2688 op
= aco_opcode::ds_read_b64
;
2690 } else if (todo
>= 8) {
2691 op
= aco_opcode::ds_read2_b32
;
2694 } else if (todo
>= 4) {
2695 op
= aco_opcode::ds_read_b32
;
2700 assert(todo
% elem_size_bytes
== 0);
2701 unsigned num_elements
= todo
/ elem_size_bytes
;
2702 unsigned offset
= base_offset
+ bytes_read
;
2703 unsigned max_offset
= read2
? 1019 : 65535;
2705 Temp address_offset
= address
;
2706 if (offset
> max_offset
) {
2707 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2708 offset
= bytes_read
;
2710 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2713 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2716 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2719 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2721 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2723 if (num_components
== 1) {
2724 assert(todo
== total_bytes
);
2725 if (dst
.type() == RegType::sgpr
)
2726 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2730 if (dst
.type() == RegType::sgpr
)
2731 res
= bld
.as_uniform(res
);
2733 if (num_elements
== 1) {
2734 result
[result_size
++] = res
;
2736 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2737 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2738 split
->operands
[0] = Operand(res
);
2739 for (unsigned i
= 0; i
< num_elements
; i
++)
2740 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2741 ctx
->block
->instructions
.emplace_back(std::move(split
));
2747 assert(result_size
== num_components
&& result_size
> 1);
2748 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2749 for (unsigned i
= 0; i
< result_size
; i
++)
2750 vec
->operands
[i
] = Operand(result
[i
]);
2751 vec
->definitions
[0] = Definition(dst
);
2752 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2753 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2756 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2758 if (start
== 0 && size
== data
.size())
2759 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2761 unsigned size_hint
= 1;
2762 auto it
= ctx
->allocated_vec
.find(data
.id());
2763 if (it
!= ctx
->allocated_vec
.end())
2764 size_hint
= it
->second
[0].size();
2765 if (size
% size_hint
|| start
% size_hint
)
2772 for (unsigned i
= 0; i
< size
; i
++)
2773 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2776 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2778 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2779 for (unsigned i
= 0; i
< size
; i
++)
2780 vec
->operands
[i
] = Operand(elems
[i
]);
2781 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2782 vec
->definitions
[0] = Definition(res
);
2783 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2787 void ds_write_helper(isel_context
*ctx
, Operand m
, Temp address
, Temp data
, unsigned data_start
, unsigned total_size
, unsigned offset0
, unsigned offset1
, unsigned align
)
2789 Builder
bld(ctx
->program
, ctx
->block
);
2790 unsigned bytes_written
= 0;
2791 while (bytes_written
< total_size
* 4) {
2792 unsigned todo
= total_size
* 4 - bytes_written
;
2793 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2794 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2796 aco_opcode op
= aco_opcode::last_opcode
;
2797 bool write2
= false;
2799 if (todo
>= 16 && aligned16
) {
2800 op
= aco_opcode::ds_write_b128
;
2802 } else if (todo
>= 16 && aligned8
) {
2803 op
= aco_opcode::ds_write2_b64
;
2806 } else if (todo
>= 12 && aligned16
) {
2807 op
= aco_opcode::ds_write_b96
;
2809 } else if (todo
>= 8 && aligned8
) {
2810 op
= aco_opcode::ds_write_b64
;
2812 } else if (todo
>= 8) {
2813 op
= aco_opcode::ds_write2_b32
;
2816 } else if (todo
>= 4) {
2817 op
= aco_opcode::ds_write_b32
;
2823 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2824 unsigned max_offset
= write2
? 1020 : 65535;
2825 Temp address_offset
= address
;
2826 if (offset
> max_offset
) {
2827 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2828 offset
= offset1
+ bytes_written
;
2830 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2833 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2834 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2835 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
2837 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2838 bld
.ds(op
, address_offset
, val
, m
, offset
);
2841 bytes_written
+= size
* 4;
2845 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2846 Temp address
, unsigned base_offset
, unsigned align
)
2848 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2850 Operand m
= load_lds_size_m0(ctx
);
2852 /* we need at most two stores for 32bit variables */
2853 int start
[2], count
[2];
2854 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2855 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2856 assert(wrmask
== 0);
2858 /* one combined store is sufficient */
2859 if (count
[0] == count
[1]) {
2860 Builder
bld(ctx
->program
, ctx
->block
);
2862 Temp address_offset
= address
;
2863 if ((base_offset
>> 2) + start
[1] > 255) {
2864 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2868 assert(count
[0] == 1);
2869 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2870 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2871 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2872 base_offset
= base_offset
/ elem_size_bytes
;
2873 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2874 base_offset
+ start
[0], base_offset
+ start
[1]);
2878 for (unsigned i
= 0; i
< 2; i
++) {
2882 unsigned elem_size_words
= elem_size_bytes
/ 4;
2883 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2884 base_offset
, start
[i
] * elem_size_bytes
, align
);
2889 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2891 if (ctx
->stage
== vertex_vs
) {
2892 visit_store_vs_output(ctx
, instr
);
2893 } else if (ctx
->stage
== fragment_fs
) {
2894 visit_store_fs_output(ctx
, instr
);
2896 unreachable("Shader stage not implemented");
2900 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2902 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2903 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2905 Builder
bld(ctx
->program
, ctx
->block
);
2906 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2907 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2910 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2912 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2913 for (unsigned i
= 0; i
< num_components
; i
++)
2914 vec
->operands
[i
] = Operand(ctx
->fs_inputs
[fs_input::frag_pos_0
+ i
]);
2916 if (ctx
->fs_vgpr_args
[fs_input::frag_pos_3
]) {
2917 assert(num_components
== 4);
2918 Builder
bld(ctx
->program
, ctx
->block
);
2919 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::frag_pos_3
]);
2922 for (Operand
& op
: vec
->operands
)
2923 op
= op
.isUndefined() ? Operand(0u) : op
;
2925 vec
->definitions
[0] = Definition(dst
);
2926 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2927 emit_split_vector(ctx
, dst
, num_components
);
2931 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2933 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2934 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2935 unsigned idx
= nir_intrinsic_base(instr
);
2936 unsigned component
= nir_intrinsic_component(instr
);
2937 Temp prim_mask
= ctx
->prim_mask
;
2939 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2941 assert(offset
->u32
== 0);
2943 /* the lower 15bit of the prim_mask contain the offset into LDS
2944 * while the upper bits contain the number of prims */
2945 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2946 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2947 Builder
bld(ctx
->program
, ctx
->block
);
2948 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2949 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2950 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2951 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2952 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2955 if (instr
->dest
.ssa
.num_components
== 1) {
2956 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2958 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2959 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2961 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2962 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2963 vec
->operands
[i
] = Operand(tmp
);
2965 vec
->definitions
[0] = Definition(dst
);
2966 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2970 unsigned get_num_channels_from_data_format(unsigned data_format
)
2972 switch (data_format
) {
2973 case V_008F0C_BUF_DATA_FORMAT_8
:
2974 case V_008F0C_BUF_DATA_FORMAT_16
:
2975 case V_008F0C_BUF_DATA_FORMAT_32
:
2977 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2978 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2979 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2981 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2982 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2983 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2985 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2986 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2987 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2988 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2989 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2998 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2999 * so we may need to fix it up. */
3000 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3002 Builder
bld(ctx
->program
, ctx
->block
);
3004 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3005 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3007 /* For the integer-like cases, do a natural sign extension.
3009 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3010 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3013 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3014 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3016 /* Convert back to the right type. */
3017 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3018 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3019 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
3020 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3021 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3022 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3028 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3030 Builder
bld(ctx
->program
, ctx
->block
);
3031 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3032 if (ctx
->stage
& sw_vs
) {
3034 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3035 if (off_instr
->type
!= nir_instr_type_load_const
) {
3036 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3037 nir_print_instr(off_instr
, stderr
);
3038 fprintf(stderr
, "\n");
3040 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3042 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, ctx
->vertex_buffers
);
3044 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3045 unsigned component
= nir_intrinsic_component(instr
);
3046 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3047 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3048 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3049 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3051 unsigned dfmt
= attrib_format
& 0xf;
3053 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3054 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
3055 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3056 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
3057 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3058 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3060 num_channels
= MAX2(num_channels
, 3);
3062 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
3065 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3066 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3068 ctx
->needs_instance_id
= true;
3071 Temp divided
= bld
.tmp(v1
);
3072 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, ctx
->instance_id
), divisor
);
3073 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, divided
);
3075 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, ctx
->instance_id
);
3078 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), ctx
->start_instance
);
3081 index
= bld
.vadd32(bld
.def(v1
), ctx
->base_vertex
, ctx
->vertex_id
);
3084 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
3085 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
3086 attrib_offset
= attrib_offset
% attrib_stride
;
3089 Operand
soffset(0u);
3090 if (attrib_offset
>= 4096) {
3091 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
3096 switch (num_channels
) {
3098 opcode
= aco_opcode::tbuffer_load_format_x
;
3101 opcode
= aco_opcode::tbuffer_load_format_xy
;
3104 opcode
= aco_opcode::tbuffer_load_format_xyz
;
3107 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
3110 unreachable("Unimplemented load_input vector size");
3113 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
3115 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
3116 mubuf
->operands
[0] = Operand(index
);
3117 mubuf
->operands
[1] = Operand(list
);
3118 mubuf
->operands
[2] = soffset
;
3119 mubuf
->definitions
[0] = Definition(tmp
);
3120 mubuf
->idxen
= true;
3121 mubuf
->can_reorder
= true;
3124 assert(attrib_offset
< 4096);
3125 mubuf
->offset
= attrib_offset
;
3126 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3128 emit_split_vector(ctx
, tmp
, tmp
.size());
3130 if (tmp
.id() != dst
.id()) {
3131 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3132 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3134 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3135 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3136 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3138 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3139 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3140 unsigned idx
= i
+ component
;
3141 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
3142 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
3143 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
3144 } else if (idx
< num_channels
) {
3145 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
3146 } else if (is_float
&& idx
== 3) {
3147 vec
->operands
[i
] = Operand(0x3f800000u
);
3148 } else if (!is_float
&& idx
== 3) {
3149 vec
->operands
[i
] = Operand(1u);
3151 vec
->operands
[i
] = Operand(0u);
3154 vec
->definitions
[0] = Definition(dst
);
3155 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3156 emit_split_vector(ctx
, dst
, dst
.size());
3159 } else if (ctx
->stage
== fragment_fs
) {
3160 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3161 if (off_instr
->type
!= nir_instr_type_load_const
||
3162 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3163 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3164 nir_print_instr(off_instr
, stderr
);
3165 fprintf(stderr
, "\n");
3168 Temp prim_mask
= ctx
->prim_mask
;
3169 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
3171 assert(offset
->u32
== 0);
3173 /* the lower 15bit of the prim_mask contain the offset into LDS
3174 * while the upper bits contain the number of prims */
3175 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3176 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3177 Builder
bld(ctx
->program
, ctx
->block
);
3178 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3179 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3180 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3181 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3182 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3185 unsigned idx
= nir_intrinsic_base(instr
);
3186 unsigned component
= nir_intrinsic_component(instr
);
3188 if (dst
.size() == 1) {
3189 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
3191 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3192 for (unsigned i
= 0; i
< dst
.size(); i
++)
3193 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
3194 vec
->definitions
[0] = Definition(dst
);
3195 bld
.insert(std::move(vec
));
3199 unreachable("Shader stage not implemented");
3203 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3205 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3206 Builder
bld(ctx
->program
, ctx
->block
);
3207 Temp ptr64
= convert_pointer_to_64_bit(ctx
, ctx
->descriptor_sets
[0]);
3208 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
3211 return ctx
->descriptor_sets
[desc_set
];
3215 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3217 Builder
bld(ctx
->program
, ctx
->block
);
3218 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3219 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3220 index
= bld
.as_uniform(index
);
3221 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3222 unsigned binding
= nir_intrinsic_binding(instr
);
3225 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3226 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3227 unsigned offset
= layout
->binding
[binding
].offset
;
3229 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3230 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3231 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3232 desc_ptr
= ctx
->push_constants
;
3233 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3236 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3237 stride
= layout
->binding
[binding
].size
;
3240 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3241 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3243 if (nir_const_index
) {
3244 const_index
= const_index
* stride
;
3245 } else if (index
.type() == RegType::vgpr
) {
3246 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3247 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3249 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3253 if (nir_const_index
) {
3254 const_index
= const_index
+ offset
;
3255 } else if (index
.type() == RegType::vgpr
) {
3256 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3258 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3262 if (nir_const_index
&& const_index
== 0) {
3264 } else if (index
.type() == RegType::vgpr
) {
3265 index
= bld
.vadd32(bld
.def(v1
),
3266 nir_const_index
? Operand(const_index
) : Operand(index
),
3269 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3270 nir_const_index
? Operand(const_index
) : Operand(index
),
3274 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3277 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
3278 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
3280 Builder
bld(ctx
->program
, ctx
->block
);
3282 unsigned num_bytes
= dst
.size() * 4;
3283 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3286 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3287 if (ctx
->options
->chip_class
< GFX8
)
3288 offset
= as_vgpr(ctx
, offset
);
3290 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3291 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3292 unsigned const_offset
= 0;
3294 Temp lower
= Temp();
3295 if (num_bytes
> 16) {
3296 assert(num_components
== 3 || num_components
== 4);
3297 op
= aco_opcode::buffer_load_dwordx4
;
3298 lower
= bld
.tmp(v4
);
3299 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3300 mubuf
->definitions
[0] = Definition(lower
);
3301 mubuf
->operands
[0] = vaddr
;
3302 mubuf
->operands
[1] = Operand(rsrc
);
3303 mubuf
->operands
[2] = soffset
;
3304 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3307 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3308 mubuf
->can_reorder
= readonly
;
3309 bld
.insert(std::move(mubuf
));
3310 emit_split_vector(ctx
, lower
, 2);
3315 switch (num_bytes
) {
3317 op
= aco_opcode::buffer_load_dword
;
3320 op
= aco_opcode::buffer_load_dwordx2
;
3323 op
= aco_opcode::buffer_load_dwordx3
;
3326 op
= aco_opcode::buffer_load_dwordx4
;
3329 unreachable("Load SSBO not implemented for this size.");
3331 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3332 mubuf
->operands
[0] = vaddr
;
3333 mubuf
->operands
[1] = Operand(rsrc
);
3334 mubuf
->operands
[2] = soffset
;
3335 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3338 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3339 mubuf
->can_reorder
= readonly
;
3340 mubuf
->offset
= const_offset
;
3341 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3343 if (dst
.size() > 4) {
3344 assert(lower
!= Temp());
3345 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3346 instr
->definitions
[0] = Definition(upper
);
3347 bld
.insert(std::move(instr
));
3348 if (dst
.size() == 8)
3349 emit_split_vector(ctx
, upper
, 2);
3350 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3351 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3352 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3353 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3354 if (dst
.size() == 8)
3355 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3358 if (dst
.type() == RegType::sgpr
) {
3359 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3360 instr
->definitions
[0] = Definition(vec
);
3361 bld
.insert(std::move(instr
));
3362 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3364 instr
->definitions
[0] = Definition(dst
);
3365 bld
.insert(std::move(instr
));
3368 switch (num_bytes
) {
3370 op
= aco_opcode::s_buffer_load_dword
;
3373 op
= aco_opcode::s_buffer_load_dwordx2
;
3377 op
= aco_opcode::s_buffer_load_dwordx4
;
3381 op
= aco_opcode::s_buffer_load_dwordx8
;
3384 unreachable("Load SSBO not implemented for this size.");
3386 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3387 load
->operands
[0] = Operand(rsrc
);
3388 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3389 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3390 load
->definitions
[0] = Definition(dst
);
3393 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3394 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3395 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3398 if (dst
.size() == 3) {
3399 Temp vec
= bld
.tmp(s4
);
3400 load
->definitions
[0] = Definition(vec
);
3401 bld
.insert(std::move(load
));
3402 emit_split_vector(ctx
, vec
, 4);
3404 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3405 emit_extract_vector(ctx
, vec
, 0, s1
),
3406 emit_extract_vector(ctx
, vec
, 1, s1
),
3407 emit_extract_vector(ctx
, vec
, 2, s1
));
3408 } else if (dst
.size() == 6) {
3409 Temp vec
= bld
.tmp(s8
);
3410 load
->definitions
[0] = Definition(vec
);
3411 bld
.insert(std::move(load
));
3412 emit_split_vector(ctx
, vec
, 4);
3414 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3415 emit_extract_vector(ctx
, vec
, 0, s2
),
3416 emit_extract_vector(ctx
, vec
, 1, s2
),
3417 emit_extract_vector(ctx
, vec
, 2, s2
));
3419 bld
.insert(std::move(load
));
3423 emit_split_vector(ctx
, dst
, num_components
);
3426 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3428 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3429 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3431 Builder
bld(ctx
->program
, ctx
->block
);
3433 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3434 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3435 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3436 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3438 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3439 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3440 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3441 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3442 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3443 if (ctx
->options
->chip_class
>= GFX10
) {
3444 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3445 S_008F0C_OOB_SELECT(3) |
3446 S_008F0C_RESOURCE_LEVEL(1);
3448 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3449 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3451 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3452 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3453 Operand(0xFFFFFFFFu
),
3454 Operand(desc_type
));
3455 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3456 rsrc
, upper_dwords
);
3458 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3459 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3462 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3465 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3467 Builder
bld(ctx
->program
, ctx
->block
);
3468 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3470 unsigned offset
= nir_intrinsic_base(instr
);
3471 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3472 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3474 unsigned count
= instr
->dest
.ssa
.num_components
;
3475 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3476 start
-= ctx
->base_inline_push_consts
;
3477 if (start
+ count
<= ctx
->num_inline_push_consts
) {
3478 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3479 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3480 for (unsigned i
= 0; i
< count
; ++i
) {
3481 elems
[i
] = ctx
->inline_push_consts
[start
+ i
];
3482 vec
->operands
[i
] = Operand
{elems
[i
]};
3484 vec
->definitions
[0] = Definition(dst
);
3485 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3486 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3491 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3492 if (offset
!= 0) // TODO check if index != 0 as well
3493 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3494 Temp ptr
= convert_pointer_to_64_bit(ctx
, ctx
->push_constants
);
3499 switch (dst
.size()) {
3501 op
= aco_opcode::s_load_dword
;
3504 op
= aco_opcode::s_load_dwordx2
;
3510 op
= aco_opcode::s_load_dwordx4
;
3516 op
= aco_opcode::s_load_dwordx8
;
3519 unreachable("unimplemented or forbidden load_push_constant.");
3522 bld
.smem(op
, Definition(vec
), ptr
, index
);
3525 emit_split_vector(ctx
, vec
, 4);
3526 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3527 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3528 emit_extract_vector(ctx
, vec
, 0, rc
),
3529 emit_extract_vector(ctx
, vec
, 1, rc
),
3530 emit_extract_vector(ctx
, vec
, 2, rc
));
3533 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3536 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3538 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3540 Builder
bld(ctx
->program
, ctx
->block
);
3542 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3543 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3544 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3545 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3546 if (ctx
->options
->chip_class
>= GFX10
) {
3547 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3548 S_008F0C_OOB_SELECT(3) |
3549 S_008F0C_RESOURCE_LEVEL(1);
3551 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3552 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3555 unsigned base
= nir_intrinsic_base(instr
);
3556 unsigned range
= nir_intrinsic_range(instr
);
3558 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3559 if (base
&& offset
.type() == RegType::sgpr
)
3560 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3561 else if (base
&& offset
.type() == RegType::vgpr
)
3562 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3564 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3565 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3566 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3567 Operand(desc_type
));
3569 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3572 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3574 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3575 ctx
->cf_info
.exec_potentially_empty
= true;
3577 ctx
->program
->needs_exact
= true;
3579 // TODO: optimize uniform conditions
3580 Builder
bld(ctx
->program
, ctx
->block
);
3581 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3582 assert(src
.regClass() == s2
);
3583 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3584 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3585 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3589 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3591 Builder
bld(ctx
->program
, ctx
->block
);
3593 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3594 ctx
->cf_info
.exec_potentially_empty
= true;
3596 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3597 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3599 if (ctx
->block
->loop_nest_depth
&&
3600 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3601 /* we handle discards the same way as jump instructions */
3602 append_logical_end(ctx
->block
);
3604 /* in loops, discard behaves like break */
3605 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3606 ctx
->block
->kind
|= block_kind_discard
;
3609 /* uniform discard - loop ends here */
3610 assert(nir_instr_is_last(&instr
->instr
));
3611 ctx
->block
->kind
|= block_kind_uniform
;
3612 ctx
->cf_info
.has_branch
= true;
3613 bld
.branch(aco_opcode::p_branch
);
3614 add_linear_edge(ctx
->block
->index
, linear_target
);
3618 /* we add a break right behind the discard() instructions */
3619 ctx
->block
->kind
|= block_kind_break
;
3620 unsigned idx
= ctx
->block
->index
;
3622 /* remove critical edges from linear CFG */
3623 bld
.branch(aco_opcode::p_branch
);
3624 Block
* break_block
= ctx
->program
->create_and_insert_block();
3625 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3626 break_block
->kind
|= block_kind_uniform
;
3627 add_linear_edge(idx
, break_block
);
3628 add_linear_edge(break_block
->index
, linear_target
);
3629 bld
.reset(break_block
);
3630 bld
.branch(aco_opcode::p_branch
);
3632 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3633 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3634 add_linear_edge(idx
, continue_block
);
3635 append_logical_start(continue_block
);
3636 ctx
->block
= continue_block
;
3641 /* it can currently happen that NIR doesn't remove the unreachable code */
3642 if (!nir_instr_is_last(&instr
->instr
)) {
3643 ctx
->program
->needs_exact
= true;
3644 /* save exec somewhere temporarily so that it doesn't get
3645 * overwritten before the discard from outer exec masks */
3646 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3647 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3648 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3652 /* This condition is incorrect for uniformly branched discards in a loop
3653 * predicated by a divergent condition, but the above code catches that case
3654 * and the discard would end up turning into a discard_if.
3664 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3665 /* program just ends here */
3666 ctx
->block
->kind
|= block_kind_uniform
;
3667 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3668 0 /* enabled mask */, 9 /* dest */,
3669 false /* compressed */, true/* done */, true /* valid mask */);
3670 bld
.sopp(aco_opcode::s_endpgm
);
3671 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3673 ctx
->block
->kind
|= block_kind_discard
;
3674 /* branch and linear edge is added by visit_if() */
3678 enum aco_descriptor_type
{
3689 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3690 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3692 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
3693 return dim
== ac_image_cube
||
3694 dim
== ac_image_1darray
||
3695 dim
== ac_image_2darray
||
3696 dim
== ac_image_2darraymsaa
;
3699 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3700 enum aco_descriptor_type desc_type
,
3701 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3703 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3704 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3705 if (it != ctx->tex_desc.end())
3708 Temp index
= Temp();
3709 bool index_set
= false;
3710 unsigned constant_index
= 0;
3711 unsigned descriptor_set
;
3712 unsigned base_index
;
3713 Builder
bld(ctx
->program
, ctx
->block
);
3716 assert(tex_instr
&& !image
);
3718 base_index
= tex_instr
->sampler_index
;
3720 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3721 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3725 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3726 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3728 constant_index
+= array_size
* const_value
->u32
;
3730 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
3731 if (indirect
.type() == RegType::vgpr
)
3732 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
3734 if (array_size
!= 1)
3735 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3741 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3745 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3747 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3748 base_index
= deref_instr
->var
->data
.binding
;
3751 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3752 list
= convert_pointer_to_64_bit(ctx
, list
);
3754 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3755 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3756 unsigned offset
= binding
->offset
;
3757 unsigned stride
= binding
->size
;
3761 assert(base_index
< layout
->binding_count
);
3763 switch (desc_type
) {
3764 case ACO_DESC_IMAGE
:
3766 opcode
= aco_opcode::s_load_dwordx8
;
3768 case ACO_DESC_FMASK
:
3770 opcode
= aco_opcode::s_load_dwordx8
;
3773 case ACO_DESC_SAMPLER
:
3775 opcode
= aco_opcode::s_load_dwordx4
;
3776 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3777 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3779 case ACO_DESC_BUFFER
:
3781 opcode
= aco_opcode::s_load_dwordx4
;
3783 case ACO_DESC_PLANE_0
:
3784 case ACO_DESC_PLANE_1
:
3786 opcode
= aco_opcode::s_load_dwordx8
;
3787 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3789 case ACO_DESC_PLANE_2
:
3791 opcode
= aco_opcode::s_load_dwordx4
;
3795 unreachable("invalid desc_type\n");
3798 offset
+= constant_index
* stride
;
3800 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3801 (!index_set
|| binding
->immutable_samplers_equal
)) {
3802 if (binding
->immutable_samplers_equal
)
3805 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3806 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3807 Operand(samplers
[constant_index
* 4 + 0]),
3808 Operand(samplers
[constant_index
* 4 + 1]),
3809 Operand(samplers
[constant_index
* 4 + 2]),
3810 Operand(samplers
[constant_index
* 4 + 3]));
3815 off
= Operand(offset
);
3817 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3818 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3821 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3823 if (desc_type
== ACO_DESC_PLANE_2
) {
3825 for (unsigned i
= 0; i
< 8; i
++)
3826 components
[i
] = bld
.tmp(s1
);
3827 bld
.pseudo(aco_opcode::p_split_vector
,
3828 Definition(components
[0]),
3829 Definition(components
[1]),
3830 Definition(components
[2]),
3831 Definition(components
[3]),
3834 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3835 bld
.pseudo(aco_opcode::p_split_vector
,
3836 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3837 Definition(components
[4]),
3838 Definition(components
[5]),
3839 Definition(components
[6]),
3840 Definition(components
[7]),
3843 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3844 components
[0], components
[1], components
[2], components
[3],
3845 components
[4], components
[5], components
[6], components
[7]);
3851 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3854 case GLSL_SAMPLER_DIM_BUF
:
3856 case GLSL_SAMPLER_DIM_1D
:
3857 return array
? 2 : 1;
3858 case GLSL_SAMPLER_DIM_2D
:
3859 return array
? 3 : 2;
3860 case GLSL_SAMPLER_DIM_MS
:
3861 return array
? 4 : 3;
3862 case GLSL_SAMPLER_DIM_3D
:
3863 case GLSL_SAMPLER_DIM_CUBE
:
3865 case GLSL_SAMPLER_DIM_RECT
:
3866 case GLSL_SAMPLER_DIM_SUBPASS
:
3868 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3877 /* Adjust the sample index according to FMASK.
3879 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3880 * which is the identity mapping. Each nibble says which physical sample
3881 * should be fetched to get that sample.
3883 * For example, 0x11111100 means there are only 2 samples stored and
3884 * the second sample covers 3/4 of the pixel. When reading samples 0
3885 * and 1, return physical sample 0 (determined by the first two 0s
3886 * in FMASK), otherwise return physical sample 1.
3888 * The sample index should be adjusted as follows:
3889 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3891 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3893 Builder
bld(ctx
->program
, ctx
->block
);
3894 Temp fmask
= bld
.tmp(v1
);
3895 unsigned dim
= ctx
->options
->chip_class
>= GFX10
3896 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
3899 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3900 load
->operands
[0] = Operand(coords
);
3901 load
->operands
[1] = Operand(fmask_desc_ptr
);
3902 load
->definitions
[0] = Definition(fmask
);
3909 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3910 ctx
->block
->instructions
.emplace_back(std::move(load
));
3912 Operand sample_index4
;
3913 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3914 sample_index4
= Operand(sample_index
.constantValue() << 2);
3915 } else if (sample_index
.regClass() == s1
) {
3916 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3918 assert(sample_index
.regClass() == v1
);
3919 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3923 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3924 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3925 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3926 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3928 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3930 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3931 * resource descriptor is 0 (invalid),
3933 Temp compare
= bld
.tmp(s2
);
3934 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3935 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3937 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3939 /* Replace the MSAA sample index. */
3940 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3943 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3946 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3947 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3948 bool is_array
= glsl_sampler_type_is_array(type
);
3949 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3950 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3951 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3952 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3953 int count
= image_type_to_components_count(dim
, is_array
);
3954 std::vector
<Operand
> coords(count
);
3957 Operand sample_index
;
3958 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3960 sample_index
= Operand(sample_cv
->u32
);
3962 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3964 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3965 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3966 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3967 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3968 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3969 vec
->definitions
[0] = Definition(fmask_load_address
);
3970 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3972 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3973 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3976 coords
[count
] = sample_index
;
3979 if (count
== 1 && !gfx9_1d
)
3980 return emit_extract_vector(ctx
, src0
, 0, v1
);
3983 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3984 coords
.resize(coords
.size() + 1);
3985 coords
[1] = Operand((uint32_t) 0);
3987 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3989 for (int i
= 0; i
< count
; i
++)
3990 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3993 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3994 for (unsigned i
= 0; i
< coords
.size(); i
++)
3995 vec
->operands
[i
] = coords
[i
];
3996 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3997 vec
->definitions
[0] = Definition(res
);
3998 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4003 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4005 Builder
bld(ctx
->program
, ctx
->block
);
4006 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4007 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4008 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4009 bool is_array
= glsl_sampler_type_is_array(type
);
4010 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4012 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4013 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4014 unsigned num_channels
= util_last_bit(mask
);
4015 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4016 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4019 switch (num_channels
) {
4021 opcode
= aco_opcode::buffer_load_format_x
;
4024 opcode
= aco_opcode::buffer_load_format_xy
;
4027 opcode
= aco_opcode::buffer_load_format_xyz
;
4030 opcode
= aco_opcode::buffer_load_format_xyzw
;
4033 unreachable(">4 channel buffer image load");
4035 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4036 load
->operands
[0] = Operand(vindex
);
4037 load
->operands
[1] = Operand(rsrc
);
4038 load
->operands
[2] = Operand((uint32_t) 0);
4040 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4043 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4044 load
->definitions
[0] = Definition(tmp
);
4046 load
->barrier
= barrier_image
;
4047 ctx
->block
->instructions
.emplace_back(std::move(load
));
4049 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4053 Temp coords
= get_image_coords(ctx
, instr
, type
);
4054 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4056 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4057 unsigned num_components
= util_bitcount(dmask
);
4059 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4062 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4064 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
4065 load
->operands
[0] = Operand(coords
);
4066 load
->operands
[1] = Operand(resource
);
4067 load
->definitions
[0] = Definition(tmp
);
4068 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4069 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4070 load
->dmask
= dmask
;
4072 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4073 load
->barrier
= barrier_image
;
4074 ctx
->block
->instructions
.emplace_back(std::move(load
));
4076 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4080 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4082 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4083 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4084 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4085 bool is_array
= glsl_sampler_type_is_array(type
);
4086 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4088 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4090 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4091 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4092 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4094 switch (data
.size()) {
4096 opcode
= aco_opcode::buffer_store_format_x
;
4099 opcode
= aco_opcode::buffer_store_format_xy
;
4102 opcode
= aco_opcode::buffer_store_format_xyz
;
4105 opcode
= aco_opcode::buffer_store_format_xyzw
;
4108 unreachable(">4 channel buffer image store");
4110 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4111 store
->operands
[0] = Operand(vindex
);
4112 store
->operands
[1] = Operand(rsrc
);
4113 store
->operands
[2] = Operand((uint32_t) 0);
4114 store
->operands
[3] = Operand(data
);
4115 store
->idxen
= true;
4118 store
->disable_wqm
= true;
4119 store
->barrier
= barrier_image
;
4120 ctx
->program
->needs_exact
= true;
4121 ctx
->block
->instructions
.emplace_back(std::move(store
));
4125 assert(data
.type() == RegType::vgpr
);
4126 Temp coords
= get_image_coords(ctx
, instr
, type
);
4127 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4129 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
4130 store
->operands
[0] = Operand(coords
);
4131 store
->operands
[1] = Operand(resource
);
4132 store
->operands
[2] = Operand(s4
);
4133 store
->operands
[3] = Operand(data
);
4136 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4137 store
->dmask
= (1 << data
.size()) - 1;
4139 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4140 store
->disable_wqm
= true;
4141 store
->barrier
= barrier_image
;
4142 ctx
->program
->needs_exact
= true;
4143 ctx
->block
->instructions
.emplace_back(std::move(store
));
4147 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4149 /* return the previous value if dest is ever used */
4150 bool return_previous
= false;
4151 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4152 return_previous
= true;
4155 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4156 return_previous
= true;
4160 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4161 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4162 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4163 bool is_array
= glsl_sampler_type_is_array(type
);
4164 Builder
bld(ctx
->program
, ctx
->block
);
4166 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4167 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4169 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4170 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4172 aco_opcode buf_op
, image_op
;
4173 switch (instr
->intrinsic
) {
4174 case nir_intrinsic_image_deref_atomic_add
:
4175 buf_op
= aco_opcode::buffer_atomic_add
;
4176 image_op
= aco_opcode::image_atomic_add
;
4178 case nir_intrinsic_image_deref_atomic_umin
:
4179 buf_op
= aco_opcode::buffer_atomic_umin
;
4180 image_op
= aco_opcode::image_atomic_umin
;
4182 case nir_intrinsic_image_deref_atomic_imin
:
4183 buf_op
= aco_opcode::buffer_atomic_smin
;
4184 image_op
= aco_opcode::image_atomic_smin
;
4186 case nir_intrinsic_image_deref_atomic_umax
:
4187 buf_op
= aco_opcode::buffer_atomic_umax
;
4188 image_op
= aco_opcode::image_atomic_umax
;
4190 case nir_intrinsic_image_deref_atomic_imax
:
4191 buf_op
= aco_opcode::buffer_atomic_smax
;
4192 image_op
= aco_opcode::image_atomic_smax
;
4194 case nir_intrinsic_image_deref_atomic_and
:
4195 buf_op
= aco_opcode::buffer_atomic_and
;
4196 image_op
= aco_opcode::image_atomic_and
;
4198 case nir_intrinsic_image_deref_atomic_or
:
4199 buf_op
= aco_opcode::buffer_atomic_or
;
4200 image_op
= aco_opcode::image_atomic_or
;
4202 case nir_intrinsic_image_deref_atomic_xor
:
4203 buf_op
= aco_opcode::buffer_atomic_xor
;
4204 image_op
= aco_opcode::image_atomic_xor
;
4206 case nir_intrinsic_image_deref_atomic_exchange
:
4207 buf_op
= aco_opcode::buffer_atomic_swap
;
4208 image_op
= aco_opcode::image_atomic_swap
;
4210 case nir_intrinsic_image_deref_atomic_comp_swap
:
4211 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4212 image_op
= aco_opcode::image_atomic_cmpswap
;
4215 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4218 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4220 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4221 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4222 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4223 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4224 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4225 mubuf
->operands
[0] = Operand(vindex
);
4226 mubuf
->operands
[1] = Operand(resource
);
4227 mubuf
->operands
[2] = Operand((uint32_t)0);
4228 mubuf
->operands
[3] = Operand(data
);
4229 if (return_previous
)
4230 mubuf
->definitions
[0] = Definition(dst
);
4232 mubuf
->idxen
= true;
4233 mubuf
->glc
= return_previous
;
4234 mubuf
->dlc
= false; /* Not needed for atomics */
4235 mubuf
->disable_wqm
= true;
4236 mubuf
->barrier
= barrier_image
;
4237 ctx
->program
->needs_exact
= true;
4238 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4242 Temp coords
= get_image_coords(ctx
, instr
, type
);
4243 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4244 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
4245 mimg
->operands
[0] = Operand(coords
);
4246 mimg
->operands
[1] = Operand(resource
);
4247 mimg
->operands
[2] = Operand(s4
); /* no sampler */
4248 mimg
->operands
[3] = Operand(data
);
4249 if (return_previous
)
4250 mimg
->definitions
[0] = Definition(dst
);
4251 mimg
->glc
= return_previous
;
4252 mimg
->dlc
= false; /* Not needed for atomics */
4253 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4254 mimg
->dmask
= (1 << data
.size()) - 1;
4256 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4257 mimg
->disable_wqm
= true;
4258 mimg
->barrier
= barrier_image
;
4259 ctx
->program
->needs_exact
= true;
4260 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4264 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4266 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4267 Builder
bld(ctx
->program
, ctx
->block
);
4269 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4270 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4271 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4272 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4274 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4275 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4277 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4278 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4279 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4281 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4283 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4284 * in case 12 (or 3?), we have to divide by 3:
4285 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4286 * use v_mul_hi_u32 with magic number to divide
4287 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4289 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4293 emit_extract_vector(ctx
, desc
, 2, dst
);
4297 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4299 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4300 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4301 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4302 bool is_array
= glsl_sampler_type_is_array(type
);
4303 Builder
bld(ctx
->program
, ctx
->block
);
4305 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4306 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4307 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4311 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4314 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4316 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4318 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4319 mimg
->operands
[0] = Operand(lod
);
4320 mimg
->operands
[1] = Operand(resource
);
4321 unsigned& dmask
= mimg
->dmask
;
4322 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4323 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4324 mimg
->da
= glsl_sampler_type_is_array(type
);
4325 mimg
->can_reorder
= true;
4326 Definition
& def
= mimg
->definitions
[0];
4327 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4329 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4330 glsl_sampler_type_is_array(type
)) {
4332 assert(instr
->dest
.ssa
.num_components
== 3);
4333 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4334 def
= Definition(tmp
);
4335 emit_split_vector(ctx
, tmp
, 3);
4337 /* divide 3rd value by 6 by multiplying with magic number */
4338 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4339 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4341 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4342 emit_extract_vector(ctx
, tmp
, 0, v1
),
4343 emit_extract_vector(ctx
, tmp
, 1, v1
),
4346 } else if (ctx
->options
->chip_class
== GFX9
&&
4347 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4348 glsl_sampler_type_is_array(type
)) {
4349 assert(instr
->dest
.ssa
.num_components
== 2);
4350 def
= Definition(dst
);
4353 def
= Definition(dst
);
4356 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4359 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4361 Builder
bld(ctx
->program
, ctx
->block
);
4362 unsigned num_components
= instr
->num_components
;
4364 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4365 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4366 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4368 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4369 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
4372 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4374 Builder
bld(ctx
->program
, ctx
->block
);
4375 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4376 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4377 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4380 if (ctx
->options
->chip_class
< GFX8
)
4381 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4383 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4385 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4386 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4388 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4389 ctx
->options
->chip_class
>= GFX8
;
4391 offset
= bld
.as_uniform(offset
);
4392 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4396 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4397 if (count
== 3 && smem
) {
4398 writemask
|= 1u << (start
+ 2);
4401 int num_bytes
= count
* elem_size_bytes
;
4403 if (num_bytes
> 16) {
4404 assert(elem_size_bytes
== 8);
4405 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4410 // TODO: check alignment of sub-dword stores
4411 // TODO: split 3 bytes. there is no store instruction for that
4414 if (count
!= instr
->num_components
) {
4415 emit_split_vector(ctx
, data
, instr
->num_components
);
4416 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4417 for (int i
= 0; i
< count
; i
++) {
4418 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4419 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4421 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4422 vec
->definitions
[0] = Definition(write_data
);
4423 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4424 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4425 assert(num_bytes
% 4 == 0);
4426 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4427 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4428 assert(num_bytes
% 4 == 0);
4429 write_data
= bld
.as_uniform(data
);
4434 aco_opcode vmem_op
, smem_op
;
4435 switch (num_bytes
) {
4437 vmem_op
= aco_opcode::buffer_store_dword
;
4438 smem_op
= aco_opcode::s_buffer_store_dword
;
4441 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4442 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4445 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4446 smem_op
= aco_opcode::last_opcode
;
4450 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4451 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4454 unreachable("Store SSBO not implemented for this size.");
4456 if (ctx
->stage
== fragment_fs
)
4457 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4460 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4461 store
->operands
[0] = Operand(rsrc
);
4463 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4464 offset
, Operand(start
* elem_size_bytes
));
4465 store
->operands
[1] = Operand(off
);
4467 store
->operands
[1] = Operand(offset
);
4469 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4470 store
->operands
[1].setFixed(m0
);
4471 store
->operands
[2] = Operand(write_data
);
4472 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4474 store
->disable_wqm
= true;
4475 store
->barrier
= barrier_buffer
;
4476 ctx
->block
->instructions
.emplace_back(std::move(store
));
4477 ctx
->program
->wb_smem_l1_on_end
= true;
4478 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4479 ctx
->block
->kind
|= block_kind_needs_lowering
;
4480 ctx
->program
->needs_exact
= true;
4483 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4484 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4485 store
->operands
[1] = Operand(rsrc
);
4486 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4487 store
->operands
[3] = Operand(write_data
);
4488 store
->offset
= start
* elem_size_bytes
;
4489 store
->offen
= (offset
.type() == RegType::vgpr
);
4490 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4492 store
->disable_wqm
= true;
4493 store
->barrier
= barrier_buffer
;
4494 ctx
->program
->needs_exact
= true;
4495 ctx
->block
->instructions
.emplace_back(std::move(store
));
4500 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4502 /* return the previous value if dest is ever used */
4503 bool return_previous
= false;
4504 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4505 return_previous
= true;
4508 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4509 return_previous
= true;
4513 Builder
bld(ctx
->program
, ctx
->block
);
4514 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4516 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4517 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4518 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4521 if (ctx
->options
->chip_class
< GFX8
)
4522 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4524 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4526 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4527 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4529 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4531 aco_opcode op32
, op64
;
4532 switch (instr
->intrinsic
) {
4533 case nir_intrinsic_ssbo_atomic_add
:
4534 op32
= aco_opcode::buffer_atomic_add
;
4535 op64
= aco_opcode::buffer_atomic_add_x2
;
4537 case nir_intrinsic_ssbo_atomic_imin
:
4538 op32
= aco_opcode::buffer_atomic_smin
;
4539 op64
= aco_opcode::buffer_atomic_smin_x2
;
4541 case nir_intrinsic_ssbo_atomic_umin
:
4542 op32
= aco_opcode::buffer_atomic_umin
;
4543 op64
= aco_opcode::buffer_atomic_umin_x2
;
4545 case nir_intrinsic_ssbo_atomic_imax
:
4546 op32
= aco_opcode::buffer_atomic_smax
;
4547 op64
= aco_opcode::buffer_atomic_smax_x2
;
4549 case nir_intrinsic_ssbo_atomic_umax
:
4550 op32
= aco_opcode::buffer_atomic_umax
;
4551 op64
= aco_opcode::buffer_atomic_umax_x2
;
4553 case nir_intrinsic_ssbo_atomic_and
:
4554 op32
= aco_opcode::buffer_atomic_and
;
4555 op64
= aco_opcode::buffer_atomic_and_x2
;
4557 case nir_intrinsic_ssbo_atomic_or
:
4558 op32
= aco_opcode::buffer_atomic_or
;
4559 op64
= aco_opcode::buffer_atomic_or_x2
;
4561 case nir_intrinsic_ssbo_atomic_xor
:
4562 op32
= aco_opcode::buffer_atomic_xor
;
4563 op64
= aco_opcode::buffer_atomic_xor_x2
;
4565 case nir_intrinsic_ssbo_atomic_exchange
:
4566 op32
= aco_opcode::buffer_atomic_swap
;
4567 op64
= aco_opcode::buffer_atomic_swap_x2
;
4569 case nir_intrinsic_ssbo_atomic_comp_swap
:
4570 op32
= aco_opcode::buffer_atomic_cmpswap
;
4571 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4574 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4576 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4577 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4578 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4579 mubuf
->operands
[1] = Operand(rsrc
);
4580 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4581 mubuf
->operands
[3] = Operand(data
);
4582 if (return_previous
)
4583 mubuf
->definitions
[0] = Definition(dst
);
4585 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4586 mubuf
->glc
= return_previous
;
4587 mubuf
->dlc
= false; /* Not needed for atomics */
4588 mubuf
->disable_wqm
= true;
4589 mubuf
->barrier
= barrier_buffer
;
4590 ctx
->program
->needs_exact
= true;
4591 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4594 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4596 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4597 Builder
bld(ctx
->program
, ctx
->block
);
4598 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4599 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4602 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4604 Builder
bld(ctx
->program
, ctx
->block
);
4605 unsigned num_components
= instr
->num_components
;
4606 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4608 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4609 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4611 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4612 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4614 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4615 bool global
= ctx
->options
->chip_class
>= GFX9
;
4617 switch (num_bytes
) {
4619 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4622 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4625 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4628 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4631 unreachable("load_global not implemented for this size.");
4633 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4634 flat
->operands
[0] = Operand(addr
);
4635 flat
->operands
[1] = Operand(s1
);
4639 if (dst
.type() == RegType::sgpr
) {
4640 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4641 flat
->definitions
[0] = Definition(vec
);
4642 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4643 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4645 flat
->definitions
[0] = Definition(dst
);
4646 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4648 emit_split_vector(ctx
, dst
, num_components
);
4650 switch (num_bytes
) {
4652 op
= aco_opcode::s_load_dword
;
4655 op
= aco_opcode::s_load_dwordx2
;
4659 op
= aco_opcode::s_load_dwordx4
;
4662 unreachable("load_global not implemented for this size.");
4664 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4665 load
->operands
[0] = Operand(addr
);
4666 load
->operands
[1] = Operand(0u);
4667 load
->definitions
[0] = Definition(dst
);
4670 load
->barrier
= barrier_buffer
;
4671 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4673 if (dst
.size() == 3) {
4675 Temp vec
= bld
.tmp(s4
);
4676 load
->definitions
[0] = Definition(vec
);
4677 ctx
->block
->instructions
.emplace_back(std::move(load
));
4678 emit_split_vector(ctx
, vec
, 4);
4680 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4681 emit_extract_vector(ctx
, vec
, 0, s1
),
4682 emit_extract_vector(ctx
, vec
, 1, s1
),
4683 emit_extract_vector(ctx
, vec
, 2, s1
));
4685 ctx
->block
->instructions
.emplace_back(std::move(load
));
4690 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4692 Builder
bld(ctx
->program
, ctx
->block
);
4693 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4695 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4696 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4698 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4701 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4702 unsigned num_bytes
= count
* elem_size_bytes
;
4704 Temp write_data
= data
;
4705 if (count
!= instr
->num_components
) {
4706 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4707 for (int i
= 0; i
< count
; i
++)
4708 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4709 write_data
= bld
.tmp(RegType::vgpr
, count
);
4710 vec
->definitions
[0] = Definition(write_data
);
4711 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4714 unsigned offset
= start
* elem_size_bytes
;
4715 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4716 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4717 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4718 Temp carry
= bld
.tmp(s2
);
4719 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4721 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4722 Operand(offset
), addr0
);
4723 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4725 carry
).def(1).setHint(vcc
);
4727 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4732 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4733 bool global
= ctx
->options
->chip_class
>= GFX9
;
4735 switch (num_bytes
) {
4737 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4740 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4743 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4746 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4749 unreachable("store_global not implemented for this size.");
4751 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4752 flat
->operands
[0] = Operand(addr
);
4753 flat
->operands
[1] = Operand(s1
);
4754 flat
->operands
[2] = Operand(data
);
4757 flat
->offset
= offset
;
4758 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4762 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4763 Builder
bld(ctx
->program
, ctx
->block
);
4764 switch(instr
->intrinsic
) {
4765 case nir_intrinsic_group_memory_barrier
:
4766 case nir_intrinsic_memory_barrier
:
4767 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4769 case nir_intrinsic_memory_barrier_atomic_counter
:
4770 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4772 case nir_intrinsic_memory_barrier_buffer
:
4773 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4775 case nir_intrinsic_memory_barrier_image
:
4776 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4778 case nir_intrinsic_memory_barrier_shared
:
4779 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4782 unreachable("Unimplemented memory barrier intrinsic");
4787 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4789 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4790 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4791 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4792 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4793 Builder
bld(ctx
->program
, ctx
->block
);
4795 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4796 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4797 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
4800 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4802 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4803 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4804 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4805 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4806 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4808 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4809 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
4812 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4814 unsigned offset
= nir_intrinsic_base(instr
);
4815 Operand m
= load_lds_size_m0(ctx
);
4816 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4817 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4819 unsigned num_operands
= 3;
4820 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4821 switch(instr
->intrinsic
) {
4822 case nir_intrinsic_shared_atomic_add
:
4823 op32
= aco_opcode::ds_add_u32
;
4824 op64
= aco_opcode::ds_add_u64
;
4825 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4826 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4828 case nir_intrinsic_shared_atomic_imin
:
4829 op32
= aco_opcode::ds_min_i32
;
4830 op64
= aco_opcode::ds_min_i64
;
4831 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4832 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4834 case nir_intrinsic_shared_atomic_umin
:
4835 op32
= aco_opcode::ds_min_u32
;
4836 op64
= aco_opcode::ds_min_u64
;
4837 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4838 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4840 case nir_intrinsic_shared_atomic_imax
:
4841 op32
= aco_opcode::ds_max_i32
;
4842 op64
= aco_opcode::ds_max_i64
;
4843 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4844 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4846 case nir_intrinsic_shared_atomic_umax
:
4847 op32
= aco_opcode::ds_max_u32
;
4848 op64
= aco_opcode::ds_max_u64
;
4849 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4850 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4852 case nir_intrinsic_shared_atomic_and
:
4853 op32
= aco_opcode::ds_and_b32
;
4854 op64
= aco_opcode::ds_and_b64
;
4855 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4856 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4858 case nir_intrinsic_shared_atomic_or
:
4859 op32
= aco_opcode::ds_or_b32
;
4860 op64
= aco_opcode::ds_or_b64
;
4861 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4862 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4864 case nir_intrinsic_shared_atomic_xor
:
4865 op32
= aco_opcode::ds_xor_b32
;
4866 op64
= aco_opcode::ds_xor_b64
;
4867 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4868 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4870 case nir_intrinsic_shared_atomic_exchange
:
4871 op32
= aco_opcode::ds_write_b32
;
4872 op64
= aco_opcode::ds_write_b64
;
4873 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4874 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4876 case nir_intrinsic_shared_atomic_comp_swap
:
4877 op32
= aco_opcode::ds_cmpst_b32
;
4878 op64
= aco_opcode::ds_cmpst_b64
;
4879 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4880 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4884 unreachable("Unhandled shared atomic intrinsic");
4887 /* return the previous value if dest is ever used */
4888 bool return_previous
= false;
4889 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4890 return_previous
= true;
4893 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4894 return_previous
= true;
4899 if (data
.size() == 1) {
4900 assert(instr
->dest
.ssa
.bit_size
== 32);
4901 op
= return_previous
? op32_rtn
: op32
;
4903 assert(instr
->dest
.ssa
.bit_size
== 64);
4904 op
= return_previous
? op64_rtn
: op64
;
4907 if (offset
> 65535) {
4908 Builder
bld(ctx
->program
, ctx
->block
);
4909 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4913 aco_ptr
<DS_instruction
> ds
;
4914 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4915 ds
->operands
[0] = Operand(address
);
4916 ds
->operands
[1] = Operand(data
);
4917 if (num_operands
== 4)
4918 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4919 ds
->operands
[num_operands
- 1] = m
;
4920 ds
->offset0
= offset
;
4921 if (return_previous
)
4922 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4923 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4926 Temp
get_scratch_resource(isel_context
*ctx
)
4928 Builder
bld(ctx
->program
, ctx
->block
);
4929 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
4930 if (ctx
->stage
!= compute_cs
)
4931 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
4933 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
4934 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
4936 if (ctx
->program
->chip_class
>= GFX10
) {
4937 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4938 S_008F0C_OOB_SELECT(3) |
4939 S_008F0C_RESOURCE_LEVEL(1);
4940 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
4941 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4942 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4945 /* older generations need element size = 16 bytes. element size removed in GFX9 */
4946 if (ctx
->program
->chip_class
<= GFX8
)
4947 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
4949 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4952 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4953 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4954 Builder
bld(ctx
->program
, ctx
->block
);
4955 Temp rsrc
= get_scratch_resource(ctx
);
4956 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4957 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4960 switch (dst
.size()) {
4962 op
= aco_opcode::buffer_load_dword
;
4965 op
= aco_opcode::buffer_load_dwordx2
;
4968 op
= aco_opcode::buffer_load_dwordx3
;
4971 op
= aco_opcode::buffer_load_dwordx4
;
4975 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4976 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4977 bld
.def(v4
), offset
, rsrc
,
4978 ctx
->program
->scratch_offset
, 0, true);
4979 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4980 aco_opcode::buffer_load_dwordx4
,
4981 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4982 offset
, rsrc
, ctx
->program
->scratch_offset
, 16, true);
4983 emit_split_vector(ctx
, lower
, 2);
4984 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4985 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4986 if (dst
.size() == 8) {
4987 emit_split_vector(ctx
, upper
, 2);
4988 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4989 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4994 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4995 Format::PSEUDO
, dst
.size() / 2, 1)};
4996 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4997 vec
->operands
[i
] = Operand(elems
[i
]);
4998 vec
->definitions
[0] = Definition(dst
);
4999 bld
.insert(std::move(vec
));
5000 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5004 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5007 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->program
->scratch_offset
, 0, true);
5008 emit_split_vector(ctx
, dst
, instr
->num_components
);
5011 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5012 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5013 Builder
bld(ctx
->program
, ctx
->block
);
5014 Temp rsrc
= get_scratch_resource(ctx
);
5015 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5016 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5018 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5019 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5023 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5024 int num_bytes
= count
* elem_size_bytes
;
5026 if (num_bytes
> 16) {
5027 assert(elem_size_bytes
== 8);
5028 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5033 // TODO: check alignment of sub-dword stores
5034 // TODO: split 3 bytes. there is no store instruction for that
5037 if (count
!= instr
->num_components
) {
5038 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5039 for (int i
= 0; i
< count
; i
++) {
5040 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5041 vec
->operands
[i
] = Operand(elem
);
5043 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5044 vec
->definitions
[0] = Definition(write_data
);
5045 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5051 switch (num_bytes
) {
5053 op
= aco_opcode::buffer_store_dword
;
5056 op
= aco_opcode::buffer_store_dwordx2
;
5059 op
= aco_opcode::buffer_store_dwordx3
;
5062 op
= aco_opcode::buffer_store_dwordx4
;
5065 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5068 bld
.mubuf(op
, offset
, rsrc
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5072 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5073 uint8_t log2_ps_iter_samples
;
5074 if (ctx
->program
->info
->ps
.force_persample
) {
5075 log2_ps_iter_samples
=
5076 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5078 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5081 /* The bit pattern matches that used by fixed function fragment
5083 static const unsigned ps_iter_masks
[] = {
5084 0xffff, /* not used */
5090 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5092 Builder
bld(ctx
->program
, ctx
->block
);
5094 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::ancillary
], Operand(8u), Operand(4u));
5095 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5096 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5097 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5098 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, ctx
->fs_inputs
[fs_input::sample_coverage
]);
5101 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5103 Builder
bld(ctx
->program
, ctx
->block
);
5105 if (cluster_size
== 1) {
5107 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5108 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5109 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5110 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5111 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
5112 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5113 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5114 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5115 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
5116 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
5117 //subgroupAnd(val) -> (exec & ~val) == 0
5118 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5119 return bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5120 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5121 //subgroupOr(val) -> (val & exec) != 0
5122 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5123 return bool_to_vector_condition(ctx
, tmp
);
5124 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5125 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5126 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5127 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5128 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5129 return bool_to_vector_condition(ctx
, tmp
);
5131 //subgroupClustered{And,Or,Xor}(val, n) ->
5132 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5133 //cluster_offset = ~(n - 1) & lane_id
5134 //cluster_mask = ((1 << n) - 1)
5135 //subgroupClusteredAnd():
5136 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5137 //subgroupClusteredOr():
5138 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5139 //subgroupClusteredXor():
5140 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5141 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5142 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5143 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5146 if (op
== nir_op_iand
)
5147 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5149 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5151 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5152 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5153 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5154 if (cluster_mask
!= 0xffffffff)
5155 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5157 Definition cmp_def
= Definition();
5158 if (op
== nir_op_iand
) {
5159 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5160 } else if (op
== nir_op_ior
) {
5161 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5162 } else if (op
== nir_op_ixor
) {
5163 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5164 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5165 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5167 cmp_def
.setHint(vcc
);
5168 return cmp_def
.getTemp();
5172 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5174 Builder
bld(ctx
->program
, ctx
->block
);
5176 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5177 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5178 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5180 if (op
== nir_op_iand
)
5181 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5183 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5185 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5186 Temp lo
= lohi
.def(0).getTemp();
5187 Temp hi
= lohi
.def(1).getTemp();
5188 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5189 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5191 Definition cmp_def
= Definition();
5192 if (op
== nir_op_iand
)
5193 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5194 else if (op
== nir_op_ior
)
5195 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5196 else if (op
== nir_op_ixor
)
5197 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5198 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5199 cmp_def
.setHint(vcc
);
5200 return cmp_def
.getTemp();
5203 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5205 Builder
bld(ctx
->program
, ctx
->block
);
5207 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5208 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5209 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5210 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5211 if (op
== nir_op_iand
)
5212 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5213 else if (op
== nir_op_ior
)
5214 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5215 else if (op
== nir_op_ixor
)
5216 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5222 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5224 Builder
bld(ctx
->program
, ctx
->block
);
5225 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5226 if (src
.regClass().type() == RegType::vgpr
) {
5227 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5228 } else if (src
.regClass() == s1
) {
5229 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5230 } else if (src
.regClass() == s2
) {
5231 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5233 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5234 nir_print_instr(&instr
->instr
, stderr
);
5235 fprintf(stderr
, "\n");
5239 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5241 Builder
bld(ctx
->program
, ctx
->block
);
5242 Temp p1
= ctx
->fs_inputs
[fs_input::persp_center_p1
];
5243 Temp p2
= ctx
->fs_inputs
[fs_input::persp_center_p2
];
5246 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5247 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5248 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5249 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5250 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5251 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5253 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5254 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5255 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5256 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5257 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5258 Temp wqm1
= bld
.tmp(v1
);
5259 emit_wqm(ctx
, tmp1
, wqm1
, true);
5260 Temp wqm2
= bld
.tmp(v1
);
5261 emit_wqm(ctx
, tmp2
, wqm2
, true);
5262 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5266 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5268 Builder
bld(ctx
->program
, ctx
->block
);
5269 switch(instr
->intrinsic
) {
5270 case nir_intrinsic_load_barycentric_sample
:
5271 case nir_intrinsic_load_barycentric_pixel
:
5272 case nir_intrinsic_load_barycentric_centroid
: {
5273 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5274 fs_input input
= get_interp_input(instr
->intrinsic
, mode
);
5276 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5277 if (input
== fs_input::max_inputs
) {
5278 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5279 Operand(0u), Operand(0u));
5281 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5282 ctx
->fs_inputs
[input
],
5283 ctx
->fs_inputs
[input
+ 1]);
5285 emit_split_vector(ctx
, dst
, 2);
5288 case nir_intrinsic_load_barycentric_at_sample
: {
5289 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5290 switch (ctx
->options
->key
.fs
.num_samples
) {
5291 case 2: sample_pos_offset
+= 1 << 3; break;
5292 case 4: sample_pos_offset
+= 3 << 3; break;
5293 case 8: sample_pos_offset
+= 7 << 3; break;
5297 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5298 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5299 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
5300 if (addr
.type() == RegType::sgpr
) {
5303 sample_pos_offset
+= const_addr
->u32
<< 3;
5304 offset
= Operand(sample_pos_offset
);
5305 } else if (ctx
->options
->chip_class
>= GFX9
) {
5306 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5308 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5309 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5311 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, Operand(offset
));
5313 } else if (ctx
->options
->chip_class
>= GFX9
) {
5314 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5315 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
5317 /* addr += private_segment_buffer + sample_pos_offset */
5318 Temp tmp0
= bld
.tmp(s1
);
5319 Temp tmp1
= bld
.tmp(s1
);
5320 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
5321 Definition scc_tmp
= bld
.def(s1
, scc
);
5322 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5323 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
5324 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5325 Temp pck0
= bld
.tmp(v1
);
5326 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5327 tmp1
= as_vgpr(ctx
, tmp1
);
5328 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5329 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5331 /* sample_pos = flat_load_dwordx2 addr */
5332 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5335 /* sample_pos -= 0.5 */
5336 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5337 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5338 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5339 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5340 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5342 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5345 case nir_intrinsic_load_barycentric_at_offset
: {
5346 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5347 RegClass rc
= RegClass(offset
.type(), 1);
5348 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5349 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5350 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5353 case nir_intrinsic_load_front_face
: {
5354 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5355 Operand(0u), ctx
->fs_inputs
[fs_input::front_face
]).def(0).setHint(vcc
);
5358 case nir_intrinsic_load_view_index
:
5359 case nir_intrinsic_load_layer_id
: {
5360 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5361 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5362 bld
.copy(Definition(dst
), Operand(ctx
->view_index
));
5366 unsigned idx
= nir_intrinsic_base(instr
);
5367 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5368 Operand(2u), bld
.m0(ctx
->prim_mask
), idx
, 0);
5371 case nir_intrinsic_load_frag_coord
: {
5372 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5375 case nir_intrinsic_load_sample_pos
: {
5376 Temp posx
= ctx
->fs_inputs
[fs_input::frag_pos_0
];
5377 Temp posy
= ctx
->fs_inputs
[fs_input::frag_pos_1
];
5378 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5379 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5380 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5383 case nir_intrinsic_load_interpolated_input
:
5384 visit_load_interpolated_input(ctx
, instr
);
5386 case nir_intrinsic_store_output
:
5387 visit_store_output(ctx
, instr
);
5389 case nir_intrinsic_load_input
:
5390 visit_load_input(ctx
, instr
);
5392 case nir_intrinsic_load_ubo
:
5393 visit_load_ubo(ctx
, instr
);
5395 case nir_intrinsic_load_push_constant
:
5396 visit_load_push_constant(ctx
, instr
);
5398 case nir_intrinsic_load_constant
:
5399 visit_load_constant(ctx
, instr
);
5401 case nir_intrinsic_vulkan_resource_index
:
5402 visit_load_resource(ctx
, instr
);
5404 case nir_intrinsic_discard
:
5405 visit_discard(ctx
, instr
);
5407 case nir_intrinsic_discard_if
:
5408 visit_discard_if(ctx
, instr
);
5410 case nir_intrinsic_load_shared
:
5411 visit_load_shared(ctx
, instr
);
5413 case nir_intrinsic_store_shared
:
5414 visit_store_shared(ctx
, instr
);
5416 case nir_intrinsic_shared_atomic_add
:
5417 case nir_intrinsic_shared_atomic_imin
:
5418 case nir_intrinsic_shared_atomic_umin
:
5419 case nir_intrinsic_shared_atomic_imax
:
5420 case nir_intrinsic_shared_atomic_umax
:
5421 case nir_intrinsic_shared_atomic_and
:
5422 case nir_intrinsic_shared_atomic_or
:
5423 case nir_intrinsic_shared_atomic_xor
:
5424 case nir_intrinsic_shared_atomic_exchange
:
5425 case nir_intrinsic_shared_atomic_comp_swap
:
5426 visit_shared_atomic(ctx
, instr
);
5428 case nir_intrinsic_image_deref_load
:
5429 visit_image_load(ctx
, instr
);
5431 case nir_intrinsic_image_deref_store
:
5432 visit_image_store(ctx
, instr
);
5434 case nir_intrinsic_image_deref_atomic_add
:
5435 case nir_intrinsic_image_deref_atomic_umin
:
5436 case nir_intrinsic_image_deref_atomic_imin
:
5437 case nir_intrinsic_image_deref_atomic_umax
:
5438 case nir_intrinsic_image_deref_atomic_imax
:
5439 case nir_intrinsic_image_deref_atomic_and
:
5440 case nir_intrinsic_image_deref_atomic_or
:
5441 case nir_intrinsic_image_deref_atomic_xor
:
5442 case nir_intrinsic_image_deref_atomic_exchange
:
5443 case nir_intrinsic_image_deref_atomic_comp_swap
:
5444 visit_image_atomic(ctx
, instr
);
5446 case nir_intrinsic_image_deref_size
:
5447 visit_image_size(ctx
, instr
);
5449 case nir_intrinsic_load_ssbo
:
5450 visit_load_ssbo(ctx
, instr
);
5452 case nir_intrinsic_store_ssbo
:
5453 visit_store_ssbo(ctx
, instr
);
5455 case nir_intrinsic_load_global
:
5456 visit_load_global(ctx
, instr
);
5458 case nir_intrinsic_store_global
:
5459 visit_store_global(ctx
, instr
);
5461 case nir_intrinsic_ssbo_atomic_add
:
5462 case nir_intrinsic_ssbo_atomic_imin
:
5463 case nir_intrinsic_ssbo_atomic_umin
:
5464 case nir_intrinsic_ssbo_atomic_imax
:
5465 case nir_intrinsic_ssbo_atomic_umax
:
5466 case nir_intrinsic_ssbo_atomic_and
:
5467 case nir_intrinsic_ssbo_atomic_or
:
5468 case nir_intrinsic_ssbo_atomic_xor
:
5469 case nir_intrinsic_ssbo_atomic_exchange
:
5470 case nir_intrinsic_ssbo_atomic_comp_swap
:
5471 visit_atomic_ssbo(ctx
, instr
);
5473 case nir_intrinsic_load_scratch
:
5474 visit_load_scratch(ctx
, instr
);
5476 case nir_intrinsic_store_scratch
:
5477 visit_store_scratch(ctx
, instr
);
5479 case nir_intrinsic_get_buffer_size
:
5480 visit_get_buffer_size(ctx
, instr
);
5482 case nir_intrinsic_barrier
: {
5483 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5484 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5485 if (workgroup_size
> 64)
5486 bld
.sopp(aco_opcode::s_barrier
);
5489 case nir_intrinsic_group_memory_barrier
:
5490 case nir_intrinsic_memory_barrier
:
5491 case nir_intrinsic_memory_barrier_atomic_counter
:
5492 case nir_intrinsic_memory_barrier_buffer
:
5493 case nir_intrinsic_memory_barrier_image
:
5494 case nir_intrinsic_memory_barrier_shared
:
5495 emit_memory_barrier(ctx
, instr
);
5497 case nir_intrinsic_load_num_work_groups
:
5498 case nir_intrinsic_load_work_group_id
:
5499 case nir_intrinsic_load_local_invocation_id
: {
5500 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5502 if (instr
->intrinsic
== nir_intrinsic_load_num_work_groups
)
5503 ids
= ctx
->num_workgroups
;
5504 else if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
5505 ids
= ctx
->workgroup_ids
;
5507 ids
= ctx
->local_invocation_ids
;
5508 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5509 ids
[0].id() ? Operand(ids
[0]) : Operand(1u),
5510 ids
[1].id() ? Operand(ids
[1]) : Operand(1u),
5511 ids
[2].id() ? Operand(ids
[2]) : Operand(1u));
5512 emit_split_vector(ctx
, dst
, 3);
5515 case nir_intrinsic_load_local_invocation_index
: {
5516 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5517 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5518 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5519 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5522 case nir_intrinsic_load_subgroup_id
: {
5523 if (ctx
->stage
== compute_cs
) {
5524 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5525 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5527 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5531 case nir_intrinsic_load_subgroup_invocation
: {
5532 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5533 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5536 case nir_intrinsic_load_num_subgroups
: {
5537 if (ctx
->stage
== compute_cs
)
5538 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
), ctx
->tg_size
);
5540 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5543 case nir_intrinsic_ballot
: {
5544 Definition tmp
= bld
.def(s2
);
5545 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5546 if (instr
->src
[0].ssa
->bit_size
== 1) {
5547 assert(src
.regClass() == s2
);
5548 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5549 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5550 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5551 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5552 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5554 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5555 nir_print_instr(&instr
->instr
, stderr
);
5556 fprintf(stderr
, "\n");
5558 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5561 case nir_intrinsic_shuffle
:
5562 case nir_intrinsic_read_invocation
: {
5563 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5564 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
5565 emit_uniform_subgroup(ctx
, instr
, src
);
5567 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5568 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
5569 tid
= bld
.as_uniform(tid
);
5570 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5571 if (src
.regClass() == v1
) {
5572 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
5573 } else if (src
.regClass() == v2
) {
5574 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5575 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5576 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
5577 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
5578 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5579 emit_split_vector(ctx
, dst
, 2);
5580 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
5581 assert(src
.regClass() == s2
);
5582 Temp tmp
= bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, tid
);
5583 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5584 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
5585 assert(src
.regClass() == s2
);
5586 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5587 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5588 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5589 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5591 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5592 nir_print_instr(&instr
->instr
, stderr
);
5593 fprintf(stderr
, "\n");
5598 case nir_intrinsic_load_sample_id
: {
5599 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5600 ctx
->fs_inputs
[ancillary
], Operand(8u), Operand(4u));
5603 case nir_intrinsic_load_sample_mask_in
: {
5604 visit_load_sample_mask_in(ctx
, instr
);
5607 case nir_intrinsic_read_first_invocation
: {
5608 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5609 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5610 if (src
.regClass() == v1
) {
5612 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5614 } else if (src
.regClass() == v2
) {
5615 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5616 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5617 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5618 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5619 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5620 emit_split_vector(ctx
, dst
, 2);
5621 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5622 assert(src
.regClass() == s2
);
5623 Temp tmp
= bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5624 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)));
5625 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
5626 } else if (src
.regClass() == s1
) {
5627 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5628 } else if (src
.regClass() == s2
) {
5629 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5631 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5632 nir_print_instr(&instr
->instr
, stderr
);
5633 fprintf(stderr
, "\n");
5637 case nir_intrinsic_vote_all
: {
5638 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5639 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5640 assert(src
.regClass() == s2
);
5641 assert(dst
.regClass() == s2
);
5643 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5644 Temp val
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(0u), Operand(-1u), bld
.scc(tmp
));
5645 emit_wqm(ctx
, val
, dst
);
5648 case nir_intrinsic_vote_any
: {
5649 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5650 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5651 assert(src
.regClass() == s2
);
5652 assert(dst
.regClass() == s2
);
5654 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5655 Temp val
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), Operand(-1u), Operand(0u), bld
.scc(tmp
));
5656 emit_wqm(ctx
, val
, dst
);
5659 case nir_intrinsic_reduce
:
5660 case nir_intrinsic_inclusive_scan
:
5661 case nir_intrinsic_exclusive_scan
: {
5662 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5663 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5664 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5665 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5666 nir_intrinsic_cluster_size(instr
) : 0;
5667 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5669 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5670 emit_uniform_subgroup(ctx
, instr
, src
);
5671 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5672 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5674 else if (op
== nir_op_iadd
)
5676 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5678 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5680 switch (instr
->intrinsic
) {
5681 case nir_intrinsic_reduce
:
5682 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5684 case nir_intrinsic_exclusive_scan
:
5685 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5687 case nir_intrinsic_inclusive_scan
:
5688 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5693 } else if (cluster_size
== 1) {
5694 bld
.copy(Definition(dst
), src
);
5696 src
= as_vgpr(ctx
, src
);
5700 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5715 unreachable("unknown reduction op");
5720 switch (instr
->intrinsic
) {
5721 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5722 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5723 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5725 unreachable("unknown reduce intrinsic");
5728 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5729 reduce
->operands
[0] = Operand(src
);
5730 // filled in by aco_reduce_assign.cpp, used internally as part of the
5732 assert(dst
.size() == 1 || dst
.size() == 2);
5733 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5734 reduce
->operands
[2] = Operand(v1
.as_linear());
5736 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5737 reduce
->definitions
[0] = Definition(tmp_dst
);
5738 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5739 reduce
->definitions
[2] = Definition();
5740 reduce
->definitions
[3] = Definition(scc
, s1
);
5741 reduce
->definitions
[4] = Definition();
5742 reduce
->reduce_op
= reduce_op
;
5743 reduce
->cluster_size
= cluster_size
;
5744 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5746 emit_wqm(ctx
, tmp_dst
, dst
);
5750 case nir_intrinsic_quad_broadcast
: {
5751 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5752 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5753 emit_uniform_subgroup(ctx
, instr
, src
);
5755 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5756 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5757 if (instr
->dest
.ssa
.bit_size
== 1) {
5758 assert(src
.regClass() == s2
);
5759 uint32_t half_mask
= 0x11111111u
<< lane
;
5760 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5761 Temp tmp
= bld
.tmp(s2
);
5762 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5763 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5764 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5765 emit_wqm(ctx
, tmp
, dst
);
5766 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5768 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5769 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5771 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5772 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5773 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5774 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5775 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5776 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5777 emit_split_vector(ctx
, dst
, 2);
5779 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5780 nir_print_instr(&instr
->instr
, stderr
);
5781 fprintf(stderr
, "\n");
5786 case nir_intrinsic_quad_swap_horizontal
:
5787 case nir_intrinsic_quad_swap_vertical
:
5788 case nir_intrinsic_quad_swap_diagonal
:
5789 case nir_intrinsic_quad_swizzle_amd
: {
5790 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5791 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5792 emit_uniform_subgroup(ctx
, instr
, src
);
5795 uint16_t dpp_ctrl
= 0;
5796 switch (instr
->intrinsic
) {
5797 case nir_intrinsic_quad_swap_horizontal
:
5798 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5800 case nir_intrinsic_quad_swap_vertical
:
5801 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5803 case nir_intrinsic_quad_swap_diagonal
:
5804 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5806 case nir_intrinsic_quad_swizzle_amd
: {
5807 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5814 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5815 if (instr
->dest
.ssa
.bit_size
== 1) {
5816 assert(src
.regClass() == s2
);
5817 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5818 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5819 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5820 emit_wqm(ctx
, tmp
, dst
);
5821 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5822 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5823 emit_wqm(ctx
, tmp
, dst
);
5824 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5825 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5826 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5827 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5828 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5829 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5830 emit_split_vector(ctx
, dst
, 2);
5832 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5833 nir_print_instr(&instr
->instr
, stderr
);
5834 fprintf(stderr
, "\n");
5838 case nir_intrinsic_masked_swizzle_amd
: {
5839 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5840 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5841 emit_uniform_subgroup(ctx
, instr
, src
);
5844 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5845 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5846 if (dst
.regClass() == v1
) {
5848 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5850 } else if (dst
.regClass() == v2
) {
5851 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5852 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5853 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5854 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5855 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5856 emit_split_vector(ctx
, dst
, 2);
5858 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5859 nir_print_instr(&instr
->instr
, stderr
);
5860 fprintf(stderr
, "\n");
5864 case nir_intrinsic_write_invocation_amd
: {
5865 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5866 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5867 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5868 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5869 if (dst
.regClass() == v1
) {
5870 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5871 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5872 } else if (dst
.regClass() == v2
) {
5873 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5874 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5875 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5876 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5877 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5878 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5879 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5880 emit_split_vector(ctx
, dst
, 2);
5882 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5883 nir_print_instr(&instr
->instr
, stderr
);
5884 fprintf(stderr
, "\n");
5888 case nir_intrinsic_mbcnt_amd
: {
5889 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5890 RegClass rc
= RegClass(src
.type(), 1);
5891 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5892 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5893 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5894 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5895 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5896 emit_wqm(ctx
, wqm_tmp
, dst
);
5899 case nir_intrinsic_load_helper_invocation
: {
5900 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5901 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5902 ctx
->block
->kind
|= block_kind_needs_lowering
;
5903 ctx
->program
->needs_exact
= true;
5906 case nir_intrinsic_is_helper_invocation
: {
5907 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5908 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5909 ctx
->block
->kind
|= block_kind_needs_lowering
;
5910 ctx
->program
->needs_exact
= true;
5913 case nir_intrinsic_demote
:
5914 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5915 ctx
->block
->kind
|= block_kind_uses_demote
;
5916 ctx
->program
->needs_exact
= true;
5918 case nir_intrinsic_demote_if
: {
5919 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5920 assert(src
.regClass() == s2
);
5921 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5922 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5923 ctx
->block
->kind
|= block_kind_uses_demote
;
5924 ctx
->program
->needs_exact
= true;
5927 case nir_intrinsic_first_invocation
: {
5928 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5929 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5932 case nir_intrinsic_shader_clock
:
5933 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
5934 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
5936 case nir_intrinsic_load_vertex_id_zero_base
: {
5937 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5938 bld
.copy(Definition(dst
), ctx
->vertex_id
);
5941 case nir_intrinsic_load_first_vertex
: {
5942 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5943 bld
.copy(Definition(dst
), ctx
->base_vertex
);
5946 case nir_intrinsic_load_base_instance
: {
5947 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5948 bld
.copy(Definition(dst
), ctx
->start_instance
);
5951 case nir_intrinsic_load_instance_id
: {
5952 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5953 bld
.copy(Definition(dst
), ctx
->instance_id
);
5956 case nir_intrinsic_load_draw_id
: {
5957 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5958 bld
.copy(Definition(dst
), ctx
->draw_id
);
5962 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5963 nir_print_instr(&instr
->instr
, stderr
);
5964 fprintf(stderr
, "\n");
5972 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
5973 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
5974 enum glsl_base_type
*stype
)
5976 nir_deref_instr
*texture_deref_instr
= NULL
;
5977 nir_deref_instr
*sampler_deref_instr
= NULL
;
5980 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5981 switch (instr
->src
[i
].src_type
) {
5982 case nir_tex_src_texture_deref
:
5983 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5985 case nir_tex_src_sampler_deref
:
5986 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5988 case nir_tex_src_plane
:
5989 plane
= nir_src_as_int(instr
->src
[i
].src
);
5996 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
5998 if (!sampler_deref_instr
)
5999 sampler_deref_instr
= texture_deref_instr
;
6002 assert(instr
->op
!= nir_texop_txf_ms
&&
6003 instr
->op
!= nir_texop_samples_identical
);
6004 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6005 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6006 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6007 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6009 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6012 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6013 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6014 fprintf(stderr
, "Unimplemented sampler descriptor: ");
6015 nir_print_instr(&instr
->instr
, stderr
);
6016 fprintf(stderr
, "\n");
6018 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
6021 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6022 instr
->op
== nir_texop_samples_identical
))
6023 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6026 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6027 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6029 Builder
bld(ctx
->program
, ctx
->block
);
6031 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6032 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6033 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6035 Operand
neg_one(0xbf800000u
);
6036 Operand
one(0x3f800000u
);
6037 Operand
two(0x40000000u
);
6038 Operand
four(0x40800000u
);
6040 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
6041 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6042 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6044 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
6045 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
6046 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
6047 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
);
6050 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6051 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6052 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6054 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6057 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6058 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6059 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6062 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6063 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6065 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6066 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6069 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6071 Builder
bld(ctx
->program
, ctx
->block
);
6072 Temp coord_args
[4], ma
, tc
, sc
, id
;
6073 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6074 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6077 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6079 // see comment in ac_prepare_cube_coords()
6080 if (ctx
->options
->chip_class
<= GFX8
)
6081 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6084 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6086 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6087 vop3a
->operands
[0] = Operand(ma
);
6088 vop3a
->abs
[0] = true;
6089 Temp invma
= bld
.tmp(v1
);
6090 vop3a
->definitions
[0] = Definition(invma
);
6091 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6093 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6095 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6097 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6099 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6101 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6104 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6105 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6107 for (unsigned i
= 0; i
< 2; i
++) {
6108 // see comment in ac_prepare_cube_coords()
6110 Temp deriv_sc
, deriv_tc
;
6111 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6112 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6114 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6116 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6117 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6118 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6119 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6120 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6121 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6122 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6125 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6126 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6130 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6131 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6135 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6138 for (unsigned i
= 0; i
< coords
.size(); i
++)
6139 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6141 Builder
bld(ctx
->program
, ctx
->block
);
6142 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6144 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6145 for (unsigned i
= 0; i
< coords
.size(); i
++)
6146 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6147 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6148 vec
->definitions
[0] = Definition(res
);
6149 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6153 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6155 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6157 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6158 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6161 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6162 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6163 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6167 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6169 Builder
bld(ctx
->program
, ctx
->block
);
6170 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6171 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6172 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6173 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6174 nir_const_value
*sample_index_cv
= NULL
;
6175 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6176 enum glsl_base_type stype
;
6177 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6179 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6180 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6181 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6182 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6184 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6185 switch (instr
->src
[i
].src_type
) {
6186 case nir_tex_src_coord
:
6187 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6189 case nir_tex_src_bias
:
6190 if (instr
->op
== nir_texop_txb
) {
6191 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6195 case nir_tex_src_lod
: {
6196 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6198 if (val
&& val
->f32
<= 0.0) {
6201 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6206 case nir_tex_src_comparator
:
6207 if (instr
->is_shadow
) {
6208 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6212 case nir_tex_src_offset
:
6213 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6214 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6217 case nir_tex_src_ddx
:
6218 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6221 case nir_tex_src_ddy
:
6222 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6225 case nir_tex_src_ms_index
:
6226 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6227 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6228 has_sample_index
= true;
6230 case nir_tex_src_texture_offset
:
6231 case nir_tex_src_sampler_offset
:
6236 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6237 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6238 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6240 if (instr
->op
== nir_texop_texture_samples
) {
6241 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6243 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6244 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6245 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 */));
6246 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6248 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6249 samples
, Operand(1u), bld
.scc(is_msaa
));
6253 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6254 aco_ptr
<Instruction
> tmp_instr
;
6255 Temp acc
, pack
= Temp();
6257 uint32_t pack_const
= 0;
6258 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6259 if (!const_offset
[i
])
6261 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6264 if (offset
.type() == RegType::sgpr
) {
6265 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6266 if (const_offset
[i
])
6269 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6270 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6273 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6276 if (pack
== Temp()) {
6279 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6283 if (pack_const
&& pack
!= Temp())
6284 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6286 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6287 if (const_offset
[i
])
6290 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6291 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6294 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6297 if (pack
== Temp()) {
6300 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6304 if (pack_const
&& pack
!= Temp())
6305 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6307 if (pack_const
&& pack
== Temp())
6308 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6309 else if (pack
== Temp())
6315 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6316 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6318 /* pack derivatives */
6319 if (has_ddx
|| has_ddy
) {
6320 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
6321 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6322 ddx
, Operand(0u), ddy
, Operand(0u));
6324 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6329 if (instr
->coord_components
> 1 &&
6330 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6332 instr
->op
!= nir_texop_txf
)
6333 coords
= apply_round_slice(ctx
, coords
, 1);
6335 if (instr
->coord_components
> 2 &&
6336 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6337 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6338 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6339 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6341 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6342 coords
= apply_round_slice(ctx
, coords
, 2);
6344 if (ctx
->options
->chip_class
== GFX9
&&
6345 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6346 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6347 assert(coords
.size() > 0 && coords
.size() < 3);
6349 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6350 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6351 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6352 if (coords
.size() > 1)
6353 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6354 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6355 vec
->definitions
[0] = Definition(coords
);
6356 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6359 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6361 if (instr
->op
== nir_texop_samples_identical
)
6362 resource
= fmask_ptr
;
6364 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6365 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6366 instr
->op
!= nir_texop_txs
) {
6367 assert(has_sample_index
);
6368 Operand
op(sample_index
);
6369 if (sample_index_cv
)
6370 op
= Operand(sample_index_cv
->u32
);
6371 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6374 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6375 Temp split_coords
[coords
.size()];
6376 emit_split_vector(ctx
, coords
, coords
.size());
6377 for (unsigned i
= 0; i
< coords
.size(); i
++)
6378 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6381 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6382 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6383 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6386 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6387 for (unsigned i
= 0; i
< coords
.size(); i
++)
6388 vec
->operands
[i
] = Operand(split_coords
[i
]);
6389 coords
= bld
.tmp(coords
.regClass());
6390 vec
->definitions
[0] = Definition(coords
);
6391 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6396 /* Build tex instruction */
6397 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6398 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
6399 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
6401 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6404 /* gather4 selects the component by dmask and always returns vec4 */
6405 if (instr
->op
== nir_texop_tg4
) {
6406 assert(instr
->dest
.ssa
.num_components
== 4);
6407 if (instr
->is_shadow
)
6410 dmask
= 1 << instr
->component
;
6411 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6412 tmp_dst
= bld
.tmp(v4
);
6413 } else if (instr
->op
== nir_texop_samples_identical
) {
6414 tmp_dst
= bld
.tmp(v1
);
6415 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6416 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6419 aco_ptr
<MIMG_instruction
> tex
;
6420 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6422 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6424 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6425 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6428 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6429 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6431 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6432 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6433 tex
->operands
[1] = Operand(resource
);
6434 if (ctx
->options
->chip_class
== GFX9
&&
6435 instr
->op
== nir_texop_txs
&&
6436 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6438 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6439 } else if (instr
->op
== nir_texop_query_levels
) {
6440 tex
->dmask
= 1 << 3;
6445 tex
->definitions
[0] = Definition(tmp_dst
);
6447 tex
->can_reorder
= true;
6448 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6451 /* divide 3rd value by 6 by multiplying with magic number */
6452 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6453 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6454 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6455 assert(instr
->dest
.ssa
.num_components
== 3);
6456 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6457 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6458 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6459 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6464 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6468 Temp tg4_compare_cube_wa64
= Temp();
6470 if (tg4_integer_workarounds
) {
6471 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6472 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6473 tex
->operands
[1] = Operand(resource
);
6477 Temp size
= bld
.tmp(v2
);
6478 tex
->definitions
[0] = Definition(size
);
6479 tex
->can_reorder
= true;
6480 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6481 emit_split_vector(ctx
, size
, size
.size());
6484 for (unsigned i
= 0; i
< 2; i
++) {
6485 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6486 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6487 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6488 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6491 Temp orig_coords
[2] = {
6492 emit_extract_vector(ctx
, coords
, 0, v1
),
6493 emit_extract_vector(ctx
, coords
, 1, v1
)};
6494 Temp new_coords
[2] = {
6495 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6496 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6499 if (tg4_integer_cube_workaround
) {
6500 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6501 Temp desc
[resource
.size()];
6502 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6503 Format::PSEUDO
, 1, resource
.size())};
6504 split
->operands
[0] = Operand(resource
);
6505 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6506 desc
[i
] = bld
.tmp(s1
);
6507 split
->definitions
[i
] = Definition(desc
[i
]);
6509 ctx
->block
->instructions
.emplace_back(std::move(split
));
6511 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6512 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6513 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6516 if (stype
== GLSL_TYPE_UINT
) {
6517 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6518 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6519 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6520 bld
.scc(compare_cube_wa
));
6522 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6523 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6524 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6525 bld
.scc(compare_cube_wa
));
6527 tg4_compare_cube_wa64
= bld
.tmp(s2
);
6528 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
6530 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6532 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6533 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6534 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6536 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6537 Format::PSEUDO
, resource
.size(), 1)};
6538 for (unsigned i
= 0; i
< resource
.size(); i
++)
6539 vec
->operands
[i
] = Operand(desc
[i
]);
6540 resource
= bld
.tmp(resource
.regClass());
6541 vec
->definitions
[0] = Definition(resource
);
6542 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6544 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6545 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6546 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6547 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6550 if (coords
.size() == 3) {
6551 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6552 new_coords
[0], new_coords
[1],
6553 emit_extract_vector(ctx
, coords
, 2, v1
));
6555 assert(coords
.size() == 2);
6556 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6557 new_coords
[0], new_coords
[1]);
6561 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6562 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6563 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6564 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6566 std::vector
<Operand
> args
;
6568 args
.emplace_back(Operand(offset
));
6570 args
.emplace_back(Operand(bias
));
6572 args
.emplace_back(Operand(compare
));
6574 args
.emplace_back(Operand(derivs
));
6575 args
.emplace_back(Operand(coords
));
6576 if (has_sample_index
)
6577 args
.emplace_back(Operand(sample_index
));
6579 args
.emplace_back(lod
);
6582 if (args
.size() > 1) {
6583 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6585 for (unsigned i
= 0; i
< args
.size(); i
++) {
6586 size
+= args
[i
].size();
6587 vec
->operands
[i
] = args
[i
];
6589 RegClass rc
= RegClass(RegType::vgpr
, size
);
6590 Temp tmp
= bld
.tmp(rc
);
6591 vec
->definitions
[0] = Definition(tmp
);
6592 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6595 assert(args
[0].isTemp());
6596 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6599 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6600 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6602 assert(coords
.size() == 1);
6603 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6607 op
= aco_opcode::buffer_load_format_x
; break;
6609 op
= aco_opcode::buffer_load_format_xy
; break;
6611 op
= aco_opcode::buffer_load_format_xyz
; break;
6613 op
= aco_opcode::buffer_load_format_xyzw
; break;
6615 unreachable("Tex instruction loads more than 4 components.");
6618 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6619 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6622 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6624 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6625 mubuf
->operands
[0] = Operand(coords
);
6626 mubuf
->operands
[1] = Operand(resource
);
6627 mubuf
->operands
[2] = Operand((uint32_t) 0);
6628 mubuf
->definitions
[0] = Definition(tmp_dst
);
6629 mubuf
->idxen
= true;
6630 mubuf
->can_reorder
= true;
6631 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6633 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6638 if (instr
->op
== nir_texop_txf
||
6639 instr
->op
== nir_texop_txf_ms
||
6640 instr
->op
== nir_texop_samples_identical
) {
6641 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6642 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6643 tex
->operands
[0] = Operand(arg
);
6644 tex
->operands
[1] = Operand(resource
);
6649 tex
->definitions
[0] = Definition(tmp_dst
);
6650 tex
->can_reorder
= true;
6651 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6653 if (instr
->op
== nir_texop_samples_identical
) {
6654 assert(dmask
== 1 && dst
.regClass() == v1
);
6655 assert(dst
.id() != tmp_dst
.id());
6657 Temp tmp
= bld
.tmp(s2
);
6658 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6659 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6662 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6667 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6668 aco_opcode opcode
= aco_opcode::image_sample
;
6669 if (has_offset
) { /* image_sample_*_o */
6671 opcode
= aco_opcode::image_sample_c_o
;
6673 opcode
= aco_opcode::image_sample_c_d_o
;
6675 opcode
= aco_opcode::image_sample_c_b_o
;
6677 opcode
= aco_opcode::image_sample_c_lz_o
;
6679 opcode
= aco_opcode::image_sample_c_l_o
;
6681 opcode
= aco_opcode::image_sample_o
;
6683 opcode
= aco_opcode::image_sample_d_o
;
6685 opcode
= aco_opcode::image_sample_b_o
;
6687 opcode
= aco_opcode::image_sample_lz_o
;
6689 opcode
= aco_opcode::image_sample_l_o
;
6691 } else { /* no offset */
6693 opcode
= aco_opcode::image_sample_c
;
6695 opcode
= aco_opcode::image_sample_c_d
;
6697 opcode
= aco_opcode::image_sample_c_b
;
6699 opcode
= aco_opcode::image_sample_c_lz
;
6701 opcode
= aco_opcode::image_sample_c_l
;
6703 opcode
= aco_opcode::image_sample
;
6705 opcode
= aco_opcode::image_sample_d
;
6707 opcode
= aco_opcode::image_sample_b
;
6709 opcode
= aco_opcode::image_sample_lz
;
6711 opcode
= aco_opcode::image_sample_l
;
6715 if (instr
->op
== nir_texop_tg4
) {
6717 opcode
= aco_opcode::image_gather4_lz_o
;
6719 opcode
= aco_opcode::image_gather4_c_lz_o
;
6721 opcode
= aco_opcode::image_gather4_lz
;
6723 opcode
= aco_opcode::image_gather4_c_lz
;
6725 } else if (instr
->op
== nir_texop_lod
) {
6726 opcode
= aco_opcode::image_get_lod
;
6729 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6730 tex
->operands
[0] = arg
;
6731 tex
->operands
[1] = Operand(resource
);
6732 tex
->operands
[2] = Operand(sampler
);
6736 tex
->definitions
[0] = Definition(tmp_dst
);
6737 tex
->can_reorder
= true;
6738 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6740 if (tg4_integer_cube_workaround
) {
6741 assert(tmp_dst
.id() != dst
.id());
6742 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6744 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6746 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6747 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6749 if (stype
== GLSL_TYPE_UINT
)
6750 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6752 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6753 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6755 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6756 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6757 val
[0], val
[1], val
[2], val
[3]);
6759 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6760 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6765 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6767 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6768 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6769 return Operand(tmp
.regClass());
6771 return Operand(tmp
);
6774 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6776 aco_ptr
<Pseudo_instruction
> phi
;
6777 unsigned num_src
= exec_list_length(&instr
->srcs
);
6778 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6779 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == s2
);
6781 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6783 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6784 bool all_undef
= true;
6785 nir_foreach_phi_src(src
, instr
) {
6786 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6787 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6791 Builder
bld(ctx
->program
, ctx
->block
);
6792 if (dst
.regClass() == s1
) {
6793 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6794 } else if (dst
.regClass() == v1
) {
6795 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6797 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6798 for (unsigned i
= 0; i
< dst
.size(); i
++)
6799 vec
->operands
[i
] = Operand(0u);
6800 vec
->definitions
[0] = Definition(dst
);
6801 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6806 /* try to scalarize vector phis */
6807 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
6808 // TODO: scalarize linear phis on divergent ifs
6809 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6810 std::array
<Temp
, 4> new_vec
;
6811 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6812 Operand src
= get_phi_operand(ctx
, pair
.second
);
6813 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6814 can_scalarize
= false;
6818 if (can_scalarize
) {
6819 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6820 assert(dst
.size() % num_components
== 0);
6821 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6823 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6824 for (unsigned k
= 0; k
< num_components
; k
++) {
6825 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6826 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6827 for (unsigned i
= 0; i
< num_src
; i
++) {
6828 Operand src
= get_phi_operand(ctx
, it
->second
);
6829 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6832 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6833 phi
->definitions
[0] = Definition(phi_dst
);
6834 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6835 new_vec
[k
] = phi_dst
;
6836 vec
->operands
[k
] = Operand(phi_dst
);
6838 vec
->definitions
[0] = Definition(dst
);
6839 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6840 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6845 unsigned extra_src
= 0;
6846 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6847 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6851 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6853 /* if we have a linear phi on a divergent if, we know that one src is undef */
6854 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6855 assert(extra_src
== 0);
6857 /* we place the phi either in the invert-block or in the current block */
6858 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6859 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6860 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6861 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6862 assert(block
->kind
& block_kind_invert
);
6863 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6865 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6867 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6869 phi
->operands
[1] = Operand(dst
.regClass());
6870 phi
->definitions
[0] = Definition(dst
);
6871 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6875 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6876 for (unsigned i
= 0; i
< num_src
; i
++) {
6877 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6880 for (unsigned i
= 0; i
< extra_src
; i
++)
6881 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6882 phi
->definitions
[0] = Definition(dst
);
6883 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6887 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6889 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6891 assert(dst
.type() == RegType::sgpr
);
6893 if (dst
.size() == 1) {
6894 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6896 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6897 for (unsigned i
= 0; i
< dst
.size(); i
++)
6898 vec
->operands
[i
] = Operand(0u);
6899 vec
->definitions
[0] = Definition(dst
);
6900 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6904 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6906 Builder
bld(ctx
->program
, ctx
->block
);
6907 Block
*logical_target
;
6908 append_logical_end(ctx
->block
);
6909 unsigned idx
= ctx
->block
->index
;
6911 switch (instr
->type
) {
6912 case nir_jump_break
:
6913 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6914 add_logical_edge(idx
, logical_target
);
6915 ctx
->block
->kind
|= block_kind_break
;
6917 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6918 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6919 /* uniform break - directly jump out of the loop */
6920 ctx
->block
->kind
|= block_kind_uniform
;
6921 ctx
->cf_info
.has_branch
= true;
6922 bld
.branch(aco_opcode::p_branch
);
6923 add_linear_edge(idx
, logical_target
);
6926 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6928 case nir_jump_continue
:
6929 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6930 add_logical_edge(idx
, logical_target
);
6931 ctx
->block
->kind
|= block_kind_continue
;
6933 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6934 /* for potential uniform breaks after this continue,
6935 we must ensure that they are handled correctly */
6936 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6937 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6939 /* uniform continue - directly jump to the loop header */
6940 ctx
->block
->kind
|= block_kind_uniform
;
6941 ctx
->cf_info
.has_branch
= true;
6942 bld
.branch(aco_opcode::p_branch
);
6943 add_linear_edge(idx
, logical_target
);
6948 fprintf(stderr
, "Unknown NIR jump instr: ");
6949 nir_print_instr(&instr
->instr
, stderr
);
6950 fprintf(stderr
, "\n");
6954 /* remove critical edges from linear CFG */
6955 bld
.branch(aco_opcode::p_branch
);
6956 Block
* break_block
= ctx
->program
->create_and_insert_block();
6957 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6958 break_block
->kind
|= block_kind_uniform
;
6959 add_linear_edge(idx
, break_block
);
6960 /* the loop_header pointer might be invalidated by this point */
6961 if (instr
->type
== nir_jump_continue
)
6962 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6963 add_linear_edge(break_block
->index
, logical_target
);
6964 bld
.reset(break_block
);
6965 bld
.branch(aco_opcode::p_branch
);
6967 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6968 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6969 add_linear_edge(idx
, continue_block
);
6970 append_logical_start(continue_block
);
6971 ctx
->block
= continue_block
;
6975 void visit_block(isel_context
*ctx
, nir_block
*block
)
6977 nir_foreach_instr(instr
, block
) {
6978 switch (instr
->type
) {
6979 case nir_instr_type_alu
:
6980 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
6982 case nir_instr_type_load_const
:
6983 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
6985 case nir_instr_type_intrinsic
:
6986 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
6988 case nir_instr_type_tex
:
6989 visit_tex(ctx
, nir_instr_as_tex(instr
));
6991 case nir_instr_type_phi
:
6992 visit_phi(ctx
, nir_instr_as_phi(instr
));
6994 case nir_instr_type_ssa_undef
:
6995 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
6997 case nir_instr_type_deref
:
6999 case nir_instr_type_jump
:
7000 visit_jump(ctx
, nir_instr_as_jump(instr
));
7003 fprintf(stderr
, "Unknown NIR instr type: ");
7004 nir_print_instr(instr
, stderr
);
7005 fprintf(stderr
, "\n");
7013 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7015 append_logical_end(ctx
->block
);
7016 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7017 Builder
bld(ctx
->program
, ctx
->block
);
7018 bld
.branch(aco_opcode::p_branch
);
7019 unsigned loop_preheader_idx
= ctx
->block
->index
;
7021 Block loop_exit
= Block();
7022 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7023 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7025 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7026 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7027 loop_header
->kind
|= block_kind_loop_header
;
7028 add_edge(loop_preheader_idx
, loop_header
);
7029 ctx
->block
= loop_header
;
7031 /* emit loop body */
7032 unsigned loop_header_idx
= loop_header
->index
;
7033 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7034 append_logical_start(ctx
->block
);
7035 visit_cf_list(ctx
, &loop
->body
);
7037 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7038 if (!ctx
->cf_info
.has_branch
) {
7039 append_logical_end(ctx
->block
);
7040 if (ctx
->cf_info
.exec_potentially_empty
) {
7041 /* Discards can result in code running with an empty exec mask.
7042 * This would result in divergent breaks not ever being taken. As a
7043 * workaround, break the loop when the loop mask is empty instead of
7044 * always continuing. */
7045 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7046 unsigned block_idx
= ctx
->block
->index
;
7048 /* create helper blocks to avoid critical edges */
7049 Block
*break_block
= ctx
->program
->create_and_insert_block();
7050 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7051 break_block
->kind
= block_kind_uniform
;
7052 bld
.reset(break_block
);
7053 bld
.branch(aco_opcode::p_branch
);
7054 add_linear_edge(block_idx
, break_block
);
7055 add_linear_edge(break_block
->index
, &loop_exit
);
7057 Block
*continue_block
= ctx
->program
->create_and_insert_block();
7058 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7059 continue_block
->kind
= block_kind_uniform
;
7060 bld
.reset(continue_block
);
7061 bld
.branch(aco_opcode::p_branch
);
7062 add_linear_edge(block_idx
, continue_block
);
7063 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7065 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
7066 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
7068 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
7069 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7070 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7072 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7075 bld
.reset(ctx
->block
);
7076 bld
.branch(aco_opcode::p_branch
);
7079 /* fixup phis in loop header from unreachable blocks */
7080 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
7081 bool linear
= ctx
->cf_info
.has_branch
;
7082 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7083 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
7084 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
7085 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
7086 /* the last operand should be the one that needs to be removed */
7087 instr
->operands
.pop_back();
7088 } else if (!is_phi(instr
)) {
7094 ctx
->cf_info
.has_branch
= false;
7096 // TODO: if the loop has not a single exit, we must add one °°
7097 /* emit loop successor block */
7098 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
7099 append_logical_start(ctx
->block
);
7102 // TODO: check if it is beneficial to not branch on continues
7103 /* trim linear phis in loop header */
7104 for (auto&& instr
: loop_entry
->instructions
) {
7105 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7106 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7107 new_phi
->definitions
[0] = instr
->definitions
[0];
7108 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7109 new_phi
->operands
[i
] = instr
->operands
[i
];
7110 /* check that the remaining operands are all the same */
7111 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7112 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7113 instr
.swap(new_phi
);
7114 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7123 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7127 append_logical_end(ctx
->block
);
7128 ctx
->block
->kind
|= block_kind_branch
;
7130 /* branch to linear then block */
7131 assert(cond
.regClass() == s2
);
7132 aco_ptr
<Pseudo_branch_instruction
> branch
;
7133 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7134 branch
->operands
[0] = Operand(cond
);
7135 ctx
->block
->instructions
.push_back(std::move(branch
));
7137 ic
->BB_if_idx
= ctx
->block
->index
;
7138 ic
->BB_invert
= Block();
7139 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7140 /* Invert blocks are intentionally not marked as top level because they
7141 * are not part of the logical cfg. */
7142 ic
->BB_invert
.kind
|= block_kind_invert
;
7143 ic
->BB_endif
= Block();
7144 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7145 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7147 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7148 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7149 ctx
->cf_info
.parent_if
.is_divergent
= true;
7150 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7152 /** emit logical then block */
7153 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7154 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7155 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7156 ctx
->block
= BB_then_logical
;
7157 append_logical_start(BB_then_logical
);
7160 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7162 Block
*BB_then_logical
= ctx
->block
;
7163 append_logical_end(BB_then_logical
);
7164 /* branch from logical then block to invert block */
7165 aco_ptr
<Pseudo_branch_instruction
> branch
;
7166 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7167 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7168 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7169 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7170 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7171 BB_then_logical
->kind
|= block_kind_uniform
;
7172 assert(!ctx
->cf_info
.has_branch
);
7173 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7174 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7176 /** emit linear then block */
7177 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7178 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7179 BB_then_linear
->kind
|= block_kind_uniform
;
7180 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7181 /* branch from linear then block to invert block */
7182 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7183 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7184 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7186 /** emit invert merge block */
7187 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7188 ic
->invert_idx
= ctx
->block
->index
;
7190 /* branch to linear else block (skip else) */
7191 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7192 branch
->operands
[0] = Operand(ic
->cond
);
7193 ctx
->block
->instructions
.push_back(std::move(branch
));
7195 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7196 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7198 /** emit logical else block */
7199 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7200 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7201 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7202 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7203 ctx
->block
= BB_else_logical
;
7204 append_logical_start(BB_else_logical
);
7207 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7209 Block
*BB_else_logical
= ctx
->block
;
7210 append_logical_end(BB_else_logical
);
7212 /* branch from logical else block to endif block */
7213 aco_ptr
<Pseudo_branch_instruction
> branch
;
7214 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7215 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7216 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7217 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7218 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7219 BB_else_logical
->kind
|= block_kind_uniform
;
7221 assert(!ctx
->cf_info
.has_branch
);
7222 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7225 /** emit linear else block */
7226 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7227 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7228 BB_else_linear
->kind
|= block_kind_uniform
;
7229 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7231 /* branch from linear else block to endif block */
7232 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7233 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7234 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7237 /** emit endif merge block */
7238 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7239 append_logical_start(ctx
->block
);
7242 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7243 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7244 /* uniform control flow never has an empty exec-mask */
7245 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7246 ctx
->cf_info
.exec_potentially_empty
= false;
7249 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7251 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7252 Builder
bld(ctx
->program
, ctx
->block
);
7253 aco_ptr
<Pseudo_branch_instruction
> branch
;
7255 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7257 * Uniform conditionals are represented in the following way*) :
7259 * The linear and logical CFG:
7262 * BB_THEN (logical) BB_ELSE (logical)
7266 * *) Exceptions may be due to break and continue statements within loops
7267 * If a break/continue happens within uniform control flow, it branches
7268 * to the loop exit/entry block. Otherwise, it branches to the next
7271 append_logical_end(ctx
->block
);
7272 ctx
->block
->kind
|= block_kind_uniform
;
7275 assert(cond
.regClass() == s2
);
7276 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7277 cond
= bool_to_scalar_condition(ctx
, cond
);
7279 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7280 branch
->operands
[0] = Operand(cond
);
7281 branch
->operands
[0].setFixed(scc
);
7282 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7284 unsigned BB_if_idx
= ctx
->block
->index
;
7285 Block BB_endif
= Block();
7286 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7287 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7289 /** emit then block */
7290 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7291 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7292 add_edge(BB_if_idx
, BB_then
);
7293 append_logical_start(BB_then
);
7294 ctx
->block
= BB_then
;
7295 visit_cf_list(ctx
, &if_stmt
->then_list
);
7296 BB_then
= ctx
->block
;
7297 bool then_branch
= ctx
->cf_info
.has_branch
;
7298 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7301 append_logical_end(BB_then
);
7302 /* branch from then block to endif block */
7303 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7304 BB_then
->instructions
.emplace_back(std::move(branch
));
7305 add_linear_edge(BB_then
->index
, &BB_endif
);
7306 if (!then_branch_divergent
)
7307 add_logical_edge(BB_then
->index
, &BB_endif
);
7308 BB_then
->kind
|= block_kind_uniform
;
7311 ctx
->cf_info
.has_branch
= false;
7312 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7314 /** emit else block */
7315 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7316 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7317 add_edge(BB_if_idx
, BB_else
);
7318 append_logical_start(BB_else
);
7319 ctx
->block
= BB_else
;
7320 visit_cf_list(ctx
, &if_stmt
->else_list
);
7321 BB_else
= ctx
->block
;
7323 if (!ctx
->cf_info
.has_branch
) {
7324 append_logical_end(BB_else
);
7325 /* branch from then block to endif block */
7326 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7327 BB_else
->instructions
.emplace_back(std::move(branch
));
7328 add_linear_edge(BB_else
->index
, &BB_endif
);
7329 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7330 add_logical_edge(BB_else
->index
, &BB_endif
);
7331 BB_else
->kind
|= block_kind_uniform
;
7334 ctx
->cf_info
.has_branch
&= then_branch
;
7335 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7337 /** emit endif merge block */
7338 if (!ctx
->cf_info
.has_branch
) {
7339 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7340 append_logical_start(ctx
->block
);
7342 } else { /* non-uniform condition */
7344 * To maintain a logical and linear CFG without critical edges,
7345 * non-uniform conditionals are represented in the following way*) :
7350 * BB_THEN (logical) BB_THEN (linear)
7352 * BB_INVERT (linear)
7354 * BB_ELSE (logical) BB_ELSE (linear)
7361 * BB_THEN (logical) BB_ELSE (logical)
7365 * *) Exceptions may be due to break and continue statements within loops
7370 begin_divergent_if_then(ctx
, &ic
, cond
);
7371 visit_cf_list(ctx
, &if_stmt
->then_list
);
7373 begin_divergent_if_else(ctx
, &ic
);
7374 visit_cf_list(ctx
, &if_stmt
->else_list
);
7376 end_divergent_if(ctx
, &ic
);
7380 static void visit_cf_list(isel_context
*ctx
,
7381 struct exec_list
*list
)
7383 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7384 switch (node
->type
) {
7385 case nir_cf_node_block
:
7386 visit_block(ctx
, nir_cf_node_as_block(node
));
7388 case nir_cf_node_if
:
7389 visit_if(ctx
, nir_cf_node_as_if(node
));
7391 case nir_cf_node_loop
:
7392 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7395 unreachable("unimplemented cf list type");
7400 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7402 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7403 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7404 if (!is_pos
&& !mask
)
7406 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7408 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7409 exp
->enabled_mask
= mask
;
7410 for (unsigned i
= 0; i
< 4; ++i
) {
7411 if (mask
& (1 << i
))
7412 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7414 exp
->operands
[i
] = Operand(v1
);
7416 exp
->valid_mask
= false;
7418 exp
->compressed
= false;
7420 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7422 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7423 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7426 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7428 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7429 exp
->enabled_mask
= 0;
7430 for (unsigned i
= 0; i
< 4; ++i
)
7431 exp
->operands
[i
] = Operand(v1
);
7432 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7433 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7434 exp
->enabled_mask
|= 0x1;
7436 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7437 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7438 exp
->enabled_mask
|= 0x4;
7440 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7441 if (ctx
->options
->chip_class
< GFX9
) {
7442 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7443 exp
->enabled_mask
|= 0x8;
7445 Builder
bld(ctx
->program
, ctx
->block
);
7447 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7448 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7449 if (exp
->operands
[2].isTemp())
7450 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7452 exp
->operands
[2] = Operand(out
);
7453 exp
->enabled_mask
|= 0x4;
7456 exp
->valid_mask
= false;
7458 exp
->compressed
= false;
7459 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7460 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7463 static void create_vs_exports(isel_context
*ctx
)
7465 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7467 if (outinfo
->export_prim_id
) {
7468 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7469 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = ctx
->vs_prim_id
;
7472 if (ctx
->options
->key
.has_multiview_view_index
) {
7473 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7474 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, ctx
->view_index
);
7477 /* the order these position exports are created is important */
7479 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7480 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7481 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7483 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7484 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7485 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7486 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7488 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7489 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7490 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7491 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7492 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7495 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7496 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7497 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7500 export_vs_varying(ctx
, i
, false, NULL
);
7504 static void emit_stream_output(isel_context
*ctx
,
7505 Temp
const *so_buffers
,
7506 Temp
const *so_write_offset
,
7507 const struct radv_stream_output
*output
)
7509 unsigned num_comps
= util_bitcount(output
->component_mask
);
7510 unsigned loc
= output
->location
;
7511 unsigned buf
= output
->buffer
;
7512 unsigned offset
= output
->offset
;
7514 assert(num_comps
&& num_comps
<= 4);
7515 if (!num_comps
|| num_comps
> 4)
7518 unsigned start
= ffs(output
->component_mask
) - 1;
7521 bool all_undef
= true;
7522 assert(ctx
->stage
== vertex_vs
);
7523 for (unsigned i
= 0; i
< num_comps
; i
++) {
7524 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7525 all_undef
= all_undef
&& !out
[i
].id();
7530 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7531 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7532 for (unsigned i
= 0; i
< num_comps
; ++i
)
7533 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7534 vec
->definitions
[0] = Definition(write_data
);
7535 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7538 switch (num_comps
) {
7540 opcode
= aco_opcode::buffer_store_dword
;
7543 opcode
= aco_opcode::buffer_store_dwordx2
;
7546 opcode
= aco_opcode::buffer_store_dwordx3
;
7549 opcode
= aco_opcode::buffer_store_dwordx4
;
7553 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7554 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7555 store
->operands
[1] = Operand(so_buffers
[buf
]);
7556 store
->operands
[2] = Operand((uint32_t) 0);
7557 store
->operands
[3] = Operand(write_data
);
7558 if (offset
> 4095) {
7559 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7560 Builder
bld(ctx
->program
, ctx
->block
);
7561 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7563 store
->offset
= offset
;
7565 store
->offen
= true;
7569 store
->can_reorder
= true;
7570 ctx
->block
->instructions
.emplace_back(std::move(store
));
7573 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7575 Builder
bld(ctx
->program
, ctx
->block
);
7578 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, ctx
->streamout_buffers
);
7579 for (unsigned i
= 0; i
< 4; i
++) {
7580 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7584 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7587 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7588 ctx
->streamout_config
, Operand(0x70010u
));
7590 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7591 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7593 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7596 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7598 bld
.reset(ctx
->block
);
7600 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), ctx
->streamout_write_idx
, tid
);
7602 Temp so_write_offset
[4];
7604 for (unsigned i
= 0; i
< 4; i
++) {
7605 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7610 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7611 ctx
->streamout_write_idx
, ctx
->streamout_offset
[i
]);
7612 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7614 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7616 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7617 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u), ctx
->streamout_offset
[i
]);
7618 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7622 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7623 struct radv_stream_output
*output
=
7624 &ctx
->program
->info
->so
.outputs
[i
];
7625 if (stream
!= output
->stream
)
7628 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7631 begin_divergent_if_else(ctx
, &ic
);
7632 end_divergent_if(ctx
, &ic
);
7635 } /* end namespace */
7637 void handle_bc_optimize(isel_context
*ctx
)
7639 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7640 Builder
bld(ctx
->program
, ctx
->block
);
7641 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7642 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7643 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7644 if (uses_center
&& uses_centroid
) {
7645 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)), ctx
->prim_mask
, Operand(0u));
7647 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7648 for (unsigned i
= 0; i
< 2; i
++) {
7649 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7650 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
],
7651 ctx
->fs_inputs
[fs_input::persp_center_p1
+ i
],
7653 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
] = new_coord
;
7657 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7658 for (unsigned i
= 0; i
< 2; i
++) {
7659 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7660 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
],
7661 ctx
->fs_inputs
[fs_input::linear_center_p1
+ i
],
7663 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
] = new_coord
;
7669 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
7671 Program
*program
= ctx
->program
;
7673 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
7675 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
7676 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
7677 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
7678 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
7679 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
7681 program
->next_fp_mode
.must_flush_denorms32
=
7682 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
7683 program
->next_fp_mode
.must_flush_denorms16_64
=
7684 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
7685 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
7687 program
->next_fp_mode
.care_about_round32
=
7688 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
7690 program
->next_fp_mode
.care_about_round16_64
=
7691 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
7692 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
7694 /* default to preserving fp16 and fp64 denorms, since it's free */
7695 if (program
->next_fp_mode
.must_flush_denorms16_64
)
7696 program
->next_fp_mode
.denorm16_64
= 0;
7698 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
7700 /* preserving fp32 denorms is expensive, so only do it if asked */
7701 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
7702 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
7704 program
->next_fp_mode
.denorm32
= 0;
7706 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
7707 program
->next_fp_mode
.round32
= fp_round_tz
;
7709 program
->next_fp_mode
.round32
= fp_round_ne
;
7711 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
7712 program
->next_fp_mode
.round16_64
= fp_round_tz
;
7714 program
->next_fp_mode
.round16_64
= fp_round_ne
;
7716 ctx
->block
->fp_mode
= program
->next_fp_mode
;
7719 void select_program(Program
*program
,
7720 unsigned shader_count
,
7721 struct nir_shader
*const *shaders
,
7722 ac_shader_config
* config
,
7723 struct radv_shader_info
*info
,
7724 const struct radv_nir_compiler_options
*options
)
7726 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, info
, options
);
7728 for (unsigned i
= 0; i
< shader_count
; i
++) {
7729 nir_shader
*nir
= shaders
[i
];
7730 init_context(&ctx
, nir
);
7732 setup_fp_mode(&ctx
, nir
);
7735 add_startpgm(&ctx
); /* needs to be after init_context() for FS */
7736 append_logical_start(ctx
.block
);
7740 if (shader_count
>= 2) {
7741 Builder
bld(ctx
.program
, ctx
.block
);
7742 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)));
7743 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7744 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7745 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7747 begin_divergent_if_then(&ctx
, &ic
, cond
);
7751 Builder
bld(ctx
.program
, ctx
.block
);
7752 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7753 bld
.sopp(aco_opcode::s_barrier
);
7756 if (ctx
.stage
== fragment_fs
)
7757 handle_bc_optimize(&ctx
);
7759 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7760 visit_cf_list(&ctx
, &func
->body
);
7762 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7763 emit_streamout(&ctx
, 0);
7765 if (ctx
.stage
== vertex_vs
)
7766 create_vs_exports(&ctx
);
7768 if (shader_count
>= 2) {
7769 begin_divergent_if_else(&ctx
, &ic
);
7770 end_divergent_if(&ctx
, &ic
);
7773 ralloc_free(ctx
.divergent_vals
);
7776 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
7778 append_logical_end(ctx
.block
);
7779 ctx
.block
->kind
|= block_kind_uniform
;
7780 Builder
bld(ctx
.program
, ctx
.block
);
7781 if (ctx
.program
->wb_smem_l1_on_end
)
7782 bld
.smem(aco_opcode::s_dcache_wb
, false);
7783 bld
.sopp(aco_opcode::s_endpgm
);
7786 for (Block
& BB
: program
->blocks
) {
7787 for (unsigned idx
: BB
.linear_preds
)
7788 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7789 for (unsigned idx
: BB
.logical_preds
)
7790 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);