2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "ac_shader_util.h"
32 #include "aco_builder.h"
33 #include "aco_interface.h"
34 #include "aco_instruction_selection_setup.cpp"
35 #include "util/fast_idiv_by_const.h"
40 class loop_info_RAII
{
42 unsigned header_idx_old
;
44 bool divergent_cont_old
;
45 bool divergent_branch_old
;
46 bool divergent_if_old
;
49 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
51 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
52 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
53 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
54 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
56 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
57 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
58 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
59 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
60 ctx
->cf_info
.parent_if
.is_divergent
= false;
61 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
66 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
67 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
68 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
70 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
71 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
72 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
73 ctx
->cf_info
.exec_potentially_empty
= false;
81 bool exec_potentially_empty_old
;
85 bool then_branch_divergent
;
90 static void visit_cf_list(struct isel_context
*ctx
,
91 struct exec_list
*list
);
93 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
95 succ
->logical_preds
.emplace_back(pred_idx
);
99 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
101 succ
->linear_preds
.emplace_back(pred_idx
);
104 static void add_edge(unsigned pred_idx
, Block
*succ
)
106 add_logical_edge(pred_idx
, succ
);
107 add_linear_edge(pred_idx
, succ
);
110 static void append_logical_start(Block
*b
)
112 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
115 static void append_logical_end(Block
*b
)
117 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
120 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
122 assert(ctx
->allocated
[def
->index
].id());
123 return ctx
->allocated
[def
->index
];
126 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
128 Builder
bld(ctx
->program
, ctx
->block
);
131 dst
= bld
.tmp(src
.regClass());
133 if (ctx
->stage
!= fragment_fs
) {
137 bld
.copy(Definition(dst
), src
);
141 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
142 ctx
->program
->needs_wqm
|= program_needs_wqm
;
146 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
148 if (index
.regClass() == s1
)
149 return bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), data
, index
);
151 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
153 /* Currently not implemented on GFX6-7 */
154 assert(ctx
->options
->chip_class
>= GFX8
);
156 if (ctx
->options
->chip_class
<= GFX9
|| ctx
->program
->wave_size
== 32) {
157 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
160 /* GFX10, wave64 mode:
161 * The bpermute instruction is limited to half-wave operation, which means that it can't
162 * properly support subgroup shuffle like older generations (or wave32 mode), so we
165 if (!ctx
->has_gfx10_wave64_bpermute
) {
166 ctx
->has_gfx10_wave64_bpermute
= true;
167 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
168 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
171 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u));
172 lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), lane_id
);
173 Temp lane_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), lane_id
);
174 Temp index_is_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x20u
), index
);
175 Temp cmp
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
, vcc
), lane_is_hi
, index_is_hi
);
177 return bld
.reduction(aco_opcode::p_wave64_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
),
178 bld
.vcc(cmp
), Operand(v2
.as_linear()), index_x4
, data
, gfx10_wave64_bpermute
);
181 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
183 if (val
.type() == RegType::sgpr
) {
184 Builder
bld(ctx
->program
, ctx
->block
);
185 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
187 assert(val
.type() == RegType::vgpr
);
191 //assumes a != 0xffffffff
192 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
195 Builder
bld(ctx
->program
, ctx
->block
);
197 if (util_is_power_of_two_or_zero(b
)) {
198 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
202 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
204 assert(info
.multiplier
<= 0xffffffff);
206 bool pre_shift
= info
.pre_shift
!= 0;
207 bool increment
= info
.increment
!= 0;
208 bool multiply
= true;
209 bool post_shift
= info
.post_shift
!= 0;
211 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
212 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
216 Temp pre_shift_dst
= a
;
218 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
219 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
222 Temp increment_dst
= pre_shift_dst
;
224 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
225 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
228 Temp multiply_dst
= increment_dst
;
230 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
231 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
232 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
236 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
240 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
242 Builder
bld(ctx
->program
, ctx
->block
);
243 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
247 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
249 /* no need to extract the whole vector */
250 if (src
.regClass() == dst_rc
) {
254 assert(src
.size() > idx
);
255 Builder
bld(ctx
->program
, ctx
->block
);
256 auto it
= ctx
->allocated_vec
.find(src
.id());
257 /* the size check needs to be early because elements other than 0 may be garbage */
258 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
259 if (it
->second
[idx
].regClass() == dst_rc
) {
260 return it
->second
[idx
];
262 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
263 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
264 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
268 if (src
.size() == dst_rc
.size()) {
270 return bld
.copy(bld
.def(dst_rc
), src
);
272 Temp dst
= bld
.tmp(dst_rc
);
273 emit_extract_vector(ctx
, src
, idx
, dst
);
278 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
280 if (num_components
== 1)
282 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
284 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
285 split
->operands
[0] = Operand(vec_src
);
286 std::array
<Temp
,4> elems
;
287 for (unsigned i
= 0; i
< num_components
; i
++) {
288 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
289 split
->definitions
[i
] = Definition(elems
[i
]);
291 ctx
->block
->instructions
.emplace_back(std::move(split
));
292 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
295 /* This vector expansion uses a mask to determine which elements in the new vector
296 * come from the original vector. The other elements are undefined. */
297 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
299 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
304 Builder
bld(ctx
->program
, ctx
->block
);
305 if (num_components
== 1) {
306 if (dst
.type() == RegType::sgpr
)
307 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
309 bld
.copy(Definition(dst
), vec_src
);
313 unsigned component_size
= dst
.size() / num_components
;
314 std::array
<Temp
,4> elems
;
316 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
317 vec
->definitions
[0] = Definition(dst
);
319 for (unsigned i
= 0; i
< num_components
; i
++) {
320 if (mask
& (1 << i
)) {
321 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
322 if (dst
.type() == RegType::sgpr
)
323 src
= bld
.as_uniform(src
);
324 vec
->operands
[i
] = Operand(src
);
326 vec
->operands
[i
] = Operand(0u);
328 elems
[i
] = vec
->operands
[i
].getTemp();
330 ctx
->block
->instructions
.emplace_back(std::move(vec
));
331 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
334 Temp
as_divergent_bool(isel_context
*ctx
, Temp val
, bool vcc_hint
)
336 if (val
.regClass() == s2
) {
339 assert(val
.regClass() == s1
);
340 Builder
bld(ctx
->program
, ctx
->block
);
341 Definition
& def
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
),
342 Operand((uint32_t) -1), Operand(0u), bld
.scc(val
)).def(0);
345 return def
.getTemp();
349 Temp
as_uniform_bool(isel_context
*ctx
, Temp val
)
351 if (val
.regClass() == s1
) {
354 assert(val
.regClass() == s2
);
355 Builder
bld(ctx
->program
, ctx
->block
);
356 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
357 Temp tmp
= bld
.tmp(s1
);
358 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, s2
)).def(1).getTemp();
359 return emit_wqm(ctx
, tmp
);
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
);
529 Builder
bld(ctx
->program
, ctx
->block
);
530 bld
.vopc(op
, Definition(dst
), src0
, src1
).def(0).setHint(vcc
);
533 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
535 if (dst
.regClass() == s2
) {
536 emit_vopc_instruction(ctx
, instr
, op
, dst
);
537 if (!ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
])
538 emit_split_vector(ctx
, dst
, 2);
539 } else if (dst
.regClass() == s1
) {
540 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
541 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
542 assert(src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
);
544 Builder
bld(ctx
->program
, ctx
->block
);
545 bld
.sopc(op
, bld
.scc(Definition(dst
)), src0
, src1
);
552 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
554 Builder
bld(ctx
->program
, ctx
->block
);
555 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
556 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
557 if (dst
.regClass() == s2
) {
558 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
),
559 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
561 assert(dst
.regClass() == s1
);
562 bld
.sop2(op32
, bld
.def(s1
), bld
.scc(Definition(dst
)),
563 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
568 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
570 Builder
bld(ctx
->program
, ctx
->block
);
571 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
572 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
573 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
575 if (dst
.type() == RegType::vgpr
) {
576 cond
= as_divergent_bool(ctx
, cond
, true);
578 aco_ptr
<Instruction
> bcsel
;
579 if (dst
.size() == 1) {
580 then
= as_vgpr(ctx
, then
);
581 els
= as_vgpr(ctx
, els
);
583 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
584 } else if (dst
.size() == 2) {
585 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
586 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
587 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
588 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
590 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
591 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
593 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
595 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
596 nir_print_instr(&instr
->instr
, stderr
);
597 fprintf(stderr
, "\n");
602 if (instr
->dest
.dest
.ssa
.bit_size
!= 1) { /* uniform condition and values in sgpr */
603 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
604 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
605 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
606 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(as_uniform_bool(ctx
, cond
)));
608 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
609 nir_print_instr(&instr
->instr
, stderr
);
610 fprintf(stderr
, "\n");
616 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
618 if (dst
.regClass() == s1
)
619 cond
= as_uniform_bool(ctx
, cond
);
621 if (cond
.regClass() == s1
) { /* uniform selection */
623 if (dst
.regClass() == s2
) {
624 op
= aco_opcode::s_cselect_b64
;
625 then
= as_divergent_bool(ctx
, then
, false);
626 els
= as_divergent_bool(ctx
, els
, false);
628 assert(dst
.regClass() == s1
);
629 op
= aco_opcode::s_cselect_b32
;
630 then
= as_uniform_bool(ctx
, then
);
631 els
= as_uniform_bool(ctx
, els
);
633 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(cond
));
637 /* divergent boolean bcsel
638 * this implements bcsel on bools: dst = s0 ? s1 : s2
639 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
640 assert (dst
.regClass() == s2
);
641 then
= as_divergent_bool(ctx
, then
, false);
642 els
= as_divergent_bool(ctx
, els
, false);
644 if (cond
.id() != then
.id())
645 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
647 if (cond
.id() == els
.id())
648 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
650 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
651 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
654 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
656 if (!instr
->dest
.dest
.is_ssa
) {
657 fprintf(stderr
, "nir alu dst not in ssa: ");
658 nir_print_instr(&instr
->instr
, stderr
);
659 fprintf(stderr
, "\n");
662 Builder
bld(ctx
->program
, ctx
->block
);
663 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
668 std::array
<Temp
,4> elems
;
669 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
670 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
671 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
672 vec
->operands
[i
] = Operand
{elems
[i
]};
674 vec
->definitions
[0] = Definition(dst
);
675 ctx
->block
->instructions
.emplace_back(std::move(vec
));
676 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
680 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
681 aco_ptr
<Instruction
> mov
;
682 if (dst
.type() == RegType::sgpr
) {
683 if (src
.type() == RegType::vgpr
)
684 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
685 else if (src
.regClass() == s1
)
686 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
687 else if (src
.regClass() == s2
)
688 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
690 unreachable("wrong src register class for nir_op_imov");
691 } else if (dst
.regClass() == v1
) {
692 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
693 } else if (dst
.regClass() == v2
) {
694 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
696 nir_print_instr(&instr
->instr
, stderr
);
697 unreachable("Should have been lowered to scalar.");
702 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
703 /* uniform booleans */
704 if (instr
->dest
.dest
.ssa
.bit_size
== 1 && dst
.regClass() == s1
) {
705 if (src
.regClass() == s1
) {
706 /* in this case, src is either 1 or 0 */
707 bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.scc(Definition(dst
)), Operand(1u), src
);
709 /* src is either exec_mask or 0 */
710 assert(src
.regClass() == s2
);
711 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
713 } else if (dst
.regClass() == v1
) {
714 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
715 } else if (dst
.type() == RegType::sgpr
) {
716 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
717 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
719 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
720 nir_print_instr(&instr
->instr
, stderr
);
721 fprintf(stderr
, "\n");
726 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
727 if (dst
.regClass() == v1
) {
728 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
729 } else if (dst
.regClass() == s1
) {
730 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
731 } else if (dst
.size() == 2) {
732 Temp src0
= bld
.tmp(dst
.type(), 1);
733 Temp src1
= bld
.tmp(dst
.type(), 1);
734 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
736 if (dst
.regClass() == s2
) {
737 Temp carry
= bld
.tmp(s1
);
738 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
739 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
740 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
742 Temp lower
= bld
.tmp(v1
);
743 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
744 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
745 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
748 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
749 nir_print_instr(&instr
->instr
, stderr
);
750 fprintf(stderr
, "\n");
755 if (dst
.regClass() == s1
) {
756 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
757 } else if (dst
.regClass() == v1
) {
758 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
759 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
761 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
762 nir_print_instr(&instr
->instr
, stderr
);
763 fprintf(stderr
, "\n");
768 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
769 if (dst
.regClass() == s1
) {
770 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
771 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
772 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
773 } else if (dst
.regClass() == s2
) {
774 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
775 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
776 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
777 } else if (dst
.regClass() == v1
) {
778 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
779 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
780 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
781 } else if (dst
.regClass() == v2
) {
782 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
783 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
784 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
785 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
786 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
787 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
789 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
790 nir_print_instr(&instr
->instr
, stderr
);
791 fprintf(stderr
, "\n");
796 if (dst
.regClass() == v1
) {
797 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
798 } else if (dst
.regClass() == s1
) {
799 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
801 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
802 nir_print_instr(&instr
->instr
, stderr
);
803 fprintf(stderr
, "\n");
808 if (dst
.regClass() == v1
) {
809 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
810 } else if (dst
.regClass() == s1
) {
811 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
813 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
814 nir_print_instr(&instr
->instr
, stderr
);
815 fprintf(stderr
, "\n");
820 if (dst
.regClass() == v1
) {
821 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
822 } else if (dst
.regClass() == s1
) {
823 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
825 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
826 nir_print_instr(&instr
->instr
, stderr
);
827 fprintf(stderr
, "\n");
832 if (dst
.regClass() == v1
) {
833 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
834 } else if (dst
.regClass() == s1
) {
835 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
837 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
838 nir_print_instr(&instr
->instr
, stderr
);
839 fprintf(stderr
, "\n");
844 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
845 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
846 } else if (dst
.regClass() == v1
) {
847 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
848 } else if (dst
.regClass() == s1
) {
849 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
850 } else if (dst
.regClass() == s2
) {
851 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
853 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
854 nir_print_instr(&instr
->instr
, stderr
);
855 fprintf(stderr
, "\n");
860 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
861 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
862 } else if (dst
.regClass() == v1
) {
863 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
864 } else if (dst
.regClass() == s1
) {
865 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
866 } else if (dst
.regClass() == s2
) {
867 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
869 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
870 nir_print_instr(&instr
->instr
, stderr
);
871 fprintf(stderr
, "\n");
876 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
877 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
878 } else if (dst
.regClass() == v1
) {
879 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
880 } else if (dst
.regClass() == s1
) {
881 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
882 } else if (dst
.regClass() == s2
) {
883 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
885 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
886 nir_print_instr(&instr
->instr
, stderr
);
887 fprintf(stderr
, "\n");
892 if (dst
.regClass() == v1
) {
893 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
894 } else if (dst
.regClass() == v2
) {
895 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
896 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
897 } else if (dst
.regClass() == s2
) {
898 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
899 } else if (dst
.regClass() == s1
) {
900 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
902 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
903 nir_print_instr(&instr
->instr
, stderr
);
904 fprintf(stderr
, "\n");
909 if (dst
.regClass() == v1
) {
910 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
911 } else if (dst
.regClass() == v2
) {
912 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
913 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
914 } else if (dst
.regClass() == s1
) {
915 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
916 } else if (dst
.regClass() == s2
) {
917 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
919 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
920 nir_print_instr(&instr
->instr
, stderr
);
921 fprintf(stderr
, "\n");
926 if (dst
.regClass() == v1
) {
927 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
928 } else if (dst
.regClass() == v2
) {
929 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
930 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
931 } else if (dst
.regClass() == s1
) {
932 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
933 } else if (dst
.regClass() == s2
) {
934 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
936 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
937 nir_print_instr(&instr
->instr
, stderr
);
938 fprintf(stderr
, "\n");
942 case nir_op_find_lsb
: {
943 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
944 if (src
.regClass() == s1
) {
945 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
946 } else if (src
.regClass() == v1
) {
947 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
948 } else if (src
.regClass() == s2
) {
949 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
951 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
952 nir_print_instr(&instr
->instr
, stderr
);
953 fprintf(stderr
, "\n");
957 case nir_op_ufind_msb
:
958 case nir_op_ifind_msb
: {
959 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
960 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
961 aco_opcode op
= src
.regClass() == s2
?
962 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
963 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
964 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
966 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
967 Operand(src
.size() * 32u - 1u), msb_rev
);
968 Temp msb
= sub
.def(0).getTemp();
969 Temp carry
= sub
.def(1).getTemp();
971 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
972 } else if (src
.regClass() == v1
) {
973 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
974 Temp msb_rev
= bld
.tmp(v1
);
975 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
976 Temp msb
= bld
.tmp(v1
);
977 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
978 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
980 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
981 nir_print_instr(&instr
->instr
, stderr
);
982 fprintf(stderr
, "\n");
986 case nir_op_bitfield_reverse
: {
987 if (dst
.regClass() == s1
) {
988 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
989 } else if (dst
.regClass() == v1
) {
990 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
992 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
993 nir_print_instr(&instr
->instr
, stderr
);
994 fprintf(stderr
, "\n");
999 if (dst
.regClass() == s1
) {
1000 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1004 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1005 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1006 if (dst
.regClass() == v1
) {
1007 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1011 assert(src0
.size() == 2 && src1
.size() == 2);
1012 Temp src00
= bld
.tmp(src0
.type(), 1);
1013 Temp src01
= bld
.tmp(dst
.type(), 1);
1014 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1015 Temp src10
= bld
.tmp(src1
.type(), 1);
1016 Temp src11
= bld
.tmp(dst
.type(), 1);
1017 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1019 if (dst
.regClass() == s2
) {
1020 Temp carry
= bld
.tmp(s1
);
1021 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1022 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1023 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1024 } else if (dst
.regClass() == v2
) {
1025 Temp dst0
= bld
.tmp(v1
);
1026 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1027 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1028 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1030 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1031 nir_print_instr(&instr
->instr
, stderr
);
1032 fprintf(stderr
, "\n");
1036 case nir_op_uadd_sat
: {
1037 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1038 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1039 if (dst
.regClass() == s1
) {
1040 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1041 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1043 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1044 } else if (dst
.regClass() == v1
) {
1045 if (ctx
->options
->chip_class
>= GFX9
) {
1046 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1047 add
->operands
[0] = Operand(src0
);
1048 add
->operands
[1] = Operand(src1
);
1049 add
->definitions
[0] = Definition(dst
);
1051 ctx
->block
->instructions
.emplace_back(std::move(add
));
1053 if (src1
.regClass() != v1
)
1054 std::swap(src0
, src1
);
1055 assert(src1
.regClass() == v1
);
1056 Temp tmp
= bld
.tmp(v1
);
1057 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1058 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1061 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1062 nir_print_instr(&instr
->instr
, stderr
);
1063 fprintf(stderr
, "\n");
1067 case nir_op_uadd_carry
: {
1068 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1069 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1070 if (dst
.regClass() == s1
) {
1071 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1074 if (dst
.regClass() == v1
) {
1075 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1076 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1080 Temp src00
= bld
.tmp(src0
.type(), 1);
1081 Temp src01
= bld
.tmp(dst
.type(), 1);
1082 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1083 Temp src10
= bld
.tmp(src1
.type(), 1);
1084 Temp src11
= bld
.tmp(dst
.type(), 1);
1085 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1086 if (dst
.regClass() == s2
) {
1087 Temp carry
= bld
.tmp(s1
);
1088 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1089 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1090 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1091 } else if (dst
.regClass() == v2
) {
1092 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1093 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1094 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1095 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1097 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1098 nir_print_instr(&instr
->instr
, stderr
);
1099 fprintf(stderr
, "\n");
1104 if (dst
.regClass() == s1
) {
1105 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1109 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1110 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1111 if (dst
.regClass() == v1
) {
1112 bld
.vsub32(Definition(dst
), src0
, src1
);
1116 Temp src00
= bld
.tmp(src0
.type(), 1);
1117 Temp src01
= bld
.tmp(dst
.type(), 1);
1118 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1119 Temp src10
= bld
.tmp(src1
.type(), 1);
1120 Temp src11
= bld
.tmp(dst
.type(), 1);
1121 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1122 if (dst
.regClass() == s2
) {
1123 Temp carry
= bld
.tmp(s1
);
1124 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1125 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1126 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1127 } else if (dst
.regClass() == v2
) {
1128 Temp lower
= bld
.tmp(v1
);
1129 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1130 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1131 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1133 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1134 nir_print_instr(&instr
->instr
, stderr
);
1135 fprintf(stderr
, "\n");
1139 case nir_op_usub_borrow
: {
1140 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1141 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1142 if (dst
.regClass() == s1
) {
1143 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1145 } else if (dst
.regClass() == v1
) {
1146 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1147 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1151 Temp src00
= bld
.tmp(src0
.type(), 1);
1152 Temp src01
= bld
.tmp(dst
.type(), 1);
1153 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1154 Temp src10
= bld
.tmp(src1
.type(), 1);
1155 Temp src11
= bld
.tmp(dst
.type(), 1);
1156 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1157 if (dst
.regClass() == s2
) {
1158 Temp borrow
= bld
.tmp(s1
);
1159 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1160 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1161 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1162 } else if (dst
.regClass() == v2
) {
1163 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1164 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1165 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1166 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1168 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1169 nir_print_instr(&instr
->instr
, stderr
);
1170 fprintf(stderr
, "\n");
1175 if (dst
.regClass() == v1
) {
1176 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1177 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1178 } else if (dst
.regClass() == s1
) {
1179 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1181 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1182 nir_print_instr(&instr
->instr
, stderr
);
1183 fprintf(stderr
, "\n");
1187 case nir_op_umul_high
: {
1188 if (dst
.regClass() == v1
) {
1189 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1190 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1191 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1192 } else if (dst
.regClass() == s1
) {
1193 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1194 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1195 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1197 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1198 nir_print_instr(&instr
->instr
, stderr
);
1199 fprintf(stderr
, "\n");
1203 case nir_op_imul_high
: {
1204 if (dst
.regClass() == v1
) {
1205 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1206 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1207 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1208 } else if (dst
.regClass() == s1
) {
1209 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1210 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1211 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1213 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1214 nir_print_instr(&instr
->instr
, stderr
);
1215 fprintf(stderr
, "\n");
1220 if (dst
.size() == 1) {
1221 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1222 } else if (dst
.size() == 2) {
1223 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1224 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1226 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr
->instr
, stderr
);
1228 fprintf(stderr
, "\n");
1233 if (dst
.size() == 1) {
1234 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1235 } else if (dst
.size() == 2) {
1236 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1237 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1239 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1240 nir_print_instr(&instr
->instr
, stderr
);
1241 fprintf(stderr
, "\n");
1246 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1247 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1248 if (dst
.size() == 1) {
1249 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1250 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1252 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1253 } else if (dst
.size() == 2) {
1254 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1255 get_alu_src(ctx
, instr
->src
[0]),
1256 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1257 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1260 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1261 nir_print_instr(&instr
->instr
, stderr
);
1262 fprintf(stderr
, "\n");
1267 if (dst
.size() == 1) {
1268 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1269 } else if (dst
.size() == 2) {
1270 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1271 get_alu_src(ctx
, instr
->src
[0]),
1272 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1274 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1275 nir_print_instr(&instr
->instr
, stderr
);
1276 fprintf(stderr
, "\n");
1281 if (dst
.size() == 1) {
1282 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1283 } else if (dst
.size() == 2) {
1284 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1285 get_alu_src(ctx
, instr
->src
[0]),
1286 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1288 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1289 nir_print_instr(&instr
->instr
, stderr
);
1290 fprintf(stderr
, "\n");
1294 case nir_op_fmax3
: {
1295 if (dst
.size() == 1) {
1296 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1298 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1299 nir_print_instr(&instr
->instr
, stderr
);
1300 fprintf(stderr
, "\n");
1304 case nir_op_fmin3
: {
1305 if (dst
.size() == 1) {
1306 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1308 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1309 nir_print_instr(&instr
->instr
, stderr
);
1310 fprintf(stderr
, "\n");
1314 case nir_op_fmed3
: {
1315 if (dst
.size() == 1) {
1316 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1318 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1319 nir_print_instr(&instr
->instr
, stderr
);
1320 fprintf(stderr
, "\n");
1324 case nir_op_umax3
: {
1325 if (dst
.size() == 1) {
1326 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1328 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1329 nir_print_instr(&instr
->instr
, stderr
);
1330 fprintf(stderr
, "\n");
1334 case nir_op_umin3
: {
1335 if (dst
.size() == 1) {
1336 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1338 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1339 nir_print_instr(&instr
->instr
, stderr
);
1340 fprintf(stderr
, "\n");
1344 case nir_op_umed3
: {
1345 if (dst
.size() == 1) {
1346 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1348 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1349 nir_print_instr(&instr
->instr
, stderr
);
1350 fprintf(stderr
, "\n");
1354 case nir_op_imax3
: {
1355 if (dst
.size() == 1) {
1356 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1358 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1359 nir_print_instr(&instr
->instr
, stderr
);
1360 fprintf(stderr
, "\n");
1364 case nir_op_imin3
: {
1365 if (dst
.size() == 1) {
1366 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1368 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1369 nir_print_instr(&instr
->instr
, stderr
);
1370 fprintf(stderr
, "\n");
1374 case nir_op_imed3
: {
1375 if (dst
.size() == 1) {
1376 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1378 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1379 nir_print_instr(&instr
->instr
, stderr
);
1380 fprintf(stderr
, "\n");
1384 case nir_op_cube_face_coord
: {
1385 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1386 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1387 emit_extract_vector(ctx
, in
, 1, v1
),
1388 emit_extract_vector(ctx
, in
, 2, v1
) };
1389 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1390 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1391 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1392 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1393 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1394 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1395 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1398 case nir_op_cube_face_index
: {
1399 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1400 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1401 emit_extract_vector(ctx
, in
, 1, v1
),
1402 emit_extract_vector(ctx
, in
, 2, v1
) };
1403 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1406 case nir_op_bcsel
: {
1407 emit_bcsel(ctx
, instr
, dst
);
1411 if (dst
.size() == 1) {
1412 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f32
, dst
);
1413 } else if (dst
.size() == 2) {
1414 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1416 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1417 nir_print_instr(&instr
->instr
, stderr
);
1418 fprintf(stderr
, "\n");
1423 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1424 if (dst
.size() == 1) {
1425 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1426 } else if (dst
.size() == 2) {
1427 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1428 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1429 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1430 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1432 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1433 nir_print_instr(&instr
->instr
, stderr
);
1434 fprintf(stderr
, "\n");
1439 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1440 if (dst
.size() == 1) {
1441 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1442 } else if (dst
.size() == 2) {
1443 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1444 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1445 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1446 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1448 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1449 nir_print_instr(&instr
->instr
, stderr
);
1450 fprintf(stderr
, "\n");
1455 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1456 if (dst
.size() == 1) {
1457 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1458 } else if (dst
.size() == 2) {
1459 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1460 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1463 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1464 nir_print_instr(&instr
->instr
, stderr
);
1465 fprintf(stderr
, "\n");
1469 case nir_op_flog2
: {
1470 if (dst
.size() == 1) {
1471 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f32
, dst
);
1473 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1474 nir_print_instr(&instr
->instr
, stderr
);
1475 fprintf(stderr
, "\n");
1480 if (dst
.size() == 1) {
1481 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f32
, dst
);
1482 } else if (dst
.size() == 2) {
1483 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1485 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1486 nir_print_instr(&instr
->instr
, stderr
);
1487 fprintf(stderr
, "\n");
1491 case nir_op_fexp2
: {
1492 if (dst
.size() == 1) {
1493 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1495 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1496 nir_print_instr(&instr
->instr
, stderr
);
1497 fprintf(stderr
, "\n");
1501 case nir_op_fsqrt
: {
1502 if (dst
.size() == 1) {
1503 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f32
, dst
);
1504 } else if (dst
.size() == 2) {
1505 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1507 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1508 nir_print_instr(&instr
->instr
, stderr
);
1509 fprintf(stderr
, "\n");
1513 case nir_op_ffract
: {
1514 if (dst
.size() == 1) {
1515 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1516 } else if (dst
.size() == 2) {
1517 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1519 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1520 nir_print_instr(&instr
->instr
, stderr
);
1521 fprintf(stderr
, "\n");
1525 case nir_op_ffloor
: {
1526 if (dst
.size() == 1) {
1527 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1528 } else if (dst
.size() == 2) {
1529 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1531 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1532 nir_print_instr(&instr
->instr
, stderr
);
1533 fprintf(stderr
, "\n");
1537 case nir_op_fceil
: {
1538 if (dst
.size() == 1) {
1539 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1540 } else if (dst
.size() == 2) {
1541 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1543 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1544 nir_print_instr(&instr
->instr
, stderr
);
1545 fprintf(stderr
, "\n");
1549 case nir_op_ftrunc
: {
1550 if (dst
.size() == 1) {
1551 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1552 } else if (dst
.size() == 2) {
1553 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1555 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1556 nir_print_instr(&instr
->instr
, stderr
);
1557 fprintf(stderr
, "\n");
1561 case nir_op_fround_even
: {
1562 if (dst
.size() == 1) {
1563 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1564 } else if (dst
.size() == 2) {
1565 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1567 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1568 nir_print_instr(&instr
->instr
, stderr
);
1569 fprintf(stderr
, "\n");
1575 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1576 aco_ptr
<Instruction
> norm
;
1577 if (dst
.size() == 1) {
1579 Operand
half_pi(0x3e22f983u
);
1580 if (src
.type() == RegType::sgpr
)
1581 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1583 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1585 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1586 if (ctx
->options
->chip_class
< GFX9
)
1587 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1589 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1590 bld
.vop1(opcode
, Definition(dst
), tmp
);
1592 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1593 nir_print_instr(&instr
->instr
, stderr
);
1594 fprintf(stderr
, "\n");
1598 case nir_op_ldexp
: {
1599 if (dst
.size() == 1) {
1600 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1601 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1602 get_alu_src(ctx
, instr
->src
[1]));
1603 } else if (dst
.size() == 2) {
1604 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1605 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1606 get_alu_src(ctx
, instr
->src
[1]));
1608 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1609 nir_print_instr(&instr
->instr
, stderr
);
1610 fprintf(stderr
, "\n");
1614 case nir_op_frexp_sig
: {
1615 if (dst
.size() == 1) {
1616 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1617 get_alu_src(ctx
, instr
->src
[0]));
1618 } else if (dst
.size() == 2) {
1619 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1620 get_alu_src(ctx
, instr
->src
[0]));
1622 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1623 nir_print_instr(&instr
->instr
, stderr
);
1624 fprintf(stderr
, "\n");
1628 case nir_op_frexp_exp
: {
1629 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1630 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1631 get_alu_src(ctx
, instr
->src
[0]));
1632 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1633 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1634 get_alu_src(ctx
, instr
->src
[0]));
1636 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1637 nir_print_instr(&instr
->instr
, stderr
);
1638 fprintf(stderr
, "\n");
1642 case nir_op_fsign
: {
1643 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1644 if (dst
.size() == 1) {
1645 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1646 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1647 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1648 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1649 } else if (dst
.size() == 2) {
1650 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1651 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1652 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, src
, cond
);
1654 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1655 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1656 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1658 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1660 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1661 nir_print_instr(&instr
->instr
, stderr
);
1662 fprintf(stderr
, "\n");
1666 case nir_op_f2f32
: {
1667 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1668 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1670 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1671 nir_print_instr(&instr
->instr
, stderr
);
1672 fprintf(stderr
, "\n");
1676 case nir_op_f2f64
: {
1677 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1678 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1680 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1681 nir_print_instr(&instr
->instr
, stderr
);
1682 fprintf(stderr
, "\n");
1686 case nir_op_i2f32
: {
1687 assert(dst
.size() == 1);
1688 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1691 case nir_op_i2f64
: {
1692 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1693 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1694 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1695 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1696 RegClass rc
= RegClass(src
.type(), 1);
1697 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1698 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1699 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1700 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1701 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1702 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1705 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1706 nir_print_instr(&instr
->instr
, stderr
);
1707 fprintf(stderr
, "\n");
1711 case nir_op_u2f32
: {
1712 assert(dst
.size() == 1);
1713 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1716 case nir_op_u2f64
: {
1717 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1718 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1719 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1720 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1721 RegClass rc
= RegClass(src
.type(), 1);
1722 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1723 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1724 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1725 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1726 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1727 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1729 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1730 nir_print_instr(&instr
->instr
, stderr
);
1731 fprintf(stderr
, "\n");
1735 case nir_op_f2i32
: {
1736 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1737 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1738 if (dst
.type() == RegType::vgpr
)
1739 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1741 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1742 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1744 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1745 if (dst
.type() == RegType::vgpr
)
1746 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1748 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1749 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1752 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1753 nir_print_instr(&instr
->instr
, stderr
);
1754 fprintf(stderr
, "\n");
1758 case nir_op_f2u32
: {
1759 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1760 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1761 if (dst
.type() == RegType::vgpr
)
1762 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1764 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1765 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1767 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1768 if (dst
.type() == RegType::vgpr
)
1769 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1771 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1772 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1775 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1776 nir_print_instr(&instr
->instr
, stderr
);
1777 fprintf(stderr
, "\n");
1781 case nir_op_f2i64
: {
1782 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1783 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1784 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1785 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1786 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1787 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1788 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1789 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1790 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1791 Temp new_exponent
= bld
.tmp(v1
);
1792 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1793 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1794 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1795 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1796 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1797 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1798 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1799 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1800 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1801 Temp new_lower
= bld
.tmp(v1
);
1802 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1803 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1804 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1806 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1807 if (src
.type() == RegType::vgpr
)
1808 src
= bld
.as_uniform(src
);
1809 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1810 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1811 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1812 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1813 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1814 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1815 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1816 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1817 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1818 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1819 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1820 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1821 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1822 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1823 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1824 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1825 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1826 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1827 Temp borrow
= bld
.tmp(s1
);
1828 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1829 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1830 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1832 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1833 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1834 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1835 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1836 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1837 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1838 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1839 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1840 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1841 if (dst
.type() == RegType::sgpr
) {
1842 lower
= bld
.as_uniform(lower
);
1843 upper
= bld
.as_uniform(upper
);
1845 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1848 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1849 nir_print_instr(&instr
->instr
, stderr
);
1850 fprintf(stderr
, "\n");
1854 case nir_op_f2u64
: {
1855 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1856 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1857 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1858 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1859 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1860 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1861 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1862 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1863 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1864 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1865 Temp new_exponent
= bld
.tmp(v1
);
1866 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1867 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1868 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1869 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1870 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1871 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1872 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1873 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1874 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1876 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1877 if (src
.type() == RegType::vgpr
)
1878 src
= bld
.as_uniform(src
);
1879 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1880 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1881 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1882 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1883 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1884 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1885 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1886 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1887 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1888 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1889 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1890 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1891 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1892 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1893 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1894 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1895 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1896 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1898 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1899 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1900 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1901 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1902 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1903 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1904 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1905 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1906 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1907 if (dst
.type() == RegType::sgpr
) {
1908 lower
= bld
.as_uniform(lower
);
1909 upper
= bld
.as_uniform(upper
);
1911 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1914 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1915 nir_print_instr(&instr
->instr
, stderr
);
1916 fprintf(stderr
, "\n");
1920 case nir_op_b2f32
: {
1921 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1922 if (dst
.regClass() == s1
) {
1923 src
= as_uniform_bool(ctx
, src
);
1924 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1925 } else if (dst
.regClass() == v1
) {
1926 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
),
1927 as_divergent_bool(ctx
, src
, true));
1929 unreachable("Wrong destination register class for nir_op_b2f32.");
1933 case nir_op_b2f64
: {
1934 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1935 if (dst
.regClass() == s2
) {
1936 src
= as_uniform_bool(ctx
, src
);
1937 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1938 } else if (dst
.regClass() == v2
) {
1939 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1940 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
,
1941 as_divergent_bool(ctx
, src
, true));
1942 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1944 unreachable("Wrong destination register class for nir_op_b2f64.");
1948 case nir_op_i2i32
: {
1949 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1950 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1951 /* we can actually just say dst = src, as it would map the lower register */
1952 emit_extract_vector(ctx
, src
, 0, dst
);
1954 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1955 nir_print_instr(&instr
->instr
, stderr
);
1956 fprintf(stderr
, "\n");
1960 case nir_op_u2u32
: {
1961 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1962 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
1963 if (dst
.regClass() == s1
) {
1964 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
1966 // TODO: do better with SDWA
1967 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
1969 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1970 /* we can actually just say dst = src, as it would map the lower register */
1971 emit_extract_vector(ctx
, src
, 0, dst
);
1973 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1974 nir_print_instr(&instr
->instr
, stderr
);
1975 fprintf(stderr
, "\n");
1979 case nir_op_i2i64
: {
1980 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1981 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1982 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1984 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1985 nir_print_instr(&instr
->instr
, stderr
);
1986 fprintf(stderr
, "\n");
1990 case nir_op_u2u64
: {
1991 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1992 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1993 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1995 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1996 nir_print_instr(&instr
->instr
, stderr
);
1997 fprintf(stderr
, "\n");
2001 case nir_op_b2i32
: {
2002 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2003 if (dst
.regClass() == s1
) {
2004 if (src
.regClass() == s1
) {
2005 bld
.copy(Definition(dst
), src
);
2007 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2008 assert(src
.regClass() == s2
);
2009 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2012 assert(dst
.regClass() == v1
&& src
.regClass() == s2
);
2013 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2018 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2019 if (dst
.regClass() == s2
) {
2020 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2021 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2022 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2024 assert(src
.regClass() == s1
&& dst
.regClass() == s1
);
2025 bld
.sopc(aco_opcode::s_cmp_lg_u32
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2029 case nir_op_pack_64_2x32_split
: {
2030 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2031 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2033 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2036 case nir_op_unpack_64_2x32_split_x
:
2037 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2039 case nir_op_unpack_64_2x32_split_y
:
2040 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2042 case nir_op_pack_half_2x16
: {
2043 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2045 if (dst
.regClass() == v1
) {
2046 Temp src0
= bld
.tmp(v1
);
2047 Temp src1
= bld
.tmp(v1
);
2048 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2049 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2052 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2053 nir_print_instr(&instr
->instr
, stderr
);
2054 fprintf(stderr
, "\n");
2058 case nir_op_unpack_half_2x16_split_x
: {
2059 if (dst
.regClass() == v1
) {
2060 Builder
bld(ctx
->program
, ctx
->block
);
2061 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2063 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2064 nir_print_instr(&instr
->instr
, stderr
);
2065 fprintf(stderr
, "\n");
2069 case nir_op_unpack_half_2x16_split_y
: {
2070 if (dst
.regClass() == v1
) {
2071 Builder
bld(ctx
->program
, ctx
->block
);
2072 /* TODO: use SDWA here */
2073 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2074 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2076 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2077 nir_print_instr(&instr
->instr
, stderr
);
2078 fprintf(stderr
, "\n");
2082 case nir_op_fquantize2f16
: {
2083 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]));
2085 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2087 Temp cmp_res
= bld
.tmp(s2
);
2088 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2090 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2092 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2096 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2097 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2099 if (dst
.regClass() == s1
) {
2100 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2101 } else if (dst
.regClass() == v1
) {
2102 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2104 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2105 nir_print_instr(&instr
->instr
, stderr
);
2106 fprintf(stderr
, "\n");
2110 case nir_op_bitfield_select
: {
2111 /* (mask & insert) | (~mask & base) */
2112 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2113 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2114 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2116 /* dst = (insert & bitmask) | (base & ~bitmask) */
2117 if (dst
.regClass() == s1
) {
2118 aco_ptr
<Instruction
> sop2
;
2119 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2120 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2122 if (const_insert
&& const_bitmask
) {
2123 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2125 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2126 lhs
= Operand(insert
);
2130 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2131 if (const_base
&& const_bitmask
) {
2132 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2134 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2135 rhs
= Operand(base
);
2138 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2140 } else if (dst
.regClass() == v1
) {
2141 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2142 base
= as_vgpr(ctx
, base
);
2143 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2144 insert
= as_vgpr(ctx
, insert
);
2146 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2149 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2150 nir_print_instr(&instr
->instr
, stderr
);
2151 fprintf(stderr
, "\n");
2157 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2158 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2159 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2161 if (dst
.type() == RegType::sgpr
) {
2163 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2164 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2165 if (const_offset
&& const_bits
) {
2166 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2167 extract
= Operand(const_extract
);
2171 width
= Operand(const_bits
->u32
<< 16);
2173 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2175 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2179 if (dst
.regClass() == s1
) {
2180 if (instr
->op
== nir_op_ubfe
)
2181 opcode
= aco_opcode::s_bfe_u32
;
2183 opcode
= aco_opcode::s_bfe_i32
;
2184 } else if (dst
.regClass() == s2
) {
2185 if (instr
->op
== nir_op_ubfe
)
2186 opcode
= aco_opcode::s_bfe_u64
;
2188 opcode
= aco_opcode::s_bfe_i64
;
2190 unreachable("Unsupported BFE bit size");
2193 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2197 if (dst
.regClass() == v1
) {
2198 if (instr
->op
== nir_op_ubfe
)
2199 opcode
= aco_opcode::v_bfe_u32
;
2201 opcode
= aco_opcode::v_bfe_i32
;
2203 unreachable("Unsupported BFE bit size");
2206 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2210 case nir_op_bit_count
: {
2211 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2212 if (src
.regClass() == s1
) {
2213 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2214 } else if (src
.regClass() == v1
) {
2215 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2216 } else if (src
.regClass() == v2
) {
2217 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2218 emit_extract_vector(ctx
, src
, 1, v1
),
2219 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2220 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2221 } else if (src
.regClass() == s2
) {
2222 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2224 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2225 nir_print_instr(&instr
->instr
, stderr
);
2226 fprintf(stderr
, "\n");
2231 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2232 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f32
, dst
);
2233 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2234 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f64
, dst
);
2238 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2239 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f32
, dst
);
2240 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2241 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f64
, dst
);
2245 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2246 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f32
, dst
);
2247 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2248 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f64
, dst
);
2252 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2253 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f32
, dst
);
2254 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2255 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f64
, dst
);
2259 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2260 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i32
, dst
);
2261 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2262 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_i32
, dst
);
2263 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2264 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i64
, dst
);
2268 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2269 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i32
, dst
);
2270 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2271 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_i32
, dst
);
2272 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2273 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i64
, dst
);
2277 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2278 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i32
, dst
);
2279 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2280 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_i32
, dst
);
2281 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2282 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i64
, dst
);
2283 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2284 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_u64
, dst
);
2285 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2286 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2287 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2288 bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.scc(Definition(dst
)),
2289 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2290 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2291 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2292 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2293 bld
.sop2(aco_opcode::s_xnor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2294 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2296 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2297 nir_print_instr(&instr
->instr
, stderr
);
2298 fprintf(stderr
, "\n");
2303 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2304 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i32
, dst
);
2305 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2306 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i64
, dst
);
2307 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2308 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_i32
, dst
);
2309 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2310 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_u64
, dst
);
2311 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2312 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2313 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2314 bld
.sopc(aco_opcode::s_cmp_lg_i32
, bld
.scc(Definition(dst
)),
2315 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2316 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2317 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2318 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2319 bld
.sop2(aco_opcode::s_xor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2320 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2322 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2323 nir_print_instr(&instr
->instr
, stderr
);
2324 fprintf(stderr
, "\n");
2329 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2330 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u32
, dst
);
2331 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2332 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_u32
, dst
);
2333 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2334 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u64
, dst
);
2338 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2339 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u32
, dst
);
2340 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2341 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_u32
, dst
);
2342 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2343 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u64
, dst
);
2348 case nir_op_fddx_fine
:
2349 case nir_op_fddy_fine
:
2350 case nir_op_fddx_coarse
:
2351 case nir_op_fddy_coarse
: {
2352 Definition tl
= bld
.def(v1
);
2354 if (instr
->op
== nir_op_fddx_fine
) {
2355 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2356 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2357 } else if (instr
->op
== nir_op_fddy_fine
) {
2358 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2359 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2361 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2362 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2363 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2365 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2368 Definition tmp
= bld
.def(v1
);
2369 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2370 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2374 fprintf(stderr
, "Unknown NIR ALU instr: ");
2375 nir_print_instr(&instr
->instr
, stderr
);
2376 fprintf(stderr
, "\n");
2380 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2382 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2384 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2385 // which get truncated the lsb if double and msb if int
2386 // for now, we only use s_mov_b64 with 64bit inline constants
2387 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2388 assert(dst
.type() == RegType::sgpr
);
2390 if (dst
.size() == 1)
2392 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2394 assert(dst
.size() != 1);
2395 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2396 if (instr
->def
.bit_size
== 64)
2397 for (unsigned i
= 0; i
< dst
.size(); i
++)
2398 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2400 for (unsigned i
= 0; i
< dst
.size(); i
++)
2401 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2403 vec
->definitions
[0] = Definition(dst
);
2404 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2408 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2410 uint32_t new_mask
= 0;
2411 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2412 if (mask
& (1u << i
))
2413 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2417 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2419 /* This wouldn't work inside control flow or with indirect offsets but
2420 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2422 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2423 unsigned component
= nir_intrinsic_component(instr
);
2424 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2425 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2427 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2428 if (off_instr
->type
!= nir_instr_type_load_const
) {
2429 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2430 nir_print_instr(off_instr
, stderr
);
2431 fprintf(stderr
, "\n");
2433 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2435 if (instr
->src
[0].ssa
->bit_size
== 64)
2436 write_mask
= widen_mask(write_mask
, 2);
2438 for (unsigned i
= 0; i
< 8; ++i
) {
2439 if (write_mask
& (1 << i
)) {
2440 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2441 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2447 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2449 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2451 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2452 for (unsigned i
= 0; i
< 4; ++i
) {
2453 if (write_mask
& (1 << i
)) {
2454 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2455 values
[i
] = Operand(tmp
);
2457 values
[i
] = Operand(v1
);
2461 unsigned index
= nir_intrinsic_base(instr
) / 4;
2462 unsigned target
, col_format
;
2463 unsigned enabled_channels
= 0xF;
2464 aco_opcode compr_op
= (aco_opcode
)0;
2466 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2467 assert(offset
&& "Non-const offsets on exports not yet supported");
2468 index
+= offset
->u32
;
2470 assert(index
!= FRAG_RESULT_COLOR
);
2472 /* Unlike vertex shader exports, it's fine to use multiple exports to
2473 * export separate channels of one target. So shaders which export both
2474 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2475 * TODO: combine the exports in those cases and create better code
2478 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2480 if (ctx
->program
->info
->ps
.writes_z
) {
2481 target
= V_008DFC_SQ_EXP_MRTZ
;
2482 enabled_channels
= 0x4;
2483 col_format
= (unsigned) -1;
2485 values
[2] = values
[0];
2486 values
[0] = Operand(v1
);
2488 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2489 exp
->valid_mask
= false;
2491 exp
->compressed
= true;
2492 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2493 exp
->enabled_mask
= 0xc;
2494 for (int i
= 0; i
< 4; i
++)
2495 exp
->operands
[i
] = Operand(v1
);
2496 exp
->operands
[1] = Operand(values
[0]);
2497 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2501 } else if (index
== FRAG_RESULT_DEPTH
) {
2503 target
= V_008DFC_SQ_EXP_MRTZ
;
2504 enabled_channels
= 0x1;
2505 col_format
= (unsigned) -1;
2507 } else if (index
== FRAG_RESULT_STENCIL
) {
2509 if (ctx
->program
->info
->ps
.writes_z
) {
2510 target
= V_008DFC_SQ_EXP_MRTZ
;
2511 enabled_channels
= 0x2;
2512 col_format
= (unsigned) -1;
2514 values
[1] = values
[0];
2515 values
[0] = Operand(v1
);
2517 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2518 shift
->operands
[0] = Operand((uint32_t) 16);
2519 shift
->operands
[1] = values
[0];
2520 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2521 shift
->definitions
[0] = Definition(tmp
);
2522 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2524 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2525 exp
->valid_mask
= false;
2527 exp
->compressed
= true;
2528 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2529 exp
->enabled_mask
= 0x3;
2530 exp
->operands
[0] = Operand(tmp
);
2531 for (int i
= 1; i
< 4; i
++)
2532 exp
->operands
[i
] = Operand(v1
);
2533 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2538 index
-= FRAG_RESULT_DATA0
;
2539 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2540 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2542 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2543 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2544 assert(!is_int8
&& !is_int10
);
2548 case V_028714_SPI_SHADER_ZERO
:
2549 enabled_channels
= 0; /* writemask */
2550 target
= V_008DFC_SQ_EXP_NULL
;
2553 case V_028714_SPI_SHADER_32_R
:
2554 enabled_channels
= 1;
2557 case V_028714_SPI_SHADER_32_GR
:
2558 enabled_channels
= 0x3;
2561 case V_028714_SPI_SHADER_32_AR
:
2562 if (ctx
->options
->chip_class
>= GFX10
) {
2563 /* Special case: on GFX10, the outputs are different for 32_AR */
2564 enabled_channels
= 0x3;
2565 values
[1] = values
[3];
2567 enabled_channels
= 0x9;
2571 case V_028714_SPI_SHADER_FP16_ABGR
:
2572 enabled_channels
= 0x5;
2573 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2576 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2577 enabled_channels
= 0x5;
2578 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2581 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2582 enabled_channels
= 0x5;
2583 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2586 case V_028714_SPI_SHADER_UINT16_ABGR
:
2587 enabled_channels
= 0x5;
2588 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2591 case V_028714_SPI_SHADER_SINT16_ABGR
:
2592 enabled_channels
= 0x5;
2593 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2596 case V_028714_SPI_SHADER_32_ABGR
:
2597 enabled_channels
= 0xF;
2604 if (target
== V_008DFC_SQ_EXP_NULL
)
2609 for (int i
= 0; i
< 2; i
++)
2611 /* check if at least one of the values to be compressed is enabled */
2612 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2614 enabled_channels
|= enabled
<< (i
*2);
2615 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2616 Temp tmp
{ctx
->program
->allocateId(), v1
};
2617 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2618 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2619 compr
->definitions
[0] = Definition(tmp
);
2620 values
[i
] = Operand(tmp
);
2621 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2623 values
[i
] = Operand(v1
);
2628 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2629 exp
->valid_mask
= false;
2631 exp
->compressed
= (bool) compr_op
;
2633 exp
->enabled_mask
= enabled_channels
;
2634 if ((bool) compr_op
) {
2635 for (int i
= 0; i
< 2; i
++)
2636 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2637 exp
->operands
[2] = Operand(v1
);
2638 exp
->operands
[3] = Operand(v1
);
2640 for (int i
= 0; i
< 4; i
++)
2641 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2644 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2647 Operand
load_lds_size_m0(isel_context
*ctx
)
2649 /* TODO: m0 does not need to be initialized on GFX9+ */
2650 Builder
bld(ctx
->program
, ctx
->block
);
2651 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
2654 void load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
2655 Temp address
, unsigned base_offset
, unsigned align
)
2657 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2659 Builder
bld(ctx
->program
, ctx
->block
);
2661 Operand m
= load_lds_size_m0(ctx
);
2663 unsigned num_components
= dst
.size() * 4u / elem_size_bytes
;
2664 unsigned bytes_read
= 0;
2665 unsigned result_size
= 0;
2666 unsigned total_bytes
= num_components
* elem_size_bytes
;
2667 std::array
<Temp
, 4> result
;
2669 while (bytes_read
< total_bytes
) {
2670 unsigned todo
= total_bytes
- bytes_read
;
2671 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
2672 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
2674 aco_opcode op
= aco_opcode::last_opcode
;
2676 if (todo
>= 16 && aligned16
) {
2677 op
= aco_opcode::ds_read_b128
;
2679 } else if (todo
>= 16 && aligned8
) {
2680 op
= aco_opcode::ds_read2_b64
;
2683 } else if (todo
>= 12 && aligned16
) {
2684 op
= aco_opcode::ds_read_b96
;
2686 } else if (todo
>= 8 && aligned8
) {
2687 op
= aco_opcode::ds_read_b64
;
2689 } else if (todo
>= 8) {
2690 op
= aco_opcode::ds_read2_b32
;
2693 } else if (todo
>= 4) {
2694 op
= aco_opcode::ds_read_b32
;
2699 assert(todo
% elem_size_bytes
== 0);
2700 unsigned num_elements
= todo
/ elem_size_bytes
;
2701 unsigned offset
= base_offset
+ bytes_read
;
2702 unsigned max_offset
= read2
? 1019 : 65535;
2704 Temp address_offset
= address
;
2705 if (offset
> max_offset
) {
2706 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2707 offset
= bytes_read
;
2709 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
2712 if (num_components
== 1 && dst
.type() == RegType::vgpr
)
2715 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
2718 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
2720 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
2722 if (num_components
== 1) {
2723 assert(todo
== total_bytes
);
2724 if (dst
.type() == RegType::sgpr
)
2725 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
2729 if (dst
.type() == RegType::sgpr
)
2730 res
= bld
.as_uniform(res
);
2732 if (num_elements
== 1) {
2733 result
[result_size
++] = res
;
2735 assert(res
!= dst
&& res
.size() % num_elements
== 0);
2736 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
2737 split
->operands
[0] = Operand(res
);
2738 for (unsigned i
= 0; i
< num_elements
; i
++)
2739 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
2740 ctx
->block
->instructions
.emplace_back(std::move(split
));
2746 assert(result_size
== num_components
&& result_size
> 1);
2747 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
2748 for (unsigned i
= 0; i
< result_size
; i
++)
2749 vec
->operands
[i
] = Operand(result
[i
]);
2750 vec
->definitions
[0] = Definition(dst
);
2751 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2752 ctx
->allocated_vec
.emplace(dst
.id(), result
);
2755 Temp
extract_subvector(isel_context
*ctx
, Temp data
, unsigned start
, unsigned size
, RegType type
)
2757 if (start
== 0 && size
== data
.size())
2758 return type
== RegType::vgpr
? as_vgpr(ctx
, data
) : data
;
2760 unsigned size_hint
= 1;
2761 auto it
= ctx
->allocated_vec
.find(data
.id());
2762 if (it
!= ctx
->allocated_vec
.end())
2763 size_hint
= it
->second
[0].size();
2764 if (size
% size_hint
|| start
% size_hint
)
2771 for (unsigned i
= 0; i
< size
; i
++)
2772 elems
[i
] = emit_extract_vector(ctx
, data
, start
+ i
, RegClass(type
, size_hint
));
2775 return type
== RegType::vgpr
? as_vgpr(ctx
, elems
[0]) : elems
[0];
2777 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
2778 for (unsigned i
= 0; i
< size
; i
++)
2779 vec
->operands
[i
] = Operand(elems
[i
]);
2780 Temp res
= {ctx
->program
->allocateId(), RegClass(type
, size
* size_hint
)};
2781 vec
->definitions
[0] = Definition(res
);
2782 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2786 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
)
2788 Builder
bld(ctx
->program
, ctx
->block
);
2789 unsigned bytes_written
= 0;
2790 while (bytes_written
< total_size
* 4) {
2791 unsigned todo
= total_size
* 4 - bytes_written
;
2792 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
2793 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
2795 aco_opcode op
= aco_opcode::last_opcode
;
2796 bool write2
= false;
2798 if (todo
>= 16 && aligned16
) {
2799 op
= aco_opcode::ds_write_b128
;
2801 } else if (todo
>= 16 && aligned8
) {
2802 op
= aco_opcode::ds_write2_b64
;
2805 } else if (todo
>= 12 && aligned16
) {
2806 op
= aco_opcode::ds_write_b96
;
2808 } else if (todo
>= 8 && aligned8
) {
2809 op
= aco_opcode::ds_write_b64
;
2811 } else if (todo
>= 8) {
2812 op
= aco_opcode::ds_write2_b32
;
2815 } else if (todo
>= 4) {
2816 op
= aco_opcode::ds_write_b32
;
2822 unsigned offset
= offset0
+ offset1
+ bytes_written
;
2823 unsigned max_offset
= write2
? 1020 : 65535;
2824 Temp address_offset
= address
;
2825 if (offset
> max_offset
) {
2826 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
2827 offset
= offset1
+ bytes_written
;
2829 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
2832 Temp val0
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
/ 2, RegType::vgpr
);
2833 Temp val1
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2) + 1, size
/ 2, RegType::vgpr
);
2834 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
2836 Temp val
= extract_subvector(ctx
, data
, data_start
+ (bytes_written
>> 2), size
, RegType::vgpr
);
2837 bld
.ds(op
, address_offset
, val
, m
, offset
);
2840 bytes_written
+= size
* 4;
2844 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
2845 Temp address
, unsigned base_offset
, unsigned align
)
2847 assert(util_is_power_of_two_nonzero(align
) && align
>= 4);
2849 Operand m
= load_lds_size_m0(ctx
);
2851 /* we need at most two stores for 32bit variables */
2852 int start
[2], count
[2];
2853 u_bit_scan_consecutive_range(&wrmask
, &start
[0], &count
[0]);
2854 u_bit_scan_consecutive_range(&wrmask
, &start
[1], &count
[1]);
2855 assert(wrmask
== 0);
2857 /* one combined store is sufficient */
2858 if (count
[0] == count
[1]) {
2859 Builder
bld(ctx
->program
, ctx
->block
);
2861 Temp address_offset
= address
;
2862 if ((base_offset
>> 2) + start
[1] > 255) {
2863 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
2867 assert(count
[0] == 1);
2868 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
2869 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
2870 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
2871 base_offset
= base_offset
/ elem_size_bytes
;
2872 bld
.ds(op
, address_offset
, val0
, val1
, m
,
2873 base_offset
+ start
[0], base_offset
+ start
[1]);
2877 for (unsigned i
= 0; i
< 2; i
++) {
2881 unsigned elem_size_words
= elem_size_bytes
/ 4;
2882 ds_write_helper(ctx
, m
, address
, data
, start
[i
] * elem_size_words
, count
[i
] * elem_size_words
,
2883 base_offset
, start
[i
] * elem_size_bytes
, align
);
2888 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2890 if (ctx
->stage
== vertex_vs
) {
2891 visit_store_vs_output(ctx
, instr
);
2892 } else if (ctx
->stage
== fragment_fs
) {
2893 visit_store_fs_output(ctx
, instr
);
2895 unreachable("Shader stage not implemented");
2899 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2901 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2902 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2904 Builder
bld(ctx
->program
, ctx
->block
);
2905 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2906 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2909 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2911 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2912 for (unsigned i
= 0; i
< num_components
; i
++)
2913 vec
->operands
[i
] = Operand(ctx
->fs_inputs
[fs_input::frag_pos_0
+ i
]);
2915 if (ctx
->fs_vgpr_args
[fs_input::frag_pos_3
]) {
2916 assert(num_components
== 4);
2917 Builder
bld(ctx
->program
, ctx
->block
);
2918 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::frag_pos_3
]);
2921 for (Operand
& op
: vec
->operands
)
2922 op
= op
.isUndefined() ? Operand(0u) : op
;
2924 vec
->definitions
[0] = Definition(dst
);
2925 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2926 emit_split_vector(ctx
, dst
, num_components
);
2930 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2932 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2933 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2934 unsigned idx
= nir_intrinsic_base(instr
);
2935 unsigned component
= nir_intrinsic_component(instr
);
2936 Temp prim_mask
= ctx
->prim_mask
;
2938 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2940 assert(offset
->u32
== 0);
2942 /* the lower 15bit of the prim_mask contain the offset into LDS
2943 * while the upper bits contain the number of prims */
2944 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2945 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2946 Builder
bld(ctx
->program
, ctx
->block
);
2947 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2948 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2949 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2950 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2951 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2954 if (instr
->dest
.ssa
.num_components
== 1) {
2955 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2957 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2958 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2960 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2961 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2962 vec
->operands
[i
] = Operand(tmp
);
2964 vec
->definitions
[0] = Definition(dst
);
2965 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2969 unsigned get_num_channels_from_data_format(unsigned data_format
)
2971 switch (data_format
) {
2972 case V_008F0C_BUF_DATA_FORMAT_8
:
2973 case V_008F0C_BUF_DATA_FORMAT_16
:
2974 case V_008F0C_BUF_DATA_FORMAT_32
:
2976 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2977 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2978 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2980 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2981 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2982 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2984 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2985 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2986 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2987 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2988 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2997 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2998 * so we may need to fix it up. */
2999 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
3001 Builder
bld(ctx
->program
, ctx
->block
);
3003 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
3004 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
3006 /* For the integer-like cases, do a natural sign extension.
3008 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
3009 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
3012 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
3013 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
3015 /* Convert back to the right type. */
3016 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
3017 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3018 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
3019 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
3020 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
3021 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
3027 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3029 Builder
bld(ctx
->program
, ctx
->block
);
3030 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3031 if (ctx
->stage
& sw_vs
) {
3033 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3034 if (off_instr
->type
!= nir_instr_type_load_const
) {
3035 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3036 nir_print_instr(off_instr
, stderr
);
3037 fprintf(stderr
, "\n");
3039 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
3041 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, ctx
->vertex_buffers
);
3043 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
3044 unsigned component
= nir_intrinsic_component(instr
);
3045 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
3046 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
3047 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
3048 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
3050 unsigned dfmt
= attrib_format
& 0xf;
3052 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
3053 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
3054 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
3055 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
3056 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
3057 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
3059 num_channels
= MAX2(num_channels
, 3);
3061 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
3064 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
3065 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
3067 ctx
->needs_instance_id
= true;
3070 Temp divided
= bld
.tmp(v1
);
3071 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, ctx
->instance_id
), divisor
);
3072 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, divided
);
3074 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, ctx
->instance_id
);
3077 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), ctx
->start_instance
);
3080 index
= bld
.vadd32(bld
.def(v1
), ctx
->base_vertex
, ctx
->vertex_id
);
3083 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
3084 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
3085 attrib_offset
= attrib_offset
% attrib_stride
;
3088 Operand
soffset(0u);
3089 if (attrib_offset
>= 4096) {
3090 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
3095 switch (num_channels
) {
3097 opcode
= aco_opcode::tbuffer_load_format_x
;
3100 opcode
= aco_opcode::tbuffer_load_format_xy
;
3103 opcode
= aco_opcode::tbuffer_load_format_xyz
;
3106 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
3109 unreachable("Unimplemented load_input vector size");
3112 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
3114 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
3115 mubuf
->operands
[0] = Operand(index
);
3116 mubuf
->operands
[1] = Operand(list
);
3117 mubuf
->operands
[2] = soffset
;
3118 mubuf
->definitions
[0] = Definition(tmp
);
3119 mubuf
->idxen
= true;
3120 mubuf
->can_reorder
= true;
3123 assert(attrib_offset
< 4096);
3124 mubuf
->offset
= attrib_offset
;
3125 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3127 emit_split_vector(ctx
, tmp
, tmp
.size());
3129 if (tmp
.id() != dst
.id()) {
3130 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
3131 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
3133 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
3134 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
3135 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
3137 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3138 for (unsigned i
= 0; i
< dst
.size(); i
++) {
3139 unsigned idx
= i
+ component
;
3140 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
3141 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
3142 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
3143 } else if (idx
< num_channels
) {
3144 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
3145 } else if (is_float
&& idx
== 3) {
3146 vec
->operands
[i
] = Operand(0x3f800000u
);
3147 } else if (!is_float
&& idx
== 3) {
3148 vec
->operands
[i
] = Operand(1u);
3150 vec
->operands
[i
] = Operand(0u);
3153 vec
->definitions
[0] = Definition(dst
);
3154 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3155 emit_split_vector(ctx
, dst
, dst
.size());
3158 } else if (ctx
->stage
== fragment_fs
) {
3159 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
3160 if (off_instr
->type
!= nir_instr_type_load_const
||
3161 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
3162 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
3163 nir_print_instr(off_instr
, stderr
);
3164 fprintf(stderr
, "\n");
3167 Temp prim_mask
= ctx
->prim_mask
;
3168 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
3170 assert(offset
->u32
== 0);
3172 /* the lower 15bit of the prim_mask contain the offset into LDS
3173 * while the upper bits contain the number of prims */
3174 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3175 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
3176 Builder
bld(ctx
->program
, ctx
->block
);
3177 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
3178 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
3179 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
3180 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
3181 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
3184 unsigned idx
= nir_intrinsic_base(instr
);
3185 unsigned component
= nir_intrinsic_component(instr
);
3187 if (dst
.size() == 1) {
3188 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
3190 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3191 for (unsigned i
= 0; i
< dst
.size(); i
++)
3192 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
3193 vec
->definitions
[0] = Definition(dst
);
3194 bld
.insert(std::move(vec
));
3198 unreachable("Shader stage not implemented");
3202 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
3204 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
3205 Builder
bld(ctx
->program
, ctx
->block
);
3206 Temp ptr64
= convert_pointer_to_64_bit(ctx
, ctx
->descriptor_sets
[0]);
3207 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
3210 return ctx
->descriptor_sets
[desc_set
];
3214 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3216 Builder
bld(ctx
->program
, ctx
->block
);
3217 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3218 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
])
3219 index
= bld
.as_uniform(index
);
3220 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3221 unsigned binding
= nir_intrinsic_binding(instr
);
3224 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
3225 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
3226 unsigned offset
= layout
->binding
[binding
].offset
;
3228 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
3229 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
3230 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
3231 desc_ptr
= ctx
->push_constants
;
3232 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
3235 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
3236 stride
= layout
->binding
[binding
].size
;
3239 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
3240 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
3242 if (nir_const_index
) {
3243 const_index
= const_index
* stride
;
3244 } else if (index
.type() == RegType::vgpr
) {
3245 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
3246 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
3248 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
3252 if (nir_const_index
) {
3253 const_index
= const_index
+ offset
;
3254 } else if (index
.type() == RegType::vgpr
) {
3255 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
3257 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
3261 if (nir_const_index
&& const_index
== 0) {
3263 } else if (index
.type() == RegType::vgpr
) {
3264 index
= bld
.vadd32(bld
.def(v1
),
3265 nir_const_index
? Operand(const_index
) : Operand(index
),
3268 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3269 nir_const_index
? Operand(const_index
) : Operand(index
),
3273 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
3276 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
,
3277 Temp rsrc
, Temp offset
, bool glc
=false, bool readonly
=true)
3279 Builder
bld(ctx
->program
, ctx
->block
);
3281 unsigned num_bytes
= dst
.size() * 4;
3282 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
3285 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3286 if (ctx
->options
->chip_class
< GFX8
)
3287 offset
= as_vgpr(ctx
, offset
);
3289 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3290 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3291 unsigned const_offset
= 0;
3293 Temp lower
= Temp();
3294 if (num_bytes
> 16) {
3295 assert(num_components
== 3 || num_components
== 4);
3296 op
= aco_opcode::buffer_load_dwordx4
;
3297 lower
= bld
.tmp(v4
);
3298 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3299 mubuf
->definitions
[0] = Definition(lower
);
3300 mubuf
->operands
[0] = vaddr
;
3301 mubuf
->operands
[1] = Operand(rsrc
);
3302 mubuf
->operands
[2] = soffset
;
3303 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3306 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3307 mubuf
->can_reorder
= readonly
;
3308 bld
.insert(std::move(mubuf
));
3309 emit_split_vector(ctx
, lower
, 2);
3314 switch (num_bytes
) {
3316 op
= aco_opcode::buffer_load_dword
;
3319 op
= aco_opcode::buffer_load_dwordx2
;
3322 op
= aco_opcode::buffer_load_dwordx3
;
3325 op
= aco_opcode::buffer_load_dwordx4
;
3328 unreachable("Load SSBO not implemented for this size.");
3330 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3331 mubuf
->operands
[0] = vaddr
;
3332 mubuf
->operands
[1] = Operand(rsrc
);
3333 mubuf
->operands
[2] = soffset
;
3334 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3337 mubuf
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3338 mubuf
->can_reorder
= readonly
;
3339 mubuf
->offset
= const_offset
;
3340 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3342 if (dst
.size() > 4) {
3343 assert(lower
!= Temp());
3344 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3345 instr
->definitions
[0] = Definition(upper
);
3346 bld
.insert(std::move(instr
));
3347 if (dst
.size() == 8)
3348 emit_split_vector(ctx
, upper
, 2);
3349 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3350 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3351 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3352 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3353 if (dst
.size() == 8)
3354 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3357 if (dst
.type() == RegType::sgpr
) {
3358 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3359 instr
->definitions
[0] = Definition(vec
);
3360 bld
.insert(std::move(instr
));
3361 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3363 instr
->definitions
[0] = Definition(dst
);
3364 bld
.insert(std::move(instr
));
3367 switch (num_bytes
) {
3369 op
= aco_opcode::s_buffer_load_dword
;
3372 op
= aco_opcode::s_buffer_load_dwordx2
;
3376 op
= aco_opcode::s_buffer_load_dwordx4
;
3380 op
= aco_opcode::s_buffer_load_dwordx8
;
3383 unreachable("Load SSBO not implemented for this size.");
3385 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3386 load
->operands
[0] = Operand(rsrc
);
3387 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3388 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3389 load
->definitions
[0] = Definition(dst
);
3392 load
->barrier
= readonly
? barrier_none
: barrier_buffer
;
3393 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3394 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3397 if (dst
.size() == 3) {
3398 Temp vec
= bld
.tmp(s4
);
3399 load
->definitions
[0] = Definition(vec
);
3400 bld
.insert(std::move(load
));
3401 emit_split_vector(ctx
, vec
, 4);
3403 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3404 emit_extract_vector(ctx
, vec
, 0, s1
),
3405 emit_extract_vector(ctx
, vec
, 1, s1
),
3406 emit_extract_vector(ctx
, vec
, 2, s1
));
3407 } else if (dst
.size() == 6) {
3408 Temp vec
= bld
.tmp(s8
);
3409 load
->definitions
[0] = Definition(vec
);
3410 bld
.insert(std::move(load
));
3411 emit_split_vector(ctx
, vec
, 4);
3413 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3414 emit_extract_vector(ctx
, vec
, 0, s2
),
3415 emit_extract_vector(ctx
, vec
, 1, s2
),
3416 emit_extract_vector(ctx
, vec
, 2, s2
));
3418 bld
.insert(std::move(load
));
3422 emit_split_vector(ctx
, dst
, num_components
);
3425 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3427 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3428 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3430 Builder
bld(ctx
->program
, ctx
->block
);
3432 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3433 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3434 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3435 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3437 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3438 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3439 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3440 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3441 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3442 if (ctx
->options
->chip_class
>= GFX10
) {
3443 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3444 S_008F0C_OOB_SELECT(3) |
3445 S_008F0C_RESOURCE_LEVEL(1);
3447 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3448 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3450 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3451 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3452 Operand(0xFFFFFFFFu
),
3453 Operand(desc_type
));
3454 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3455 rsrc
, upper_dwords
);
3457 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3458 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3461 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3464 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3466 Builder
bld(ctx
->program
, ctx
->block
);
3467 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3469 unsigned offset
= nir_intrinsic_base(instr
);
3470 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3471 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3473 unsigned count
= instr
->dest
.ssa
.num_components
;
3474 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3475 start
-= ctx
->base_inline_push_consts
;
3476 if (start
+ count
<= ctx
->num_inline_push_consts
) {
3477 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3478 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3479 for (unsigned i
= 0; i
< count
; ++i
) {
3480 elems
[i
] = ctx
->inline_push_consts
[start
+ i
];
3481 vec
->operands
[i
] = Operand
{elems
[i
]};
3483 vec
->definitions
[0] = Definition(dst
);
3484 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3485 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3490 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3491 if (offset
!= 0) // TODO check if index != 0 as well
3492 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3493 Temp ptr
= convert_pointer_to_64_bit(ctx
, ctx
->push_constants
);
3498 switch (dst
.size()) {
3500 op
= aco_opcode::s_load_dword
;
3503 op
= aco_opcode::s_load_dwordx2
;
3509 op
= aco_opcode::s_load_dwordx4
;
3515 op
= aco_opcode::s_load_dwordx8
;
3518 unreachable("unimplemented or forbidden load_push_constant.");
3521 bld
.smem(op
, Definition(vec
), ptr
, index
);
3524 emit_split_vector(ctx
, vec
, 4);
3525 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3526 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3527 emit_extract_vector(ctx
, vec
, 0, rc
),
3528 emit_extract_vector(ctx
, vec
, 1, rc
),
3529 emit_extract_vector(ctx
, vec
, 2, rc
));
3532 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3535 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3537 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3539 Builder
bld(ctx
->program
, ctx
->block
);
3541 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3542 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3543 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3544 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3545 if (ctx
->options
->chip_class
>= GFX10
) {
3546 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3547 S_008F0C_OOB_SELECT(3) |
3548 S_008F0C_RESOURCE_LEVEL(1);
3550 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3551 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3554 unsigned base
= nir_intrinsic_base(instr
);
3555 unsigned range
= nir_intrinsic_range(instr
);
3557 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3558 if (base
&& offset
.type() == RegType::sgpr
)
3559 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3560 else if (base
&& offset
.type() == RegType::vgpr
)
3561 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3563 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3564 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
3565 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
3566 Operand(desc_type
));
3568 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3571 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3573 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3574 ctx
->cf_info
.exec_potentially_empty
= true;
3576 ctx
->program
->needs_exact
= true;
3578 // TODO: optimize uniform conditions
3579 Builder
bld(ctx
->program
, ctx
->block
);
3580 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
3581 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3582 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3583 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3587 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3589 Builder
bld(ctx
->program
, ctx
->block
);
3591 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3592 ctx
->cf_info
.exec_potentially_empty
= true;
3594 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3595 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3597 if (ctx
->block
->loop_nest_depth
&&
3598 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3599 /* we handle discards the same way as jump instructions */
3600 append_logical_end(ctx
->block
);
3602 /* in loops, discard behaves like break */
3603 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3604 ctx
->block
->kind
|= block_kind_discard
;
3607 /* uniform discard - loop ends here */
3608 assert(nir_instr_is_last(&instr
->instr
));
3609 ctx
->block
->kind
|= block_kind_uniform
;
3610 ctx
->cf_info
.has_branch
= true;
3611 bld
.branch(aco_opcode::p_branch
);
3612 add_linear_edge(ctx
->block
->index
, linear_target
);
3616 /* we add a break right behind the discard() instructions */
3617 ctx
->block
->kind
|= block_kind_break
;
3618 unsigned idx
= ctx
->block
->index
;
3620 /* remove critical edges from linear CFG */
3621 bld
.branch(aco_opcode::p_branch
);
3622 Block
* break_block
= ctx
->program
->create_and_insert_block();
3623 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3624 break_block
->kind
|= block_kind_uniform
;
3625 add_linear_edge(idx
, break_block
);
3626 add_linear_edge(break_block
->index
, linear_target
);
3627 bld
.reset(break_block
);
3628 bld
.branch(aco_opcode::p_branch
);
3630 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3631 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3632 add_linear_edge(idx
, continue_block
);
3633 append_logical_start(continue_block
);
3634 ctx
->block
= continue_block
;
3639 /* it can currently happen that NIR doesn't remove the unreachable code */
3640 if (!nir_instr_is_last(&instr
->instr
)) {
3641 ctx
->program
->needs_exact
= true;
3642 /* save exec somewhere temporarily so that it doesn't get
3643 * overwritten before the discard from outer exec masks */
3644 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3645 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3646 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3650 /* This condition is incorrect for uniformly branched discards in a loop
3651 * predicated by a divergent condition, but the above code catches that case
3652 * and the discard would end up turning into a discard_if.
3662 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3663 /* program just ends here */
3664 ctx
->block
->kind
|= block_kind_uniform
;
3665 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3666 0 /* enabled mask */, 9 /* dest */,
3667 false /* compressed */, true/* done */, true /* valid mask */);
3668 bld
.sopp(aco_opcode::s_endpgm
);
3669 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3671 ctx
->block
->kind
|= block_kind_discard
;
3672 /* branch and linear edge is added by visit_if() */
3676 enum aco_descriptor_type
{
3687 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3688 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3690 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
3691 return dim
== ac_image_cube
||
3692 dim
== ac_image_1darray
||
3693 dim
== ac_image_2darray
||
3694 dim
== ac_image_2darraymsaa
;
3697 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3698 enum aco_descriptor_type desc_type
,
3699 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3701 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3702 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3703 if (it != ctx->tex_desc.end())
3706 Temp index
= Temp();
3707 bool index_set
= false;
3708 unsigned constant_index
= 0;
3709 unsigned descriptor_set
;
3710 unsigned base_index
;
3711 Builder
bld(ctx
->program
, ctx
->block
);
3714 assert(tex_instr
&& !image
);
3716 base_index
= tex_instr
->sampler_index
;
3718 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3719 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3723 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3724 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3726 constant_index
+= array_size
* const_value
->u32
;
3728 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
3729 if (indirect
.type() == RegType::vgpr
)
3730 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
3732 if (array_size
!= 1)
3733 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3739 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3743 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3745 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3746 base_index
= deref_instr
->var
->data
.binding
;
3749 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3750 list
= convert_pointer_to_64_bit(ctx
, list
);
3752 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3753 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3754 unsigned offset
= binding
->offset
;
3755 unsigned stride
= binding
->size
;
3759 assert(base_index
< layout
->binding_count
);
3761 switch (desc_type
) {
3762 case ACO_DESC_IMAGE
:
3764 opcode
= aco_opcode::s_load_dwordx8
;
3766 case ACO_DESC_FMASK
:
3768 opcode
= aco_opcode::s_load_dwordx8
;
3771 case ACO_DESC_SAMPLER
:
3773 opcode
= aco_opcode::s_load_dwordx4
;
3774 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3775 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3777 case ACO_DESC_BUFFER
:
3779 opcode
= aco_opcode::s_load_dwordx4
;
3781 case ACO_DESC_PLANE_0
:
3782 case ACO_DESC_PLANE_1
:
3784 opcode
= aco_opcode::s_load_dwordx8
;
3785 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3787 case ACO_DESC_PLANE_2
:
3789 opcode
= aco_opcode::s_load_dwordx4
;
3793 unreachable("invalid desc_type\n");
3796 offset
+= constant_index
* stride
;
3798 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3799 (!index_set
|| binding
->immutable_samplers_equal
)) {
3800 if (binding
->immutable_samplers_equal
)
3803 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3804 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3805 Operand(samplers
[constant_index
* 4 + 0]),
3806 Operand(samplers
[constant_index
* 4 + 1]),
3807 Operand(samplers
[constant_index
* 4 + 2]),
3808 Operand(samplers
[constant_index
* 4 + 3]));
3813 off
= Operand(offset
);
3815 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3816 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3819 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3821 if (desc_type
== ACO_DESC_PLANE_2
) {
3823 for (unsigned i
= 0; i
< 8; i
++)
3824 components
[i
] = bld
.tmp(s1
);
3825 bld
.pseudo(aco_opcode::p_split_vector
,
3826 Definition(components
[0]),
3827 Definition(components
[1]),
3828 Definition(components
[2]),
3829 Definition(components
[3]),
3832 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3833 bld
.pseudo(aco_opcode::p_split_vector
,
3834 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3835 Definition(components
[4]),
3836 Definition(components
[5]),
3837 Definition(components
[6]),
3838 Definition(components
[7]),
3841 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3842 components
[0], components
[1], components
[2], components
[3],
3843 components
[4], components
[5], components
[6], components
[7]);
3849 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3852 case GLSL_SAMPLER_DIM_BUF
:
3854 case GLSL_SAMPLER_DIM_1D
:
3855 return array
? 2 : 1;
3856 case GLSL_SAMPLER_DIM_2D
:
3857 return array
? 3 : 2;
3858 case GLSL_SAMPLER_DIM_MS
:
3859 return array
? 4 : 3;
3860 case GLSL_SAMPLER_DIM_3D
:
3861 case GLSL_SAMPLER_DIM_CUBE
:
3863 case GLSL_SAMPLER_DIM_RECT
:
3864 case GLSL_SAMPLER_DIM_SUBPASS
:
3866 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3875 /* Adjust the sample index according to FMASK.
3877 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3878 * which is the identity mapping. Each nibble says which physical sample
3879 * should be fetched to get that sample.
3881 * For example, 0x11111100 means there are only 2 samples stored and
3882 * the second sample covers 3/4 of the pixel. When reading samples 0
3883 * and 1, return physical sample 0 (determined by the first two 0s
3884 * in FMASK), otherwise return physical sample 1.
3886 * The sample index should be adjusted as follows:
3887 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3889 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3891 Builder
bld(ctx
->program
, ctx
->block
);
3892 Temp fmask
= bld
.tmp(v1
);
3893 unsigned dim
= ctx
->options
->chip_class
>= GFX10
3894 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
3897 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3898 load
->operands
[0] = Operand(coords
);
3899 load
->operands
[1] = Operand(fmask_desc_ptr
);
3900 load
->definitions
[0] = Definition(fmask
);
3907 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3908 ctx
->block
->instructions
.emplace_back(std::move(load
));
3910 Operand sample_index4
;
3911 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3912 sample_index4
= Operand(sample_index
.constantValue() << 2);
3913 } else if (sample_index
.regClass() == s1
) {
3914 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3916 assert(sample_index
.regClass() == v1
);
3917 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3921 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3922 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3923 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3924 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3926 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3928 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3929 * resource descriptor is 0 (invalid),
3931 Temp compare
= bld
.tmp(s2
);
3932 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3933 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3935 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3937 /* Replace the MSAA sample index. */
3938 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3941 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3944 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3945 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3946 bool is_array
= glsl_sampler_type_is_array(type
);
3947 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3948 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3949 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3950 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3951 int count
= image_type_to_components_count(dim
, is_array
);
3952 std::vector
<Operand
> coords(count
);
3955 Operand sample_index
;
3956 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3958 sample_index
= Operand(sample_cv
->u32
);
3960 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3962 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3963 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3964 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3965 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3966 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3967 vec
->definitions
[0] = Definition(fmask_load_address
);
3968 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3970 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3971 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3974 coords
[count
] = sample_index
;
3977 if (count
== 1 && !gfx9_1d
)
3978 return emit_extract_vector(ctx
, src0
, 0, v1
);
3981 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3982 coords
.resize(coords
.size() + 1);
3983 coords
[1] = Operand((uint32_t) 0);
3985 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3987 for (int i
= 0; i
< count
; i
++)
3988 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3991 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3992 for (unsigned i
= 0; i
< coords
.size(); i
++)
3993 vec
->operands
[i
] = coords
[i
];
3994 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3995 vec
->definitions
[0] = Definition(res
);
3996 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4001 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4003 Builder
bld(ctx
->program
, ctx
->block
);
4004 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4005 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4006 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4007 bool is_array
= glsl_sampler_type_is_array(type
);
4008 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4010 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4011 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4012 unsigned num_channels
= util_last_bit(mask
);
4013 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4014 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4017 switch (num_channels
) {
4019 opcode
= aco_opcode::buffer_load_format_x
;
4022 opcode
= aco_opcode::buffer_load_format_xy
;
4025 opcode
= aco_opcode::buffer_load_format_xyz
;
4028 opcode
= aco_opcode::buffer_load_format_xyzw
;
4031 unreachable(">4 channel buffer image load");
4033 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
4034 load
->operands
[0] = Operand(vindex
);
4035 load
->operands
[1] = Operand(rsrc
);
4036 load
->operands
[2] = Operand((uint32_t) 0);
4038 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4041 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
4042 load
->definitions
[0] = Definition(tmp
);
4044 load
->barrier
= barrier_image
;
4045 ctx
->block
->instructions
.emplace_back(std::move(load
));
4047 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
4051 Temp coords
= get_image_coords(ctx
, instr
, type
);
4052 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4054 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
4055 unsigned num_components
= util_bitcount(dmask
);
4057 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
4060 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
4062 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
4063 load
->operands
[0] = Operand(coords
);
4064 load
->operands
[1] = Operand(resource
);
4065 load
->definitions
[0] = Definition(tmp
);
4066 load
->glc
= var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
4067 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4068 load
->dmask
= dmask
;
4070 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4071 load
->barrier
= barrier_image
;
4072 ctx
->block
->instructions
.emplace_back(std::move(load
));
4074 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
4078 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4080 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4081 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4082 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4083 bool is_array
= glsl_sampler_type_is_array(type
);
4084 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4086 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
4088 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4089 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4090 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4092 switch (data
.size()) {
4094 opcode
= aco_opcode::buffer_store_format_x
;
4097 opcode
= aco_opcode::buffer_store_format_xy
;
4100 opcode
= aco_opcode::buffer_store_format_xyz
;
4103 opcode
= aco_opcode::buffer_store_format_xyzw
;
4106 unreachable(">4 channel buffer image store");
4108 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
4109 store
->operands
[0] = Operand(vindex
);
4110 store
->operands
[1] = Operand(rsrc
);
4111 store
->operands
[2] = Operand((uint32_t) 0);
4112 store
->operands
[3] = Operand(data
);
4113 store
->idxen
= true;
4116 store
->disable_wqm
= true;
4117 store
->barrier
= barrier_image
;
4118 ctx
->program
->needs_exact
= true;
4119 ctx
->block
->instructions
.emplace_back(std::move(store
));
4123 assert(data
.type() == RegType::vgpr
);
4124 Temp coords
= get_image_coords(ctx
, instr
, type
);
4125 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4127 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
4128 store
->operands
[0] = Operand(coords
);
4129 store
->operands
[1] = Operand(resource
);
4130 store
->operands
[2] = Operand(s4
);
4131 store
->operands
[3] = Operand(data
);
4134 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4135 store
->dmask
= (1 << data
.size()) - 1;
4137 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4138 store
->disable_wqm
= true;
4139 store
->barrier
= barrier_image
;
4140 ctx
->program
->needs_exact
= true;
4141 ctx
->block
->instructions
.emplace_back(std::move(store
));
4145 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4147 /* return the previous value if dest is ever used */
4148 bool return_previous
= false;
4149 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4150 return_previous
= true;
4153 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4154 return_previous
= true;
4158 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4159 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4160 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4161 bool is_array
= glsl_sampler_type_is_array(type
);
4162 Builder
bld(ctx
->program
, ctx
->block
);
4164 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
4165 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
4167 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
4168 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
4170 aco_opcode buf_op
, image_op
;
4171 switch (instr
->intrinsic
) {
4172 case nir_intrinsic_image_deref_atomic_add
:
4173 buf_op
= aco_opcode::buffer_atomic_add
;
4174 image_op
= aco_opcode::image_atomic_add
;
4176 case nir_intrinsic_image_deref_atomic_umin
:
4177 buf_op
= aco_opcode::buffer_atomic_umin
;
4178 image_op
= aco_opcode::image_atomic_umin
;
4180 case nir_intrinsic_image_deref_atomic_imin
:
4181 buf_op
= aco_opcode::buffer_atomic_smin
;
4182 image_op
= aco_opcode::image_atomic_smin
;
4184 case nir_intrinsic_image_deref_atomic_umax
:
4185 buf_op
= aco_opcode::buffer_atomic_umax
;
4186 image_op
= aco_opcode::image_atomic_umax
;
4188 case nir_intrinsic_image_deref_atomic_imax
:
4189 buf_op
= aco_opcode::buffer_atomic_smax
;
4190 image_op
= aco_opcode::image_atomic_smax
;
4192 case nir_intrinsic_image_deref_atomic_and
:
4193 buf_op
= aco_opcode::buffer_atomic_and
;
4194 image_op
= aco_opcode::image_atomic_and
;
4196 case nir_intrinsic_image_deref_atomic_or
:
4197 buf_op
= aco_opcode::buffer_atomic_or
;
4198 image_op
= aco_opcode::image_atomic_or
;
4200 case nir_intrinsic_image_deref_atomic_xor
:
4201 buf_op
= aco_opcode::buffer_atomic_xor
;
4202 image_op
= aco_opcode::image_atomic_xor
;
4204 case nir_intrinsic_image_deref_atomic_exchange
:
4205 buf_op
= aco_opcode::buffer_atomic_swap
;
4206 image_op
= aco_opcode::image_atomic_swap
;
4208 case nir_intrinsic_image_deref_atomic_comp_swap
:
4209 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
4210 image_op
= aco_opcode::image_atomic_cmpswap
;
4213 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
4216 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4218 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
4219 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
4220 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
4221 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
4222 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4223 mubuf
->operands
[0] = Operand(vindex
);
4224 mubuf
->operands
[1] = Operand(resource
);
4225 mubuf
->operands
[2] = Operand((uint32_t)0);
4226 mubuf
->operands
[3] = Operand(data
);
4227 if (return_previous
)
4228 mubuf
->definitions
[0] = Definition(dst
);
4230 mubuf
->idxen
= true;
4231 mubuf
->glc
= return_previous
;
4232 mubuf
->dlc
= false; /* Not needed for atomics */
4233 mubuf
->disable_wqm
= true;
4234 mubuf
->barrier
= barrier_image
;
4235 ctx
->program
->needs_exact
= true;
4236 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4240 Temp coords
= get_image_coords(ctx
, instr
, type
);
4241 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
4242 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
4243 mimg
->operands
[0] = Operand(coords
);
4244 mimg
->operands
[1] = Operand(resource
);
4245 mimg
->operands
[2] = Operand(s4
); /* no sampler */
4246 mimg
->operands
[3] = Operand(data
);
4247 if (return_previous
)
4248 mimg
->definitions
[0] = Definition(dst
);
4249 mimg
->glc
= return_previous
;
4250 mimg
->dlc
= false; /* Not needed for atomics */
4251 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4252 mimg
->dmask
= (1 << data
.size()) - 1;
4254 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
4255 mimg
->disable_wqm
= true;
4256 mimg
->barrier
= barrier_image
;
4257 ctx
->program
->needs_exact
= true;
4258 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4262 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4264 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4265 Builder
bld(ctx
->program
, ctx
->block
);
4267 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4268 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4269 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4270 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4272 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4273 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4275 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4276 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4277 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4279 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4281 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4282 * in case 12 (or 3?), we have to divide by 3:
4283 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4284 * use v_mul_hi_u32 with magic number to divide
4285 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4287 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4291 emit_extract_vector(ctx
, desc
, 2, dst
);
4295 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4297 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4298 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4299 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
4300 bool is_array
= glsl_sampler_type_is_array(type
);
4301 Builder
bld(ctx
->program
, ctx
->block
);
4303 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4304 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4305 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4309 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4312 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4314 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4316 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4317 mimg
->operands
[0] = Operand(lod
);
4318 mimg
->operands
[1] = Operand(resource
);
4319 unsigned& dmask
= mimg
->dmask
;
4320 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
4321 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4322 mimg
->da
= glsl_sampler_type_is_array(type
);
4323 mimg
->can_reorder
= true;
4324 Definition
& def
= mimg
->definitions
[0];
4325 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4327 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4328 glsl_sampler_type_is_array(type
)) {
4330 assert(instr
->dest
.ssa
.num_components
== 3);
4331 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4332 def
= Definition(tmp
);
4333 emit_split_vector(ctx
, tmp
, 3);
4335 /* divide 3rd value by 6 by multiplying with magic number */
4336 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4337 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4339 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4340 emit_extract_vector(ctx
, tmp
, 0, v1
),
4341 emit_extract_vector(ctx
, tmp
, 1, v1
),
4344 } else if (ctx
->options
->chip_class
== GFX9
&&
4345 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4346 glsl_sampler_type_is_array(type
)) {
4347 assert(instr
->dest
.ssa
.num_components
== 2);
4348 def
= Definition(dst
);
4351 def
= Definition(dst
);
4354 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4357 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4359 Builder
bld(ctx
->program
, ctx
->block
);
4360 unsigned num_components
= instr
->num_components
;
4362 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4363 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4364 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4366 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4367 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
, false);
4370 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4372 Builder
bld(ctx
->program
, ctx
->block
);
4373 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4374 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4375 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4378 if (ctx
->options
->chip_class
< GFX8
)
4379 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4381 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4383 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4384 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4386 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4387 ctx
->options
->chip_class
>= GFX8
;
4389 offset
= bld
.as_uniform(offset
);
4390 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4394 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4395 if (count
== 3 && smem
) {
4396 writemask
|= 1u << (start
+ 2);
4399 int num_bytes
= count
* elem_size_bytes
;
4401 if (num_bytes
> 16) {
4402 assert(elem_size_bytes
== 8);
4403 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4408 // TODO: check alignment of sub-dword stores
4409 // TODO: split 3 bytes. there is no store instruction for that
4412 if (count
!= instr
->num_components
) {
4413 emit_split_vector(ctx
, data
, instr
->num_components
);
4414 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4415 for (int i
= 0; i
< count
; i
++) {
4416 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4417 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4419 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4420 vec
->definitions
[0] = Definition(write_data
);
4421 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4422 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4423 assert(num_bytes
% 4 == 0);
4424 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4425 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4426 assert(num_bytes
% 4 == 0);
4427 write_data
= bld
.as_uniform(data
);
4432 aco_opcode vmem_op
, smem_op
;
4433 switch (num_bytes
) {
4435 vmem_op
= aco_opcode::buffer_store_dword
;
4436 smem_op
= aco_opcode::s_buffer_store_dword
;
4439 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4440 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4443 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4444 smem_op
= aco_opcode::last_opcode
;
4448 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4449 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4452 unreachable("Store SSBO not implemented for this size.");
4454 if (ctx
->stage
== fragment_fs
)
4455 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4458 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4459 store
->operands
[0] = Operand(rsrc
);
4461 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4462 offset
, Operand(start
* elem_size_bytes
));
4463 store
->operands
[1] = Operand(off
);
4465 store
->operands
[1] = Operand(offset
);
4467 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4468 store
->operands
[1].setFixed(m0
);
4469 store
->operands
[2] = Operand(write_data
);
4470 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4472 store
->disable_wqm
= true;
4473 store
->barrier
= barrier_buffer
;
4474 ctx
->block
->instructions
.emplace_back(std::move(store
));
4475 ctx
->program
->wb_smem_l1_on_end
= true;
4476 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4477 ctx
->block
->kind
|= block_kind_needs_lowering
;
4478 ctx
->program
->needs_exact
= true;
4481 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4482 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4483 store
->operands
[1] = Operand(rsrc
);
4484 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4485 store
->operands
[3] = Operand(write_data
);
4486 store
->offset
= start
* elem_size_bytes
;
4487 store
->offen
= (offset
.type() == RegType::vgpr
);
4488 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4490 store
->disable_wqm
= true;
4491 store
->barrier
= barrier_buffer
;
4492 ctx
->program
->needs_exact
= true;
4493 ctx
->block
->instructions
.emplace_back(std::move(store
));
4498 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4500 /* return the previous value if dest is ever used */
4501 bool return_previous
= false;
4502 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4503 return_previous
= true;
4506 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4507 return_previous
= true;
4511 Builder
bld(ctx
->program
, ctx
->block
);
4512 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4514 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4515 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4516 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4519 if (ctx
->options
->chip_class
< GFX8
)
4520 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4522 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4524 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4525 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4527 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4529 aco_opcode op32
, op64
;
4530 switch (instr
->intrinsic
) {
4531 case nir_intrinsic_ssbo_atomic_add
:
4532 op32
= aco_opcode::buffer_atomic_add
;
4533 op64
= aco_opcode::buffer_atomic_add_x2
;
4535 case nir_intrinsic_ssbo_atomic_imin
:
4536 op32
= aco_opcode::buffer_atomic_smin
;
4537 op64
= aco_opcode::buffer_atomic_smin_x2
;
4539 case nir_intrinsic_ssbo_atomic_umin
:
4540 op32
= aco_opcode::buffer_atomic_umin
;
4541 op64
= aco_opcode::buffer_atomic_umin_x2
;
4543 case nir_intrinsic_ssbo_atomic_imax
:
4544 op32
= aco_opcode::buffer_atomic_smax
;
4545 op64
= aco_opcode::buffer_atomic_smax_x2
;
4547 case nir_intrinsic_ssbo_atomic_umax
:
4548 op32
= aco_opcode::buffer_atomic_umax
;
4549 op64
= aco_opcode::buffer_atomic_umax_x2
;
4551 case nir_intrinsic_ssbo_atomic_and
:
4552 op32
= aco_opcode::buffer_atomic_and
;
4553 op64
= aco_opcode::buffer_atomic_and_x2
;
4555 case nir_intrinsic_ssbo_atomic_or
:
4556 op32
= aco_opcode::buffer_atomic_or
;
4557 op64
= aco_opcode::buffer_atomic_or_x2
;
4559 case nir_intrinsic_ssbo_atomic_xor
:
4560 op32
= aco_opcode::buffer_atomic_xor
;
4561 op64
= aco_opcode::buffer_atomic_xor_x2
;
4563 case nir_intrinsic_ssbo_atomic_exchange
:
4564 op32
= aco_opcode::buffer_atomic_swap
;
4565 op64
= aco_opcode::buffer_atomic_swap_x2
;
4567 case nir_intrinsic_ssbo_atomic_comp_swap
:
4568 op32
= aco_opcode::buffer_atomic_cmpswap
;
4569 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4572 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4574 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4575 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4576 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4577 mubuf
->operands
[1] = Operand(rsrc
);
4578 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4579 mubuf
->operands
[3] = Operand(data
);
4580 if (return_previous
)
4581 mubuf
->definitions
[0] = Definition(dst
);
4583 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4584 mubuf
->glc
= return_previous
;
4585 mubuf
->dlc
= false; /* Not needed for atomics */
4586 mubuf
->disable_wqm
= true;
4587 mubuf
->barrier
= barrier_buffer
;
4588 ctx
->program
->needs_exact
= true;
4589 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4592 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4594 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4595 Builder
bld(ctx
->program
, ctx
->block
);
4596 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4597 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4600 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4602 Builder
bld(ctx
->program
, ctx
->block
);
4603 unsigned num_components
= instr
->num_components
;
4604 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4606 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4607 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4609 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4610 bool dlc
= glc
&& ctx
->options
->chip_class
>= GFX10
;
4612 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4613 bool global
= ctx
->options
->chip_class
>= GFX9
;
4615 switch (num_bytes
) {
4617 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4620 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4623 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4626 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4629 unreachable("load_global not implemented for this size.");
4631 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4632 flat
->operands
[0] = Operand(addr
);
4633 flat
->operands
[1] = Operand(s1
);
4637 if (dst
.type() == RegType::sgpr
) {
4638 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4639 flat
->definitions
[0] = Definition(vec
);
4640 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4641 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4643 flat
->definitions
[0] = Definition(dst
);
4644 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4646 emit_split_vector(ctx
, dst
, num_components
);
4648 switch (num_bytes
) {
4650 op
= aco_opcode::s_load_dword
;
4653 op
= aco_opcode::s_load_dwordx2
;
4657 op
= aco_opcode::s_load_dwordx4
;
4660 unreachable("load_global not implemented for this size.");
4662 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4663 load
->operands
[0] = Operand(addr
);
4664 load
->operands
[1] = Operand(0u);
4665 load
->definitions
[0] = Definition(dst
);
4668 load
->barrier
= barrier_buffer
;
4669 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4671 if (dst
.size() == 3) {
4673 Temp vec
= bld
.tmp(s4
);
4674 load
->definitions
[0] = Definition(vec
);
4675 ctx
->block
->instructions
.emplace_back(std::move(load
));
4676 emit_split_vector(ctx
, vec
, 4);
4678 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4679 emit_extract_vector(ctx
, vec
, 0, s1
),
4680 emit_extract_vector(ctx
, vec
, 1, s1
),
4681 emit_extract_vector(ctx
, vec
, 2, s1
));
4683 ctx
->block
->instructions
.emplace_back(std::move(load
));
4688 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4690 Builder
bld(ctx
->program
, ctx
->block
);
4691 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4693 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4694 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4696 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4699 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4700 unsigned num_bytes
= count
* elem_size_bytes
;
4702 Temp write_data
= data
;
4703 if (count
!= instr
->num_components
) {
4704 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4705 for (int i
= 0; i
< count
; i
++)
4706 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4707 write_data
= bld
.tmp(RegType::vgpr
, count
);
4708 vec
->definitions
[0] = Definition(write_data
);
4709 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4712 unsigned offset
= start
* elem_size_bytes
;
4713 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4714 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4715 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4716 Temp carry
= bld
.tmp(s2
);
4717 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4719 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4720 Operand(offset
), addr0
);
4721 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4723 carry
).def(1).setHint(vcc
);
4725 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4730 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4731 bool global
= ctx
->options
->chip_class
>= GFX9
;
4733 switch (num_bytes
) {
4735 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4738 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4741 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4744 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4747 unreachable("store_global not implemented for this size.");
4749 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4750 flat
->operands
[0] = Operand(addr
);
4751 flat
->operands
[1] = Operand(s1
);
4752 flat
->operands
[2] = Operand(data
);
4755 flat
->offset
= offset
;
4756 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4760 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4761 Builder
bld(ctx
->program
, ctx
->block
);
4762 switch(instr
->intrinsic
) {
4763 case nir_intrinsic_group_memory_barrier
:
4764 case nir_intrinsic_memory_barrier
:
4765 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4767 case nir_intrinsic_memory_barrier_atomic_counter
:
4768 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4770 case nir_intrinsic_memory_barrier_buffer
:
4771 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4773 case nir_intrinsic_memory_barrier_image
:
4774 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4776 case nir_intrinsic_memory_barrier_shared
:
4777 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4780 unreachable("Unimplemented memory barrier intrinsic");
4785 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4787 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4788 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4789 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4790 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4791 Builder
bld(ctx
->program
, ctx
->block
);
4793 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4794 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4795 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
4798 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4800 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4801 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4802 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4803 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4804 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4806 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4807 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
4810 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4812 unsigned offset
= nir_intrinsic_base(instr
);
4813 Operand m
= load_lds_size_m0(ctx
);
4814 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4815 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4817 unsigned num_operands
= 3;
4818 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4819 switch(instr
->intrinsic
) {
4820 case nir_intrinsic_shared_atomic_add
:
4821 op32
= aco_opcode::ds_add_u32
;
4822 op64
= aco_opcode::ds_add_u64
;
4823 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4824 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4826 case nir_intrinsic_shared_atomic_imin
:
4827 op32
= aco_opcode::ds_min_i32
;
4828 op64
= aco_opcode::ds_min_i64
;
4829 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4830 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4832 case nir_intrinsic_shared_atomic_umin
:
4833 op32
= aco_opcode::ds_min_u32
;
4834 op64
= aco_opcode::ds_min_u64
;
4835 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4836 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4838 case nir_intrinsic_shared_atomic_imax
:
4839 op32
= aco_opcode::ds_max_i32
;
4840 op64
= aco_opcode::ds_max_i64
;
4841 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4842 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4844 case nir_intrinsic_shared_atomic_umax
:
4845 op32
= aco_opcode::ds_max_u32
;
4846 op64
= aco_opcode::ds_max_u64
;
4847 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4848 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4850 case nir_intrinsic_shared_atomic_and
:
4851 op32
= aco_opcode::ds_and_b32
;
4852 op64
= aco_opcode::ds_and_b64
;
4853 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4854 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4856 case nir_intrinsic_shared_atomic_or
:
4857 op32
= aco_opcode::ds_or_b32
;
4858 op64
= aco_opcode::ds_or_b64
;
4859 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4860 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4862 case nir_intrinsic_shared_atomic_xor
:
4863 op32
= aco_opcode::ds_xor_b32
;
4864 op64
= aco_opcode::ds_xor_b64
;
4865 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4866 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4868 case nir_intrinsic_shared_atomic_exchange
:
4869 op32
= aco_opcode::ds_write_b32
;
4870 op64
= aco_opcode::ds_write_b64
;
4871 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4872 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4874 case nir_intrinsic_shared_atomic_comp_swap
:
4875 op32
= aco_opcode::ds_cmpst_b32
;
4876 op64
= aco_opcode::ds_cmpst_b64
;
4877 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4878 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4882 unreachable("Unhandled shared atomic intrinsic");
4885 /* return the previous value if dest is ever used */
4886 bool return_previous
= false;
4887 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4888 return_previous
= true;
4891 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4892 return_previous
= true;
4897 if (data
.size() == 1) {
4898 assert(instr
->dest
.ssa
.bit_size
== 32);
4899 op
= return_previous
? op32_rtn
: op32
;
4901 assert(instr
->dest
.ssa
.bit_size
== 64);
4902 op
= return_previous
? op64_rtn
: op64
;
4905 if (offset
> 65535) {
4906 Builder
bld(ctx
->program
, ctx
->block
);
4907 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4911 aco_ptr
<DS_instruction
> ds
;
4912 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4913 ds
->operands
[0] = Operand(address
);
4914 ds
->operands
[1] = Operand(data
);
4915 if (num_operands
== 4)
4916 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4917 ds
->operands
[num_operands
- 1] = m
;
4918 ds
->offset0
= offset
;
4919 if (return_previous
)
4920 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4921 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4924 Temp
get_scratch_resource(isel_context
*ctx
)
4926 Builder
bld(ctx
->program
, ctx
->block
);
4927 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
4928 if (ctx
->stage
!= compute_cs
)
4929 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
4931 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
4932 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
4934 if (ctx
->program
->chip_class
>= GFX10
) {
4935 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
4936 S_008F0C_OOB_SELECT(3) |
4937 S_008F0C_RESOURCE_LEVEL(1);
4938 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
4939 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4940 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
4943 /* older generations need element size = 16 bytes. element size removed in GFX9 */
4944 if (ctx
->program
->chip_class
<= GFX8
)
4945 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
4947 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4950 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4951 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4952 Builder
bld(ctx
->program
, ctx
->block
);
4953 Temp rsrc
= get_scratch_resource(ctx
);
4954 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4955 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4958 switch (dst
.size()) {
4960 op
= aco_opcode::buffer_load_dword
;
4963 op
= aco_opcode::buffer_load_dwordx2
;
4966 op
= aco_opcode::buffer_load_dwordx3
;
4969 op
= aco_opcode::buffer_load_dwordx4
;
4973 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4974 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4975 bld
.def(v4
), offset
, rsrc
,
4976 ctx
->program
->scratch_offset
, 0, true);
4977 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4978 aco_opcode::buffer_load_dwordx4
,
4979 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4980 offset
, rsrc
, ctx
->program
->scratch_offset
, 16, true);
4981 emit_split_vector(ctx
, lower
, 2);
4982 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4983 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4984 if (dst
.size() == 8) {
4985 emit_split_vector(ctx
, upper
, 2);
4986 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4987 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4992 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4993 Format::PSEUDO
, dst
.size() / 2, 1)};
4994 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4995 vec
->operands
[i
] = Operand(elems
[i
]);
4996 vec
->definitions
[0] = Definition(dst
);
4997 bld
.insert(std::move(vec
));
4998 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5002 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
5005 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->program
->scratch_offset
, 0, true);
5006 emit_split_vector(ctx
, dst
, instr
->num_components
);
5009 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5010 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
5011 Builder
bld(ctx
->program
, ctx
->block
);
5012 Temp rsrc
= get_scratch_resource(ctx
);
5013 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5014 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5016 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
5017 unsigned writemask
= nir_intrinsic_write_mask(instr
);
5021 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
5022 int num_bytes
= count
* elem_size_bytes
;
5024 if (num_bytes
> 16) {
5025 assert(elem_size_bytes
== 8);
5026 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
5031 // TODO: check alignment of sub-dword stores
5032 // TODO: split 3 bytes. there is no store instruction for that
5035 if (count
!= instr
->num_components
) {
5036 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5037 for (int i
= 0; i
< count
; i
++) {
5038 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
5039 vec
->operands
[i
] = Operand(elem
);
5041 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
5042 vec
->definitions
[0] = Definition(write_data
);
5043 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5049 switch (num_bytes
) {
5051 op
= aco_opcode::buffer_store_dword
;
5054 op
= aco_opcode::buffer_store_dwordx2
;
5057 op
= aco_opcode::buffer_store_dwordx3
;
5060 op
= aco_opcode::buffer_store_dwordx4
;
5063 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
5066 bld
.mubuf(op
, offset
, rsrc
, ctx
->program
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
5070 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
5071 uint8_t log2_ps_iter_samples
;
5072 if (ctx
->program
->info
->ps
.force_persample
) {
5073 log2_ps_iter_samples
=
5074 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
5076 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
5079 /* The bit pattern matches that used by fixed function fragment
5081 static const unsigned ps_iter_masks
[] = {
5082 0xffff, /* not used */
5088 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
5090 Builder
bld(ctx
->program
, ctx
->block
);
5092 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::ancillary
], Operand(8u), Operand(4u));
5093 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
5094 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
5095 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5096 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, ctx
->fs_inputs
[fs_input::sample_coverage
]);
5099 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
5101 Builder
bld(ctx
->program
, ctx
->block
);
5103 if (cluster_size
== 1) {
5105 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5106 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5107 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5108 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5109 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
5110 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5111 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5112 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5113 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
5114 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
5115 //subgroupAnd(val) -> (exec & ~val) == 0
5116 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5117 return bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), tmp
, Operand(0u));
5118 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5119 //subgroupOr(val) -> (val & exec) != 0
5120 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5121 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5122 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5123 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5124 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5125 return bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5127 //subgroupClustered{And,Or,Xor}(val, n) ->
5128 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5129 //cluster_offset = ~(n - 1) & lane_id
5130 //cluster_mask = ((1 << n) - 1)
5131 //subgroupClusteredAnd():
5132 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5133 //subgroupClusteredOr():
5134 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5135 //subgroupClusteredXor():
5136 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5137 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5138 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5139 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5142 if (op
== nir_op_iand
)
5143 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5145 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5147 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5148 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5149 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5150 if (cluster_mask
!= 0xffffffff)
5151 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5153 Definition cmp_def
= Definition();
5154 if (op
== nir_op_iand
) {
5155 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5156 } else if (op
== nir_op_ior
) {
5157 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5158 } else if (op
== nir_op_ixor
) {
5159 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5160 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5161 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5163 cmp_def
.setHint(vcc
);
5164 return cmp_def
.getTemp();
5168 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5170 Builder
bld(ctx
->program
, ctx
->block
);
5172 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5173 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5174 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5176 if (op
== nir_op_iand
)
5177 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5179 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5181 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5182 Temp lo
= lohi
.def(0).getTemp();
5183 Temp hi
= lohi
.def(1).getTemp();
5184 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5185 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5187 Definition cmp_def
= Definition();
5188 if (op
== nir_op_iand
)
5189 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5190 else if (op
== nir_op_ior
)
5191 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5192 else if (op
== nir_op_ixor
)
5193 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5194 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5195 cmp_def
.setHint(vcc
);
5196 return cmp_def
.getTemp();
5199 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5201 Builder
bld(ctx
->program
, ctx
->block
);
5203 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5204 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5205 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5206 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5207 if (op
== nir_op_iand
)
5208 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5209 else if (op
== nir_op_ior
)
5210 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5211 else if (op
== nir_op_ixor
)
5212 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5218 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5220 Builder
bld(ctx
->program
, ctx
->block
);
5221 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5222 if (src
.regClass().type() == RegType::vgpr
) {
5223 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5224 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5225 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(dst
), Operand(0u), Operand(src
));
5226 } else if (src
.regClass() == s1
) {
5227 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5228 } else if (src
.regClass() == s2
) {
5229 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5231 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5232 nir_print_instr(&instr
->instr
, stderr
);
5233 fprintf(stderr
, "\n");
5237 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5239 Builder
bld(ctx
->program
, ctx
->block
);
5240 Temp p1
= ctx
->fs_inputs
[fs_input::persp_center_p1
];
5241 Temp p2
= ctx
->fs_inputs
[fs_input::persp_center_p2
];
5244 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5245 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5246 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5247 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5248 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5249 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5251 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5252 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5253 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5254 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5255 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5256 Temp wqm1
= bld
.tmp(v1
);
5257 emit_wqm(ctx
, tmp1
, wqm1
, true);
5258 Temp wqm2
= bld
.tmp(v1
);
5259 emit_wqm(ctx
, tmp2
, wqm2
, true);
5260 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5264 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5266 Builder
bld(ctx
->program
, ctx
->block
);
5267 switch(instr
->intrinsic
) {
5268 case nir_intrinsic_load_barycentric_sample
:
5269 case nir_intrinsic_load_barycentric_pixel
:
5270 case nir_intrinsic_load_barycentric_centroid
: {
5271 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5272 fs_input input
= get_interp_input(instr
->intrinsic
, mode
);
5274 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5275 if (input
== fs_input::max_inputs
) {
5276 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5277 Operand(0u), Operand(0u));
5279 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5280 ctx
->fs_inputs
[input
],
5281 ctx
->fs_inputs
[input
+ 1]);
5283 emit_split_vector(ctx
, dst
, 2);
5286 case nir_intrinsic_load_barycentric_at_sample
: {
5287 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5288 switch (ctx
->options
->key
.fs
.num_samples
) {
5289 case 2: sample_pos_offset
+= 1 << 3; break;
5290 case 4: sample_pos_offset
+= 3 << 3; break;
5291 case 8: sample_pos_offset
+= 7 << 3; break;
5295 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5296 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5297 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
5298 if (addr
.type() == RegType::sgpr
) {
5301 sample_pos_offset
+= const_addr
->u32
<< 3;
5302 offset
= Operand(sample_pos_offset
);
5303 } else if (ctx
->options
->chip_class
>= GFX9
) {
5304 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5306 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5307 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5309 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, Operand(offset
));
5311 } else if (ctx
->options
->chip_class
>= GFX9
) {
5312 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5313 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
5315 /* addr += private_segment_buffer + sample_pos_offset */
5316 Temp tmp0
= bld
.tmp(s1
);
5317 Temp tmp1
= bld
.tmp(s1
);
5318 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
5319 Definition scc_tmp
= bld
.def(s1
, scc
);
5320 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5321 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
5322 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5323 Temp pck0
= bld
.tmp(v1
);
5324 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5325 tmp1
= as_vgpr(ctx
, tmp1
);
5326 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5327 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5329 /* sample_pos = flat_load_dwordx2 addr */
5330 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5333 /* sample_pos -= 0.5 */
5334 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5335 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5336 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5337 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5338 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5340 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5343 case nir_intrinsic_load_barycentric_at_offset
: {
5344 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5345 RegClass rc
= RegClass(offset
.type(), 1);
5346 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5347 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5348 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5351 case nir_intrinsic_load_front_face
: {
5352 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5353 Operand(0u), ctx
->fs_inputs
[fs_input::front_face
]).def(0).setHint(vcc
);
5356 case nir_intrinsic_load_view_index
:
5357 case nir_intrinsic_load_layer_id
: {
5358 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5359 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5360 bld
.copy(Definition(dst
), Operand(ctx
->view_index
));
5364 unsigned idx
= nir_intrinsic_base(instr
);
5365 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5366 Operand(2u), bld
.m0(ctx
->prim_mask
), idx
, 0);
5369 case nir_intrinsic_load_frag_coord
: {
5370 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5373 case nir_intrinsic_load_sample_pos
: {
5374 Temp posx
= ctx
->fs_inputs
[fs_input::frag_pos_0
];
5375 Temp posy
= ctx
->fs_inputs
[fs_input::frag_pos_1
];
5376 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5377 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5378 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5381 case nir_intrinsic_load_interpolated_input
:
5382 visit_load_interpolated_input(ctx
, instr
);
5384 case nir_intrinsic_store_output
:
5385 visit_store_output(ctx
, instr
);
5387 case nir_intrinsic_load_input
:
5388 visit_load_input(ctx
, instr
);
5390 case nir_intrinsic_load_ubo
:
5391 visit_load_ubo(ctx
, instr
);
5393 case nir_intrinsic_load_push_constant
:
5394 visit_load_push_constant(ctx
, instr
);
5396 case nir_intrinsic_load_constant
:
5397 visit_load_constant(ctx
, instr
);
5399 case nir_intrinsic_vulkan_resource_index
:
5400 visit_load_resource(ctx
, instr
);
5402 case nir_intrinsic_discard
:
5403 visit_discard(ctx
, instr
);
5405 case nir_intrinsic_discard_if
:
5406 visit_discard_if(ctx
, instr
);
5408 case nir_intrinsic_load_shared
:
5409 visit_load_shared(ctx
, instr
);
5411 case nir_intrinsic_store_shared
:
5412 visit_store_shared(ctx
, instr
);
5414 case nir_intrinsic_shared_atomic_add
:
5415 case nir_intrinsic_shared_atomic_imin
:
5416 case nir_intrinsic_shared_atomic_umin
:
5417 case nir_intrinsic_shared_atomic_imax
:
5418 case nir_intrinsic_shared_atomic_umax
:
5419 case nir_intrinsic_shared_atomic_and
:
5420 case nir_intrinsic_shared_atomic_or
:
5421 case nir_intrinsic_shared_atomic_xor
:
5422 case nir_intrinsic_shared_atomic_exchange
:
5423 case nir_intrinsic_shared_atomic_comp_swap
:
5424 visit_shared_atomic(ctx
, instr
);
5426 case nir_intrinsic_image_deref_load
:
5427 visit_image_load(ctx
, instr
);
5429 case nir_intrinsic_image_deref_store
:
5430 visit_image_store(ctx
, instr
);
5432 case nir_intrinsic_image_deref_atomic_add
:
5433 case nir_intrinsic_image_deref_atomic_umin
:
5434 case nir_intrinsic_image_deref_atomic_imin
:
5435 case nir_intrinsic_image_deref_atomic_umax
:
5436 case nir_intrinsic_image_deref_atomic_imax
:
5437 case nir_intrinsic_image_deref_atomic_and
:
5438 case nir_intrinsic_image_deref_atomic_or
:
5439 case nir_intrinsic_image_deref_atomic_xor
:
5440 case nir_intrinsic_image_deref_atomic_exchange
:
5441 case nir_intrinsic_image_deref_atomic_comp_swap
:
5442 visit_image_atomic(ctx
, instr
);
5444 case nir_intrinsic_image_deref_size
:
5445 visit_image_size(ctx
, instr
);
5447 case nir_intrinsic_load_ssbo
:
5448 visit_load_ssbo(ctx
, instr
);
5450 case nir_intrinsic_store_ssbo
:
5451 visit_store_ssbo(ctx
, instr
);
5453 case nir_intrinsic_load_global
:
5454 visit_load_global(ctx
, instr
);
5456 case nir_intrinsic_store_global
:
5457 visit_store_global(ctx
, instr
);
5459 case nir_intrinsic_ssbo_atomic_add
:
5460 case nir_intrinsic_ssbo_atomic_imin
:
5461 case nir_intrinsic_ssbo_atomic_umin
:
5462 case nir_intrinsic_ssbo_atomic_imax
:
5463 case nir_intrinsic_ssbo_atomic_umax
:
5464 case nir_intrinsic_ssbo_atomic_and
:
5465 case nir_intrinsic_ssbo_atomic_or
:
5466 case nir_intrinsic_ssbo_atomic_xor
:
5467 case nir_intrinsic_ssbo_atomic_exchange
:
5468 case nir_intrinsic_ssbo_atomic_comp_swap
:
5469 visit_atomic_ssbo(ctx
, instr
);
5471 case nir_intrinsic_load_scratch
:
5472 visit_load_scratch(ctx
, instr
);
5474 case nir_intrinsic_store_scratch
:
5475 visit_store_scratch(ctx
, instr
);
5477 case nir_intrinsic_get_buffer_size
:
5478 visit_get_buffer_size(ctx
, instr
);
5480 case nir_intrinsic_barrier
: {
5481 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5482 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5483 if (workgroup_size
> 64)
5484 bld
.sopp(aco_opcode::s_barrier
);
5487 case nir_intrinsic_group_memory_barrier
:
5488 case nir_intrinsic_memory_barrier
:
5489 case nir_intrinsic_memory_barrier_atomic_counter
:
5490 case nir_intrinsic_memory_barrier_buffer
:
5491 case nir_intrinsic_memory_barrier_image
:
5492 case nir_intrinsic_memory_barrier_shared
:
5493 emit_memory_barrier(ctx
, instr
);
5495 case nir_intrinsic_load_num_work_groups
:
5496 case nir_intrinsic_load_work_group_id
:
5497 case nir_intrinsic_load_local_invocation_id
: {
5498 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5500 if (instr
->intrinsic
== nir_intrinsic_load_num_work_groups
)
5501 ids
= ctx
->num_workgroups
;
5502 else if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
5503 ids
= ctx
->workgroup_ids
;
5505 ids
= ctx
->local_invocation_ids
;
5506 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5507 ids
[0].id() ? Operand(ids
[0]) : Operand(1u),
5508 ids
[1].id() ? Operand(ids
[1]) : Operand(1u),
5509 ids
[2].id() ? Operand(ids
[2]) : Operand(1u));
5510 emit_split_vector(ctx
, dst
, 3);
5513 case nir_intrinsic_load_local_invocation_index
: {
5514 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5515 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5516 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5517 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5520 case nir_intrinsic_load_subgroup_id
: {
5521 if (ctx
->stage
== compute_cs
) {
5522 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5523 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5525 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5529 case nir_intrinsic_load_subgroup_invocation
: {
5530 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5531 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5534 case nir_intrinsic_load_num_subgroups
: {
5535 if (ctx
->stage
== compute_cs
)
5536 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
), ctx
->tg_size
);
5538 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5541 case nir_intrinsic_ballot
: {
5542 Definition tmp
= bld
.def(s2
);
5543 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5544 if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s2
) {
5545 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5546 } else if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s1
) {
5547 bld
.sop2(aco_opcode::s_cselect_b64
, tmp
, Operand(exec
, s2
), Operand(0u), bld
.scc(src
));
5548 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5549 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5550 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5551 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5553 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5554 nir_print_instr(&instr
->instr
, stderr
);
5555 fprintf(stderr
, "\n");
5557 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5560 case nir_intrinsic_shuffle
:
5561 case nir_intrinsic_read_invocation
: {
5562 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5563 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
]) {
5564 emit_uniform_subgroup(ctx
, instr
, src
);
5566 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5567 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !ctx
->divergent_vals
[instr
->src
[1].ssa
->index
])
5568 tid
= bld
.as_uniform(tid
);
5569 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5570 if (src
.regClass() == v1
) {
5571 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
5572 } else if (src
.regClass() == v2
) {
5573 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5574 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5575 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
5576 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
5577 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5578 emit_split_vector(ctx
, dst
, 2);
5579 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
&& tid
.regClass() == s1
) {
5580 emit_wqm(ctx
, bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, tid
), dst
);
5581 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5582 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5583 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5584 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5585 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5587 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5588 nir_print_instr(&instr
->instr
, stderr
);
5589 fprintf(stderr
, "\n");
5594 case nir_intrinsic_load_sample_id
: {
5595 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5596 ctx
->fs_inputs
[ancillary
], Operand(8u), Operand(4u));
5599 case nir_intrinsic_load_sample_mask_in
: {
5600 visit_load_sample_mask_in(ctx
, instr
);
5603 case nir_intrinsic_read_first_invocation
: {
5604 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5605 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5606 if (src
.regClass() == v1
) {
5608 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5610 } else if (src
.regClass() == v2
) {
5611 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5612 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5613 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5614 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5615 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5616 emit_split_vector(ctx
, dst
, 2);
5617 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5619 bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5620 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
))),
5622 } else if (src
.regClass() == s1
) {
5623 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5624 } else if (src
.regClass() == s2
) {
5625 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5627 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5628 nir_print_instr(&instr
->instr
, stderr
);
5629 fprintf(stderr
, "\n");
5633 case nir_intrinsic_vote_all
: {
5634 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5635 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5636 assert(src
.regClass() == s2
);
5637 assert(dst
.regClass() == s1
);
5639 Definition tmp
= bld
.def(s1
);
5640 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(tmp
),
5641 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)),
5643 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5646 case nir_intrinsic_vote_any
: {
5647 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5648 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5649 assert(src
.regClass() == s2
);
5650 assert(dst
.regClass() == s1
);
5652 Definition tmp
= bld
.def(s1
);
5653 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(tmp
), src
, Operand(exec
, s2
));
5654 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5657 case nir_intrinsic_reduce
:
5658 case nir_intrinsic_inclusive_scan
:
5659 case nir_intrinsic_exclusive_scan
: {
5660 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5661 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5662 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5663 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5664 nir_intrinsic_cluster_size(instr
) : 0;
5665 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5667 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5668 emit_uniform_subgroup(ctx
, instr
, src
);
5669 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5670 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5672 else if (op
== nir_op_iadd
)
5674 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5676 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5678 switch (instr
->intrinsic
) {
5679 case nir_intrinsic_reduce
:
5680 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5682 case nir_intrinsic_exclusive_scan
:
5683 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5685 case nir_intrinsic_inclusive_scan
:
5686 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5691 } else if (cluster_size
== 1) {
5692 bld
.copy(Definition(dst
), src
);
5694 src
= as_vgpr(ctx
, src
);
5698 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5713 unreachable("unknown reduction op");
5718 switch (instr
->intrinsic
) {
5719 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5720 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5721 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5723 unreachable("unknown reduce intrinsic");
5726 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5727 reduce
->operands
[0] = Operand(src
);
5728 // filled in by aco_reduce_assign.cpp, used internally as part of the
5730 assert(dst
.size() == 1 || dst
.size() == 2);
5731 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5732 reduce
->operands
[2] = Operand(v1
.as_linear());
5734 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5735 reduce
->definitions
[0] = Definition(tmp_dst
);
5736 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5737 reduce
->definitions
[2] = Definition();
5738 reduce
->definitions
[3] = Definition(scc
, s1
);
5739 reduce
->definitions
[4] = Definition();
5740 reduce
->reduce_op
= reduce_op
;
5741 reduce
->cluster_size
= cluster_size
;
5742 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5744 emit_wqm(ctx
, tmp_dst
, dst
);
5748 case nir_intrinsic_quad_broadcast
: {
5749 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5750 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5751 emit_uniform_subgroup(ctx
, instr
, src
);
5753 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5754 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5755 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5756 uint32_t half_mask
= 0x11111111u
<< lane
;
5757 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5758 Temp tmp
= bld
.tmp(s2
);
5759 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5760 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5761 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5762 emit_wqm(ctx
, tmp
, dst
);
5763 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5765 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5766 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5768 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5769 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5770 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5771 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5772 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5773 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5774 emit_split_vector(ctx
, dst
, 2);
5776 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5777 nir_print_instr(&instr
->instr
, stderr
);
5778 fprintf(stderr
, "\n");
5783 case nir_intrinsic_quad_swap_horizontal
:
5784 case nir_intrinsic_quad_swap_vertical
:
5785 case nir_intrinsic_quad_swap_diagonal
:
5786 case nir_intrinsic_quad_swizzle_amd
: {
5787 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5788 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5789 emit_uniform_subgroup(ctx
, instr
, src
);
5792 uint16_t dpp_ctrl
= 0;
5793 switch (instr
->intrinsic
) {
5794 case nir_intrinsic_quad_swap_horizontal
:
5795 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5797 case nir_intrinsic_quad_swap_vertical
:
5798 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5800 case nir_intrinsic_quad_swap_diagonal
:
5801 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5803 case nir_intrinsic_quad_swizzle_amd
: {
5804 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5811 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5812 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5813 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5814 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5815 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5816 emit_wqm(ctx
, tmp
, dst
);
5817 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5818 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5819 emit_wqm(ctx
, tmp
, dst
);
5820 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5821 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5822 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5823 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5824 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5825 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5826 emit_split_vector(ctx
, dst
, 2);
5828 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5829 nir_print_instr(&instr
->instr
, stderr
);
5830 fprintf(stderr
, "\n");
5834 case nir_intrinsic_masked_swizzle_amd
: {
5835 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5836 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5837 emit_uniform_subgroup(ctx
, instr
, src
);
5840 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5841 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5842 if (dst
.regClass() == v1
) {
5844 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5846 } else if (dst
.regClass() == v2
) {
5847 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5848 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5849 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5850 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5851 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5852 emit_split_vector(ctx
, dst
, 2);
5854 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5855 nir_print_instr(&instr
->instr
, stderr
);
5856 fprintf(stderr
, "\n");
5860 case nir_intrinsic_write_invocation_amd
: {
5861 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5862 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5863 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5864 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5865 if (dst
.regClass() == v1
) {
5866 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5867 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5868 } else if (dst
.regClass() == v2
) {
5869 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5870 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5871 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5872 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5873 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5874 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5875 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5876 emit_split_vector(ctx
, dst
, 2);
5878 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5879 nir_print_instr(&instr
->instr
, stderr
);
5880 fprintf(stderr
, "\n");
5884 case nir_intrinsic_mbcnt_amd
: {
5885 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5886 RegClass rc
= RegClass(src
.type(), 1);
5887 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5888 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5889 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5890 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5891 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5892 emit_wqm(ctx
, wqm_tmp
, dst
);
5895 case nir_intrinsic_load_helper_invocation
: {
5896 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5897 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5898 ctx
->block
->kind
|= block_kind_needs_lowering
;
5899 ctx
->program
->needs_exact
= true;
5902 case nir_intrinsic_is_helper_invocation
: {
5903 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5904 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5905 ctx
->block
->kind
|= block_kind_needs_lowering
;
5906 ctx
->program
->needs_exact
= true;
5909 case nir_intrinsic_demote
:
5910 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5911 ctx
->block
->kind
|= block_kind_uses_demote
;
5912 ctx
->program
->needs_exact
= true;
5914 case nir_intrinsic_demote_if
: {
5915 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5916 as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false),
5918 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5919 ctx
->block
->kind
|= block_kind_uses_demote
;
5920 ctx
->program
->needs_exact
= true;
5923 case nir_intrinsic_first_invocation
: {
5924 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5925 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5928 case nir_intrinsic_shader_clock
:
5929 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
5930 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
5932 case nir_intrinsic_load_vertex_id_zero_base
: {
5933 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5934 bld
.copy(Definition(dst
), ctx
->vertex_id
);
5937 case nir_intrinsic_load_first_vertex
: {
5938 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5939 bld
.copy(Definition(dst
), ctx
->base_vertex
);
5942 case nir_intrinsic_load_base_instance
: {
5943 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5944 bld
.copy(Definition(dst
), ctx
->start_instance
);
5947 case nir_intrinsic_load_instance_id
: {
5948 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5949 bld
.copy(Definition(dst
), ctx
->instance_id
);
5952 case nir_intrinsic_load_draw_id
: {
5953 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5954 bld
.copy(Definition(dst
), ctx
->draw_id
);
5958 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5959 nir_print_instr(&instr
->instr
, stderr
);
5960 fprintf(stderr
, "\n");
5968 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
5969 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
5970 enum glsl_base_type
*stype
)
5972 nir_deref_instr
*texture_deref_instr
= NULL
;
5973 nir_deref_instr
*sampler_deref_instr
= NULL
;
5976 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5977 switch (instr
->src
[i
].src_type
) {
5978 case nir_tex_src_texture_deref
:
5979 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5981 case nir_tex_src_sampler_deref
:
5982 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5984 case nir_tex_src_plane
:
5985 plane
= nir_src_as_int(instr
->src
[i
].src
);
5992 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
5994 if (!sampler_deref_instr
)
5995 sampler_deref_instr
= texture_deref_instr
;
5998 assert(instr
->op
!= nir_texop_txf_ms
&&
5999 instr
->op
!= nir_texop_samples_identical
);
6000 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
6001 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
6002 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6003 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
6005 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
6008 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
6009 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
6010 fprintf(stderr
, "Unimplemented sampler descriptor: ");
6011 nir_print_instr(&instr
->instr
, stderr
);
6012 fprintf(stderr
, "\n");
6014 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
6017 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
6018 instr
->op
== nir_texop_samples_identical
))
6019 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
6022 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
6023 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
6025 Builder
bld(ctx
->program
, ctx
->block
);
6027 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
6028 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
6029 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
6031 Operand
neg_one(0xbf800000u
);
6032 Operand
one(0x3f800000u
);
6033 Operand
two(0x40000000u
);
6034 Operand
four(0x40800000u
);
6036 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
6037 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
6038 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
6040 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
6041 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
6042 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
6043 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
);
6046 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
6047 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6048 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
6050 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6053 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
6054 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
6055 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
6058 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6059 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
6061 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
6062 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
6065 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
6067 Builder
bld(ctx
->program
, ctx
->block
);
6068 Temp coord_args
[4], ma
, tc
, sc
, id
;
6069 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
6070 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
6073 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
6075 // see comment in ac_prepare_cube_coords()
6076 if (ctx
->options
->chip_class
<= GFX8
)
6077 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6080 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6082 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6083 vop3a
->operands
[0] = Operand(ma
);
6084 vop3a
->abs
[0] = true;
6085 Temp invma
= bld
.tmp(v1
);
6086 vop3a
->definitions
[0] = Definition(invma
);
6087 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6089 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6091 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6093 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6095 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6097 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6100 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6101 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6103 for (unsigned i
= 0; i
< 2; i
++) {
6104 // see comment in ac_prepare_cube_coords()
6106 Temp deriv_sc
, deriv_tc
;
6107 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6108 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6110 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6112 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6113 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6114 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6115 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6116 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6117 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6118 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6121 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6122 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6126 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6127 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6131 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6134 for (unsigned i
= 0; i
< coords
.size(); i
++)
6135 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6137 Builder
bld(ctx
->program
, ctx
->block
);
6138 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6140 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6141 for (unsigned i
= 0; i
< coords
.size(); i
++)
6142 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6143 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6144 vec
->definitions
[0] = Definition(res
);
6145 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6149 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6151 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6153 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6154 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6157 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6158 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6159 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6163 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6165 Builder
bld(ctx
->program
, ctx
->block
);
6166 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6167 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6168 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6169 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6170 nir_const_value
*sample_index_cv
= NULL
;
6171 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6172 enum glsl_base_type stype
;
6173 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6175 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6176 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6177 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6178 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6180 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6181 switch (instr
->src
[i
].src_type
) {
6182 case nir_tex_src_coord
:
6183 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6185 case nir_tex_src_bias
:
6186 if (instr
->op
== nir_texop_txb
) {
6187 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6191 case nir_tex_src_lod
: {
6192 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6194 if (val
&& val
->f32
<= 0.0) {
6197 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6202 case nir_tex_src_comparator
:
6203 if (instr
->is_shadow
) {
6204 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6208 case nir_tex_src_offset
:
6209 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6210 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6213 case nir_tex_src_ddx
:
6214 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6217 case nir_tex_src_ddy
:
6218 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6221 case nir_tex_src_ms_index
:
6222 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6223 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6224 has_sample_index
= true;
6226 case nir_tex_src_texture_offset
:
6227 case nir_tex_src_sampler_offset
:
6232 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6233 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6234 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6236 if (instr
->op
== nir_texop_texture_samples
) {
6237 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6239 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6240 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6241 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 */));
6242 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6244 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6245 samples
, Operand(1u), bld
.scc(is_msaa
));
6249 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6250 aco_ptr
<Instruction
> tmp_instr
;
6251 Temp acc
, pack
= Temp();
6253 uint32_t pack_const
= 0;
6254 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6255 if (!const_offset
[i
])
6257 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6260 if (offset
.type() == RegType::sgpr
) {
6261 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6262 if (const_offset
[i
])
6265 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6266 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6269 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6272 if (pack
== Temp()) {
6275 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6279 if (pack_const
&& pack
!= Temp())
6280 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6282 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6283 if (const_offset
[i
])
6286 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6287 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6290 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6293 if (pack
== Temp()) {
6296 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6300 if (pack_const
&& pack
!= Temp())
6301 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6303 if (pack_const
&& pack
== Temp())
6304 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6305 else if (pack
== Temp())
6311 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6312 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6314 /* pack derivatives */
6315 if (has_ddx
|| has_ddy
) {
6316 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
6317 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6318 ddx
, Operand(0u), ddy
, Operand(0u));
6320 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6325 if (instr
->coord_components
> 1 &&
6326 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6328 instr
->op
!= nir_texop_txf
)
6329 coords
= apply_round_slice(ctx
, coords
, 1);
6331 if (instr
->coord_components
> 2 &&
6332 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6333 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6334 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6335 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6337 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6338 coords
= apply_round_slice(ctx
, coords
, 2);
6340 if (ctx
->options
->chip_class
== GFX9
&&
6341 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6342 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6343 assert(coords
.size() > 0 && coords
.size() < 3);
6345 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6346 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6347 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6348 if (coords
.size() > 1)
6349 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6350 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6351 vec
->definitions
[0] = Definition(coords
);
6352 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6355 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6357 if (instr
->op
== nir_texop_samples_identical
)
6358 resource
= fmask_ptr
;
6360 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6361 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6362 instr
->op
!= nir_texop_txs
) {
6363 assert(has_sample_index
);
6364 Operand
op(sample_index
);
6365 if (sample_index_cv
)
6366 op
= Operand(sample_index_cv
->u32
);
6367 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6370 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6371 Temp split_coords
[coords
.size()];
6372 emit_split_vector(ctx
, coords
, coords
.size());
6373 for (unsigned i
= 0; i
< coords
.size(); i
++)
6374 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6377 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6378 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6379 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6382 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6383 for (unsigned i
= 0; i
< coords
.size(); i
++)
6384 vec
->operands
[i
] = Operand(split_coords
[i
]);
6385 coords
= bld
.tmp(coords
.regClass());
6386 vec
->definitions
[0] = Definition(coords
);
6387 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6392 /* Build tex instruction */
6393 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6394 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
6395 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
6397 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6400 /* gather4 selects the component by dmask and always returns vec4 */
6401 if (instr
->op
== nir_texop_tg4
) {
6402 assert(instr
->dest
.ssa
.num_components
== 4);
6403 if (instr
->is_shadow
)
6406 dmask
= 1 << instr
->component
;
6407 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6408 tmp_dst
= bld
.tmp(v4
);
6409 } else if (instr
->op
== nir_texop_samples_identical
) {
6410 tmp_dst
= bld
.tmp(v1
);
6411 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6412 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6415 aco_ptr
<MIMG_instruction
> tex
;
6416 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6418 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6420 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6421 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6424 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6425 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6427 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6428 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6429 tex
->operands
[1] = Operand(resource
);
6430 if (ctx
->options
->chip_class
== GFX9
&&
6431 instr
->op
== nir_texop_txs
&&
6432 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6434 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6435 } else if (instr
->op
== nir_texop_query_levels
) {
6436 tex
->dmask
= 1 << 3;
6441 tex
->definitions
[0] = Definition(tmp_dst
);
6443 tex
->can_reorder
= true;
6444 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6447 /* divide 3rd value by 6 by multiplying with magic number */
6448 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6449 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6450 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6451 assert(instr
->dest
.ssa
.num_components
== 3);
6452 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6453 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6454 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6455 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6460 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6464 Temp tg4_compare_cube_wa64
= Temp();
6466 if (tg4_integer_workarounds
) {
6467 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6468 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6469 tex
->operands
[1] = Operand(resource
);
6473 Temp size
= bld
.tmp(v2
);
6474 tex
->definitions
[0] = Definition(size
);
6475 tex
->can_reorder
= true;
6476 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6477 emit_split_vector(ctx
, size
, size
.size());
6480 for (unsigned i
= 0; i
< 2; i
++) {
6481 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6482 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6483 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6484 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6487 Temp orig_coords
[2] = {
6488 emit_extract_vector(ctx
, coords
, 0, v1
),
6489 emit_extract_vector(ctx
, coords
, 1, v1
)};
6490 Temp new_coords
[2] = {
6491 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6492 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6495 if (tg4_integer_cube_workaround
) {
6496 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6497 Temp desc
[resource
.size()];
6498 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6499 Format::PSEUDO
, 1, resource
.size())};
6500 split
->operands
[0] = Operand(resource
);
6501 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6502 desc
[i
] = bld
.tmp(s1
);
6503 split
->definitions
[i
] = Definition(desc
[i
]);
6505 ctx
->block
->instructions
.emplace_back(std::move(split
));
6507 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6508 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6509 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6512 if (stype
== GLSL_TYPE_UINT
) {
6513 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6514 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6515 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6516 bld
.scc(compare_cube_wa
));
6518 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6519 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6520 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6521 bld
.scc(compare_cube_wa
));
6523 tg4_compare_cube_wa64
= as_divergent_bool(ctx
, compare_cube_wa
, true);
6524 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6526 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6527 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6528 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6530 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6531 Format::PSEUDO
, resource
.size(), 1)};
6532 for (unsigned i
= 0; i
< resource
.size(); i
++)
6533 vec
->operands
[i
] = Operand(desc
[i
]);
6534 resource
= bld
.tmp(resource
.regClass());
6535 vec
->definitions
[0] = Definition(resource
);
6536 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6538 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6539 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6540 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6541 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6544 if (coords
.size() == 3) {
6545 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6546 new_coords
[0], new_coords
[1],
6547 emit_extract_vector(ctx
, coords
, 2, v1
));
6549 assert(coords
.size() == 2);
6550 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6551 new_coords
[0], new_coords
[1]);
6555 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6556 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6557 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6558 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6560 std::vector
<Operand
> args
;
6562 args
.emplace_back(Operand(offset
));
6564 args
.emplace_back(Operand(bias
));
6566 args
.emplace_back(Operand(compare
));
6568 args
.emplace_back(Operand(derivs
));
6569 args
.emplace_back(Operand(coords
));
6570 if (has_sample_index
)
6571 args
.emplace_back(Operand(sample_index
));
6573 args
.emplace_back(lod
);
6576 if (args
.size() > 1) {
6577 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6579 for (unsigned i
= 0; i
< args
.size(); i
++) {
6580 size
+= args
[i
].size();
6581 vec
->operands
[i
] = args
[i
];
6583 RegClass rc
= RegClass(RegType::vgpr
, size
);
6584 Temp tmp
= bld
.tmp(rc
);
6585 vec
->definitions
[0] = Definition(tmp
);
6586 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6589 assert(args
[0].isTemp());
6590 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6593 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6594 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6596 assert(coords
.size() == 1);
6597 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6601 op
= aco_opcode::buffer_load_format_x
; break;
6603 op
= aco_opcode::buffer_load_format_xy
; break;
6605 op
= aco_opcode::buffer_load_format_xyz
; break;
6607 op
= aco_opcode::buffer_load_format_xyzw
; break;
6609 unreachable("Tex instruction loads more than 4 components.");
6612 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6613 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6616 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6618 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6619 mubuf
->operands
[0] = Operand(coords
);
6620 mubuf
->operands
[1] = Operand(resource
);
6621 mubuf
->operands
[2] = Operand((uint32_t) 0);
6622 mubuf
->definitions
[0] = Definition(tmp_dst
);
6623 mubuf
->idxen
= true;
6624 mubuf
->can_reorder
= true;
6625 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6627 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6632 if (instr
->op
== nir_texop_txf
||
6633 instr
->op
== nir_texop_txf_ms
||
6634 instr
->op
== nir_texop_samples_identical
) {
6635 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6636 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6637 tex
->operands
[0] = Operand(arg
);
6638 tex
->operands
[1] = Operand(resource
);
6643 tex
->definitions
[0] = Definition(tmp_dst
);
6644 tex
->can_reorder
= true;
6645 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6647 if (instr
->op
== nir_texop_samples_identical
) {
6648 assert(dmask
== 1 && dst
.regClass() == v1
);
6649 assert(dst
.id() != tmp_dst
.id());
6651 Temp tmp
= bld
.tmp(s2
);
6652 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6653 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6656 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6661 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6662 aco_opcode opcode
= aco_opcode::image_sample
;
6663 if (has_offset
) { /* image_sample_*_o */
6665 opcode
= aco_opcode::image_sample_c_o
;
6667 opcode
= aco_opcode::image_sample_c_d_o
;
6669 opcode
= aco_opcode::image_sample_c_b_o
;
6671 opcode
= aco_opcode::image_sample_c_lz_o
;
6673 opcode
= aco_opcode::image_sample_c_l_o
;
6675 opcode
= aco_opcode::image_sample_o
;
6677 opcode
= aco_opcode::image_sample_d_o
;
6679 opcode
= aco_opcode::image_sample_b_o
;
6681 opcode
= aco_opcode::image_sample_lz_o
;
6683 opcode
= aco_opcode::image_sample_l_o
;
6685 } else { /* no offset */
6687 opcode
= aco_opcode::image_sample_c
;
6689 opcode
= aco_opcode::image_sample_c_d
;
6691 opcode
= aco_opcode::image_sample_c_b
;
6693 opcode
= aco_opcode::image_sample_c_lz
;
6695 opcode
= aco_opcode::image_sample_c_l
;
6697 opcode
= aco_opcode::image_sample
;
6699 opcode
= aco_opcode::image_sample_d
;
6701 opcode
= aco_opcode::image_sample_b
;
6703 opcode
= aco_opcode::image_sample_lz
;
6705 opcode
= aco_opcode::image_sample_l
;
6709 if (instr
->op
== nir_texop_tg4
) {
6711 opcode
= aco_opcode::image_gather4_lz_o
;
6713 opcode
= aco_opcode::image_gather4_c_lz_o
;
6715 opcode
= aco_opcode::image_gather4_lz
;
6717 opcode
= aco_opcode::image_gather4_c_lz
;
6719 } else if (instr
->op
== nir_texop_lod
) {
6720 opcode
= aco_opcode::image_get_lod
;
6723 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6724 tex
->operands
[0] = arg
;
6725 tex
->operands
[1] = Operand(resource
);
6726 tex
->operands
[2] = Operand(sampler
);
6730 tex
->definitions
[0] = Definition(tmp_dst
);
6731 tex
->can_reorder
= true;
6732 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6734 if (tg4_integer_cube_workaround
) {
6735 assert(tmp_dst
.id() != dst
.id());
6736 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6738 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6740 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6741 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6743 if (stype
== GLSL_TYPE_UINT
)
6744 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6746 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6747 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6749 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6750 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6751 val
[0], val
[1], val
[2], val
[3]);
6753 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6754 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6759 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6761 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6762 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6763 return Operand(tmp
.regClass());
6765 return Operand(tmp
);
6768 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6770 aco_ptr
<Pseudo_instruction
> phi
;
6771 unsigned num_src
= exec_list_length(&instr
->srcs
);
6772 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6774 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6776 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6777 bool all_undef
= true;
6778 nir_foreach_phi_src(src
, instr
) {
6779 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6780 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6784 Builder
bld(ctx
->program
, ctx
->block
);
6785 if (dst
.regClass() == s1
) {
6786 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6787 } else if (dst
.regClass() == v1
) {
6788 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6790 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6791 for (unsigned i
= 0; i
< dst
.size(); i
++)
6792 vec
->operands
[i
] = Operand(0u);
6793 vec
->definitions
[0] = Definition(dst
);
6794 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6799 /* try to scalarize vector phis */
6800 if (dst
.size() > 1) {
6801 // TODO: scalarize linear phis on divergent ifs
6802 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6803 std::array
<Temp
, 4> new_vec
;
6804 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6805 Operand src
= get_phi_operand(ctx
, pair
.second
);
6806 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6807 can_scalarize
= false;
6811 if (can_scalarize
) {
6812 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6813 assert(dst
.size() % num_components
== 0);
6814 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6816 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6817 for (unsigned k
= 0; k
< num_components
; k
++) {
6818 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6819 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6820 for (unsigned i
= 0; i
< num_src
; i
++) {
6821 Operand src
= get_phi_operand(ctx
, it
->second
);
6822 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6825 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6826 phi
->definitions
[0] = Definition(phi_dst
);
6827 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6828 new_vec
[k
] = phi_dst
;
6829 vec
->operands
[k
] = Operand(phi_dst
);
6831 vec
->definitions
[0] = Definition(dst
);
6832 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6833 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6838 unsigned extra_src
= 0;
6839 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6840 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6844 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6846 /* if we have a linear phi on a divergent if, we know that one src is undef */
6847 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6848 assert(extra_src
== 0);
6850 /* we place the phi either in the invert-block or in the current block */
6851 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6852 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6853 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6854 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6855 assert(block
->kind
& block_kind_invert
);
6856 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6858 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6860 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6862 phi
->operands
[1] = Operand(dst
.regClass());
6863 phi
->definitions
[0] = Definition(dst
);
6864 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6868 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6869 for (unsigned i
= 0; i
< num_src
; i
++) {
6870 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6873 for (unsigned i
= 0; i
< extra_src
; i
++)
6874 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6875 phi
->definitions
[0] = Definition(dst
);
6876 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6880 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6882 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6884 assert(dst
.type() == RegType::sgpr
);
6886 if (dst
.size() == 1) {
6887 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6889 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6890 for (unsigned i
= 0; i
< dst
.size(); i
++)
6891 vec
->operands
[i
] = Operand(0u);
6892 vec
->definitions
[0] = Definition(dst
);
6893 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6897 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6899 Builder
bld(ctx
->program
, ctx
->block
);
6900 Block
*logical_target
;
6901 append_logical_end(ctx
->block
);
6902 unsigned idx
= ctx
->block
->index
;
6904 switch (instr
->type
) {
6905 case nir_jump_break
:
6906 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6907 add_logical_edge(idx
, logical_target
);
6908 ctx
->block
->kind
|= block_kind_break
;
6910 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6911 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6912 /* uniform break - directly jump out of the loop */
6913 ctx
->block
->kind
|= block_kind_uniform
;
6914 ctx
->cf_info
.has_branch
= true;
6915 bld
.branch(aco_opcode::p_branch
);
6916 add_linear_edge(idx
, logical_target
);
6919 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6921 case nir_jump_continue
:
6922 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6923 add_logical_edge(idx
, logical_target
);
6924 ctx
->block
->kind
|= block_kind_continue
;
6926 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6927 /* for potential uniform breaks after this continue,
6928 we must ensure that they are handled correctly */
6929 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6930 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6932 /* uniform continue - directly jump to the loop header */
6933 ctx
->block
->kind
|= block_kind_uniform
;
6934 ctx
->cf_info
.has_branch
= true;
6935 bld
.branch(aco_opcode::p_branch
);
6936 add_linear_edge(idx
, logical_target
);
6941 fprintf(stderr
, "Unknown NIR jump instr: ");
6942 nir_print_instr(&instr
->instr
, stderr
);
6943 fprintf(stderr
, "\n");
6947 /* remove critical edges from linear CFG */
6948 bld
.branch(aco_opcode::p_branch
);
6949 Block
* break_block
= ctx
->program
->create_and_insert_block();
6950 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6951 break_block
->kind
|= block_kind_uniform
;
6952 add_linear_edge(idx
, break_block
);
6953 /* the loop_header pointer might be invalidated by this point */
6954 if (instr
->type
== nir_jump_continue
)
6955 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6956 add_linear_edge(break_block
->index
, logical_target
);
6957 bld
.reset(break_block
);
6958 bld
.branch(aco_opcode::p_branch
);
6960 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6961 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6962 add_linear_edge(idx
, continue_block
);
6963 append_logical_start(continue_block
);
6964 ctx
->block
= continue_block
;
6968 void visit_block(isel_context
*ctx
, nir_block
*block
)
6970 nir_foreach_instr(instr
, block
) {
6971 switch (instr
->type
) {
6972 case nir_instr_type_alu
:
6973 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
6975 case nir_instr_type_load_const
:
6976 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
6978 case nir_instr_type_intrinsic
:
6979 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
6981 case nir_instr_type_tex
:
6982 visit_tex(ctx
, nir_instr_as_tex(instr
));
6984 case nir_instr_type_phi
:
6985 visit_phi(ctx
, nir_instr_as_phi(instr
));
6987 case nir_instr_type_ssa_undef
:
6988 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
6990 case nir_instr_type_deref
:
6992 case nir_instr_type_jump
:
6993 visit_jump(ctx
, nir_instr_as_jump(instr
));
6996 fprintf(stderr
, "Unknown NIR instr type: ");
6997 nir_print_instr(instr
, stderr
);
6998 fprintf(stderr
, "\n");
7006 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
7008 append_logical_end(ctx
->block
);
7009 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
7010 Builder
bld(ctx
->program
, ctx
->block
);
7011 bld
.branch(aco_opcode::p_branch
);
7012 unsigned loop_preheader_idx
= ctx
->block
->index
;
7014 Block loop_exit
= Block();
7015 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7016 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
7018 Block
* loop_header
= ctx
->program
->create_and_insert_block();
7019 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
7020 loop_header
->kind
|= block_kind_loop_header
;
7021 add_edge(loop_preheader_idx
, loop_header
);
7022 ctx
->block
= loop_header
;
7024 /* emit loop body */
7025 unsigned loop_header_idx
= loop_header
->index
;
7026 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
7027 append_logical_start(ctx
->block
);
7028 visit_cf_list(ctx
, &loop
->body
);
7030 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
7031 if (!ctx
->cf_info
.has_branch
) {
7032 append_logical_end(ctx
->block
);
7033 if (ctx
->cf_info
.exec_potentially_empty
) {
7034 /* Discards can result in code running with an empty exec mask.
7035 * This would result in divergent breaks not ever being taken. As a
7036 * workaround, break the loop when the loop mask is empty instead of
7037 * always continuing. */
7038 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
7039 unsigned block_idx
= ctx
->block
->index
;
7041 /* create helper blocks to avoid critical edges */
7042 Block
*break_block
= ctx
->program
->create_and_insert_block();
7043 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7044 break_block
->kind
= block_kind_uniform
;
7045 bld
.reset(break_block
);
7046 bld
.branch(aco_opcode::p_branch
);
7047 add_linear_edge(block_idx
, break_block
);
7048 add_linear_edge(break_block
->index
, &loop_exit
);
7050 Block
*continue_block
= ctx
->program
->create_and_insert_block();
7051 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7052 continue_block
->kind
= block_kind_uniform
;
7053 bld
.reset(continue_block
);
7054 bld
.branch(aco_opcode::p_branch
);
7055 add_linear_edge(block_idx
, continue_block
);
7056 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7058 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
7059 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
7061 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
7062 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7063 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7065 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
7068 bld
.reset(ctx
->block
);
7069 bld
.branch(aco_opcode::p_branch
);
7072 /* fixup phis in loop header from unreachable blocks */
7073 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
7074 bool linear
= ctx
->cf_info
.has_branch
;
7075 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7076 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
7077 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
7078 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
7079 /* the last operand should be the one that needs to be removed */
7080 instr
->operands
.pop_back();
7081 } else if (!is_phi(instr
)) {
7087 ctx
->cf_info
.has_branch
= false;
7089 // TODO: if the loop has not a single exit, we must add one °°
7090 /* emit loop successor block */
7091 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
7092 append_logical_start(ctx
->block
);
7095 // TODO: check if it is beneficial to not branch on continues
7096 /* trim linear phis in loop header */
7097 for (auto&& instr
: loop_entry
->instructions
) {
7098 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7099 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7100 new_phi
->definitions
[0] = instr
->definitions
[0];
7101 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7102 new_phi
->operands
[i
] = instr
->operands
[i
];
7103 /* check that the remaining operands are all the same */
7104 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7105 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7106 instr
.swap(new_phi
);
7107 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7116 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7120 append_logical_end(ctx
->block
);
7121 ctx
->block
->kind
|= block_kind_branch
;
7123 /* branch to linear then block */
7124 assert(cond
.regClass() == s2
);
7125 aco_ptr
<Pseudo_branch_instruction
> branch
;
7126 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7127 branch
->operands
[0] = Operand(cond
);
7128 ctx
->block
->instructions
.push_back(std::move(branch
));
7130 ic
->BB_if_idx
= ctx
->block
->index
;
7131 ic
->BB_invert
= Block();
7132 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7133 /* Invert blocks are intentionally not marked as top level because they
7134 * are not part of the logical cfg. */
7135 ic
->BB_invert
.kind
|= block_kind_invert
;
7136 ic
->BB_endif
= Block();
7137 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7138 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7140 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7141 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7142 ctx
->cf_info
.parent_if
.is_divergent
= true;
7143 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7145 /** emit logical then block */
7146 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7147 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7148 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7149 ctx
->block
= BB_then_logical
;
7150 append_logical_start(BB_then_logical
);
7153 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7155 Block
*BB_then_logical
= ctx
->block
;
7156 append_logical_end(BB_then_logical
);
7157 /* branch from logical then block to invert block */
7158 aco_ptr
<Pseudo_branch_instruction
> branch
;
7159 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7160 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7161 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7162 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7163 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7164 BB_then_logical
->kind
|= block_kind_uniform
;
7165 assert(!ctx
->cf_info
.has_branch
);
7166 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7167 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7169 /** emit linear then block */
7170 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7171 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7172 BB_then_linear
->kind
|= block_kind_uniform
;
7173 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7174 /* branch from linear then block to invert block */
7175 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7176 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7177 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7179 /** emit invert merge block */
7180 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7181 ic
->invert_idx
= ctx
->block
->index
;
7183 /* branch to linear else block (skip else) */
7184 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7185 branch
->operands
[0] = Operand(ic
->cond
);
7186 ctx
->block
->instructions
.push_back(std::move(branch
));
7188 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7189 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7191 /** emit logical else block */
7192 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7193 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7194 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7195 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7196 ctx
->block
= BB_else_logical
;
7197 append_logical_start(BB_else_logical
);
7200 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7202 Block
*BB_else_logical
= ctx
->block
;
7203 append_logical_end(BB_else_logical
);
7205 /* branch from logical else block to endif block */
7206 aco_ptr
<Pseudo_branch_instruction
> branch
;
7207 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7208 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7209 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7210 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7211 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7212 BB_else_logical
->kind
|= block_kind_uniform
;
7214 assert(!ctx
->cf_info
.has_branch
);
7215 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7218 /** emit linear else block */
7219 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7220 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7221 BB_else_linear
->kind
|= block_kind_uniform
;
7222 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7224 /* branch from linear else block to endif block */
7225 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7226 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7227 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7230 /** emit endif merge block */
7231 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7232 append_logical_start(ctx
->block
);
7235 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7236 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7237 /* uniform control flow never has an empty exec-mask */
7238 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7239 ctx
->cf_info
.exec_potentially_empty
= false;
7242 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7244 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7245 Builder
bld(ctx
->program
, ctx
->block
);
7246 aco_ptr
<Pseudo_branch_instruction
> branch
;
7248 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7250 * Uniform conditionals are represented in the following way*) :
7252 * The linear and logical CFG:
7255 * BB_THEN (logical) BB_ELSE (logical)
7259 * *) Exceptions may be due to break and continue statements within loops
7260 * If a break/continue happens within uniform control flow, it branches
7261 * to the loop exit/entry block. Otherwise, it branches to the next
7264 append_logical_end(ctx
->block
);
7265 ctx
->block
->kind
|= block_kind_uniform
;
7268 if (cond
.regClass() == s2
) {
7269 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7270 cond
= as_uniform_bool(ctx
, cond
);
7272 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7273 branch
->operands
[0] = Operand(cond
);
7274 branch
->operands
[0].setFixed(scc
);
7275 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7277 unsigned BB_if_idx
= ctx
->block
->index
;
7278 Block BB_endif
= Block();
7279 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7280 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7282 /** emit then block */
7283 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7284 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7285 add_edge(BB_if_idx
, BB_then
);
7286 append_logical_start(BB_then
);
7287 ctx
->block
= BB_then
;
7288 visit_cf_list(ctx
, &if_stmt
->then_list
);
7289 BB_then
= ctx
->block
;
7290 bool then_branch
= ctx
->cf_info
.has_branch
;
7291 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7294 append_logical_end(BB_then
);
7295 /* branch from then block to endif block */
7296 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7297 BB_then
->instructions
.emplace_back(std::move(branch
));
7298 add_linear_edge(BB_then
->index
, &BB_endif
);
7299 if (!then_branch_divergent
)
7300 add_logical_edge(BB_then
->index
, &BB_endif
);
7301 BB_then
->kind
|= block_kind_uniform
;
7304 ctx
->cf_info
.has_branch
= false;
7305 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7307 /** emit else block */
7308 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7309 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7310 add_edge(BB_if_idx
, BB_else
);
7311 append_logical_start(BB_else
);
7312 ctx
->block
= BB_else
;
7313 visit_cf_list(ctx
, &if_stmt
->else_list
);
7314 BB_else
= ctx
->block
;
7316 if (!ctx
->cf_info
.has_branch
) {
7317 append_logical_end(BB_else
);
7318 /* branch from then block to endif block */
7319 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7320 BB_else
->instructions
.emplace_back(std::move(branch
));
7321 add_linear_edge(BB_else
->index
, &BB_endif
);
7322 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7323 add_logical_edge(BB_else
->index
, &BB_endif
);
7324 BB_else
->kind
|= block_kind_uniform
;
7327 ctx
->cf_info
.has_branch
&= then_branch
;
7328 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7330 /** emit endif merge block */
7331 if (!ctx
->cf_info
.has_branch
) {
7332 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7333 append_logical_start(ctx
->block
);
7335 } else { /* non-uniform condition */
7337 * To maintain a logical and linear CFG without critical edges,
7338 * non-uniform conditionals are represented in the following way*) :
7343 * BB_THEN (logical) BB_THEN (linear)
7345 * BB_INVERT (linear)
7347 * BB_ELSE (logical) BB_ELSE (linear)
7354 * BB_THEN (logical) BB_ELSE (logical)
7358 * *) Exceptions may be due to break and continue statements within loops
7363 begin_divergent_if_then(ctx
, &ic
, cond
);
7364 visit_cf_list(ctx
, &if_stmt
->then_list
);
7366 begin_divergent_if_else(ctx
, &ic
);
7367 visit_cf_list(ctx
, &if_stmt
->else_list
);
7369 end_divergent_if(ctx
, &ic
);
7373 static void visit_cf_list(isel_context
*ctx
,
7374 struct exec_list
*list
)
7376 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7377 switch (node
->type
) {
7378 case nir_cf_node_block
:
7379 visit_block(ctx
, nir_cf_node_as_block(node
));
7381 case nir_cf_node_if
:
7382 visit_if(ctx
, nir_cf_node_as_if(node
));
7384 case nir_cf_node_loop
:
7385 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7388 unreachable("unimplemented cf list type");
7393 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7395 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7396 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7397 if (!is_pos
&& !mask
)
7399 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7401 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7402 exp
->enabled_mask
= mask
;
7403 for (unsigned i
= 0; i
< 4; ++i
) {
7404 if (mask
& (1 << i
))
7405 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7407 exp
->operands
[i
] = Operand(v1
);
7409 exp
->valid_mask
= false;
7411 exp
->compressed
= false;
7413 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7415 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7416 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7419 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7421 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7422 exp
->enabled_mask
= 0;
7423 for (unsigned i
= 0; i
< 4; ++i
)
7424 exp
->operands
[i
] = Operand(v1
);
7425 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7426 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7427 exp
->enabled_mask
|= 0x1;
7429 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7430 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7431 exp
->enabled_mask
|= 0x4;
7433 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7434 if (ctx
->options
->chip_class
< GFX9
) {
7435 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7436 exp
->enabled_mask
|= 0x8;
7438 Builder
bld(ctx
->program
, ctx
->block
);
7440 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7441 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7442 if (exp
->operands
[2].isTemp())
7443 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7445 exp
->operands
[2] = Operand(out
);
7446 exp
->enabled_mask
|= 0x4;
7449 exp
->valid_mask
= false;
7451 exp
->compressed
= false;
7452 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7453 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7456 static void create_vs_exports(isel_context
*ctx
)
7458 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7460 if (outinfo
->export_prim_id
) {
7461 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7462 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = ctx
->vs_prim_id
;
7465 if (ctx
->options
->key
.has_multiview_view_index
) {
7466 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7467 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, ctx
->view_index
);
7470 /* the order these position exports are created is important */
7472 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7473 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7474 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7476 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7477 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7478 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7479 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7481 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7482 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7483 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7484 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7485 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7488 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7489 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7490 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7493 export_vs_varying(ctx
, i
, false, NULL
);
7497 static void emit_stream_output(isel_context
*ctx
,
7498 Temp
const *so_buffers
,
7499 Temp
const *so_write_offset
,
7500 const struct radv_stream_output
*output
)
7502 unsigned num_comps
= util_bitcount(output
->component_mask
);
7503 unsigned loc
= output
->location
;
7504 unsigned buf
= output
->buffer
;
7505 unsigned offset
= output
->offset
;
7507 assert(num_comps
&& num_comps
<= 4);
7508 if (!num_comps
|| num_comps
> 4)
7511 unsigned start
= ffs(output
->component_mask
) - 1;
7514 bool all_undef
= true;
7515 assert(ctx
->stage
== vertex_vs
);
7516 for (unsigned i
= 0; i
< num_comps
; i
++) {
7517 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7518 all_undef
= all_undef
&& !out
[i
].id();
7523 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7524 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7525 for (unsigned i
= 0; i
< num_comps
; ++i
)
7526 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7527 vec
->definitions
[0] = Definition(write_data
);
7528 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7531 switch (num_comps
) {
7533 opcode
= aco_opcode::buffer_store_dword
;
7536 opcode
= aco_opcode::buffer_store_dwordx2
;
7539 opcode
= aco_opcode::buffer_store_dwordx3
;
7542 opcode
= aco_opcode::buffer_store_dwordx4
;
7546 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7547 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7548 store
->operands
[1] = Operand(so_buffers
[buf
]);
7549 store
->operands
[2] = Operand((uint32_t) 0);
7550 store
->operands
[3] = Operand(write_data
);
7551 if (offset
> 4095) {
7552 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7553 Builder
bld(ctx
->program
, ctx
->block
);
7554 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7556 store
->offset
= offset
;
7558 store
->offen
= true;
7562 store
->can_reorder
= true;
7563 ctx
->block
->instructions
.emplace_back(std::move(store
));
7566 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7568 Builder
bld(ctx
->program
, ctx
->block
);
7571 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, ctx
->streamout_buffers
);
7572 for (unsigned i
= 0; i
< 4; i
++) {
7573 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7577 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7580 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7581 ctx
->streamout_config
, Operand(0x70010u
));
7583 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7584 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7586 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7589 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7591 bld
.reset(ctx
->block
);
7593 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), ctx
->streamout_write_idx
, tid
);
7595 Temp so_write_offset
[4];
7597 for (unsigned i
= 0; i
< 4; i
++) {
7598 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7603 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7604 ctx
->streamout_write_idx
, ctx
->streamout_offset
[i
]);
7605 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7607 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7609 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7610 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u), ctx
->streamout_offset
[i
]);
7611 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7615 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7616 struct radv_stream_output
*output
=
7617 &ctx
->program
->info
->so
.outputs
[i
];
7618 if (stream
!= output
->stream
)
7621 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7624 begin_divergent_if_else(ctx
, &ic
);
7625 end_divergent_if(ctx
, &ic
);
7628 } /* end namespace */
7630 void handle_bc_optimize(isel_context
*ctx
)
7632 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7633 Builder
bld(ctx
->program
, ctx
->block
);
7634 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7635 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7636 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7637 if (uses_center
&& uses_centroid
) {
7638 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)), ctx
->prim_mask
, Operand(0u));
7640 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7641 for (unsigned i
= 0; i
< 2; i
++) {
7642 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7643 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
],
7644 ctx
->fs_inputs
[fs_input::persp_center_p1
+ i
],
7646 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
] = new_coord
;
7650 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7651 for (unsigned i
= 0; i
< 2; i
++) {
7652 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7653 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
],
7654 ctx
->fs_inputs
[fs_input::linear_center_p1
+ i
],
7656 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
] = new_coord
;
7662 void select_program(Program
*program
,
7663 unsigned shader_count
,
7664 struct nir_shader
*const *shaders
,
7665 ac_shader_config
* config
,
7666 struct radv_shader_info
*info
,
7667 struct radv_nir_compiler_options
*options
)
7669 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, info
, options
);
7671 for (unsigned i
= 0; i
< shader_count
; i
++) {
7672 nir_shader
*nir
= shaders
[i
];
7673 init_context(&ctx
, nir
);
7676 add_startpgm(&ctx
); /* needs to be after init_context() for FS */
7677 append_logical_start(ctx
.block
);
7681 if (shader_count
>= 2) {
7682 Builder
bld(ctx
.program
, ctx
.block
);
7683 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)));
7684 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7685 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7686 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7688 begin_divergent_if_then(&ctx
, &ic
, cond
);
7692 Builder
bld(ctx
.program
, ctx
.block
);
7693 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7694 bld
.sopp(aco_opcode::s_barrier
);
7697 if (ctx
.stage
== fragment_fs
)
7698 handle_bc_optimize(&ctx
);
7700 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7701 visit_cf_list(&ctx
, &func
->body
);
7703 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7704 emit_streamout(&ctx
, 0);
7706 if (ctx
.stage
== vertex_vs
)
7707 create_vs_exports(&ctx
);
7709 if (shader_count
>= 2) {
7710 begin_divergent_if_else(&ctx
, &ic
);
7711 end_divergent_if(&ctx
, &ic
);
7714 ralloc_free(ctx
.divergent_vals
);
7717 append_logical_end(ctx
.block
);
7718 ctx
.block
->kind
|= block_kind_uniform
;
7719 Builder
bld(ctx
.program
, ctx
.block
);
7720 if (ctx
.program
->wb_smem_l1_on_end
)
7721 bld
.smem(aco_opcode::s_dcache_wb
, false);
7722 bld
.sopp(aco_opcode::s_endpgm
);
7725 for (Block
& BB
: program
->blocks
) {
7726 for (unsigned idx
: BB
.linear_preds
)
7727 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7728 for (unsigned idx
: BB
.logical_preds
)
7729 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);