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 "aco_builder.h"
31 #include "aco_interface.h"
32 #include "aco_instruction_selection_setup.cpp"
33 #include "util/fast_idiv_by_const.h"
38 class loop_info_RAII
{
40 unsigned header_idx_old
;
42 bool divergent_cont_old
;
43 bool divergent_branch_old
;
44 bool divergent_if_old
;
47 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
49 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
50 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
51 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
52 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
54 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
55 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
56 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
57 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
58 ctx
->cf_info
.parent_if
.is_divergent
= false;
59 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
64 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
65 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
66 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
67 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
68 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
69 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
70 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
71 ctx
->cf_info
.exec_potentially_empty
= false;
79 bool exec_potentially_empty_old
;
83 bool then_branch_divergent
;
88 static void visit_cf_list(struct isel_context
*ctx
,
89 struct exec_list
*list
);
91 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
93 succ
->logical_preds
.emplace_back(pred_idx
);
97 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
99 succ
->linear_preds
.emplace_back(pred_idx
);
102 static void add_edge(unsigned pred_idx
, Block
*succ
)
104 add_logical_edge(pred_idx
, succ
);
105 add_linear_edge(pred_idx
, succ
);
108 static void append_logical_start(Block
*b
)
110 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
113 static void append_logical_end(Block
*b
)
115 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
118 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
120 assert(ctx
->allocated
[def
->index
].id());
121 return ctx
->allocated
[def
->index
];
124 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
126 Builder
bld(ctx
->program
, ctx
->block
);
129 dst
= bld
.tmp(src
.regClass());
131 if (ctx
->stage
!= fragment_fs
) {
135 if (src
.type() == RegType::vgpr
|| src
.size() > 1)
136 bld
.copy(Definition(dst
), src
);
138 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
142 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
143 ctx
->program
->needs_wqm
|= program_needs_wqm
;
147 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
149 if (val
.type() == RegType::sgpr
) {
150 Builder
bld(ctx
->program
, ctx
->block
);
151 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
153 assert(val
.type() == RegType::vgpr
);
157 //assumes a != 0xffffffff
158 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
161 Builder
bld(ctx
->program
, ctx
->block
);
163 if (util_is_power_of_two_or_zero(b
)) {
164 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
168 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
170 assert(info
.multiplier
<= 0xffffffff);
172 bool pre_shift
= info
.pre_shift
!= 0;
173 bool increment
= info
.increment
!= 0;
174 bool multiply
= true;
175 bool post_shift
= info
.post_shift
!= 0;
177 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
178 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
182 Temp pre_shift_dst
= a
;
184 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
185 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
188 Temp increment_dst
= pre_shift_dst
;
190 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
191 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
194 Temp multiply_dst
= increment_dst
;
196 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
197 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
198 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
202 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
206 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
208 Builder
bld(ctx
->program
, ctx
->block
);
209 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
213 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
215 /* no need to extract the whole vector */
216 if (src
.regClass() == dst_rc
) {
220 assert(src
.size() > idx
);
221 Builder
bld(ctx
->program
, ctx
->block
);
222 auto it
= ctx
->allocated_vec
.find(src
.id());
223 /* the size check needs to be early because elements other than 0 may be garbage */
224 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
225 if (it
->second
[idx
].regClass() == dst_rc
) {
226 return it
->second
[idx
];
228 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
229 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
230 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
234 if (src
.size() == dst_rc
.size()) {
236 return bld
.copy(bld
.def(dst_rc
), src
);
238 Temp dst
= bld
.tmp(dst_rc
);
239 emit_extract_vector(ctx
, src
, idx
, dst
);
244 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
246 if (num_components
== 1)
248 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
250 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
251 split
->operands
[0] = Operand(vec_src
);
252 std::array
<Temp
,4> elems
;
253 for (unsigned i
= 0; i
< num_components
; i
++) {
254 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
255 split
->definitions
[i
] = Definition(elems
[i
]);
257 ctx
->block
->instructions
.emplace_back(std::move(split
));
258 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
261 /* This vector expansion uses a mask to determine which elements in the new vector
262 * come from the original vector. The other elements are undefined. */
263 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
265 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
270 Builder
bld(ctx
->program
, ctx
->block
);
271 if (num_components
== 1) {
272 if (dst
.type() == RegType::sgpr
)
273 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
275 bld
.copy(Definition(dst
), vec_src
);
279 unsigned component_size
= dst
.size() / num_components
;
280 std::array
<Temp
,4> elems
;
282 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
283 vec
->definitions
[0] = Definition(dst
);
285 for (unsigned i
= 0; i
< num_components
; i
++) {
286 if (mask
& (1 << i
)) {
287 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
288 if (dst
.type() == RegType::sgpr
)
289 src
= bld
.as_uniform(src
);
290 vec
->operands
[i
] = Operand(src
);
292 vec
->operands
[i
] = Operand(0u);
294 elems
[i
] = vec
->operands
[i
].getTemp();
296 ctx
->block
->instructions
.emplace_back(std::move(vec
));
297 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
300 Temp
as_divergent_bool(isel_context
*ctx
, Temp val
, bool vcc_hint
)
302 if (val
.regClass() == s2
) {
305 assert(val
.regClass() == s1
);
306 Builder
bld(ctx
->program
, ctx
->block
);
307 Definition
& def
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
),
308 Operand((uint32_t) -1), Operand(0u), bld
.scc(val
)).def(0);
311 return def
.getTemp();
315 Temp
as_uniform_bool(isel_context
*ctx
, Temp val
)
317 if (val
.regClass() == s1
) {
320 assert(val
.regClass() == s2
);
321 Builder
bld(ctx
->program
, ctx
->block
);
322 return bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), Operand(0u), Operand(val
));
326 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
328 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
329 return get_ssa_temp(ctx
, src
.src
.ssa
);
331 if (src
.src
.ssa
->num_components
== size
) {
332 bool identity_swizzle
= true;
333 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
334 if (src
.swizzle
[i
] != i
)
335 identity_swizzle
= false;
337 if (identity_swizzle
)
338 return get_ssa_temp(ctx
, src
.src
.ssa
);
341 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
342 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
343 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
344 assert(vec
.size() % elem_size
== 0);
346 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
348 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
351 std::array
<Temp
,4> elems
;
352 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
353 for (unsigned i
= 0; i
< size
; ++i
) {
354 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
355 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
357 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
358 vec_instr
->definitions
[0] = Definition(dst
);
359 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
360 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
365 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
369 Builder
bld(ctx
->program
, ctx
->block
);
370 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
371 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
374 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
376 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
377 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
378 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
379 sop2
->definitions
[0] = Definition(dst
);
381 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
382 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
385 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
387 Builder
bld(ctx
->program
, ctx
->block
);
388 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
389 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
390 if (src1
.type() == RegType::sgpr
) {
391 if (commutative
&& src0
.type() == RegType::vgpr
) {
395 } else if (src0
.type() == RegType::vgpr
&&
396 op
!= aco_opcode::v_madmk_f32
&&
397 op
!= aco_opcode::v_madak_f32
&&
398 op
!= aco_opcode::v_madmk_f16
&&
399 op
!= aco_opcode::v_madak_f16
) {
400 /* If the instruction is not commutative, we emit a VOP3A instruction */
401 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
404 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
407 bld
.vop2(op
, Definition(dst
), src0
, src1
);
410 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
412 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
413 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
414 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
416 /* ensure that the instruction has at most 1 sgpr operand
417 * The optimizer will inline constants for us */
418 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
419 src0
= as_vgpr(ctx
, src0
);
420 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
421 src1
= as_vgpr(ctx
, src1
);
422 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
423 src2
= as_vgpr(ctx
, src2
);
425 Builder
bld(ctx
->program
, ctx
->block
);
426 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
429 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
431 Builder
bld(ctx
->program
, ctx
->block
);
432 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
435 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
437 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
438 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
439 aco_ptr
<Instruction
> vopc
;
440 if (src1
.type() == RegType::sgpr
) {
441 if (src0
.type() == RegType::vgpr
) {
442 /* to swap the operands, we might also have to change the opcode */
444 case aco_opcode::v_cmp_lt_f32
:
445 op
= aco_opcode::v_cmp_gt_f32
;
447 case aco_opcode::v_cmp_ge_f32
:
448 op
= aco_opcode::v_cmp_le_f32
;
450 case aco_opcode::v_cmp_lt_i32
:
451 op
= aco_opcode::v_cmp_gt_i32
;
453 case aco_opcode::v_cmp_ge_i32
:
454 op
= aco_opcode::v_cmp_le_i32
;
456 case aco_opcode::v_cmp_lt_u32
:
457 op
= aco_opcode::v_cmp_gt_u32
;
459 case aco_opcode::v_cmp_ge_u32
:
460 op
= aco_opcode::v_cmp_le_u32
;
462 case aco_opcode::v_cmp_lt_f64
:
463 op
= aco_opcode::v_cmp_gt_f64
;
465 case aco_opcode::v_cmp_ge_f64
:
466 op
= aco_opcode::v_cmp_le_f64
;
468 case aco_opcode::v_cmp_lt_i64
:
469 op
= aco_opcode::v_cmp_gt_i64
;
471 case aco_opcode::v_cmp_ge_i64
:
472 op
= aco_opcode::v_cmp_le_i64
;
474 case aco_opcode::v_cmp_lt_u64
:
475 op
= aco_opcode::v_cmp_gt_u64
;
477 case aco_opcode::v_cmp_ge_u64
:
478 op
= aco_opcode::v_cmp_le_u64
;
480 default: /* eq and ne are commutative */
487 src1
= as_vgpr(ctx
, src1
);
490 Builder
bld(ctx
->program
, ctx
->block
);
491 bld
.vopc(op
, Definition(dst
), src0
, src1
).def(0).setHint(vcc
);
494 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
496 if (dst
.regClass() == s2
) {
497 emit_vopc_instruction(ctx
, instr
, op
, dst
);
498 if (!ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
])
499 emit_split_vector(ctx
, dst
, 2);
500 } else if (dst
.regClass() == s1
) {
501 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
502 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
503 assert(src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
);
505 Builder
bld(ctx
->program
, ctx
->block
);
506 bld
.sopc(op
, bld
.scc(Definition(dst
)), src0
, src1
);
513 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
515 Builder
bld(ctx
->program
, ctx
->block
);
516 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
517 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
518 if (dst
.regClass() == s2
) {
519 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
),
520 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
522 assert(dst
.regClass() == s1
);
523 bld
.sop2(op32
, bld
.def(s1
), bld
.scc(Definition(dst
)),
524 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
529 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
531 Builder
bld(ctx
->program
, ctx
->block
);
532 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
533 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
534 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
536 if (dst
.type() == RegType::vgpr
) {
537 cond
= as_divergent_bool(ctx
, cond
, true);
539 aco_ptr
<Instruction
> bcsel
;
540 if (dst
.size() == 1) {
541 then
= as_vgpr(ctx
, then
);
542 els
= as_vgpr(ctx
, els
);
544 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
545 } else if (dst
.size() == 2) {
546 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
547 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
548 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
549 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
551 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
552 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
554 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
556 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
557 nir_print_instr(&instr
->instr
, stderr
);
558 fprintf(stderr
, "\n");
563 if (instr
->dest
.dest
.ssa
.bit_size
!= 1) { /* uniform condition and values in sgpr */
564 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
565 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
566 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
567 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(as_uniform_bool(ctx
, cond
)));
569 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
570 nir_print_instr(&instr
->instr
, stderr
);
571 fprintf(stderr
, "\n");
577 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
579 if (dst
.regClass() == s1
)
580 cond
= as_uniform_bool(ctx
, cond
);
582 if (cond
.regClass() == s1
) { /* uniform selection */
584 if (dst
.regClass() == s2
) {
585 op
= aco_opcode::s_cselect_b64
;
586 then
= as_divergent_bool(ctx
, then
, false);
587 els
= as_divergent_bool(ctx
, els
, false);
589 assert(dst
.regClass() == s1
);
590 op
= aco_opcode::s_cselect_b32
;
591 then
= as_uniform_bool(ctx
, then
);
592 els
= as_uniform_bool(ctx
, els
);
594 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(cond
));
598 /* divergent boolean bcsel
599 * this implements bcsel on bools: dst = s0 ? s1 : s2
600 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
601 assert (dst
.regClass() == s2
);
602 then
= as_divergent_bool(ctx
, then
, false);
603 els
= as_divergent_bool(ctx
, els
, false);
605 if (cond
.id() != then
.id())
606 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
608 if (cond
.id() == els
.id())
609 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
611 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
612 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
615 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
617 if (!instr
->dest
.dest
.is_ssa
) {
618 fprintf(stderr
, "nir alu dst not in ssa: ");
619 nir_print_instr(&instr
->instr
, stderr
);
620 fprintf(stderr
, "\n");
623 Builder
bld(ctx
->program
, ctx
->block
);
624 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
629 std::array
<Temp
,4> elems
;
630 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
631 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
632 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
633 vec
->operands
[i
] = Operand
{elems
[i
]};
635 vec
->definitions
[0] = Definition(dst
);
636 ctx
->block
->instructions
.emplace_back(std::move(vec
));
637 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
641 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
642 aco_ptr
<Instruction
> mov
;
643 if (dst
.type() == RegType::sgpr
) {
644 if (src
.type() == RegType::vgpr
)
645 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
646 else if (src
.regClass() == s1
)
647 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
648 else if (src
.regClass() == s2
)
649 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
651 unreachable("wrong src register class for nir_op_imov");
652 } else if (dst
.regClass() == v1
) {
653 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
654 } else if (dst
.regClass() == v2
) {
655 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
657 nir_print_instr(&instr
->instr
, stderr
);
658 unreachable("Should have been lowered to scalar.");
663 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
664 /* uniform booleans */
665 if (instr
->dest
.dest
.ssa
.bit_size
== 1 && dst
.regClass() == s1
) {
666 if (src
.regClass() == s1
) {
667 /* in this case, src is either 1 or 0 */
668 bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.scc(Definition(dst
)), Operand(1u), src
);
670 /* src is either exec_mask or 0 */
671 assert(src
.regClass() == s2
);
672 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
674 } else if (dst
.regClass() == v1
) {
675 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
676 } else if (dst
.type() == RegType::sgpr
) {
677 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
678 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
680 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
681 nir_print_instr(&instr
->instr
, stderr
);
682 fprintf(stderr
, "\n");
687 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
688 if (dst
.regClass() == v1
) {
689 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
690 } else if (dst
.regClass() == s1
) {
691 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
693 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
694 nir_print_instr(&instr
->instr
, stderr
);
695 fprintf(stderr
, "\n");
700 if (dst
.regClass() == s1
) {
701 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
702 } else if (dst
.regClass() == v1
) {
703 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
704 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
706 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
707 nir_print_instr(&instr
->instr
, stderr
);
708 fprintf(stderr
, "\n");
713 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
714 if (dst
.regClass() == s1
) {
715 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
716 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
717 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
718 } else if (dst
.regClass() == s2
) {
719 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
720 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
721 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
722 } else if (dst
.regClass() == v1
) {
723 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
724 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
725 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
726 } else if (dst
.regClass() == v2
) {
727 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
728 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
729 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
730 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
731 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
732 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
734 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
735 nir_print_instr(&instr
->instr
, stderr
);
736 fprintf(stderr
, "\n");
741 if (dst
.regClass() == v1
) {
742 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
743 } else if (dst
.regClass() == s1
) {
744 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
746 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
747 nir_print_instr(&instr
->instr
, stderr
);
748 fprintf(stderr
, "\n");
753 if (dst
.regClass() == v1
) {
754 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
755 } else if (dst
.regClass() == s1
) {
756 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
758 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
759 nir_print_instr(&instr
->instr
, stderr
);
760 fprintf(stderr
, "\n");
765 if (dst
.regClass() == v1
) {
766 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
767 } else if (dst
.regClass() == s1
) {
768 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
770 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
771 nir_print_instr(&instr
->instr
, stderr
);
772 fprintf(stderr
, "\n");
777 if (dst
.regClass() == v1
) {
778 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
779 } else if (dst
.regClass() == s1
) {
780 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
782 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
783 nir_print_instr(&instr
->instr
, stderr
);
784 fprintf(stderr
, "\n");
789 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
790 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
791 } else if (dst
.regClass() == v1
) {
792 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
793 } else if (dst
.regClass() == s1
) {
794 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
795 } else if (dst
.regClass() == s2
) {
796 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
798 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
799 nir_print_instr(&instr
->instr
, stderr
);
800 fprintf(stderr
, "\n");
805 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
806 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
807 } else if (dst
.regClass() == v1
) {
808 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
809 } else if (dst
.regClass() == s1
) {
810 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
811 } else if (dst
.regClass() == s2
) {
812 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
814 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
815 nir_print_instr(&instr
->instr
, stderr
);
816 fprintf(stderr
, "\n");
821 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
822 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
823 } else if (dst
.regClass() == v1
) {
824 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
825 } else if (dst
.regClass() == s1
) {
826 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
827 } else if (dst
.regClass() == s2
) {
828 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
830 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
831 nir_print_instr(&instr
->instr
, stderr
);
832 fprintf(stderr
, "\n");
837 if (dst
.regClass() == v1
) {
838 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
839 } else if (dst
.regClass() == v2
) {
840 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
841 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
842 } else if (dst
.regClass() == s2
) {
843 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
844 } else if (dst
.regClass() == s1
) {
845 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
847 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
848 nir_print_instr(&instr
->instr
, stderr
);
849 fprintf(stderr
, "\n");
854 if (dst
.regClass() == v1
) {
855 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
856 } else if (dst
.regClass() == v2
) {
857 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
858 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
859 } else if (dst
.regClass() == s1
) {
860 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
861 } else if (dst
.regClass() == s2
) {
862 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
864 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
865 nir_print_instr(&instr
->instr
, stderr
);
866 fprintf(stderr
, "\n");
871 if (dst
.regClass() == v1
) {
872 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
873 } else if (dst
.regClass() == v2
) {
874 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
875 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
876 } else if (dst
.regClass() == s1
) {
877 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
878 } else if (dst
.regClass() == s2
) {
879 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
881 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
882 nir_print_instr(&instr
->instr
, stderr
);
883 fprintf(stderr
, "\n");
887 case nir_op_find_lsb
: {
888 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
889 if (src
.regClass() == s1
) {
890 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
891 } else if (src
.regClass() == v1
) {
892 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
893 } else if (src
.regClass() == s2
) {
894 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
896 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
897 nir_print_instr(&instr
->instr
, stderr
);
898 fprintf(stderr
, "\n");
902 case nir_op_ufind_msb
:
903 case nir_op_ifind_msb
: {
904 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
905 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
906 aco_opcode op
= src
.regClass() == s2
?
907 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
908 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
909 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
911 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
912 Operand(src
.size() * 32u - 1u), msb_rev
);
913 Temp msb
= sub
.def(0).getTemp();
914 Temp carry
= sub
.def(1).getTemp();
916 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
917 } else if (src
.regClass() == v1
) {
918 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
919 Temp msb_rev
= bld
.tmp(v1
);
920 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
921 Temp msb
= bld
.tmp(v1
);
922 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
923 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
925 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
926 nir_print_instr(&instr
->instr
, stderr
);
927 fprintf(stderr
, "\n");
931 case nir_op_bitfield_reverse
: {
932 if (dst
.regClass() == s1
) {
933 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
934 } else if (dst
.regClass() == v1
) {
935 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
937 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
938 nir_print_instr(&instr
->instr
, stderr
);
939 fprintf(stderr
, "\n");
944 if (dst
.regClass() == s1
) {
945 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
949 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
950 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
951 if (dst
.regClass() == v1
) {
952 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
956 assert(src0
.size() == 2 && src1
.size() == 2);
957 Temp src00
= bld
.tmp(src0
.type(), 1);
958 Temp src01
= bld
.tmp(dst
.type(), 1);
959 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
960 Temp src10
= bld
.tmp(src1
.type(), 1);
961 Temp src11
= bld
.tmp(dst
.type(), 1);
962 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
964 if (dst
.regClass() == s2
) {
965 Temp carry
= bld
.tmp(s1
);
966 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
967 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
968 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
969 } else if (dst
.regClass() == v2
) {
970 Temp dst0
= bld
.tmp(v1
);
971 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
972 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
973 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
975 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
976 nir_print_instr(&instr
->instr
, stderr
);
977 fprintf(stderr
, "\n");
981 case nir_op_uadd_sat
: {
982 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
983 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
984 if (dst
.regClass() == s1
) {
985 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
986 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
988 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
989 } else if (dst
.regClass() == v1
) {
990 if (ctx
->options
->chip_class
>= GFX9
) {
991 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
992 add
->operands
[0] = Operand(src0
);
993 add
->operands
[1] = Operand(src1
);
994 add
->definitions
[0] = Definition(dst
);
996 ctx
->block
->instructions
.emplace_back(std::move(add
));
998 if (src1
.regClass() != v1
)
999 std::swap(src0
, src1
);
1000 assert(src1
.regClass() == v1
);
1001 Temp tmp
= bld
.tmp(v1
);
1002 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1003 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1006 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1007 nir_print_instr(&instr
->instr
, stderr
);
1008 fprintf(stderr
, "\n");
1012 case nir_op_uadd_carry
: {
1013 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1014 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1015 if (dst
.regClass() == s1
) {
1016 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1019 if (dst
.regClass() == v1
) {
1020 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1021 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1025 Temp src00
= bld
.tmp(src0
.type(), 1);
1026 Temp src01
= bld
.tmp(dst
.type(), 1);
1027 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1028 Temp src10
= bld
.tmp(src1
.type(), 1);
1029 Temp src11
= bld
.tmp(dst
.type(), 1);
1030 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1031 if (dst
.regClass() == s2
) {
1032 Temp carry
= bld
.tmp(s1
);
1033 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1034 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1035 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1036 } else if (dst
.regClass() == v2
) {
1037 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1038 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1039 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1040 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1042 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1043 nir_print_instr(&instr
->instr
, stderr
);
1044 fprintf(stderr
, "\n");
1049 if (dst
.regClass() == s1
) {
1050 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1054 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1055 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1056 if (dst
.regClass() == v1
) {
1057 bld
.vsub32(Definition(dst
), src0
, src1
);
1061 Temp src00
= bld
.tmp(src0
.type(), 1);
1062 Temp src01
= bld
.tmp(dst
.type(), 1);
1063 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1064 Temp src10
= bld
.tmp(src1
.type(), 1);
1065 Temp src11
= bld
.tmp(dst
.type(), 1);
1066 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1067 if (dst
.regClass() == s2
) {
1068 Temp carry
= bld
.tmp(s1
);
1069 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1070 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1071 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1072 } else if (dst
.regClass() == v2
) {
1073 Temp lower
= bld
.tmp(v1
);
1074 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1075 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1076 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1078 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1079 nir_print_instr(&instr
->instr
, stderr
);
1080 fprintf(stderr
, "\n");
1084 case nir_op_usub_borrow
: {
1085 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1086 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1087 if (dst
.regClass() == s1
) {
1088 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1090 } else if (dst
.regClass() == v1
) {
1091 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1092 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1096 Temp src00
= bld
.tmp(src0
.type(), 1);
1097 Temp src01
= bld
.tmp(dst
.type(), 1);
1098 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1099 Temp src10
= bld
.tmp(src1
.type(), 1);
1100 Temp src11
= bld
.tmp(dst
.type(), 1);
1101 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1102 if (dst
.regClass() == s2
) {
1103 Temp borrow
= bld
.tmp(s1
);
1104 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1105 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1106 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1107 } else if (dst
.regClass() == v2
) {
1108 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1109 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1110 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1111 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1113 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1114 nir_print_instr(&instr
->instr
, stderr
);
1115 fprintf(stderr
, "\n");
1120 if (dst
.regClass() == v1
) {
1121 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1122 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1123 } else if (dst
.regClass() == s1
) {
1124 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1126 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1127 nir_print_instr(&instr
->instr
, stderr
);
1128 fprintf(stderr
, "\n");
1132 case nir_op_umul_high
: {
1133 if (dst
.regClass() == v1
) {
1134 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1135 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1136 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1137 } else if (dst
.regClass() == s1
) {
1138 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1139 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1140 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1142 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1143 nir_print_instr(&instr
->instr
, stderr
);
1144 fprintf(stderr
, "\n");
1148 case nir_op_imul_high
: {
1149 if (dst
.regClass() == v1
) {
1150 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1151 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1152 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1153 } else if (dst
.regClass() == s1
) {
1154 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1155 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1156 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1158 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1159 nir_print_instr(&instr
->instr
, stderr
);
1160 fprintf(stderr
, "\n");
1165 if (dst
.size() == 1) {
1166 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1167 } else if (dst
.size() == 2) {
1168 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1169 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1171 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1172 nir_print_instr(&instr
->instr
, stderr
);
1173 fprintf(stderr
, "\n");
1178 if (dst
.size() == 1) {
1179 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1180 } else if (dst
.size() == 2) {
1181 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1182 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1184 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1185 nir_print_instr(&instr
->instr
, stderr
);
1186 fprintf(stderr
, "\n");
1191 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1192 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1193 if (dst
.size() == 1) {
1194 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1195 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1197 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1198 } else if (dst
.size() == 2) {
1199 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1200 get_alu_src(ctx
, instr
->src
[0]),
1201 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1202 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1205 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1206 nir_print_instr(&instr
->instr
, stderr
);
1207 fprintf(stderr
, "\n");
1213 if (dst
.size() == 1) {
1214 Temp rcp
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[1]));
1215 Temp mul
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]), rcp
);
1217 aco_opcode op
= instr
->op
== nir_op_fmod
? aco_opcode::v_floor_f32
: aco_opcode::v_trunc_f32
;
1218 Temp floor
= bld
.vop1(op
, bld
.def(v1
), mul
);
1220 mul
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[1]), floor
);
1221 bld
.vop2(aco_opcode::v_sub_f32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), mul
);
1222 } else if (dst
.size() == 2) {
1223 Temp rcp
= bld
.vop1(aco_opcode::v_rcp_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[1]));
1224 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[0]), rcp
);
1226 aco_opcode op
= instr
->op
== nir_op_fmod
? aco_opcode::v_floor_f64
: aco_opcode::v_trunc_f64
;
1227 Temp floor
= bld
.vop1(op
, bld
.def(v1
), mul
);
1229 mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[1]), floor
);
1230 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), mul
);
1231 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1234 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1235 nir_print_instr(&instr
->instr
, stderr
);
1236 fprintf(stderr
, "\n");
1241 if (dst
.size() == 1) {
1242 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1243 } else if (dst
.size() == 2) {
1244 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1245 get_alu_src(ctx
, instr
->src
[0]),
1246 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1248 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1249 nir_print_instr(&instr
->instr
, stderr
);
1250 fprintf(stderr
, "\n");
1255 if (dst
.size() == 1) {
1256 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1257 } else if (dst
.size() == 2) {
1258 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1259 get_alu_src(ctx
, instr
->src
[0]),
1260 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1262 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1263 nir_print_instr(&instr
->instr
, stderr
);
1264 fprintf(stderr
, "\n");
1268 case nir_op_fmax3
: {
1269 if (dst
.size() == 1) {
1270 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1272 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1273 nir_print_instr(&instr
->instr
, stderr
);
1274 fprintf(stderr
, "\n");
1278 case nir_op_fmin3
: {
1279 if (dst
.size() == 1) {
1280 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1282 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1283 nir_print_instr(&instr
->instr
, stderr
);
1284 fprintf(stderr
, "\n");
1288 case nir_op_fmed3
: {
1289 if (dst
.size() == 1) {
1290 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1292 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1293 nir_print_instr(&instr
->instr
, stderr
);
1294 fprintf(stderr
, "\n");
1298 case nir_op_umax3
: {
1299 if (dst
.size() == 1) {
1300 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1302 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1303 nir_print_instr(&instr
->instr
, stderr
);
1304 fprintf(stderr
, "\n");
1308 case nir_op_umin3
: {
1309 if (dst
.size() == 1) {
1310 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1312 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1313 nir_print_instr(&instr
->instr
, stderr
);
1314 fprintf(stderr
, "\n");
1318 case nir_op_umed3
: {
1319 if (dst
.size() == 1) {
1320 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1322 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1323 nir_print_instr(&instr
->instr
, stderr
);
1324 fprintf(stderr
, "\n");
1328 case nir_op_imax3
: {
1329 if (dst
.size() == 1) {
1330 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1332 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1333 nir_print_instr(&instr
->instr
, stderr
);
1334 fprintf(stderr
, "\n");
1338 case nir_op_imin3
: {
1339 if (dst
.size() == 1) {
1340 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1342 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1343 nir_print_instr(&instr
->instr
, stderr
);
1344 fprintf(stderr
, "\n");
1348 case nir_op_imed3
: {
1349 if (dst
.size() == 1) {
1350 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1352 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1353 nir_print_instr(&instr
->instr
, stderr
);
1354 fprintf(stderr
, "\n");
1358 case nir_op_cube_face_coord
: {
1359 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1360 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1361 emit_extract_vector(ctx
, in
, 1, v1
),
1362 emit_extract_vector(ctx
, in
, 2, v1
) };
1363 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1364 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1365 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1366 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1367 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1368 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1369 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1372 case nir_op_cube_face_index
: {
1373 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1374 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1375 emit_extract_vector(ctx
, in
, 1, v1
),
1376 emit_extract_vector(ctx
, in
, 2, v1
) };
1377 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1380 case nir_op_bcsel
: {
1381 emit_bcsel(ctx
, instr
, dst
);
1385 if (dst
.size() == 1) {
1386 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f32
, dst
);
1387 } else if (dst
.size() == 2) {
1388 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1390 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1391 nir_print_instr(&instr
->instr
, stderr
);
1392 fprintf(stderr
, "\n");
1397 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1398 if (dst
.size() == 1) {
1399 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1400 } else if (dst
.size() == 2) {
1401 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1402 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1403 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1404 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1406 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1407 nir_print_instr(&instr
->instr
, stderr
);
1408 fprintf(stderr
, "\n");
1413 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1414 if (dst
.size() == 1) {
1415 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1416 } else if (dst
.size() == 2) {
1417 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1418 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1419 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1420 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1422 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1423 nir_print_instr(&instr
->instr
, stderr
);
1424 fprintf(stderr
, "\n");
1429 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1430 if (dst
.size() == 1) {
1431 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1432 } else if (dst
.size() == 2) {
1433 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1434 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1437 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1438 nir_print_instr(&instr
->instr
, stderr
);
1439 fprintf(stderr
, "\n");
1443 case nir_op_flog2
: {
1444 if (dst
.size() == 1) {
1445 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f32
, dst
);
1447 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1448 nir_print_instr(&instr
->instr
, stderr
);
1449 fprintf(stderr
, "\n");
1454 if (dst
.size() == 1) {
1455 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f32
, dst
);
1456 } else if (dst
.size() == 2) {
1457 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1459 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1460 nir_print_instr(&instr
->instr
, stderr
);
1461 fprintf(stderr
, "\n");
1465 case nir_op_fexp2
: {
1466 if (dst
.size() == 1) {
1467 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1469 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1470 nir_print_instr(&instr
->instr
, stderr
);
1471 fprintf(stderr
, "\n");
1475 case nir_op_fsqrt
: {
1476 if (dst
.size() == 1) {
1477 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f32
, dst
);
1478 } else if (dst
.size() == 2) {
1479 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1481 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1482 nir_print_instr(&instr
->instr
, stderr
);
1483 fprintf(stderr
, "\n");
1487 case nir_op_ffract
: {
1488 if (dst
.size() == 1) {
1489 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1490 } else if (dst
.size() == 2) {
1491 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1493 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1494 nir_print_instr(&instr
->instr
, stderr
);
1495 fprintf(stderr
, "\n");
1499 case nir_op_ffloor
: {
1500 if (dst
.size() == 1) {
1501 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1502 } else if (dst
.size() == 2) {
1503 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1505 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1506 nir_print_instr(&instr
->instr
, stderr
);
1507 fprintf(stderr
, "\n");
1511 case nir_op_fceil
: {
1512 if (dst
.size() == 1) {
1513 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1514 } else if (dst
.size() == 2) {
1515 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1517 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1518 nir_print_instr(&instr
->instr
, stderr
);
1519 fprintf(stderr
, "\n");
1523 case nir_op_ftrunc
: {
1524 if (dst
.size() == 1) {
1525 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1526 } else if (dst
.size() == 2) {
1527 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1529 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1530 nir_print_instr(&instr
->instr
, stderr
);
1531 fprintf(stderr
, "\n");
1535 case nir_op_fround_even
: {
1536 if (dst
.size() == 1) {
1537 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1538 } else if (dst
.size() == 2) {
1539 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1541 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1542 nir_print_instr(&instr
->instr
, stderr
);
1543 fprintf(stderr
, "\n");
1549 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1550 aco_ptr
<Instruction
> norm
;
1551 if (dst
.size() == 1) {
1553 Operand
half_pi(0x3e22f983u
);
1554 if (src
.type() == RegType::sgpr
)
1555 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1557 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1559 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1560 if (ctx
->options
->chip_class
< GFX9
)
1561 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1563 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1564 bld
.vop1(opcode
, Definition(dst
), tmp
);
1566 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1567 nir_print_instr(&instr
->instr
, stderr
);
1568 fprintf(stderr
, "\n");
1572 case nir_op_ldexp
: {
1573 if (dst
.size() == 1) {
1574 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1575 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1576 get_alu_src(ctx
, instr
->src
[1]));
1577 } else if (dst
.size() == 2) {
1578 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1579 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1580 get_alu_src(ctx
, instr
->src
[1]));
1582 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1583 nir_print_instr(&instr
->instr
, stderr
);
1584 fprintf(stderr
, "\n");
1588 case nir_op_frexp_sig
: {
1589 if (dst
.size() == 1) {
1590 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1591 get_alu_src(ctx
, instr
->src
[0]));
1592 } else if (dst
.size() == 2) {
1593 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1594 get_alu_src(ctx
, instr
->src
[0]));
1596 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1597 nir_print_instr(&instr
->instr
, stderr
);
1598 fprintf(stderr
, "\n");
1602 case nir_op_frexp_exp
: {
1603 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1604 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1605 get_alu_src(ctx
, instr
->src
[0]));
1606 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1607 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1608 get_alu_src(ctx
, instr
->src
[0]));
1610 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1611 nir_print_instr(&instr
->instr
, stderr
);
1612 fprintf(stderr
, "\n");
1616 case nir_op_fsign
: {
1617 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1618 if (dst
.size() == 1) {
1619 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1620 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1621 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1622 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1623 } else if (dst
.size() == 2) {
1624 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1625 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1626 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, src
, cond
);
1628 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1629 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1630 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1632 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1634 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1635 nir_print_instr(&instr
->instr
, stderr
);
1636 fprintf(stderr
, "\n");
1640 case nir_op_f2f32
: {
1641 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1642 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1644 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1645 nir_print_instr(&instr
->instr
, stderr
);
1646 fprintf(stderr
, "\n");
1650 case nir_op_f2f64
: {
1651 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1652 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1654 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1655 nir_print_instr(&instr
->instr
, stderr
);
1656 fprintf(stderr
, "\n");
1660 case nir_op_i2f32
: {
1661 assert(dst
.size() == 1);
1662 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1665 case nir_op_i2f64
: {
1666 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1667 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1668 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1669 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1670 RegClass rc
= RegClass(src
.type(), 1);
1671 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1672 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1673 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1674 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1675 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1676 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1679 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1680 nir_print_instr(&instr
->instr
, stderr
);
1681 fprintf(stderr
, "\n");
1685 case nir_op_u2f32
: {
1686 assert(dst
.size() == 1);
1687 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1690 case nir_op_u2f64
: {
1691 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1692 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1693 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1694 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1695 RegClass rc
= RegClass(src
.type(), 1);
1696 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1697 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1698 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1699 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1700 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1701 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1703 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1704 nir_print_instr(&instr
->instr
, stderr
);
1705 fprintf(stderr
, "\n");
1709 case nir_op_f2i32
: {
1710 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1711 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1712 if (dst
.type() == RegType::vgpr
)
1713 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1715 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1716 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1718 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1719 if (dst
.type() == RegType::vgpr
)
1720 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1722 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1723 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1726 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1727 nir_print_instr(&instr
->instr
, stderr
);
1728 fprintf(stderr
, "\n");
1732 case nir_op_f2u32
: {
1733 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1734 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1735 if (dst
.type() == RegType::vgpr
)
1736 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1738 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1739 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1741 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1742 if (dst
.type() == RegType::vgpr
)
1743 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1745 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1746 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1749 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1750 nir_print_instr(&instr
->instr
, stderr
);
1751 fprintf(stderr
, "\n");
1755 case nir_op_f2i64
: {
1756 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1757 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1758 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1759 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1760 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1761 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1762 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1763 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1764 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1765 Temp new_exponent
= bld
.tmp(v1
);
1766 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1767 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1768 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1769 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1770 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1771 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1772 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1773 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1774 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1775 Temp new_lower
= bld
.tmp(v1
);
1776 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1777 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1778 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1780 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1781 if (src
.type() == RegType::vgpr
)
1782 src
= bld
.as_uniform(src
);
1783 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1784 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1785 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1786 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1787 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1788 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1789 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1790 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1791 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1792 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1793 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1794 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1795 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1796 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1797 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1798 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1799 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1800 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1801 Temp borrow
= bld
.tmp(s1
);
1802 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1803 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1804 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1806 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1807 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1808 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1809 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1810 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1811 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1812 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1813 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1814 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1815 if (dst
.type() == RegType::sgpr
) {
1816 lower
= bld
.as_uniform(lower
);
1817 upper
= bld
.as_uniform(upper
);
1819 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1822 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1823 nir_print_instr(&instr
->instr
, stderr
);
1824 fprintf(stderr
, "\n");
1828 case nir_op_f2u64
: {
1829 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1830 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1831 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1832 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1833 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1834 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1835 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1836 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1837 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1838 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1839 Temp new_exponent
= bld
.tmp(v1
);
1840 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1841 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1842 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1843 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1844 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1845 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1846 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1847 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1848 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1850 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1851 if (src
.type() == RegType::vgpr
)
1852 src
= bld
.as_uniform(src
);
1853 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1854 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1855 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1856 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1857 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1858 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1859 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1860 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1861 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1862 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1863 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1864 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1865 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1866 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1867 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1868 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1869 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1870 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1872 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1873 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1874 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1875 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1876 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1877 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1878 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1879 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1880 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1881 if (dst
.type() == RegType::sgpr
) {
1882 lower
= bld
.as_uniform(lower
);
1883 upper
= bld
.as_uniform(upper
);
1885 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1888 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1889 nir_print_instr(&instr
->instr
, stderr
);
1890 fprintf(stderr
, "\n");
1894 case nir_op_b2f32
: {
1895 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1896 if (dst
.regClass() == s1
) {
1897 src
= as_uniform_bool(ctx
, src
);
1898 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1899 } else if (dst
.regClass() == v1
) {
1900 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
),
1901 as_divergent_bool(ctx
, src
, true));
1903 unreachable("Wrong destination register class for nir_op_b2f32.");
1907 case nir_op_b2f64
: {
1908 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1909 if (dst
.regClass() == s2
) {
1910 src
= as_uniform_bool(ctx
, src
);
1911 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1912 } else if (dst
.regClass() == v2
) {
1913 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1914 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
,
1915 as_divergent_bool(ctx
, src
, true));
1916 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1918 unreachable("Wrong destination register class for nir_op_b2f64.");
1922 case nir_op_i2i32
: {
1923 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1924 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1925 /* we can actually just say dst = src, as it would map the lower register */
1926 emit_extract_vector(ctx
, src
, 0, dst
);
1928 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1929 nir_print_instr(&instr
->instr
, stderr
);
1930 fprintf(stderr
, "\n");
1934 case nir_op_u2u32
: {
1935 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1936 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
1937 if (dst
.regClass() == s1
) {
1938 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
1940 // TODO: do better with SDWA
1941 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
1943 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1944 /* we can actually just say dst = src, as it would map the lower register */
1945 emit_extract_vector(ctx
, src
, 0, dst
);
1947 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1948 nir_print_instr(&instr
->instr
, stderr
);
1949 fprintf(stderr
, "\n");
1953 case nir_op_i2i64
: {
1954 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1955 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1956 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1958 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1959 nir_print_instr(&instr
->instr
, stderr
);
1960 fprintf(stderr
, "\n");
1964 case nir_op_u2u64
: {
1965 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1966 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1967 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1969 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1970 nir_print_instr(&instr
->instr
, stderr
);
1971 fprintf(stderr
, "\n");
1975 case nir_op_b2i32
: {
1976 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1977 if (dst
.regClass() == s1
) {
1978 if (src
.regClass() == s1
) {
1979 bld
.copy(Definition(dst
), src
);
1981 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
1982 assert(src
.regClass() == s2
);
1983 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
1986 assert(dst
.regClass() == v1
&& src
.regClass() == s2
);
1987 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
1992 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1993 if (dst
.regClass() == s2
) {
1994 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
1995 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
1996 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
1998 assert(src
.regClass() == s1
&& dst
.regClass() == s1
);
1999 bld
.sopc(aco_opcode::s_cmp_lg_u32
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2003 case nir_op_pack_64_2x32_split
: {
2004 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2005 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2007 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2010 case nir_op_unpack_64_2x32_split_x
:
2011 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2013 case nir_op_unpack_64_2x32_split_y
:
2014 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2016 case nir_op_pack_half_2x16
: {
2017 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2019 if (dst
.regClass() == v1
) {
2020 Temp src0
= bld
.tmp(v1
);
2021 Temp src1
= bld
.tmp(v1
);
2022 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2023 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2026 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2027 nir_print_instr(&instr
->instr
, stderr
);
2028 fprintf(stderr
, "\n");
2032 case nir_op_unpack_half_2x16_split_x
: {
2033 if (dst
.regClass() == v1
) {
2034 Builder
bld(ctx
->program
, ctx
->block
);
2035 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2037 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2038 nir_print_instr(&instr
->instr
, stderr
);
2039 fprintf(stderr
, "\n");
2043 case nir_op_unpack_half_2x16_split_y
: {
2044 if (dst
.regClass() == v1
) {
2045 Builder
bld(ctx
->program
, ctx
->block
);
2046 /* TODO: use SDWA here */
2047 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2048 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2050 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2051 nir_print_instr(&instr
->instr
, stderr
);
2052 fprintf(stderr
, "\n");
2056 case nir_op_fquantize2f16
: {
2057 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]));
2059 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2061 Temp cmp_res
= bld
.tmp(s2
);
2062 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2064 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2066 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2070 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2071 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2073 if (dst
.regClass() == s1
) {
2074 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2075 } else if (dst
.regClass() == v1
) {
2076 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2078 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2079 nir_print_instr(&instr
->instr
, stderr
);
2080 fprintf(stderr
, "\n");
2084 case nir_op_bitfield_select
: {
2085 /* (mask & insert) | (~mask & base) */
2086 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2087 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2088 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2090 /* dst = (insert & bitmask) | (base & ~bitmask) */
2091 if (dst
.regClass() == s1
) {
2092 aco_ptr
<Instruction
> sop2
;
2093 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2094 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2096 if (const_insert
&& const_bitmask
) {
2097 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2099 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2100 lhs
= Operand(insert
);
2104 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2105 if (const_base
&& const_bitmask
) {
2106 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2108 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2109 rhs
= Operand(base
);
2112 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2114 } else if (dst
.regClass() == v1
) {
2115 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2116 base
= as_vgpr(ctx
, base
);
2117 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2118 insert
= as_vgpr(ctx
, insert
);
2120 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2123 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2124 nir_print_instr(&instr
->instr
, stderr
);
2125 fprintf(stderr
, "\n");
2131 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2132 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2133 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2135 if (dst
.type() == RegType::sgpr
) {
2137 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2138 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2139 if (const_offset
&& const_bits
) {
2140 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2141 extract
= Operand(const_extract
);
2145 width
= Operand(const_bits
->u32
<< 16);
2147 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2149 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2153 if (dst
.regClass() == s1
) {
2154 if (instr
->op
== nir_op_ubfe
)
2155 opcode
= aco_opcode::s_bfe_u32
;
2157 opcode
= aco_opcode::s_bfe_i32
;
2158 } else if (dst
.regClass() == s2
) {
2159 if (instr
->op
== nir_op_ubfe
)
2160 opcode
= aco_opcode::s_bfe_u64
;
2162 opcode
= aco_opcode::s_bfe_i64
;
2164 unreachable("Unsupported BFE bit size");
2167 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2171 if (dst
.regClass() == v1
) {
2172 if (instr
->op
== nir_op_ubfe
)
2173 opcode
= aco_opcode::v_bfe_u32
;
2175 opcode
= aco_opcode::v_bfe_i32
;
2177 unreachable("Unsupported BFE bit size");
2180 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2184 case nir_op_bit_count
: {
2185 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2186 if (src
.regClass() == s1
) {
2187 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2188 } else if (src
.regClass() == v1
) {
2189 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2190 } else if (src
.regClass() == v2
) {
2191 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2192 emit_extract_vector(ctx
, src
, 1, v1
),
2193 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2194 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2195 } else if (src
.regClass() == s2
) {
2196 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2198 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2199 nir_print_instr(&instr
->instr
, stderr
);
2200 fprintf(stderr
, "\n");
2205 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2206 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f32
, dst
);
2207 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2208 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f64
, dst
);
2212 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2213 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f32
, dst
);
2214 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2215 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f64
, dst
);
2219 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2220 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f32
, dst
);
2221 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2222 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f64
, dst
);
2226 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2227 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f32
, dst
);
2228 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2229 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f64
, dst
);
2233 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2234 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i32
, dst
);
2235 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2236 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_i32
, dst
);
2237 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2238 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i64
, dst
);
2242 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2243 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i32
, dst
);
2244 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2245 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_i32
, dst
);
2246 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2247 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i64
, dst
);
2251 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2252 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i32
, dst
);
2253 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2254 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_i32
, dst
);
2255 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2256 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i64
, dst
);
2257 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2258 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_u64
, dst
);
2259 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2260 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2261 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2262 bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.scc(Definition(dst
)),
2263 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2264 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2265 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2266 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2267 bld
.sop2(aco_opcode::s_xnor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2268 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2270 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2271 nir_print_instr(&instr
->instr
, stderr
);
2272 fprintf(stderr
, "\n");
2277 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2278 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i32
, dst
);
2279 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2280 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i64
, dst
);
2281 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2282 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_i32
, dst
);
2283 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2284 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_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_lg_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_xor_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_lt_u32
, dst
);
2305 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2306 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_u32
, dst
);
2307 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2308 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u64
, dst
);
2312 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2313 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u32
, dst
);
2314 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2315 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_u32
, dst
);
2316 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2317 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u64
, dst
);
2322 case nir_op_fddx_fine
:
2323 case nir_op_fddy_fine
:
2324 case nir_op_fddx_coarse
:
2325 case nir_op_fddy_coarse
: {
2326 Definition tl
= bld
.def(v1
);
2328 if (instr
->op
== nir_op_fddx_fine
) {
2329 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2330 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2331 } else if (instr
->op
== nir_op_fddy_fine
) {
2332 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2333 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2335 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2336 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2337 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2339 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2342 Definition tmp
= bld
.def(v1
);
2343 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2344 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2348 fprintf(stderr
, "Unknown NIR ALU instr: ");
2349 nir_print_instr(&instr
->instr
, stderr
);
2350 fprintf(stderr
, "\n");
2354 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2356 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2358 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2359 // which get truncated the lsb if double and msb if int
2360 // for now, we only use s_mov_b64 with 64bit inline constants
2361 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2362 assert(dst
.type() == RegType::sgpr
);
2364 if (dst
.size() == 1)
2366 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2368 assert(dst
.size() != 1);
2369 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2370 if (instr
->def
.bit_size
== 64)
2371 for (unsigned i
= 0; i
< dst
.size(); i
++)
2372 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2374 for (unsigned i
= 0; i
< dst
.size(); i
++)
2375 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2377 vec
->definitions
[0] = Definition(dst
);
2378 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2382 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2384 uint32_t new_mask
= 0;
2385 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2386 if (mask
& (1u << i
))
2387 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2391 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2393 /* This wouldn't work inside control flow or with indirect offsets but
2394 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2396 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2397 unsigned component
= nir_intrinsic_component(instr
);
2398 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2399 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2401 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2402 if (off_instr
->type
!= nir_instr_type_load_const
) {
2403 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2404 nir_print_instr(off_instr
, stderr
);
2405 fprintf(stderr
, "\n");
2407 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2409 if (instr
->src
[0].ssa
->bit_size
== 64)
2410 write_mask
= widen_mask(write_mask
, 2);
2412 for (unsigned i
= 0; i
< 8; ++i
) {
2413 if (write_mask
& (1 << i
)) {
2414 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2415 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2421 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2423 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2425 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2426 for (unsigned i
= 0; i
< 4; ++i
) {
2427 if (write_mask
& (1 << i
)) {
2428 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2429 values
[i
] = Operand(tmp
);
2431 values
[i
] = Operand(v1
);
2435 unsigned index
= nir_intrinsic_base(instr
) / 4;
2436 unsigned target
, col_format
;
2437 unsigned enabled_channels
= 0xF;
2438 aco_opcode compr_op
= (aco_opcode
)0;
2440 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2441 assert(offset
&& "Non-const offsets on exports not yet supported");
2442 index
+= offset
->u32
;
2444 assert(index
!= FRAG_RESULT_COLOR
);
2446 /* Unlike vertex shader exports, it's fine to use multiple exports to
2447 * export separate channels of one target. So shaders which export both
2448 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2449 * TODO: combine the exports in those cases and create better code
2452 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2454 if (ctx
->program
->info
->ps
.writes_z
) {
2455 target
= V_008DFC_SQ_EXP_MRTZ
;
2456 enabled_channels
= 0x4;
2457 col_format
= (unsigned) -1;
2459 values
[2] = values
[0];
2460 values
[0] = Operand(v1
);
2462 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2463 exp
->valid_mask
= false;
2465 exp
->compressed
= true;
2466 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2467 exp
->enabled_mask
= 0xc;
2468 for (int i
= 0; i
< 4; i
++)
2469 exp
->operands
[i
] = Operand(v1
);
2470 exp
->operands
[1] = Operand(values
[0]);
2471 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2475 } else if (index
== FRAG_RESULT_DEPTH
) {
2477 target
= V_008DFC_SQ_EXP_MRTZ
;
2478 enabled_channels
= 0x1;
2479 col_format
= (unsigned) -1;
2481 } else if (index
== FRAG_RESULT_STENCIL
) {
2483 if (ctx
->program
->info
->ps
.writes_z
) {
2484 target
= V_008DFC_SQ_EXP_MRTZ
;
2485 enabled_channels
= 0x2;
2486 col_format
= (unsigned) -1;
2488 values
[1] = values
[0];
2489 values
[0] = Operand(v1
);
2491 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2492 shift
->operands
[0] = Operand((uint32_t) 16);
2493 shift
->operands
[1] = values
[0];
2494 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2495 shift
->definitions
[0] = Definition(tmp
);
2496 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2498 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2499 exp
->valid_mask
= false;
2501 exp
->compressed
= true;
2502 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2503 exp
->enabled_mask
= 0x3;
2504 exp
->operands
[0] = Operand(tmp
);
2505 for (int i
= 1; i
< 4; i
++)
2506 exp
->operands
[i
] = Operand(v1
);
2507 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2512 index
-= FRAG_RESULT_DATA0
;
2513 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2514 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2516 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2517 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2518 assert(!is_int8
&& !is_int10
);
2522 case V_028714_SPI_SHADER_ZERO
:
2523 enabled_channels
= 0; /* writemask */
2524 target
= V_008DFC_SQ_EXP_NULL
;
2527 case V_028714_SPI_SHADER_32_R
:
2528 enabled_channels
= 1;
2531 case V_028714_SPI_SHADER_32_GR
:
2532 enabled_channels
= 0x3;
2535 case V_028714_SPI_SHADER_32_AR
:
2536 enabled_channels
= 0x9;
2539 case V_028714_SPI_SHADER_FP16_ABGR
:
2540 enabled_channels
= 0x5;
2541 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2544 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2545 enabled_channels
= 0x5;
2546 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2549 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2550 enabled_channels
= 0x5;
2551 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2554 case V_028714_SPI_SHADER_UINT16_ABGR
:
2555 enabled_channels
= 0x5;
2556 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2559 case V_028714_SPI_SHADER_SINT16_ABGR
:
2560 enabled_channels
= 0x5;
2561 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2564 case V_028714_SPI_SHADER_32_ABGR
:
2565 enabled_channels
= 0xF;
2572 if (target
== V_008DFC_SQ_EXP_NULL
)
2577 for (int i
= 0; i
< 2; i
++)
2579 /* check if at least one of the values to be compressed is enabled */
2580 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2582 enabled_channels
|= enabled
<< (i
*2);
2583 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2584 Temp tmp
{ctx
->program
->allocateId(), v1
};
2585 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2586 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2587 compr
->definitions
[0] = Definition(tmp
);
2588 values
[i
] = Operand(tmp
);
2589 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2591 values
[i
] = Operand(v1
);
2596 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2597 exp
->valid_mask
= false;
2599 exp
->compressed
= (bool) compr_op
;
2601 exp
->enabled_mask
= enabled_channels
;
2602 if ((bool) compr_op
) {
2603 for (int i
= 0; i
< 2; i
++)
2604 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2605 exp
->operands
[2] = Operand(v1
);
2606 exp
->operands
[3] = Operand(v1
);
2608 for (int i
= 0; i
< 4; i
++)
2609 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2612 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2615 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2617 if (ctx
->stage
== vertex_vs
) {
2618 visit_store_vs_output(ctx
, instr
);
2619 } else if (ctx
->stage
== fragment_fs
) {
2620 visit_store_fs_output(ctx
, instr
);
2622 unreachable("Shader stage not implemented");
2626 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2628 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2629 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2631 Builder
bld(ctx
->program
, ctx
->block
);
2632 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2633 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2636 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2638 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2639 for (unsigned i
= 0; i
< num_components
; i
++)
2640 vec
->operands
[i
] = Operand(ctx
->fs_inputs
[fs_input::frag_pos_0
+ i
]);
2642 if (ctx
->fs_vgpr_args
[fs_input::frag_pos_3
]) {
2643 assert(num_components
== 4);
2644 Builder
bld(ctx
->program
, ctx
->block
);
2645 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::frag_pos_3
]);
2648 for (Operand
& op
: vec
->operands
)
2649 op
= op
.isUndefined() ? Operand(0u) : op
;
2651 vec
->definitions
[0] = Definition(dst
);
2652 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2653 emit_split_vector(ctx
, dst
, num_components
);
2657 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2659 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2660 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2661 unsigned idx
= nir_intrinsic_base(instr
);
2662 unsigned component
= nir_intrinsic_component(instr
);
2663 Temp prim_mask
= ctx
->prim_mask
;
2665 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2667 assert(offset
->u32
== 0);
2669 /* the lower 15bit of the prim_mask contain the offset into LDS
2670 * while the upper bits contain the number of prims */
2671 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2672 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2673 Builder
bld(ctx
->program
, ctx
->block
);
2674 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2675 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2676 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2677 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2678 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2681 if (instr
->dest
.ssa
.num_components
== 1) {
2682 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2684 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2685 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2687 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2688 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2689 vec
->operands
[i
] = Operand(tmp
);
2691 vec
->definitions
[0] = Definition(dst
);
2692 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2696 unsigned get_num_channels_from_data_format(unsigned data_format
)
2698 switch (data_format
) {
2699 case V_008F0C_BUF_DATA_FORMAT_8
:
2700 case V_008F0C_BUF_DATA_FORMAT_16
:
2701 case V_008F0C_BUF_DATA_FORMAT_32
:
2703 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2704 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2705 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2707 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2708 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2709 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2711 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2712 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2713 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2714 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2715 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2724 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2725 * so we may need to fix it up. */
2726 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
2728 Builder
bld(ctx
->program
, ctx
->block
);
2730 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
2731 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
2733 /* For the integer-like cases, do a natural sign extension.
2735 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
2736 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
2739 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
2740 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
2742 /* Convert back to the right type. */
2743 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
2744 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
2745 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
2746 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
2747 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
2748 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
2754 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2756 Builder
bld(ctx
->program
, ctx
->block
);
2757 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2758 if (ctx
->stage
& sw_vs
) {
2760 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
2761 if (off_instr
->type
!= nir_instr_type_load_const
) {
2762 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2763 nir_print_instr(off_instr
, stderr
);
2764 fprintf(stderr
, "\n");
2766 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
2768 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, ctx
->vertex_buffers
);
2770 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
2771 unsigned component
= nir_intrinsic_component(instr
);
2772 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
2773 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
2774 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
2775 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
2777 unsigned dfmt
= attrib_format
& 0xf;
2779 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
2780 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
2781 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
2782 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
2783 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
2784 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
2786 num_channels
= MAX2(num_channels
, 3);
2788 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
2791 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
2792 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
2794 ctx
->needs_instance_id
= true;
2797 Temp divided
= bld
.tmp(v1
);
2798 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, ctx
->instance_id
), divisor
);
2799 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, divided
);
2801 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, ctx
->instance_id
);
2804 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), ctx
->start_instance
);
2807 index
= bld
.vadd32(bld
.def(v1
), ctx
->base_vertex
, ctx
->vertex_id
);
2810 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
2811 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
2812 attrib_offset
= attrib_offset
% attrib_stride
;
2815 Operand
soffset(0u);
2816 if (attrib_offset
>= 4096) {
2817 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
2822 switch (num_channels
) {
2824 opcode
= aco_opcode::tbuffer_load_format_x
;
2827 opcode
= aco_opcode::tbuffer_load_format_xy
;
2830 opcode
= aco_opcode::tbuffer_load_format_xyz
;
2833 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
2836 unreachable("Unimplemented load_input vector size");
2839 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
2841 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
2842 mubuf
->operands
[0] = Operand(index
);
2843 mubuf
->operands
[1] = Operand(list
);
2844 mubuf
->operands
[2] = soffset
;
2845 mubuf
->definitions
[0] = Definition(tmp
);
2846 mubuf
->idxen
= true;
2847 mubuf
->can_reorder
= true;
2850 assert(attrib_offset
< 4096);
2851 mubuf
->offset
= attrib_offset
;
2852 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
2854 emit_split_vector(ctx
, tmp
, tmp
.size());
2856 if (tmp
.id() != dst
.id()) {
2857 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
2858 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
2860 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
2861 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
2862 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
2864 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2865 for (unsigned i
= 0; i
< dst
.size(); i
++) {
2866 unsigned idx
= i
+ component
;
2867 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
2868 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
2869 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
2870 } else if (idx
< num_channels
) {
2871 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
2872 } else if (is_float
&& idx
== 3) {
2873 vec
->operands
[i
] = Operand(0x3f800000u
);
2874 } else if (!is_float
&& idx
== 3) {
2875 vec
->operands
[i
] = Operand(1u);
2877 vec
->operands
[i
] = Operand(0u);
2880 vec
->definitions
[0] = Definition(dst
);
2881 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2882 emit_split_vector(ctx
, dst
, dst
.size());
2885 } else if (ctx
->stage
== fragment_fs
) {
2886 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
2887 if (off_instr
->type
!= nir_instr_type_load_const
||
2888 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
2889 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2890 nir_print_instr(off_instr
, stderr
);
2891 fprintf(stderr
, "\n");
2894 Temp prim_mask
= ctx
->prim_mask
;
2895 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
2897 assert(offset
->u32
== 0);
2899 /* the lower 15bit of the prim_mask contain the offset into LDS
2900 * while the upper bits contain the number of prims */
2901 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2902 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2903 Builder
bld(ctx
->program
, ctx
->block
);
2904 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2905 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2906 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2907 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2908 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2911 unsigned idx
= nir_intrinsic_base(instr
);
2912 unsigned component
= nir_intrinsic_component(instr
);
2914 if (dst
.size() == 1) {
2915 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
2917 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2918 for (unsigned i
= 0; i
< dst
.size(); i
++)
2919 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
2920 vec
->definitions
[0] = Definition(dst
);
2921 bld
.insert(std::move(vec
));
2925 unreachable("Shader stage not implemented");
2929 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
2931 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
2932 Builder
bld(ctx
->program
, ctx
->block
);
2933 Temp ptr64
= convert_pointer_to_64_bit(ctx
, ctx
->descriptor_sets
[0]);
2934 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
2937 return ctx
->descriptor_sets
[desc_set
];
2941 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2943 Builder
bld(ctx
->program
, ctx
->block
);
2944 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
2945 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2946 unsigned binding
= nir_intrinsic_binding(instr
);
2949 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2950 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2951 unsigned offset
= layout
->binding
[binding
].offset
;
2953 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2954 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2955 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
2956 desc_ptr
= ctx
->push_constants
;
2957 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2960 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
2961 stride
= layout
->binding
[binding
].size
;
2964 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
2965 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
2967 if (nir_const_index
) {
2968 const_index
= const_index
* stride
;
2970 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
2974 if (nir_const_index
) {
2975 const_index
= const_index
+ offset
;
2977 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
2981 if (nir_const_index
&& const_index
== 0) {
2984 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
2985 nir_const_index
? Operand(const_index
) : Operand(index
),
2989 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2990 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), index
);
2993 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
, Temp rsrc
, Temp offset
, bool glc
=false)
2995 Builder
bld(ctx
->program
, ctx
->block
);
2997 unsigned num_bytes
= dst
.size() * 4;
3000 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3001 if (ctx
->options
->chip_class
< GFX8
)
3002 offset
= as_vgpr(ctx
, offset
);
3004 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3005 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3006 unsigned const_offset
= 0;
3008 Temp lower
= Temp();
3009 if (num_bytes
> 16) {
3010 assert(num_components
== 3 || num_components
== 4);
3011 op
= aco_opcode::buffer_load_dwordx4
;
3012 lower
= bld
.tmp(v4
);
3013 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3014 mubuf
->definitions
[0] = Definition(lower
);
3015 mubuf
->operands
[0] = vaddr
;
3016 mubuf
->operands
[1] = Operand(rsrc
);
3017 mubuf
->operands
[2] = soffset
;
3018 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3020 mubuf
->barrier
= barrier_buffer
;
3021 bld
.insert(std::move(mubuf
));
3022 emit_split_vector(ctx
, lower
, 2);
3027 switch (num_bytes
) {
3029 op
= aco_opcode::buffer_load_dword
;
3032 op
= aco_opcode::buffer_load_dwordx2
;
3035 op
= aco_opcode::buffer_load_dwordx3
;
3038 op
= aco_opcode::buffer_load_dwordx4
;
3041 unreachable("Load SSBO not implemented for this size.");
3043 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3044 mubuf
->operands
[0] = vaddr
;
3045 mubuf
->operands
[1] = Operand(rsrc
);
3046 mubuf
->operands
[2] = soffset
;
3047 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3049 mubuf
->barrier
= barrier_buffer
;
3050 mubuf
->offset
= const_offset
;
3051 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3053 if (dst
.size() > 4) {
3054 assert(lower
!= Temp());
3055 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3056 instr
->definitions
[0] = Definition(upper
);
3057 bld
.insert(std::move(instr
));
3058 if (dst
.size() == 8)
3059 emit_split_vector(ctx
, upper
, 2);
3060 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3061 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3062 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3063 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3064 if (dst
.size() == 8)
3065 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3068 if (dst
.type() == RegType::sgpr
) {
3069 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3070 instr
->definitions
[0] = Definition(vec
);
3071 bld
.insert(std::move(instr
));
3072 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3074 instr
->definitions
[0] = Definition(dst
);
3075 bld
.insert(std::move(instr
));
3078 switch (num_bytes
) {
3080 op
= aco_opcode::s_buffer_load_dword
;
3083 op
= aco_opcode::s_buffer_load_dwordx2
;
3087 op
= aco_opcode::s_buffer_load_dwordx4
;
3091 op
= aco_opcode::s_buffer_load_dwordx8
;
3094 unreachable("Load SSBO not implemented for this size.");
3096 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3097 load
->operands
[0] = Operand(rsrc
);
3098 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3099 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3100 load
->definitions
[0] = Definition(dst
);
3102 load
->barrier
= barrier_buffer
;
3103 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3106 if (dst
.size() == 3) {
3107 Temp vec
= bld
.tmp(s4
);
3108 load
->definitions
[0] = Definition(vec
);
3109 bld
.insert(std::move(load
));
3110 emit_split_vector(ctx
, vec
, 4);
3112 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3113 emit_extract_vector(ctx
, vec
, 0, s1
),
3114 emit_extract_vector(ctx
, vec
, 1, s1
),
3115 emit_extract_vector(ctx
, vec
, 2, s1
));
3116 } else if (dst
.size() == 6) {
3117 Temp vec
= bld
.tmp(s8
);
3118 load
->definitions
[0] = Definition(vec
);
3119 bld
.insert(std::move(load
));
3120 emit_split_vector(ctx
, vec
, 4);
3122 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3123 emit_extract_vector(ctx
, vec
, 0, s2
),
3124 emit_extract_vector(ctx
, vec
, 1, s2
),
3125 emit_extract_vector(ctx
, vec
, 2, s2
));
3127 bld
.insert(std::move(load
));
3131 emit_split_vector(ctx
, dst
, num_components
);
3134 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3136 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3137 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3139 Builder
bld(ctx
->program
, ctx
->block
);
3141 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3142 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3143 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3144 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3146 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3147 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3148 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3149 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3150 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
3151 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3152 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3153 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3154 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3155 Operand(0xFFFFFFFFu
),
3156 Operand(desc_type
));
3157 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3158 rsrc
, upper_dwords
);
3160 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3161 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3164 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3167 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3169 Builder
bld(ctx
->program
, ctx
->block
);
3170 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3172 unsigned offset
= nir_intrinsic_base(instr
);
3173 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3174 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3176 unsigned count
= instr
->dest
.ssa
.num_components
;
3177 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3178 start
-= ctx
->base_inline_push_consts
;
3179 if (start
+ count
<= ctx
->num_inline_push_consts
) {
3180 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3181 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3182 for (unsigned i
= 0; i
< count
; ++i
) {
3183 elems
[i
] = ctx
->inline_push_consts
[start
+ i
];
3184 vec
->operands
[i
] = Operand
{elems
[i
]};
3186 vec
->definitions
[0] = Definition(dst
);
3187 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3188 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3193 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3194 if (offset
!= 0) // TODO check if index != 0 as well
3195 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3196 Temp ptr
= convert_pointer_to_64_bit(ctx
, ctx
->push_constants
);
3201 switch (dst
.size()) {
3203 op
= aco_opcode::s_load_dword
;
3206 op
= aco_opcode::s_load_dwordx2
;
3212 op
= aco_opcode::s_load_dwordx4
;
3218 op
= aco_opcode::s_load_dwordx8
;
3221 unreachable("unimplemented or forbidden load_push_constant.");
3224 bld
.smem(op
, Definition(vec
), ptr
, index
);
3227 emit_split_vector(ctx
, vec
, 4);
3228 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3229 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3230 emit_extract_vector(ctx
, vec
, 0, rc
),
3231 emit_extract_vector(ctx
, vec
, 1, rc
),
3232 emit_extract_vector(ctx
, vec
, 2, rc
));
3235 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3238 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3240 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3242 Builder
bld(ctx
->program
, ctx
->block
);
3244 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3245 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3246 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3247 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3248 if (ctx
->options
->chip_class
>= GFX10
) {
3249 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3250 S_008F0C_OOB_SELECT(3) |
3251 S_008F0C_RESOURCE_LEVEL(1);
3253 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3254 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3257 unsigned base
= nir_intrinsic_base(instr
) + ctx
->constant_data_offset
;
3258 unsigned range
= nir_intrinsic_range(instr
);
3260 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3261 if (base
&& offset
.type() == RegType::sgpr
)
3262 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3263 else if (base
&& offset
.type() == RegType::vgpr
)
3264 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3266 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3267 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u)),
3268 Operand(MIN2(range
, ctx
->shader
->constant_data_size
- nir_intrinsic_base(instr
))),
3269 Operand(desc_type
));
3271 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3274 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3276 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3277 ctx
->cf_info
.exec_potentially_empty
= true;
3279 ctx
->program
->needs_exact
= true;
3281 Builder
bld(ctx
->program
, ctx
->block
);
3282 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
3283 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3284 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3285 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3289 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3291 Builder
bld(ctx
->program
, ctx
->block
);
3293 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3294 ctx
->cf_info
.exec_potentially_empty
= true;
3296 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3297 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3299 if (ctx
->block
->loop_nest_depth
&&
3300 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3301 /* we handle discards the same way as jump instructions */
3302 append_logical_end(ctx
->block
);
3304 /* in loops, discard behaves like break */
3305 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3306 ctx
->block
->kind
|= block_kind_discard
;
3309 /* uniform discard - loop ends here */
3310 assert(nir_instr_is_last(&instr
->instr
));
3311 ctx
->block
->kind
|= block_kind_uniform
;
3312 ctx
->cf_info
.has_branch
= true;
3313 bld
.branch(aco_opcode::p_branch
);
3314 add_linear_edge(ctx
->block
->index
, linear_target
);
3318 /* we add a break right behind the discard() instructions */
3319 ctx
->block
->kind
|= block_kind_break
;
3320 unsigned idx
= ctx
->block
->index
;
3322 /* remove critical edges from linear CFG */
3323 bld
.branch(aco_opcode::p_branch
);
3324 Block
* break_block
= ctx
->program
->create_and_insert_block();
3325 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3326 break_block
->kind
|= block_kind_uniform
;
3327 add_linear_edge(idx
, break_block
);
3328 add_linear_edge(break_block
->index
, linear_target
);
3329 bld
.reset(break_block
);
3330 bld
.branch(aco_opcode::p_branch
);
3332 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3333 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3334 add_linear_edge(idx
, continue_block
);
3335 append_logical_start(continue_block
);
3336 ctx
->block
= continue_block
;
3341 /* it can currently happen that NIR doesn't remove the unreachable code */
3342 if (!nir_instr_is_last(&instr
->instr
)) {
3343 ctx
->program
->needs_exact
= true;
3344 /* save exec somewhere temporarily so that it doesn't get
3345 * overwritten before the discard from outer exec masks */
3346 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3347 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3348 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3352 /* This condition is incorrect for uniformly branched discards in a loop
3353 * predicated by a divergent condition, but the above code catches that case
3354 * and the discard would end up turning into a discard_if.
3364 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3365 /* program just ends here */
3366 ctx
->block
->kind
|= block_kind_uniform
;
3367 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3368 0 /* enabled mask */, 9 /* dest */,
3369 false /* compressed */, true/* done */, true /* valid mask */);
3370 bld
.sopp(aco_opcode::s_endpgm
);
3371 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3373 ctx
->block
->kind
|= block_kind_discard
;
3374 /* branch and linear edge is added by visit_if() */
3378 enum aco_descriptor_type
{
3388 enum aco_image_dim
{
3392 aco_image_cube
, // includes cube arrays
3396 aco_image_2darraymsaa
,
3399 static enum aco_image_dim
3400 get_sampler_dim(isel_context
*ctx
, enum glsl_sampler_dim dim
, bool is_array
)
3403 case GLSL_SAMPLER_DIM_1D
:
3404 if (ctx
->options
->chip_class
>= GFX9
)
3405 return is_array
? aco_image_2darray
: aco_image_2d
;
3406 return is_array
? aco_image_1darray
: aco_image_1d
;
3407 case GLSL_SAMPLER_DIM_2D
:
3408 case GLSL_SAMPLER_DIM_RECT
:
3409 case GLSL_SAMPLER_DIM_EXTERNAL
:
3410 return is_array
? aco_image_2darray
: aco_image_2d
;
3411 case GLSL_SAMPLER_DIM_3D
:
3412 return aco_image_3d
;
3413 case GLSL_SAMPLER_DIM_CUBE
:
3414 return aco_image_cube
;
3415 case GLSL_SAMPLER_DIM_MS
:
3416 return is_array
? aco_image_2darraymsaa
: aco_image_2dmsaa
;
3417 case GLSL_SAMPLER_DIM_SUBPASS
:
3418 return aco_image_2darray
;
3419 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3420 return aco_image_2darraymsaa
;
3422 unreachable("bad sampler dim");
3427 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3428 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3430 aco_image_dim dim
= get_sampler_dim(ctx
, sampler_dim
, is_array
);
3431 return dim
== aco_image_cube
||
3432 dim
== aco_image_1darray
||
3433 dim
== aco_image_2darray
||
3434 dim
== aco_image_2darraymsaa
;
3437 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3438 enum aco_descriptor_type desc_type
,
3439 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3441 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3442 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3443 if (it != ctx->tex_desc.end())
3446 Temp index
= Temp();
3447 bool index_set
= false;
3448 unsigned constant_index
= 0;
3449 unsigned descriptor_set
;
3450 unsigned base_index
;
3451 Builder
bld(ctx
->program
, ctx
->block
);
3454 assert(tex_instr
&& !image
);
3456 base_index
= tex_instr
->sampler_index
;
3458 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3459 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3463 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3464 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3466 constant_index
+= array_size
* const_value
->u32
;
3468 Temp indirect
= bld
.as_uniform(get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
));
3470 if (array_size
!= 1)
3471 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3477 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3481 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3483 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3484 base_index
= deref_instr
->var
->data
.binding
;
3487 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3488 list
= convert_pointer_to_64_bit(ctx
, list
);
3490 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3491 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3492 unsigned offset
= binding
->offset
;
3493 unsigned stride
= binding
->size
;
3497 assert(base_index
< layout
->binding_count
);
3499 switch (desc_type
) {
3500 case ACO_DESC_IMAGE
:
3502 opcode
= aco_opcode::s_load_dwordx8
;
3504 case ACO_DESC_FMASK
:
3506 opcode
= aco_opcode::s_load_dwordx8
;
3509 case ACO_DESC_SAMPLER
:
3511 opcode
= aco_opcode::s_load_dwordx4
;
3512 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3513 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3515 case ACO_DESC_BUFFER
:
3517 opcode
= aco_opcode::s_load_dwordx4
;
3519 case ACO_DESC_PLANE_0
:
3520 case ACO_DESC_PLANE_1
:
3522 opcode
= aco_opcode::s_load_dwordx8
;
3523 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3525 case ACO_DESC_PLANE_2
:
3527 opcode
= aco_opcode::s_load_dwordx4
;
3531 unreachable("invalid desc_type\n");
3534 offset
+= constant_index
* stride
;
3536 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3537 (!index_set
|| binding
->immutable_samplers_equal
)) {
3538 if (binding
->immutable_samplers_equal
)
3541 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3542 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3543 Operand(samplers
[constant_index
* 4 + 0]),
3544 Operand(samplers
[constant_index
* 4 + 1]),
3545 Operand(samplers
[constant_index
* 4 + 2]),
3546 Operand(samplers
[constant_index
* 4 + 3]));
3551 off
= Operand(offset
);
3553 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3554 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3557 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3559 if (desc_type
== ACO_DESC_PLANE_2
) {
3561 for (unsigned i
= 0; i
< 8; i
++)
3562 components
[i
] = bld
.tmp(s1
);
3563 bld
.pseudo(aco_opcode::p_split_vector
,
3564 Definition(components
[0]),
3565 Definition(components
[1]),
3566 Definition(components
[2]),
3567 Definition(components
[3]),
3570 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3571 bld
.pseudo(aco_opcode::p_split_vector
,
3572 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3573 Definition(components
[4]),
3574 Definition(components
[5]),
3575 Definition(components
[6]),
3576 Definition(components
[7]),
3579 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3580 components
[0], components
[1], components
[2], components
[3],
3581 components
[4], components
[5], components
[6], components
[7]);
3587 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3590 case GLSL_SAMPLER_DIM_BUF
:
3592 case GLSL_SAMPLER_DIM_1D
:
3593 return array
? 2 : 1;
3594 case GLSL_SAMPLER_DIM_2D
:
3595 return array
? 3 : 2;
3596 case GLSL_SAMPLER_DIM_MS
:
3597 return array
? 4 : 3;
3598 case GLSL_SAMPLER_DIM_3D
:
3599 case GLSL_SAMPLER_DIM_CUBE
:
3601 case GLSL_SAMPLER_DIM_RECT
:
3602 case GLSL_SAMPLER_DIM_SUBPASS
:
3604 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3613 /* Adjust the sample index according to FMASK.
3615 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3616 * which is the identity mapping. Each nibble says which physical sample
3617 * should be fetched to get that sample.
3619 * For example, 0x11111100 means there are only 2 samples stored and
3620 * the second sample covers 3/4 of the pixel. When reading samples 0
3621 * and 1, return physical sample 0 (determined by the first two 0s
3622 * in FMASK), otherwise return physical sample 1.
3624 * The sample index should be adjusted as follows:
3625 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3627 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3629 Builder
bld(ctx
->program
, ctx
->block
);
3630 Temp fmask
= bld
.tmp(v1
);
3632 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3633 load
->operands
[0] = Operand(coords
);
3634 load
->operands
[1] = Operand(fmask_desc_ptr
);
3635 load
->definitions
[0] = Definition(fmask
);
3640 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3641 ctx
->block
->instructions
.emplace_back(std::move(load
));
3643 Operand sample_index4
;
3644 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3645 sample_index4
= Operand(sample_index
.constantValue() << 2);
3646 } else if (sample_index
.regClass() == s1
) {
3647 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3649 assert(sample_index
.regClass() == v1
);
3650 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3654 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3655 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3656 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3657 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3659 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3661 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3662 * resource descriptor is 0 (invalid),
3664 Temp compare
= bld
.tmp(s2
);
3665 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3666 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3668 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3670 /* Replace the MSAA sample index. */
3671 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3674 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3677 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3678 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3679 bool is_array
= glsl_sampler_type_is_array(type
);
3680 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3681 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3682 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3683 bool gfx9_1d
= ctx
->options
->chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3684 int count
= image_type_to_components_count(dim
, is_array
);
3685 std::vector
<Operand
> coords(count
);
3688 Operand sample_index
;
3689 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3691 sample_index
= Operand(sample_cv
->u32
);
3693 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3695 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3696 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3697 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3698 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3699 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3700 vec
->definitions
[0] = Definition(fmask_load_address
);
3701 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3703 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3704 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3707 coords
[count
] = sample_index
;
3710 if (count
== 1 && !gfx9_1d
)
3711 return emit_extract_vector(ctx
, src0
, 0, v1
);
3714 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3715 coords
.resize(coords
.size() + 1);
3716 coords
[1] = Operand((uint32_t) 0);
3718 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3720 for (int i
= 0; i
< count
; i
++)
3721 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3724 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3725 for (unsigned i
= 0; i
< coords
.size(); i
++)
3726 vec
->operands
[i
] = coords
[i
];
3727 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3728 vec
->definitions
[0] = Definition(res
);
3729 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3734 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3736 Builder
bld(ctx
->program
, ctx
->block
);
3737 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3738 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3739 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3740 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3742 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3743 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
3744 unsigned num_channels
= util_last_bit(mask
);
3745 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3746 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3749 switch (num_channels
) {
3751 opcode
= aco_opcode::buffer_load_format_x
;
3754 opcode
= aco_opcode::buffer_load_format_xy
;
3757 opcode
= aco_opcode::buffer_load_format_xyz
;
3760 opcode
= aco_opcode::buffer_load_format_xyzw
;
3763 unreachable(">4 channel buffer image load");
3765 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
3766 load
->operands
[0] = Operand(vindex
);
3767 load
->operands
[1] = Operand(rsrc
);
3768 load
->operands
[2] = Operand((uint32_t) 0);
3770 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
3773 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
3774 load
->definitions
[0] = Definition(tmp
);
3776 load
->barrier
= barrier_image
;
3777 ctx
->block
->instructions
.emplace_back(std::move(load
));
3779 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
3783 Temp coords
= get_image_coords(ctx
, instr
, type
);
3784 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3785 //aco_image_dim img_dim = get_image_dim(ctx, glsl_get_sampler_dim(type), glsl_sampler_type_is_array(type));
3787 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
3788 unsigned num_components
= util_bitcount(dmask
);
3790 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
3793 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
3795 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3796 load
->operands
[0] = Operand(coords
);
3797 load
->operands
[1] = Operand(resource
);
3798 load
->definitions
[0] = Definition(tmp
);
3799 load
->glc
= var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
3800 load
->dmask
= dmask
;
3802 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3803 load
->barrier
= barrier_image
;
3804 ctx
->block
->instructions
.emplace_back(std::move(load
));
3806 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
3810 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3812 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3813 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3814 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3815 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
3817 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
3819 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3820 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3821 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3823 switch (data
.size()) {
3825 opcode
= aco_opcode::buffer_store_format_x
;
3828 opcode
= aco_opcode::buffer_store_format_xy
;
3831 opcode
= aco_opcode::buffer_store_format_xyz
;
3834 opcode
= aco_opcode::buffer_store_format_xyzw
;
3837 unreachable(">4 channel buffer image store");
3839 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
3840 store
->operands
[0] = Operand(vindex
);
3841 store
->operands
[1] = Operand(rsrc
);
3842 store
->operands
[2] = Operand((uint32_t) 0);
3843 store
->operands
[3] = Operand(data
);
3844 store
->idxen
= true;
3846 store
->disable_wqm
= true;
3847 store
->barrier
= barrier_image
;
3848 ctx
->program
->needs_exact
= true;
3849 ctx
->block
->instructions
.emplace_back(std::move(store
));
3853 assert(data
.type() == RegType::vgpr
);
3854 Temp coords
= get_image_coords(ctx
, instr
, type
);
3855 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3857 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
3858 store
->operands
[0] = Operand(coords
);
3859 store
->operands
[1] = Operand(resource
);
3860 store
->operands
[2] = Operand(s4
);
3861 store
->operands
[3] = Operand(data
);
3863 store
->dmask
= (1 << data
.size()) - 1;
3865 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3866 store
->disable_wqm
= true;
3867 store
->barrier
= barrier_image
;
3868 ctx
->program
->needs_exact
= true;
3869 ctx
->block
->instructions
.emplace_back(std::move(store
));
3873 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3875 /* return the previous value if dest is ever used */
3876 bool return_previous
= false;
3877 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
3878 return_previous
= true;
3881 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
3882 return_previous
= true;
3886 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3887 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3888 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3889 Builder
bld(ctx
->program
, ctx
->block
);
3891 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
3892 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
3894 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
3895 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
3897 aco_opcode buf_op
, image_op
;
3898 switch (instr
->intrinsic
) {
3899 case nir_intrinsic_image_deref_atomic_add
:
3900 buf_op
= aco_opcode::buffer_atomic_add
;
3901 image_op
= aco_opcode::image_atomic_add
;
3903 case nir_intrinsic_image_deref_atomic_umin
:
3904 buf_op
= aco_opcode::buffer_atomic_umin
;
3905 image_op
= aco_opcode::image_atomic_umin
;
3907 case nir_intrinsic_image_deref_atomic_imin
:
3908 buf_op
= aco_opcode::buffer_atomic_smin
;
3909 image_op
= aco_opcode::image_atomic_smin
;
3911 case nir_intrinsic_image_deref_atomic_umax
:
3912 buf_op
= aco_opcode::buffer_atomic_umax
;
3913 image_op
= aco_opcode::image_atomic_umax
;
3915 case nir_intrinsic_image_deref_atomic_imax
:
3916 buf_op
= aco_opcode::buffer_atomic_smax
;
3917 image_op
= aco_opcode::image_atomic_smax
;
3919 case nir_intrinsic_image_deref_atomic_and
:
3920 buf_op
= aco_opcode::buffer_atomic_and
;
3921 image_op
= aco_opcode::image_atomic_and
;
3923 case nir_intrinsic_image_deref_atomic_or
:
3924 buf_op
= aco_opcode::buffer_atomic_or
;
3925 image_op
= aco_opcode::image_atomic_or
;
3927 case nir_intrinsic_image_deref_atomic_xor
:
3928 buf_op
= aco_opcode::buffer_atomic_xor
;
3929 image_op
= aco_opcode::image_atomic_xor
;
3931 case nir_intrinsic_image_deref_atomic_exchange
:
3932 buf_op
= aco_opcode::buffer_atomic_swap
;
3933 image_op
= aco_opcode::image_atomic_swap
;
3935 case nir_intrinsic_image_deref_atomic_comp_swap
:
3936 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
3937 image_op
= aco_opcode::image_atomic_cmpswap
;
3940 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
3943 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3945 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3946 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3947 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3948 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
3949 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
3950 mubuf
->operands
[0] = Operand(vindex
);
3951 mubuf
->operands
[1] = Operand(resource
);
3952 mubuf
->operands
[2] = Operand((uint32_t)0);
3953 mubuf
->operands
[3] = Operand(data
);
3954 if (return_previous
)
3955 mubuf
->definitions
[0] = Definition(dst
);
3957 mubuf
->idxen
= true;
3958 mubuf
->glc
= return_previous
;
3959 mubuf
->disable_wqm
= true;
3960 mubuf
->barrier
= barrier_image
;
3961 ctx
->program
->needs_exact
= true;
3962 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3966 Temp coords
= get_image_coords(ctx
, instr
, type
);
3967 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3968 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
3969 mimg
->operands
[0] = Operand(coords
);
3970 mimg
->operands
[1] = Operand(resource
);
3971 mimg
->operands
[2] = Operand(s4
); /* no sampler */
3972 mimg
->operands
[3] = Operand(data
);
3973 if (return_previous
)
3974 mimg
->definitions
[0] = Definition(dst
);
3975 mimg
->glc
= return_previous
;
3976 mimg
->dmask
= (1 << data
.size()) - 1;
3978 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3979 mimg
->disable_wqm
= true;
3980 mimg
->barrier
= barrier_image
;
3981 ctx
->program
->needs_exact
= true;
3982 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
3986 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
3988 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
3989 Builder
bld(ctx
->program
, ctx
->block
);
3991 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
3992 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
3993 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
3994 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
3996 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
3997 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
3999 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4000 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4001 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4003 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4005 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4006 * in case 12 (or 3?), we have to divide by 3:
4007 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4008 * use v_mul_hi_u32 with magic number to divide
4009 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4011 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4015 emit_extract_vector(ctx
, desc
, 2, dst
);
4019 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4021 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4022 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4023 Builder
bld(ctx
->program
, ctx
->block
);
4025 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4026 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4027 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4031 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4034 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4036 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4038 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4039 mimg
->operands
[0] = Operand(lod
);
4040 mimg
->operands
[1] = Operand(resource
);
4041 unsigned& dmask
= mimg
->dmask
;
4042 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4043 mimg
->da
= glsl_sampler_type_is_array(type
);
4044 mimg
->can_reorder
= true;
4045 Definition
& def
= mimg
->definitions
[0];
4046 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4048 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4049 glsl_sampler_type_is_array(type
)) {
4051 assert(instr
->dest
.ssa
.num_components
== 3);
4052 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4053 def
= Definition(tmp
);
4054 emit_split_vector(ctx
, tmp
, 3);
4056 /* divide 3rd value by 6 by multiplying with magic number */
4057 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4058 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4060 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4061 emit_extract_vector(ctx
, tmp
, 0, v1
),
4062 emit_extract_vector(ctx
, tmp
, 1, v1
),
4065 } else if (ctx
->options
->chip_class
>= GFX9
&&
4066 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4067 glsl_sampler_type_is_array(type
)) {
4068 assert(instr
->dest
.ssa
.num_components
== 2);
4069 def
= Definition(dst
);
4072 def
= Definition(dst
);
4075 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4078 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4080 Builder
bld(ctx
->program
, ctx
->block
);
4081 unsigned num_components
= instr
->num_components
;
4083 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4084 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4085 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4087 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4088 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
);
4091 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4093 Builder
bld(ctx
->program
, ctx
->block
);
4094 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4095 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4096 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4099 if (ctx
->options
->chip_class
< GFX8
)
4100 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4102 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4104 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4105 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4107 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4108 ctx
->options
->chip_class
>= GFX8
;
4110 offset
= bld
.as_uniform(offset
);
4111 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4115 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4116 if (count
== 3 && smem
) {
4117 writemask
|= 1u << (start
+ 2);
4120 int num_bytes
= count
* elem_size_bytes
;
4122 if (num_bytes
> 16) {
4123 assert(elem_size_bytes
== 8);
4124 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4129 // TODO: check alignment of sub-dword stores
4130 // TODO: split 3 bytes. there is no store instruction for that
4133 if (count
!= instr
->num_components
) {
4134 emit_split_vector(ctx
, data
, instr
->num_components
);
4135 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4136 for (int i
= 0; i
< count
; i
++) {
4137 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4138 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4140 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4141 vec
->definitions
[0] = Definition(write_data
);
4142 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4143 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4144 assert(num_bytes
% 4 == 0);
4145 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4146 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4147 assert(num_bytes
% 4 == 0);
4148 write_data
= bld
.as_uniform(data
);
4153 aco_opcode vmem_op
, smem_op
;
4154 switch (num_bytes
) {
4156 vmem_op
= aco_opcode::buffer_store_dword
;
4157 smem_op
= aco_opcode::s_buffer_store_dword
;
4160 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4161 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4164 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4165 smem_op
= aco_opcode::last_opcode
;
4169 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4170 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4173 unreachable("Store SSBO not implemented for this size.");
4175 if (ctx
->stage
== fragment_fs
)
4176 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4179 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4180 store
->operands
[0] = Operand(rsrc
);
4182 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4183 offset
, Operand(start
* elem_size_bytes
));
4184 store
->operands
[1] = Operand(off
);
4186 store
->operands
[1] = Operand(offset
);
4188 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4189 store
->operands
[1].setFixed(m0
);
4190 store
->operands
[2] = Operand(write_data
);
4191 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4192 store
->disable_wqm
= true;
4193 store
->barrier
= barrier_buffer
;
4194 ctx
->block
->instructions
.emplace_back(std::move(store
));
4195 ctx
->program
->wb_smem_l1_on_end
= true;
4196 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4197 ctx
->block
->kind
|= block_kind_needs_lowering
;
4198 ctx
->program
->needs_exact
= true;
4201 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4202 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4203 store
->operands
[1] = Operand(rsrc
);
4204 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4205 store
->operands
[3] = Operand(write_data
);
4206 store
->offset
= start
* elem_size_bytes
;
4207 store
->offen
= (offset
.type() == RegType::vgpr
);
4208 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4209 store
->disable_wqm
= true;
4210 store
->barrier
= barrier_buffer
;
4211 ctx
->program
->needs_exact
= true;
4212 ctx
->block
->instructions
.emplace_back(std::move(store
));
4217 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4219 /* return the previous value if dest is ever used */
4220 bool return_previous
= false;
4221 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4222 return_previous
= true;
4225 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4226 return_previous
= true;
4230 Builder
bld(ctx
->program
, ctx
->block
);
4231 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4233 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4234 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4235 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4238 if (ctx
->options
->chip_class
< GFX8
)
4239 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4241 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4243 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4244 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4246 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4248 aco_opcode op32
, op64
;
4249 switch (instr
->intrinsic
) {
4250 case nir_intrinsic_ssbo_atomic_add
:
4251 op32
= aco_opcode::buffer_atomic_add
;
4252 op64
= aco_opcode::buffer_atomic_add_x2
;
4254 case nir_intrinsic_ssbo_atomic_imin
:
4255 op32
= aco_opcode::buffer_atomic_smin
;
4256 op64
= aco_opcode::buffer_atomic_smin_x2
;
4258 case nir_intrinsic_ssbo_atomic_umin
:
4259 op32
= aco_opcode::buffer_atomic_umin
;
4260 op64
= aco_opcode::buffer_atomic_umin_x2
;
4262 case nir_intrinsic_ssbo_atomic_imax
:
4263 op32
= aco_opcode::buffer_atomic_smax
;
4264 op64
= aco_opcode::buffer_atomic_smax_x2
;
4266 case nir_intrinsic_ssbo_atomic_umax
:
4267 op32
= aco_opcode::buffer_atomic_umax
;
4268 op64
= aco_opcode::buffer_atomic_umax_x2
;
4270 case nir_intrinsic_ssbo_atomic_and
:
4271 op32
= aco_opcode::buffer_atomic_and
;
4272 op64
= aco_opcode::buffer_atomic_and_x2
;
4274 case nir_intrinsic_ssbo_atomic_or
:
4275 op32
= aco_opcode::buffer_atomic_or
;
4276 op64
= aco_opcode::buffer_atomic_or_x2
;
4278 case nir_intrinsic_ssbo_atomic_xor
:
4279 op32
= aco_opcode::buffer_atomic_xor
;
4280 op64
= aco_opcode::buffer_atomic_xor_x2
;
4282 case nir_intrinsic_ssbo_atomic_exchange
:
4283 op32
= aco_opcode::buffer_atomic_swap
;
4284 op64
= aco_opcode::buffer_atomic_swap_x2
;
4286 case nir_intrinsic_ssbo_atomic_comp_swap
:
4287 op32
= aco_opcode::buffer_atomic_cmpswap
;
4288 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4291 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4293 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4294 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4295 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4296 mubuf
->operands
[1] = Operand(rsrc
);
4297 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4298 mubuf
->operands
[3] = Operand(data
);
4299 if (return_previous
)
4300 mubuf
->definitions
[0] = Definition(dst
);
4302 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4303 mubuf
->glc
= return_previous
;
4304 mubuf
->disable_wqm
= true;
4305 mubuf
->barrier
= barrier_buffer
;
4306 ctx
->program
->needs_exact
= true;
4307 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4310 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4312 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4313 Builder
bld(ctx
->program
, ctx
->block
);
4314 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4315 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4318 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4320 Builder
bld(ctx
->program
, ctx
->block
);
4321 unsigned num_components
= instr
->num_components
;
4322 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4324 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4325 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4327 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4329 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4330 bool global
= ctx
->options
->chip_class
>= GFX9
;
4332 switch (num_bytes
) {
4334 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4337 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4340 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4343 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4346 unreachable("load_global not implemented for this size.");
4348 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4349 flat
->operands
[0] = Operand(addr
);
4350 flat
->operands
[1] = Operand(s1
);
4353 if (dst
.type() == RegType::sgpr
) {
4354 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4355 flat
->definitions
[0] = Definition(vec
);
4356 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4357 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4359 flat
->definitions
[0] = Definition(dst
);
4360 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4362 emit_split_vector(ctx
, dst
, num_components
);
4364 switch (num_bytes
) {
4366 op
= aco_opcode::s_load_dword
;
4369 op
= aco_opcode::s_load_dwordx2
;
4373 op
= aco_opcode::s_load_dwordx4
;
4376 unreachable("load_global not implemented for this size.");
4378 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4379 load
->operands
[0] = Operand(addr
);
4380 load
->operands
[1] = Operand(0u);
4381 load
->definitions
[0] = Definition(dst
);
4383 load
->barrier
= barrier_buffer
;
4384 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4386 if (dst
.size() == 3) {
4388 Temp vec
= bld
.tmp(s4
);
4389 load
->definitions
[0] = Definition(vec
);
4390 ctx
->block
->instructions
.emplace_back(std::move(load
));
4391 emit_split_vector(ctx
, vec
, 4);
4393 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4394 emit_extract_vector(ctx
, vec
, 0, s1
),
4395 emit_extract_vector(ctx
, vec
, 1, s1
),
4396 emit_extract_vector(ctx
, vec
, 2, s1
));
4398 ctx
->block
->instructions
.emplace_back(std::move(load
));
4403 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4405 Builder
bld(ctx
->program
, ctx
->block
);
4406 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4408 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4409 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4411 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4414 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4415 unsigned num_bytes
= count
* elem_size_bytes
;
4417 Temp write_data
= data
;
4418 if (count
!= instr
->num_components
) {
4419 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4420 for (int i
= 0; i
< count
; i
++)
4421 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4422 write_data
= bld
.tmp(RegType::vgpr
, count
);
4423 vec
->definitions
[0] = Definition(write_data
);
4424 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4427 unsigned offset
= start
* elem_size_bytes
;
4428 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4429 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4430 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4431 Temp carry
= bld
.tmp(s2
);
4432 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4434 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4435 Operand(offset
), addr0
);
4436 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4438 carry
).def(1).setHint(vcc
);
4440 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4445 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4446 bool global
= ctx
->options
->chip_class
>= GFX9
;
4448 switch (num_bytes
) {
4450 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4453 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4456 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4459 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4462 unreachable("store_global not implemented for this size.");
4464 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4465 flat
->operands
[0] = Operand(addr
);
4466 flat
->operands
[1] = Operand(s1
);
4467 flat
->operands
[2] = Operand(data
);
4469 flat
->offset
= offset
;
4470 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4474 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4475 Builder
bld(ctx
->program
, ctx
->block
);
4476 switch(instr
->intrinsic
) {
4477 case nir_intrinsic_group_memory_barrier
:
4478 case nir_intrinsic_memory_barrier
:
4479 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4481 case nir_intrinsic_memory_barrier_atomic_counter
:
4482 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4484 case nir_intrinsic_memory_barrier_buffer
:
4485 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4487 case nir_intrinsic_memory_barrier_image
:
4488 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4490 case nir_intrinsic_memory_barrier_shared
:
4491 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4494 unreachable("Unimplemented memory barrier intrinsic");
4499 Operand
load_lds_size_m0(isel_context
*ctx
)
4501 /* TODO: m0 does not need to be initialized on GFX9+ */
4502 Builder
bld(ctx
->program
, ctx
->block
);
4503 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
4507 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4509 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4510 Operand m
= load_lds_size_m0(ctx
);
4511 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4512 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4513 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4514 Builder
bld(ctx
->program
, ctx
->block
);
4516 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4517 unsigned bytes_read
= 0;
4518 unsigned result_size
= 0;
4519 unsigned total_bytes
= instr
->num_components
* elem_size_bytes
;
4520 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : instr
->dest
.ssa
.bit_size
/ 8;
4521 std::array
<Temp
, 4> result
;
4523 while (bytes_read
< total_bytes
) {
4524 unsigned todo
= total_bytes
- bytes_read
;
4525 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
4526 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
4528 aco_opcode op
= aco_opcode::last_opcode
;
4529 if (todo
>= 16 && aligned16
) {
4530 op
= aco_opcode::ds_read_b128
;
4532 } else if (todo
>= 12 && aligned16
) {
4533 op
= aco_opcode::ds_read_b96
;
4535 } else if (todo
>= 8) {
4536 op
= aligned8
? aco_opcode::ds_read_b64
: aco_opcode::ds_read2_b32
;
4538 } else if (todo
>= 4) {
4539 op
= aco_opcode::ds_read_b32
;
4544 assert(todo
% elem_size_bytes
== 0);
4545 unsigned num_elements
= todo
/ elem_size_bytes
;
4546 unsigned offset
= nir_intrinsic_base(instr
) + bytes_read
;
4547 unsigned max_offset
= op
== aco_opcode::ds_read2_b32
? 1019 : 65535;
4549 Temp address_offset
= address
;
4550 if (offset
> max_offset
) {
4551 address_offset
= bld
.vadd32(bld
.def(v1
), Operand((uint32_t)nir_intrinsic_base(instr
)), address_offset
);
4552 offset
= bytes_read
;
4554 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
4557 if (instr
->num_components
== 1 && dst
.type() == RegType::vgpr
)
4560 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
4562 if (op
== aco_opcode::ds_read2_b32
)
4563 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
4565 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
4567 if (instr
->num_components
== 1) {
4568 assert(todo
== total_bytes
);
4569 if (dst
.type() == RegType::sgpr
)
4570 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4574 if (dst
.type() == RegType::sgpr
)
4575 res
= bld
.as_uniform(res
);
4577 if (num_elements
== 1) {
4578 result
[result_size
++] = res
;
4580 assert(res
!= dst
&& res
.size() % num_elements
== 0);
4581 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
4582 split
->operands
[0] = Operand(res
);
4583 for (unsigned i
= 0; i
< num_elements
; i
++)
4584 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
4585 ctx
->block
->instructions
.emplace_back(std::move(split
));
4591 assert(result_size
== instr
->num_components
&& result_size
> 1);
4592 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
4593 for (unsigned i
= 0; i
< result_size
; i
++)
4594 vec
->operands
[i
] = Operand(result
[i
]);
4595 vec
->definitions
[0] = Definition(dst
);
4596 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4597 ctx
->allocated_vec
.emplace(dst
.id(), result
);
4600 void ds_write_helper(isel_context
*ctx
, Operand m
, Temp address
, Temp data
, unsigned offset0
, unsigned offset1
, unsigned align
)
4602 Builder
bld(ctx
->program
, ctx
->block
);
4603 unsigned bytes_written
= 0;
4604 while (bytes_written
< data
.size() * 4) {
4605 unsigned todo
= data
.size() * 4 - bytes_written
;
4606 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
4607 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
4609 aco_opcode op
= aco_opcode::last_opcode
;
4611 if (todo
>= 16 && aligned16
) {
4612 op
= aco_opcode::ds_write_b128
;
4614 } else if (todo
>= 12 && aligned16
) {
4615 op
= aco_opcode::ds_write_b96
;
4617 } else if (todo
>= 8) {
4618 op
= aligned8
? aco_opcode::ds_write_b64
: aco_opcode::ds_write2_b32
;
4620 } else if (todo
>= 4) {
4621 op
= aco_opcode::ds_write_b32
;
4627 bool write2
= op
== aco_opcode::ds_write2_b32
;
4628 unsigned offset
= offset0
+ offset1
+ bytes_written
;
4629 unsigned max_offset
= write2
? 1020 : 65535;
4630 Temp address_offset
= address
;
4631 if (offset
> max_offset
) {
4632 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
4633 offset
= offset1
+ bytes_written
;
4635 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
4638 Temp val0
= emit_extract_vector(ctx
, data
, bytes_written
>> 2, v1
);
4639 Temp val1
= emit_extract_vector(ctx
, data
, (bytes_written
>> 2) + 1, v1
);
4640 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
4642 Temp val
= emit_extract_vector(ctx
, data
, bytes_written
>> 2, RegClass(RegType::vgpr
, size
));
4643 bld
.ds(op
, address_offset
, val
, m
, offset
);
4646 bytes_written
+= size
* 4;
4650 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4652 unsigned offset
= nir_intrinsic_base(instr
);
4653 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4654 Operand m
= load_lds_size_m0(ctx
);
4655 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4656 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4657 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4658 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4660 /* we need at most two stores for 32bit variables */
4661 int start
[2], count
[2];
4662 u_bit_scan_consecutive_range(&writemask
, &start
[0], &count
[0]);
4663 u_bit_scan_consecutive_range(&writemask
, &start
[1], &count
[1]);
4664 assert(writemask
== 0);
4666 /* one combined store is sufficient */
4667 if (count
[0] == count
[1]) {
4668 Builder
bld(ctx
->program
, ctx
->block
);
4670 Temp address_offset
= address
;
4671 if ((offset
>> 2) + start
[1] > 255) {
4672 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address_offset
);
4676 assert(count
[0] == 1);
4677 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
4678 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
4679 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
4680 offset
= offset
/ elem_size_bytes
;
4681 bld
.ds(op
, address_offset
, val0
, val1
, m
,
4682 offset
+ start
[0], offset
+ start
[1]);
4686 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4687 for (unsigned i
= 0; i
< 2; i
++) {
4691 Temp write_data
= emit_extract_vector(ctx
, data
, start
[i
], RegClass(RegType::vgpr
, count
[i
] * elem_size_bytes
/ 4));
4692 ds_write_helper(ctx
, m
, address
, write_data
, offset
, start
[i
] * elem_size_bytes
, align
);
4697 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4699 unsigned offset
= nir_intrinsic_base(instr
);
4700 Operand m
= load_lds_size_m0(ctx
);
4701 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4702 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4704 unsigned num_operands
= 3;
4705 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4706 switch(instr
->intrinsic
) {
4707 case nir_intrinsic_shared_atomic_add
:
4708 op32
= aco_opcode::ds_add_u32
;
4709 op64
= aco_opcode::ds_add_u64
;
4710 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4711 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4713 case nir_intrinsic_shared_atomic_imin
:
4714 op32
= aco_opcode::ds_min_i32
;
4715 op64
= aco_opcode::ds_min_i64
;
4716 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4717 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4719 case nir_intrinsic_shared_atomic_umin
:
4720 op32
= aco_opcode::ds_min_u32
;
4721 op64
= aco_opcode::ds_min_u64
;
4722 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4723 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4725 case nir_intrinsic_shared_atomic_imax
:
4726 op32
= aco_opcode::ds_max_i32
;
4727 op64
= aco_opcode::ds_max_i64
;
4728 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4729 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4731 case nir_intrinsic_shared_atomic_umax
:
4732 op32
= aco_opcode::ds_max_u32
;
4733 op64
= aco_opcode::ds_max_u64
;
4734 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4735 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4737 case nir_intrinsic_shared_atomic_and
:
4738 op32
= aco_opcode::ds_and_b32
;
4739 op64
= aco_opcode::ds_and_b64
;
4740 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4741 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4743 case nir_intrinsic_shared_atomic_or
:
4744 op32
= aco_opcode::ds_or_b32
;
4745 op64
= aco_opcode::ds_or_b64
;
4746 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4747 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4749 case nir_intrinsic_shared_atomic_xor
:
4750 op32
= aco_opcode::ds_xor_b32
;
4751 op64
= aco_opcode::ds_xor_b64
;
4752 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4753 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4755 case nir_intrinsic_shared_atomic_exchange
:
4756 op32
= aco_opcode::ds_write_b32
;
4757 op64
= aco_opcode::ds_write_b64
;
4758 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4759 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4761 case nir_intrinsic_shared_atomic_comp_swap
:
4762 op32
= aco_opcode::ds_cmpst_b32
;
4763 op64
= aco_opcode::ds_cmpst_b64
;
4764 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4765 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4769 unreachable("Unhandled shared atomic intrinsic");
4772 /* return the previous value if dest is ever used */
4773 bool return_previous
= false;
4774 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4775 return_previous
= true;
4778 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4779 return_previous
= true;
4784 if (data
.size() == 1) {
4785 assert(instr
->dest
.ssa
.bit_size
== 32);
4786 op
= return_previous
? op32_rtn
: op32
;
4788 assert(instr
->dest
.ssa
.bit_size
== 64);
4789 op
= return_previous
? op64_rtn
: op64
;
4792 if (offset
> 65535) {
4793 Builder
bld(ctx
->program
, ctx
->block
);
4794 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4798 aco_ptr
<DS_instruction
> ds
;
4799 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4800 ds
->operands
[0] = Operand(address
);
4801 ds
->operands
[1] = Operand(data
);
4802 if (num_operands
== 4)
4803 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4804 ds
->operands
[num_operands
- 1] = m
;
4805 ds
->offset0
= offset
;
4806 if (return_previous
)
4807 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4808 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4811 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4812 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4813 Builder
bld(ctx
->program
, ctx
->block
);
4814 Temp scratch_addr
= ctx
->private_segment_buffer
;
4815 if (ctx
->stage
!= MESA_SHADER_COMPUTE
)
4816 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), ctx
->private_segment_buffer
, Operand(0u));
4818 /* older generations need element size = 16 bytes */
4819 if (ctx
->program
->chip_class
>= GFX9
)
4820 rsrc_conf
= 0x00E00000u
;
4822 rsrc_conf
= 0x00F80000u
;
4823 /* buffer res = addr + num_records = -1, index_stride = 64, add_tid_enable = true */
4824 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4825 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4826 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4829 switch (dst
.size()) {
4831 op
= aco_opcode::buffer_load_dword
;
4834 op
= aco_opcode::buffer_load_dwordx2
;
4837 op
= aco_opcode::buffer_load_dwordx3
;
4840 op
= aco_opcode::buffer_load_dwordx4
;
4844 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4845 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4846 bld
.def(v4
), offset
, rsrc
,
4847 ctx
->scratch_offset
, 0, true);
4848 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4849 aco_opcode::buffer_load_dwordx4
,
4850 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4851 offset
, rsrc
, ctx
->scratch_offset
, 16, true);
4852 emit_split_vector(ctx
, lower
, 2);
4853 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4854 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4855 if (dst
.size() == 8) {
4856 emit_split_vector(ctx
, upper
, 2);
4857 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4858 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4863 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4864 Format::PSEUDO
, dst
.size() / 2, 1)};
4865 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4866 vec
->operands
[i
] = Operand(elems
[i
]);
4867 vec
->definitions
[0] = Definition(dst
);
4868 bld
.insert(std::move(vec
));
4869 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4873 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
4876 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->scratch_offset
, 0, true);
4877 emit_split_vector(ctx
, dst
, instr
->num_components
);
4880 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4881 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
4882 Builder
bld(ctx
->program
, ctx
->block
);
4883 Temp scratch_addr
= ctx
->private_segment_buffer
;
4884 if (ctx
->stage
!= MESA_SHADER_COMPUTE
)
4885 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), ctx
->private_segment_buffer
, Operand(0u));
4887 /* older generations need element size = 16 bytes */
4888 if (ctx
->program
->chip_class
>= GFX9
)
4889 rsrc_conf
= 0x00E00000u
;
4891 rsrc_conf
= 0x00F80000u
;
4892 /* buffer res = addr + num_records = -1, index_stride = 64, add_tid_enable = true */
4893 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4894 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4895 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4897 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4898 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4902 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4903 int num_bytes
= count
* elem_size_bytes
;
4905 if (num_bytes
> 16) {
4906 assert(elem_size_bytes
== 8);
4907 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4912 // TODO: check alignment of sub-dword stores
4913 // TODO: split 3 bytes. there is no store instruction for that
4916 if (count
!= instr
->num_components
) {
4917 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4918 for (int i
= 0; i
< count
; i
++) {
4919 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
4920 vec
->operands
[i
] = Operand(elem
);
4922 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
4923 vec
->definitions
[0] = Definition(write_data
);
4924 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4930 switch (num_bytes
) {
4932 op
= aco_opcode::buffer_store_dword
;
4935 op
= aco_opcode::buffer_store_dwordx2
;
4938 op
= aco_opcode::buffer_store_dwordx3
;
4941 op
= aco_opcode::buffer_store_dwordx4
;
4944 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
4947 bld
.mubuf(op
, offset
, rsrc
, ctx
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
4951 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4952 uint8_t log2_ps_iter_samples
;
4953 if (ctx
->program
->info
->ps
.force_persample
) {
4954 log2_ps_iter_samples
=
4955 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
4957 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
4960 /* The bit pattern matches that used by fixed function fragment
4962 static const unsigned ps_iter_masks
[] = {
4963 0xffff, /* not used */
4969 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
4971 Builder
bld(ctx
->program
, ctx
->block
);
4973 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::ancillary
], Operand(8u), Operand(4u));
4974 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
4975 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
4976 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4977 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, ctx
->fs_inputs
[fs_input::sample_coverage
]);
4980 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
4982 Builder
bld(ctx
->program
, ctx
->block
);
4984 if (cluster_size
== 1) {
4986 } if (op
== nir_op_iand
&& cluster_size
== 4) {
4987 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
4988 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
4989 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
4990 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
4991 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
4992 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
4993 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
4994 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
4995 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
4996 //subgroupAnd(val) -> (exec & ~val) == 0
4997 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
4998 return bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), tmp
, Operand(0u));
4999 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5000 //subgroupOr(val) -> (val & exec) != 0
5001 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5002 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5003 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5004 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5005 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5006 return bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5008 //subgroupClustered{And,Or,Xor}(val, n) ->
5009 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5010 //cluster_offset = ~(n - 1) & lane_id
5011 //cluster_mask = ((1 << n) - 1)
5012 //subgroupClusteredAnd():
5013 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5014 //subgroupClusteredOr():
5015 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5016 //subgroupClusteredXor():
5017 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5018 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5019 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5020 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5023 if (op
== nir_op_iand
)
5024 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5026 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5028 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5029 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5030 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5031 if (cluster_mask
!= 0xffffffff)
5032 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5034 Definition cmp_def
= Definition();
5035 if (op
== nir_op_iand
) {
5036 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5037 } else if (op
== nir_op_ior
) {
5038 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5039 } else if (op
== nir_op_ixor
) {
5040 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5041 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5042 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5044 cmp_def
.setHint(vcc
);
5045 return cmp_def
.getTemp();
5049 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5051 Builder
bld(ctx
->program
, ctx
->block
);
5053 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5054 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5055 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5057 if (op
== nir_op_iand
)
5058 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5060 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5062 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5063 Temp lo
= lohi
.def(0).getTemp();
5064 Temp hi
= lohi
.def(1).getTemp();
5065 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5066 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5068 Definition cmp_def
= Definition();
5069 if (op
== nir_op_iand
)
5070 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5071 else if (op
== nir_op_ior
)
5072 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5073 else if (op
== nir_op_ixor
)
5074 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5075 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5076 cmp_def
.setHint(vcc
);
5077 return cmp_def
.getTemp();
5080 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5082 Builder
bld(ctx
->program
, ctx
->block
);
5084 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5085 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5086 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5087 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5088 if (op
== nir_op_iand
)
5089 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5090 else if (op
== nir_op_ior
)
5091 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5092 else if (op
== nir_op_ixor
)
5093 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5099 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5101 Builder
bld(ctx
->program
, ctx
->block
);
5102 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5103 if (src
.regClass().type() == RegType::vgpr
) {
5104 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5105 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5106 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(dst
), Operand(0u), Operand(src
));
5107 } else if (src
.regClass() == s1
) {
5108 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5109 } else if (src
.regClass() == s2
) {
5110 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5112 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5113 nir_print_instr(&instr
->instr
, stderr
);
5114 fprintf(stderr
, "\n");
5118 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5120 Builder
bld(ctx
->program
, ctx
->block
);
5121 Temp p1
= ctx
->fs_inputs
[fs_input::persp_center_p1
];
5122 Temp p2
= ctx
->fs_inputs
[fs_input::persp_center_p2
];
5125 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5126 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5127 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5128 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5129 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5130 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5132 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5133 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5134 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5135 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5136 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5137 Temp wqm1
= bld
.tmp(v1
);
5138 emit_wqm(ctx
, tmp1
, wqm1
, true);
5139 Temp wqm2
= bld
.tmp(v1
);
5140 emit_wqm(ctx
, tmp2
, wqm2
, true);
5141 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5145 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5147 Builder
bld(ctx
->program
, ctx
->block
);
5148 switch(instr
->intrinsic
) {
5149 case nir_intrinsic_load_barycentric_sample
:
5150 case nir_intrinsic_load_barycentric_pixel
:
5151 case nir_intrinsic_load_barycentric_centroid
: {
5152 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5153 fs_input input
= get_interp_input(instr
->intrinsic
, mode
);
5155 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5156 if (input
== fs_input::max_inputs
) {
5157 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5158 Operand(0u), Operand(0u));
5160 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5161 ctx
->fs_inputs
[input
],
5162 ctx
->fs_inputs
[input
+ 1]);
5164 emit_split_vector(ctx
, dst
, 2);
5167 case nir_intrinsic_load_barycentric_at_sample
: {
5168 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5169 switch (ctx
->options
->key
.fs
.num_samples
) {
5170 case 2: sample_pos_offset
+= 1 << 3; break;
5171 case 4: sample_pos_offset
+= 3 << 3; break;
5172 case 8: sample_pos_offset
+= 7 << 3; break;
5176 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5177 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5178 if (addr
.type() == RegType::sgpr
) {
5181 sample_pos_offset
+= const_addr
->u32
<< 3;
5182 offset
= Operand(sample_pos_offset
);
5183 } else if (ctx
->options
->chip_class
>= GFX9
) {
5184 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5186 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5187 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5189 addr
= ctx
->private_segment_buffer
;
5190 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), addr
, Operand(offset
));
5192 } else if (ctx
->options
->chip_class
>= GFX9
) {
5193 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5194 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, ctx
->private_segment_buffer
, sample_pos_offset
);
5196 /* addr += ctx->private_segment_buffer + sample_pos_offset */
5197 Temp tmp0
= bld
.tmp(s1
);
5198 Temp tmp1
= bld
.tmp(s1
);
5199 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), ctx
->private_segment_buffer
);
5200 Definition scc_tmp
= bld
.def(s1
, scc
);
5201 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5202 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), scc_tmp
.getTemp());
5203 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5204 Temp pck0
= bld
.tmp(v1
);
5205 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5206 tmp1
= as_vgpr(ctx
, tmp1
);
5207 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5208 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5210 /* sample_pos = flat_load_dwordx2 addr */
5211 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5214 /* sample_pos -= 0.5 */
5215 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5216 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5217 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5218 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5219 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5221 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5224 case nir_intrinsic_load_barycentric_at_offset
: {
5225 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5226 RegClass rc
= RegClass(offset
.type(), 1);
5227 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5228 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5229 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5232 case nir_intrinsic_load_front_face
: {
5233 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5234 Operand(0u), ctx
->fs_inputs
[fs_input::front_face
]).def(0).setHint(vcc
);
5237 case nir_intrinsic_load_view_index
:
5238 case nir_intrinsic_load_layer_id
: {
5239 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5240 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5241 bld
.copy(Definition(dst
), Operand(ctx
->view_index
));
5245 unsigned idx
= nir_intrinsic_base(instr
);
5246 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5247 Operand(2u), bld
.m0(ctx
->prim_mask
), idx
, 0);
5250 case nir_intrinsic_load_frag_coord
: {
5251 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5254 case nir_intrinsic_load_sample_pos
: {
5255 Temp posx
= ctx
->fs_inputs
[fs_input::frag_pos_0
];
5256 Temp posy
= ctx
->fs_inputs
[fs_input::frag_pos_1
];
5257 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5258 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5259 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5262 case nir_intrinsic_load_interpolated_input
:
5263 visit_load_interpolated_input(ctx
, instr
);
5265 case nir_intrinsic_store_output
:
5266 visit_store_output(ctx
, instr
);
5268 case nir_intrinsic_load_input
:
5269 visit_load_input(ctx
, instr
);
5271 case nir_intrinsic_load_ubo
:
5272 visit_load_ubo(ctx
, instr
);
5274 case nir_intrinsic_load_push_constant
:
5275 visit_load_push_constant(ctx
, instr
);
5277 case nir_intrinsic_load_constant
:
5278 visit_load_constant(ctx
, instr
);
5280 case nir_intrinsic_vulkan_resource_index
:
5281 visit_load_resource(ctx
, instr
);
5283 case nir_intrinsic_discard
:
5284 visit_discard(ctx
, instr
);
5286 case nir_intrinsic_discard_if
:
5287 visit_discard_if(ctx
, instr
);
5289 case nir_intrinsic_load_shared
:
5290 visit_load_shared(ctx
, instr
);
5292 case nir_intrinsic_store_shared
:
5293 visit_store_shared(ctx
, instr
);
5295 case nir_intrinsic_shared_atomic_add
:
5296 case nir_intrinsic_shared_atomic_imin
:
5297 case nir_intrinsic_shared_atomic_umin
:
5298 case nir_intrinsic_shared_atomic_imax
:
5299 case nir_intrinsic_shared_atomic_umax
:
5300 case nir_intrinsic_shared_atomic_and
:
5301 case nir_intrinsic_shared_atomic_or
:
5302 case nir_intrinsic_shared_atomic_xor
:
5303 case nir_intrinsic_shared_atomic_exchange
:
5304 case nir_intrinsic_shared_atomic_comp_swap
:
5305 visit_shared_atomic(ctx
, instr
);
5307 case nir_intrinsic_image_deref_load
:
5308 visit_image_load(ctx
, instr
);
5310 case nir_intrinsic_image_deref_store
:
5311 visit_image_store(ctx
, instr
);
5313 case nir_intrinsic_image_deref_atomic_add
:
5314 case nir_intrinsic_image_deref_atomic_umin
:
5315 case nir_intrinsic_image_deref_atomic_imin
:
5316 case nir_intrinsic_image_deref_atomic_umax
:
5317 case nir_intrinsic_image_deref_atomic_imax
:
5318 case nir_intrinsic_image_deref_atomic_and
:
5319 case nir_intrinsic_image_deref_atomic_or
:
5320 case nir_intrinsic_image_deref_atomic_xor
:
5321 case nir_intrinsic_image_deref_atomic_exchange
:
5322 case nir_intrinsic_image_deref_atomic_comp_swap
:
5323 visit_image_atomic(ctx
, instr
);
5325 case nir_intrinsic_image_deref_size
:
5326 visit_image_size(ctx
, instr
);
5328 case nir_intrinsic_load_ssbo
:
5329 visit_load_ssbo(ctx
, instr
);
5331 case nir_intrinsic_store_ssbo
:
5332 visit_store_ssbo(ctx
, instr
);
5334 case nir_intrinsic_load_global
:
5335 visit_load_global(ctx
, instr
);
5337 case nir_intrinsic_store_global
:
5338 visit_store_global(ctx
, instr
);
5340 case nir_intrinsic_ssbo_atomic_add
:
5341 case nir_intrinsic_ssbo_atomic_imin
:
5342 case nir_intrinsic_ssbo_atomic_umin
:
5343 case nir_intrinsic_ssbo_atomic_imax
:
5344 case nir_intrinsic_ssbo_atomic_umax
:
5345 case nir_intrinsic_ssbo_atomic_and
:
5346 case nir_intrinsic_ssbo_atomic_or
:
5347 case nir_intrinsic_ssbo_atomic_xor
:
5348 case nir_intrinsic_ssbo_atomic_exchange
:
5349 case nir_intrinsic_ssbo_atomic_comp_swap
:
5350 visit_atomic_ssbo(ctx
, instr
);
5352 case nir_intrinsic_load_scratch
:
5353 visit_load_scratch(ctx
, instr
);
5355 case nir_intrinsic_store_scratch
:
5356 visit_store_scratch(ctx
, instr
);
5358 case nir_intrinsic_get_buffer_size
:
5359 visit_get_buffer_size(ctx
, instr
);
5361 case nir_intrinsic_barrier
: {
5362 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5363 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5364 if (workgroup_size
> 64)
5365 bld
.sopp(aco_opcode::s_barrier
);
5368 case nir_intrinsic_group_memory_barrier
:
5369 case nir_intrinsic_memory_barrier
:
5370 case nir_intrinsic_memory_barrier_atomic_counter
:
5371 case nir_intrinsic_memory_barrier_buffer
:
5372 case nir_intrinsic_memory_barrier_image
:
5373 case nir_intrinsic_memory_barrier_shared
:
5374 emit_memory_barrier(ctx
, instr
);
5376 case nir_intrinsic_load_num_work_groups
:
5377 case nir_intrinsic_load_work_group_id
:
5378 case nir_intrinsic_load_local_invocation_id
: {
5379 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5381 if (instr
->intrinsic
== nir_intrinsic_load_num_work_groups
)
5382 ids
= ctx
->num_workgroups
;
5383 else if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
5384 ids
= ctx
->workgroup_ids
;
5386 ids
= ctx
->local_invocation_ids
;
5387 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5388 ids
[0].id() ? Operand(ids
[0]) : Operand(1u),
5389 ids
[1].id() ? Operand(ids
[1]) : Operand(1u),
5390 ids
[2].id() ? Operand(ids
[2]) : Operand(1u));
5391 emit_split_vector(ctx
, dst
, 3);
5394 case nir_intrinsic_load_local_invocation_index
: {
5395 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5396 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5397 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5398 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5401 case nir_intrinsic_load_subgroup_id
: {
5402 if (ctx
->stage
== compute_cs
) {
5403 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5404 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5406 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5410 case nir_intrinsic_load_subgroup_invocation
: {
5411 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5412 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5415 case nir_intrinsic_load_num_subgroups
: {
5416 if (ctx
->stage
== compute_cs
)
5417 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
), ctx
->tg_size
);
5419 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5422 case nir_intrinsic_ballot
: {
5423 Definition tmp
= bld
.def(s2
);
5424 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5425 if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s2
) {
5426 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5427 } else if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s1
) {
5428 bld
.sop2(aco_opcode::s_cselect_b64
, tmp
, Operand(exec
, s2
), Operand(0u), bld
.scc(src
));
5429 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5430 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5431 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5432 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5434 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5435 nir_print_instr(&instr
->instr
, stderr
);
5436 fprintf(stderr
, "\n");
5438 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5441 case nir_intrinsic_shuffle
: {
5442 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5443 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5444 emit_uniform_subgroup(ctx
, instr
, src
);
5446 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5447 assert(tid
.regClass() == v1
);
5448 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5449 if (src
.regClass() == v1
) {
5450 tid
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), tid
);
5451 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, src
), dst
);
5452 } else if (src
.regClass() == v2
) {
5453 tid
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), tid
);
5455 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5456 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5457 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, lo
));
5458 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, hi
));
5459 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5460 emit_split_vector(ctx
, dst
, 2);
5461 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5462 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5463 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5464 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5465 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5467 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5468 nir_print_instr(&instr
->instr
, stderr
);
5469 fprintf(stderr
, "\n");
5474 case nir_intrinsic_load_sample_id
: {
5475 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5476 ctx
->fs_inputs
[ancillary
], Operand(8u), Operand(4u));
5479 case nir_intrinsic_load_sample_mask_in
: {
5480 visit_load_sample_mask_in(ctx
, instr
);
5483 case nir_intrinsic_read_first_invocation
: {
5484 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5485 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5486 if (src
.regClass() == v1
) {
5488 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5490 } else if (src
.regClass() == v2
) {
5491 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5492 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5493 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5494 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5495 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5496 emit_split_vector(ctx
, dst
, 2);
5497 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5499 bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5500 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
))),
5502 } else if (src
.regClass() == s1
) {
5503 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5504 } else if (src
.regClass() == s2
) {
5505 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5507 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5508 nir_print_instr(&instr
->instr
, stderr
);
5509 fprintf(stderr
, "\n");
5513 case nir_intrinsic_read_invocation
: {
5514 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5515 Temp lane
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5516 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5517 assert(lane
.regClass() == s1
);
5518 if (src
.regClass() == v1
) {
5519 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), src
, lane
), dst
);
5520 } else if (src
.regClass() == v2
) {
5521 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5522 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5523 lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), lo
, lane
));
5524 hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), hi
, lane
));
5525 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5526 emit_split_vector(ctx
, dst
, 2);
5527 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5528 emit_wqm(ctx
, bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, lane
), dst
);
5529 } else if (src
.regClass() == s1
) {
5530 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5531 } else if (src
.regClass() == s2
) {
5532 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5534 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5535 nir_print_instr(&instr
->instr
, stderr
);
5536 fprintf(stderr
, "\n");
5540 case nir_intrinsic_vote_all
: {
5541 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5542 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5543 assert(src
.regClass() == s2
);
5544 assert(dst
.regClass() == s1
);
5546 Definition tmp
= bld
.def(s1
);
5547 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(tmp
),
5548 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)),
5550 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5553 case nir_intrinsic_vote_any
: {
5554 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5555 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5556 assert(src
.regClass() == s2
);
5557 assert(dst
.regClass() == s1
);
5559 Definition tmp
= bld
.def(s1
);
5560 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(tmp
), src
, Operand(exec
, s2
));
5561 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5564 case nir_intrinsic_reduce
:
5565 case nir_intrinsic_inclusive_scan
:
5566 case nir_intrinsic_exclusive_scan
: {
5567 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5568 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5569 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5570 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5571 nir_intrinsic_cluster_size(instr
) : 0;
5572 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5574 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5575 emit_uniform_subgroup(ctx
, instr
, src
);
5576 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5577 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5579 else if (op
== nir_op_iadd
)
5581 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5583 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5585 switch (instr
->intrinsic
) {
5586 case nir_intrinsic_reduce
:
5587 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5589 case nir_intrinsic_exclusive_scan
:
5590 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5592 case nir_intrinsic_inclusive_scan
:
5593 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5598 } else if (cluster_size
== 1) {
5599 bld
.copy(Definition(dst
), src
);
5601 src
= as_vgpr(ctx
, src
);
5605 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5620 unreachable("unknown reduction op");
5625 switch (instr
->intrinsic
) {
5626 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5627 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5628 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5630 unreachable("unknown reduce intrinsic");
5633 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5634 reduce
->operands
[0] = Operand(src
);
5635 // filled in by aco_reduce_assign.cpp, used internally as part of the
5637 assert(dst
.size() == 1 || dst
.size() == 2);
5638 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5639 reduce
->operands
[2] = Operand(v1
.as_linear());
5641 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5642 reduce
->definitions
[0] = Definition(tmp_dst
);
5643 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5644 reduce
->definitions
[2] = Definition();
5645 reduce
->definitions
[3] = Definition(scc
, s1
);
5646 reduce
->definitions
[4] = Definition();
5647 reduce
->reduce_op
= reduce_op
;
5648 reduce
->cluster_size
= cluster_size
;
5649 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5651 emit_wqm(ctx
, tmp_dst
, dst
);
5655 case nir_intrinsic_quad_broadcast
: {
5656 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5657 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5658 emit_uniform_subgroup(ctx
, instr
, src
);
5660 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5661 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5662 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5663 uint32_t half_mask
= 0x11111111u
<< lane
;
5664 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5665 Temp tmp
= bld
.tmp(s2
);
5666 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5667 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5668 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5669 emit_wqm(ctx
, tmp
, dst
);
5670 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5672 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5673 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5675 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5676 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5677 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5678 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5679 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5680 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5681 emit_split_vector(ctx
, dst
, 2);
5683 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5684 nir_print_instr(&instr
->instr
, stderr
);
5685 fprintf(stderr
, "\n");
5690 case nir_intrinsic_quad_swap_horizontal
:
5691 case nir_intrinsic_quad_swap_vertical
:
5692 case nir_intrinsic_quad_swap_diagonal
:
5693 case nir_intrinsic_quad_swizzle_amd
: {
5694 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5695 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5696 emit_uniform_subgroup(ctx
, instr
, src
);
5699 uint16_t dpp_ctrl
= 0;
5700 switch (instr
->intrinsic
) {
5701 case nir_intrinsic_quad_swap_horizontal
:
5702 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5704 case nir_intrinsic_quad_swap_vertical
:
5705 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5707 case nir_intrinsic_quad_swap_diagonal
:
5708 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5710 case nir_intrinsic_quad_swizzle_amd
: {
5711 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5718 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5719 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5720 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5721 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5722 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5723 emit_wqm(ctx
, tmp
, dst
);
5724 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5725 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5726 emit_wqm(ctx
, tmp
, dst
);
5727 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5728 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5729 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5730 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5731 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5732 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5733 emit_split_vector(ctx
, dst
, 2);
5735 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5736 nir_print_instr(&instr
->instr
, stderr
);
5737 fprintf(stderr
, "\n");
5741 case nir_intrinsic_masked_swizzle_amd
: {
5742 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5743 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5744 emit_uniform_subgroup(ctx
, instr
, src
);
5747 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5748 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5749 if (dst
.regClass() == v1
) {
5751 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5753 } else if (dst
.regClass() == v2
) {
5754 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5755 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5756 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5757 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5758 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5759 emit_split_vector(ctx
, dst
, 2);
5761 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5762 nir_print_instr(&instr
->instr
, stderr
);
5763 fprintf(stderr
, "\n");
5767 case nir_intrinsic_write_invocation_amd
: {
5768 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5769 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5770 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5771 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5772 if (dst
.regClass() == v1
) {
5773 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5774 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5775 } else if (dst
.regClass() == v2
) {
5776 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5777 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5778 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5779 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5780 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5781 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5782 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5783 emit_split_vector(ctx
, dst
, 2);
5785 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5786 nir_print_instr(&instr
->instr
, stderr
);
5787 fprintf(stderr
, "\n");
5791 case nir_intrinsic_mbcnt_amd
: {
5792 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5793 RegClass rc
= RegClass(src
.type(), 1);
5794 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5795 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5796 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5797 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5798 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5799 emit_wqm(ctx
, wqm_tmp
, dst
);
5802 case nir_intrinsic_load_helper_invocation
: {
5803 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5804 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5805 ctx
->block
->kind
|= block_kind_needs_lowering
;
5806 ctx
->program
->needs_exact
= true;
5809 case nir_intrinsic_is_helper_invocation
: {
5810 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5811 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5812 ctx
->block
->kind
|= block_kind_needs_lowering
;
5813 ctx
->program
->needs_exact
= true;
5816 case nir_intrinsic_demote
:
5817 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5818 ctx
->block
->kind
|= block_kind_needs_lowering
;
5819 ctx
->program
->needs_exact
= true;
5821 case nir_intrinsic_demote_if
: {
5822 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5823 as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false),
5825 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5826 ctx
->block
->kind
|= block_kind_needs_lowering
;
5827 ctx
->program
->needs_exact
= true;
5830 case nir_intrinsic_first_invocation
: {
5831 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5832 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5835 case nir_intrinsic_shader_clock
:
5836 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
5838 case nir_intrinsic_load_vertex_id_zero_base
: {
5839 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5840 bld
.copy(Definition(dst
), ctx
->vertex_id
);
5843 case nir_intrinsic_load_first_vertex
: {
5844 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5845 bld
.copy(Definition(dst
), ctx
->base_vertex
);
5848 case nir_intrinsic_load_base_instance
: {
5849 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5850 bld
.copy(Definition(dst
), ctx
->start_instance
);
5853 case nir_intrinsic_load_instance_id
: {
5854 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5855 bld
.copy(Definition(dst
), ctx
->instance_id
);
5858 case nir_intrinsic_load_draw_id
: {
5859 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5860 bld
.copy(Definition(dst
), ctx
->draw_id
);
5864 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5865 nir_print_instr(&instr
->instr
, stderr
);
5866 fprintf(stderr
, "\n");
5874 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
5875 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
5876 enum glsl_base_type
*stype
)
5878 nir_deref_instr
*texture_deref_instr
= NULL
;
5879 nir_deref_instr
*sampler_deref_instr
= NULL
;
5882 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5883 switch (instr
->src
[i
].src_type
) {
5884 case nir_tex_src_texture_deref
:
5885 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5887 case nir_tex_src_sampler_deref
:
5888 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5890 case nir_tex_src_plane
:
5891 plane
= nir_src_as_int(instr
->src
[i
].src
);
5898 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
5900 if (!sampler_deref_instr
)
5901 sampler_deref_instr
= texture_deref_instr
;
5904 assert(instr
->op
!= nir_texop_txf_ms
&&
5905 instr
->op
!= nir_texop_samples_identical
);
5906 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
5907 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
5908 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
5909 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
5911 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
5914 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
5915 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
5916 fprintf(stderr
, "Unimplemented sampler descriptor: ");
5917 nir_print_instr(&instr
->instr
, stderr
);
5918 fprintf(stderr
, "\n");
5920 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
5923 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
5924 instr
->op
== nir_texop_samples_identical
))
5925 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
5928 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
5929 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
5931 Builder
bld(ctx
->program
, ctx
->block
);
5933 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
5934 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
5935 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
5937 Operand
neg_one(0xbf800000u
);
5938 Operand
one(0x3f800000u
);
5939 Operand
two(0x40000000u
);
5940 Operand
four(0x40800000u
);
5942 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
5943 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
5944 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
5946 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
5947 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
5948 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
5949 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
);
5952 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
5953 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
5954 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
5956 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
5959 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
5960 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
5961 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
5964 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
5965 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
5967 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
5968 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
5971 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
5973 Builder
bld(ctx
->program
, ctx
->block
);
5974 Temp coord_args
[4], ma
, tc
, sc
, id
;
5975 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
5976 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
5979 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
5981 // see comment in ac_prepare_cube_coords()
5982 if (ctx
->options
->chip_class
<= GFX8
)
5983 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
5986 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
5988 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
5989 vop3a
->operands
[0] = Operand(ma
);
5990 vop3a
->abs
[0] = true;
5991 Temp invma
= bld
.tmp(v1
);
5992 vop3a
->definitions
[0] = Definition(invma
);
5993 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
5995 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
5997 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
5999 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6001 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6003 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6006 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6007 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6009 for (unsigned i
= 0; i
< 2; i
++) {
6010 // see comment in ac_prepare_cube_coords()
6012 Temp deriv_sc
, deriv_tc
;
6013 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6014 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6016 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6018 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6019 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6020 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6021 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6022 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6023 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6024 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6027 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6028 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6032 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6033 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6037 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6040 for (unsigned i
= 0; i
< coords
.size(); i
++)
6041 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6043 Builder
bld(ctx
->program
, ctx
->block
);
6044 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6046 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6047 for (unsigned i
= 0; i
< coords
.size(); i
++)
6048 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6049 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6050 vec
->definitions
[0] = Definition(res
);
6051 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6055 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6057 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6059 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6060 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6063 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6064 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6065 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6069 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6071 Builder
bld(ctx
->program
, ctx
->block
);
6072 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6073 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6074 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6075 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6076 nir_const_value
*sample_index_cv
= NULL
;
6077 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6078 enum glsl_base_type stype
;
6079 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6081 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6082 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6083 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6084 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6086 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6087 switch (instr
->src
[i
].src_type
) {
6088 case nir_tex_src_coord
:
6089 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6091 case nir_tex_src_bias
:
6092 if (instr
->op
== nir_texop_txb
) {
6093 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6097 case nir_tex_src_lod
: {
6098 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6100 if (val
&& val
->f32
<= 0.0) {
6103 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6108 case nir_tex_src_comparator
:
6109 if (instr
->is_shadow
) {
6110 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6114 case nir_tex_src_offset
:
6115 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6116 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6119 case nir_tex_src_ddx
:
6120 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6123 case nir_tex_src_ddy
:
6124 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6127 case nir_tex_src_ms_index
:
6128 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6129 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6130 has_sample_index
= true;
6132 case nir_tex_src_texture_offset
:
6133 case nir_tex_src_sampler_offset
:
6138 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6139 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6140 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6142 if (instr
->op
== nir_texop_texture_samples
) {
6143 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6145 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6146 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6147 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 */));
6148 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6150 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6151 samples
, Operand(1u), bld
.scc(is_msaa
));
6155 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6156 aco_ptr
<Instruction
> tmp_instr
;
6157 Temp acc
, pack
= Temp();
6159 uint32_t pack_const
= 0;
6160 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6161 if (!const_offset
[i
])
6163 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6166 if (offset
.type() == RegType::sgpr
) {
6167 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6168 if (const_offset
[i
])
6171 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6172 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6175 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6178 if (pack
== Temp()) {
6181 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6185 if (pack_const
&& pack
!= Temp())
6186 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6188 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6189 if (const_offset
[i
])
6192 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6193 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6196 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6199 if (pack
== Temp()) {
6202 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6206 if (pack_const
&& pack
!= Temp())
6207 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6209 if (pack_const
&& pack
== Temp())
6210 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6211 else if (pack
== Temp())
6217 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6218 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6220 /* pack derivatives */
6221 if (has_ddx
|| has_ddy
) {
6222 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
>= GFX9
) {
6223 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6224 ddx
, Operand(0u), ddy
, Operand(0u));
6226 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6231 if (instr
->coord_components
> 1 &&
6232 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6234 instr
->op
!= nir_texop_txf
)
6235 coords
= apply_round_slice(ctx
, coords
, 1);
6237 if (instr
->coord_components
> 2 &&
6238 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6239 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6240 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6241 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6243 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6244 coords
= apply_round_slice(ctx
, coords
, 2);
6246 if (ctx
->options
->chip_class
>= GFX9
&&
6247 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6248 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6249 assert(coords
.size() > 0 && coords
.size() < 3);
6251 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6252 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6253 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6254 if (coords
.size() > 1)
6255 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6256 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6257 vec
->definitions
[0] = Definition(coords
);
6258 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6261 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6263 if (instr
->op
== nir_texop_samples_identical
)
6264 resource
= fmask_ptr
;
6266 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6267 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6268 instr
->op
!= nir_texop_txs
) {
6269 assert(has_sample_index
);
6270 Operand
op(sample_index
);
6271 if (sample_index_cv
)
6272 op
= Operand(sample_index_cv
->u32
);
6273 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6276 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6277 Temp split_coords
[coords
.size()];
6278 emit_split_vector(ctx
, coords
, coords
.size());
6279 for (unsigned i
= 0; i
< coords
.size(); i
++)
6280 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6283 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6284 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6285 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6288 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6289 for (unsigned i
= 0; i
< coords
.size(); i
++)
6290 vec
->operands
[i
] = Operand(split_coords
[i
]);
6291 coords
= bld
.tmp(coords
.regClass());
6292 vec
->definitions
[0] = Definition(coords
);
6293 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6298 /* Build tex instruction */
6299 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6300 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6303 /* gather4 selects the component by dmask and always returns vec4 */
6304 if (instr
->op
== nir_texop_tg4
) {
6305 assert(instr
->dest
.ssa
.num_components
== 4);
6306 if (instr
->is_shadow
)
6309 dmask
= 1 << instr
->component
;
6310 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6311 tmp_dst
= bld
.tmp(v4
);
6312 } else if (instr
->op
== nir_texop_samples_identical
) {
6313 tmp_dst
= bld
.tmp(v1
);
6314 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6315 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6318 aco_ptr
<MIMG_instruction
> tex
;
6319 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6321 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6323 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6324 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6327 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6328 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6330 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6331 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6332 tex
->operands
[1] = Operand(resource
);
6333 if (ctx
->options
->chip_class
>= GFX9
&&
6334 instr
->op
== nir_texop_txs
&&
6335 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6337 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6338 } else if (instr
->op
== nir_texop_query_levels
) {
6339 tex
->dmask
= 1 << 3;
6344 tex
->definitions
[0] = Definition(tmp_dst
);
6345 tex
->can_reorder
= true;
6346 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6349 /* divide 3rd value by 6 by multiplying with magic number */
6350 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6351 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6352 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6353 assert(instr
->dest
.ssa
.num_components
== 3);
6354 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6355 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6356 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6357 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6362 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6366 Temp tg4_compare_cube_wa64
= Temp();
6368 if (tg4_integer_workarounds
) {
6369 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6370 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6371 tex
->operands
[1] = Operand(resource
);
6374 Temp size
= bld
.tmp(v2
);
6375 tex
->definitions
[0] = Definition(size
);
6376 tex
->can_reorder
= true;
6377 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6378 emit_split_vector(ctx
, size
, size
.size());
6381 for (unsigned i
= 0; i
< 2; i
++) {
6382 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6383 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6384 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6385 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6388 Temp orig_coords
[2] = {
6389 emit_extract_vector(ctx
, coords
, 0, v1
),
6390 emit_extract_vector(ctx
, coords
, 1, v1
)};
6391 Temp new_coords
[2] = {
6392 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6393 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6396 if (tg4_integer_cube_workaround
) {
6397 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6398 Temp desc
[resource
.size()];
6399 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6400 Format::PSEUDO
, 1, resource
.size())};
6401 split
->operands
[0] = Operand(resource
);
6402 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6403 desc
[i
] = bld
.tmp(s1
);
6404 split
->definitions
[i
] = Definition(desc
[i
]);
6406 ctx
->block
->instructions
.emplace_back(std::move(split
));
6408 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6409 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6410 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6413 if (stype
== GLSL_TYPE_UINT
) {
6414 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6415 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6416 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6417 bld
.scc(compare_cube_wa
));
6419 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6420 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6421 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6422 bld
.scc(compare_cube_wa
));
6424 tg4_compare_cube_wa64
= as_divergent_bool(ctx
, compare_cube_wa
, true);
6425 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6427 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6428 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6429 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6431 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6432 Format::PSEUDO
, resource
.size(), 1)};
6433 for (unsigned i
= 0; i
< resource
.size(); i
++)
6434 vec
->operands
[i
] = Operand(desc
[i
]);
6435 resource
= bld
.tmp(resource
.regClass());
6436 vec
->definitions
[0] = Definition(resource
);
6437 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6439 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6440 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6441 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6442 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6445 if (coords
.size() == 3) {
6446 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6447 new_coords
[0], new_coords
[1],
6448 emit_extract_vector(ctx
, coords
, 2, v1
));
6450 assert(coords
.size() == 2);
6451 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6452 new_coords
[0], new_coords
[1]);
6456 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6457 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6458 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6459 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6461 std::vector
<Operand
> args
;
6463 args
.emplace_back(Operand(offset
));
6465 args
.emplace_back(Operand(bias
));
6467 args
.emplace_back(Operand(compare
));
6469 args
.emplace_back(Operand(derivs
));
6470 args
.emplace_back(Operand(coords
));
6471 if (has_sample_index
)
6472 args
.emplace_back(Operand(sample_index
));
6474 args
.emplace_back(lod
);
6477 if (args
.size() > 1) {
6478 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6480 for (unsigned i
= 0; i
< args
.size(); i
++) {
6481 size
+= args
[i
].size();
6482 vec
->operands
[i
] = args
[i
];
6484 RegClass rc
= RegClass(RegType::vgpr
, size
);
6485 Temp tmp
= bld
.tmp(rc
);
6486 vec
->definitions
[0] = Definition(tmp
);
6487 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6490 assert(args
[0].isTemp());
6491 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6494 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6495 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6497 assert(coords
.size() == 1);
6498 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6502 op
= aco_opcode::buffer_load_format_x
; break;
6504 op
= aco_opcode::buffer_load_format_xy
; break;
6506 op
= aco_opcode::buffer_load_format_xyz
; break;
6508 op
= aco_opcode::buffer_load_format_xyzw
; break;
6510 unreachable("Tex instruction loads more than 4 components.");
6513 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6514 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6517 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6519 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6520 mubuf
->operands
[0] = Operand(coords
);
6521 mubuf
->operands
[1] = Operand(resource
);
6522 mubuf
->operands
[2] = Operand((uint32_t) 0);
6523 mubuf
->definitions
[0] = Definition(tmp_dst
);
6524 mubuf
->idxen
= true;
6525 mubuf
->can_reorder
= true;
6526 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6528 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6533 if (instr
->op
== nir_texop_txf
||
6534 instr
->op
== nir_texop_txf_ms
||
6535 instr
->op
== nir_texop_samples_identical
) {
6536 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6537 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6538 tex
->operands
[0] = Operand(arg
);
6539 tex
->operands
[1] = Operand(resource
);
6543 tex
->definitions
[0] = Definition(tmp_dst
);
6544 tex
->can_reorder
= true;
6545 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6547 if (instr
->op
== nir_texop_samples_identical
) {
6548 assert(dmask
== 1 && dst
.regClass() == v1
);
6549 assert(dst
.id() != tmp_dst
.id());
6551 Temp tmp
= bld
.tmp(s2
);
6552 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6553 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6556 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6561 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6562 aco_opcode opcode
= aco_opcode::image_sample
;
6563 if (has_offset
) { /* image_sample_*_o */
6565 opcode
= aco_opcode::image_sample_c_o
;
6567 opcode
= aco_opcode::image_sample_c_d_o
;
6569 opcode
= aco_opcode::image_sample_c_b_o
;
6571 opcode
= aco_opcode::image_sample_c_lz_o
;
6573 opcode
= aco_opcode::image_sample_c_l_o
;
6575 opcode
= aco_opcode::image_sample_o
;
6577 opcode
= aco_opcode::image_sample_d_o
;
6579 opcode
= aco_opcode::image_sample_b_o
;
6581 opcode
= aco_opcode::image_sample_lz_o
;
6583 opcode
= aco_opcode::image_sample_l_o
;
6585 } else { /* no offset */
6587 opcode
= aco_opcode::image_sample_c
;
6589 opcode
= aco_opcode::image_sample_c_d
;
6591 opcode
= aco_opcode::image_sample_c_b
;
6593 opcode
= aco_opcode::image_sample_c_lz
;
6595 opcode
= aco_opcode::image_sample_c_l
;
6597 opcode
= aco_opcode::image_sample
;
6599 opcode
= aco_opcode::image_sample_d
;
6601 opcode
= aco_opcode::image_sample_b
;
6603 opcode
= aco_opcode::image_sample_lz
;
6605 opcode
= aco_opcode::image_sample_l
;
6609 if (instr
->op
== nir_texop_tg4
) {
6611 opcode
= aco_opcode::image_gather4_lz_o
;
6613 opcode
= aco_opcode::image_gather4_c_lz_o
;
6615 opcode
= aco_opcode::image_gather4_lz
;
6617 opcode
= aco_opcode::image_gather4_c_lz
;
6619 } else if (instr
->op
== nir_texop_lod
) {
6620 opcode
= aco_opcode::image_get_lod
;
6623 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6624 tex
->operands
[0] = arg
;
6625 tex
->operands
[1] = Operand(resource
);
6626 tex
->operands
[2] = Operand(sampler
);
6629 tex
->definitions
[0] = Definition(tmp_dst
);
6630 tex
->can_reorder
= true;
6631 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6633 if (tg4_integer_cube_workaround
) {
6634 assert(tmp_dst
.id() != dst
.id());
6635 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6637 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6639 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6640 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6642 if (stype
== GLSL_TYPE_UINT
)
6643 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6645 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6646 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6648 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6649 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6650 val
[0], val
[1], val
[2], val
[3]);
6652 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6653 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6658 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6660 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6661 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6662 return Operand(tmp
.regClass());
6664 return Operand(tmp
);
6667 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6669 aco_ptr
<Pseudo_instruction
> phi
;
6670 unsigned num_src
= exec_list_length(&instr
->srcs
);
6671 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6673 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6675 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6676 bool all_undef
= true;
6677 nir_foreach_phi_src(src
, instr
) {
6678 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6679 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6683 Builder
bld(ctx
->program
, ctx
->block
);
6684 if (dst
.regClass() == s1
) {
6685 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6686 } else if (dst
.regClass() == v1
) {
6687 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6689 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6690 for (unsigned i
= 0; i
< dst
.size(); i
++)
6691 vec
->operands
[i
] = Operand(0u);
6692 vec
->definitions
[0] = Definition(dst
);
6693 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6698 /* try to scalarize vector phis */
6699 if (dst
.size() > 1) {
6700 // TODO: scalarize linear phis on divergent ifs
6701 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6702 std::array
<Temp
, 4> new_vec
;
6703 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6704 Operand src
= get_phi_operand(ctx
, pair
.second
);
6705 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6706 can_scalarize
= false;
6710 if (can_scalarize
) {
6711 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6712 assert(dst
.size() % num_components
== 0);
6713 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6715 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6716 for (unsigned k
= 0; k
< num_components
; k
++) {
6717 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6718 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6719 for (unsigned i
= 0; i
< num_src
; i
++) {
6720 Operand src
= get_phi_operand(ctx
, it
->second
);
6721 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6724 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6725 phi
->definitions
[0] = Definition(phi_dst
);
6726 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6727 new_vec
[k
] = phi_dst
;
6728 vec
->operands
[k
] = Operand(phi_dst
);
6730 vec
->definitions
[0] = Definition(dst
);
6731 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6732 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6737 unsigned extra_src
= 0;
6738 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6739 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6743 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6745 /* if we have a linear phi on a divergent if, we know that one src is undef */
6746 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6747 assert(extra_src
== 0);
6749 /* we place the phi either in the invert-block or in the current block */
6750 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6751 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6752 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6753 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6754 assert(block
->kind
& block_kind_invert
);
6755 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6757 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6759 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6761 phi
->operands
[1] = Operand(dst
.regClass());
6762 phi
->definitions
[0] = Definition(dst
);
6763 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6767 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6768 for (unsigned i
= 0; i
< num_src
; i
++) {
6769 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6772 for (unsigned i
= 0; i
< extra_src
; i
++)
6773 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6774 phi
->definitions
[0] = Definition(dst
);
6775 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6779 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6781 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6783 assert(dst
.type() == RegType::sgpr
);
6785 if (dst
.size() == 1) {
6786 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6788 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6789 for (unsigned i
= 0; i
< dst
.size(); i
++)
6790 vec
->operands
[i
] = Operand(0u);
6791 vec
->definitions
[0] = Definition(dst
);
6792 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6796 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6798 Builder
bld(ctx
->program
, ctx
->block
);
6799 Block
*logical_target
;
6800 append_logical_end(ctx
->block
);
6801 unsigned idx
= ctx
->block
->index
;
6803 switch (instr
->type
) {
6804 case nir_jump_break
:
6805 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6806 add_logical_edge(idx
, logical_target
);
6807 ctx
->block
->kind
|= block_kind_break
;
6809 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6810 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6811 /* uniform break - directly jump out of the loop */
6812 ctx
->block
->kind
|= block_kind_uniform
;
6813 ctx
->cf_info
.has_branch
= true;
6814 bld
.branch(aco_opcode::p_branch
);
6815 add_linear_edge(idx
, logical_target
);
6818 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6820 case nir_jump_continue
:
6821 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6822 add_logical_edge(idx
, logical_target
);
6823 ctx
->block
->kind
|= block_kind_continue
;
6825 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6826 /* for potential uniform breaks after this continue,
6827 we must ensure that they are handled correctly */
6828 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6829 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6831 /* uniform continue - directly jump to the loop header */
6832 ctx
->block
->kind
|= block_kind_uniform
;
6833 ctx
->cf_info
.has_branch
= true;
6834 bld
.branch(aco_opcode::p_branch
);
6835 add_linear_edge(idx
, logical_target
);
6840 fprintf(stderr
, "Unknown NIR jump instr: ");
6841 nir_print_instr(&instr
->instr
, stderr
);
6842 fprintf(stderr
, "\n");
6846 /* remove critical edges from linear CFG */
6847 bld
.branch(aco_opcode::p_branch
);
6848 Block
* break_block
= ctx
->program
->create_and_insert_block();
6849 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6850 break_block
->kind
|= block_kind_uniform
;
6851 add_linear_edge(idx
, break_block
);
6852 /* the loop_header pointer might be invalidated by this point */
6853 if (instr
->type
== nir_jump_continue
)
6854 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6855 add_linear_edge(break_block
->index
, logical_target
);
6856 bld
.reset(break_block
);
6857 bld
.branch(aco_opcode::p_branch
);
6859 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6860 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6861 add_linear_edge(idx
, continue_block
);
6862 append_logical_start(continue_block
);
6863 ctx
->block
= continue_block
;
6867 void visit_block(isel_context
*ctx
, nir_block
*block
)
6869 nir_foreach_instr(instr
, block
) {
6870 switch (instr
->type
) {
6871 case nir_instr_type_alu
:
6872 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
6874 case nir_instr_type_load_const
:
6875 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
6877 case nir_instr_type_intrinsic
:
6878 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
6880 case nir_instr_type_tex
:
6881 visit_tex(ctx
, nir_instr_as_tex(instr
));
6883 case nir_instr_type_phi
:
6884 visit_phi(ctx
, nir_instr_as_phi(instr
));
6886 case nir_instr_type_ssa_undef
:
6887 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
6889 case nir_instr_type_deref
:
6891 case nir_instr_type_jump
:
6892 visit_jump(ctx
, nir_instr_as_jump(instr
));
6895 fprintf(stderr
, "Unknown NIR instr type: ");
6896 nir_print_instr(instr
, stderr
);
6897 fprintf(stderr
, "\n");
6905 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
6907 append_logical_end(ctx
->block
);
6908 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
6909 Builder
bld(ctx
->program
, ctx
->block
);
6910 bld
.branch(aco_opcode::p_branch
);
6911 unsigned loop_preheader_idx
= ctx
->block
->index
;
6913 Block loop_exit
= Block();
6914 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6915 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
6917 Block
* loop_header
= ctx
->program
->create_and_insert_block();
6918 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
6919 loop_header
->kind
|= block_kind_loop_header
;
6920 add_edge(loop_preheader_idx
, loop_header
);
6921 ctx
->block
= loop_header
;
6923 /* emit loop body */
6924 unsigned loop_header_idx
= loop_header
->index
;
6925 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
6926 append_logical_start(ctx
->block
);
6927 visit_cf_list(ctx
, &loop
->body
);
6929 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
6930 if (!ctx
->cf_info
.has_branch
) {
6931 append_logical_end(ctx
->block
);
6932 if (ctx
->cf_info
.exec_potentially_empty
) {
6933 /* Discards can result in code running with an empty exec mask.
6934 * This would result in divergent breaks not ever being taken. As a
6935 * workaround, break the loop when the loop mask is empty instead of
6936 * always continuing. */
6937 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
6939 /* create "loop_almost_exit" to avoid critical edges */
6940 unsigned block_idx
= ctx
->block
->index
;
6941 Block
*loop_almost_exit
= ctx
->program
->create_and_insert_block();
6942 loop_almost_exit
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6943 loop_almost_exit
->kind
= block_kind_uniform
;
6944 bld
.reset(loop_almost_exit
);
6945 bld
.branch(aco_opcode::p_branch
);
6947 add_linear_edge(block_idx
, loop_almost_exit
);
6948 add_linear_edge(loop_almost_exit
->index
, &loop_exit
);
6950 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
6952 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
6954 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
6955 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
6957 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
6958 bld
.reset(ctx
->block
);
6959 bld
.branch(aco_opcode::p_branch
);
6962 /* fixup phis in loop header from unreachable blocks */
6963 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
6964 bool linear
= ctx
->cf_info
.has_branch
;
6965 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
6966 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
6967 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
6968 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
6969 /* the last operand should be the one that needs to be removed */
6970 instr
->operands
.pop_back();
6971 } else if (!is_phi(instr
)) {
6977 ctx
->cf_info
.has_branch
= false;
6979 // TODO: if the loop has not a single exit, we must add one °°
6980 /* emit loop successor block */
6981 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
6982 append_logical_start(ctx
->block
);
6985 // TODO: check if it is beneficial to not branch on continues
6986 /* trim linear phis in loop header */
6987 for (auto&& instr
: loop_entry
->instructions
) {
6988 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
6989 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
6990 new_phi
->definitions
[0] = instr
->definitions
[0];
6991 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
6992 new_phi
->operands
[i
] = instr
->operands
[i
];
6993 /* check that the remaining operands are all the same */
6994 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
6995 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
6996 instr
.swap(new_phi
);
6997 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7006 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7010 append_logical_end(ctx
->block
);
7011 ctx
->block
->kind
|= block_kind_branch
;
7013 /* branch to linear then block */
7014 assert(cond
.regClass() == s2
);
7015 aco_ptr
<Pseudo_branch_instruction
> branch
;
7016 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7017 branch
->operands
[0] = Operand(cond
);
7018 ctx
->block
->instructions
.push_back(std::move(branch
));
7020 ic
->BB_if_idx
= ctx
->block
->index
;
7021 ic
->BB_invert
= Block();
7022 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7023 /* Invert blocks are intentionally not marked as top level because they
7024 * are not part of the logical cfg. */
7025 ic
->BB_invert
.kind
|= block_kind_invert
;
7026 ic
->BB_endif
= Block();
7027 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7028 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7030 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7031 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7032 ctx
->cf_info
.parent_if
.is_divergent
= true;
7033 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7035 /** emit logical then block */
7036 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7037 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7038 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7039 ctx
->block
= BB_then_logical
;
7040 append_logical_start(BB_then_logical
);
7043 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7045 Block
*BB_then_logical
= ctx
->block
;
7046 append_logical_end(BB_then_logical
);
7047 /* branch from logical then block to invert block */
7048 aco_ptr
<Pseudo_branch_instruction
> branch
;
7049 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7050 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7051 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7052 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7053 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7054 BB_then_logical
->kind
|= block_kind_uniform
;
7055 assert(!ctx
->cf_info
.has_branch
);
7056 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7057 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7059 /** emit linear then block */
7060 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7061 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7062 BB_then_linear
->kind
|= block_kind_uniform
;
7063 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7064 /* branch from linear then block to invert block */
7065 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7066 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7067 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7069 /** emit invert merge block */
7070 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7071 ic
->invert_idx
= ctx
->block
->index
;
7073 /* branch to linear else block (skip else) */
7074 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7075 branch
->operands
[0] = Operand(ic
->cond
);
7076 ctx
->block
->instructions
.push_back(std::move(branch
));
7078 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7079 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7081 /** emit logical else block */
7082 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7083 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7084 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7085 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7086 ctx
->block
= BB_else_logical
;
7087 append_logical_start(BB_else_logical
);
7090 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7092 Block
*BB_else_logical
= ctx
->block
;
7093 append_logical_end(BB_else_logical
);
7095 /* branch from logical else block to endif block */
7096 aco_ptr
<Pseudo_branch_instruction
> branch
;
7097 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7098 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7099 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7100 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7101 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7102 BB_else_logical
->kind
|= block_kind_uniform
;
7104 assert(!ctx
->cf_info
.has_branch
);
7105 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7108 /** emit linear else block */
7109 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7110 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7111 BB_else_linear
->kind
|= block_kind_uniform
;
7112 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7114 /* branch from linear else block to endif block */
7115 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7116 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7117 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7120 /** emit endif merge block */
7121 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7122 append_logical_start(ctx
->block
);
7125 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7126 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7127 /* uniform control flow never has an empty exec-mask */
7128 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7129 ctx
->cf_info
.exec_potentially_empty
= false;
7132 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7134 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7135 Builder
bld(ctx
->program
, ctx
->block
);
7136 aco_ptr
<Pseudo_branch_instruction
> branch
;
7138 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7140 * Uniform conditionals are represented in the following way*) :
7142 * The linear and logical CFG:
7145 * BB_THEN (logical) BB_ELSE (logical)
7149 * *) Exceptions may be due to break and continue statements within loops
7150 * If a break/continue happens within uniform control flow, it branches
7151 * to the loop exit/entry block. Otherwise, it branches to the next
7154 append_logical_end(ctx
->block
);
7155 ctx
->block
->kind
|= block_kind_uniform
;
7158 if (cond
.regClass() == s2
) {
7159 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7160 cond
= as_uniform_bool(ctx
, cond
);
7162 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7163 branch
->operands
[0] = Operand(cond
);
7164 branch
->operands
[0].setFixed(scc
);
7165 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7167 unsigned BB_if_idx
= ctx
->block
->index
;
7168 Block BB_endif
= Block();
7169 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7170 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7172 /** emit then block */
7173 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7174 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7175 add_edge(BB_if_idx
, BB_then
);
7176 append_logical_start(BB_then
);
7177 ctx
->block
= BB_then
;
7178 visit_cf_list(ctx
, &if_stmt
->then_list
);
7179 BB_then
= ctx
->block
;
7180 bool then_branch
= ctx
->cf_info
.has_branch
;
7181 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7184 append_logical_end(BB_then
);
7185 /* branch from then block to endif block */
7186 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7187 BB_then
->instructions
.emplace_back(std::move(branch
));
7188 add_linear_edge(BB_then
->index
, &BB_endif
);
7189 if (!then_branch_divergent
)
7190 add_logical_edge(BB_then
->index
, &BB_endif
);
7191 BB_then
->kind
|= block_kind_uniform
;
7194 ctx
->cf_info
.has_branch
= false;
7195 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7197 /** emit else block */
7198 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7199 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7200 add_edge(BB_if_idx
, BB_else
);
7201 append_logical_start(BB_else
);
7202 ctx
->block
= BB_else
;
7203 visit_cf_list(ctx
, &if_stmt
->else_list
);
7204 BB_else
= ctx
->block
;
7206 if (!ctx
->cf_info
.has_branch
) {
7207 append_logical_end(BB_else
);
7208 /* branch from then block to endif block */
7209 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7210 BB_else
->instructions
.emplace_back(std::move(branch
));
7211 add_linear_edge(BB_else
->index
, &BB_endif
);
7212 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7213 add_logical_edge(BB_else
->index
, &BB_endif
);
7214 BB_else
->kind
|= block_kind_uniform
;
7217 ctx
->cf_info
.has_branch
&= then_branch
;
7218 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7220 /** emit endif merge block */
7221 if (!ctx
->cf_info
.has_branch
) {
7222 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7223 append_logical_start(ctx
->block
);
7225 } else { /* non-uniform condition */
7227 * To maintain a logical and linear CFG without critical edges,
7228 * non-uniform conditionals are represented in the following way*) :
7233 * BB_THEN (logical) BB_THEN (linear)
7235 * BB_INVERT (linear)
7237 * BB_ELSE (logical) BB_ELSE (linear)
7244 * BB_THEN (logical) BB_ELSE (logical)
7248 * *) Exceptions may be due to break and continue statements within loops
7253 begin_divergent_if_then(ctx
, &ic
, cond
);
7254 visit_cf_list(ctx
, &if_stmt
->then_list
);
7256 begin_divergent_if_else(ctx
, &ic
);
7257 visit_cf_list(ctx
, &if_stmt
->else_list
);
7259 end_divergent_if(ctx
, &ic
);
7263 static void visit_cf_list(isel_context
*ctx
,
7264 struct exec_list
*list
)
7266 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7267 switch (node
->type
) {
7268 case nir_cf_node_block
:
7269 visit_block(ctx
, nir_cf_node_as_block(node
));
7271 case nir_cf_node_if
:
7272 visit_if(ctx
, nir_cf_node_as_if(node
));
7274 case nir_cf_node_loop
:
7275 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7278 unreachable("unimplemented cf list type");
7283 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7285 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7286 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7287 if (!is_pos
&& !mask
)
7289 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7291 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7292 exp
->enabled_mask
= mask
;
7293 for (unsigned i
= 0; i
< 4; ++i
) {
7294 if (mask
& (1 << i
))
7295 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7297 exp
->operands
[i
] = Operand(v1
);
7299 exp
->valid_mask
= false;
7301 exp
->compressed
= false;
7303 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7305 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7306 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7309 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7311 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7312 exp
->enabled_mask
= 0;
7313 for (unsigned i
= 0; i
< 4; ++i
)
7314 exp
->operands
[i
] = Operand(v1
);
7315 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7316 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7317 exp
->enabled_mask
|= 0x1;
7319 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7320 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7321 exp
->enabled_mask
|= 0x4;
7323 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7324 if (ctx
->options
->chip_class
< GFX9
) {
7325 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7326 exp
->enabled_mask
|= 0x8;
7328 Builder
bld(ctx
->program
, ctx
->block
);
7330 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7331 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7332 if (exp
->operands
[2].isTemp())
7333 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7335 exp
->operands
[2] = Operand(out
);
7336 exp
->enabled_mask
|= 0x4;
7339 exp
->valid_mask
= false;
7341 exp
->compressed
= false;
7342 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7343 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7346 static void create_vs_exports(isel_context
*ctx
)
7348 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7350 if (outinfo
->export_prim_id
) {
7351 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7352 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = ctx
->vs_prim_id
;
7355 if (ctx
->options
->key
.has_multiview_view_index
) {
7356 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7357 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, ctx
->view_index
);
7360 /* the order these position exports are created is important */
7362 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7363 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7364 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7366 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7367 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7368 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7369 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7371 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7372 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7373 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7374 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7375 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7378 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7379 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7380 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7383 export_vs_varying(ctx
, i
, false, NULL
);
7387 static void emit_stream_output(isel_context
*ctx
,
7388 Temp
const *so_buffers
,
7389 Temp
const *so_write_offset
,
7390 const struct radv_stream_output
*output
)
7392 unsigned num_comps
= util_bitcount(output
->component_mask
);
7393 unsigned loc
= output
->location
;
7394 unsigned buf
= output
->buffer
;
7395 unsigned offset
= output
->offset
;
7397 assert(num_comps
&& num_comps
<= 4);
7398 if (!num_comps
|| num_comps
> 4)
7401 unsigned start
= ffs(output
->component_mask
) - 1;
7404 bool all_undef
= true;
7405 assert(ctx
->stage
== vertex_vs
);
7406 for (unsigned i
= 0; i
< num_comps
; i
++) {
7407 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7408 all_undef
= all_undef
&& !out
[i
].id();
7413 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7414 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7415 for (unsigned i
= 0; i
< num_comps
; ++i
)
7416 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7417 vec
->definitions
[0] = Definition(write_data
);
7418 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7421 switch (num_comps
) {
7423 opcode
= aco_opcode::buffer_store_dword
;
7426 opcode
= aco_opcode::buffer_store_dwordx2
;
7429 opcode
= aco_opcode::buffer_store_dwordx3
;
7432 opcode
= aco_opcode::buffer_store_dwordx4
;
7436 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7437 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7438 store
->operands
[1] = Operand(so_buffers
[buf
]);
7439 store
->operands
[2] = Operand((uint32_t) 0);
7440 store
->operands
[3] = Operand(write_data
);
7441 if (offset
> 4095) {
7442 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7443 Builder
bld(ctx
->program
, ctx
->block
);
7444 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7446 store
->offset
= offset
;
7448 store
->offen
= true;
7451 store
->can_reorder
= true;
7452 ctx
->block
->instructions
.emplace_back(std::move(store
));
7455 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7457 Builder
bld(ctx
->program
, ctx
->block
);
7460 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, ctx
->streamout_buffers
);
7461 for (unsigned i
= 0; i
< 4; i
++) {
7462 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7466 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7469 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7470 ctx
->streamout_config
, Operand(0x70010u
));
7472 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7473 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7475 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7478 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7480 bld
.reset(ctx
->block
);
7482 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), ctx
->streamout_write_idx
, tid
);
7484 Temp so_write_offset
[4];
7486 for (unsigned i
= 0; i
< 4; i
++) {
7487 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7492 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7493 ctx
->streamout_write_idx
, ctx
->streamout_offset
[i
]);
7494 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7496 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7498 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7499 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u), ctx
->streamout_offset
[i
]);
7500 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7504 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7505 struct radv_stream_output
*output
=
7506 &ctx
->program
->info
->so
.outputs
[i
];
7507 if (stream
!= output
->stream
)
7510 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7513 begin_divergent_if_else(ctx
, &ic
);
7514 end_divergent_if(ctx
, &ic
);
7517 } /* end namespace */
7519 void handle_bc_optimize(isel_context
*ctx
)
7521 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7522 Builder
bld(ctx
->program
, ctx
->block
);
7523 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7524 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7525 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7526 if (uses_center
&& uses_centroid
) {
7527 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)), ctx
->prim_mask
, Operand(0u));
7529 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7530 for (unsigned i
= 0; i
< 2; i
++) {
7531 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7532 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
],
7533 ctx
->fs_inputs
[fs_input::persp_center_p1
+ i
],
7535 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
] = new_coord
;
7539 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7540 for (unsigned i
= 0; i
< 2; i
++) {
7541 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7542 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
],
7543 ctx
->fs_inputs
[fs_input::linear_center_p1
+ i
],
7545 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
] = new_coord
;
7551 void select_program(Program
*program
,
7552 unsigned shader_count
,
7553 struct nir_shader
*const *shaders
,
7554 ac_shader_config
* config
,
7555 struct radv_shader_info
*info
,
7556 struct radv_nir_compiler_options
*options
)
7558 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, info
, options
);
7560 for (unsigned i
= 0; i
< shader_count
; i
++) {
7561 nir_shader
*nir
= shaders
[i
];
7562 init_context(&ctx
, nir
);
7565 add_startpgm(&ctx
); /* needs to be after init_context() for FS */
7566 append_logical_start(ctx
.block
);
7570 if (shader_count
>= 2) {
7571 Builder
bld(ctx
.program
, ctx
.block
);
7572 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)));
7573 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7574 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7575 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7577 begin_divergent_if_then(&ctx
, &ic
, cond
);
7581 Builder
bld(ctx
.program
, ctx
.block
);
7582 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7583 bld
.sopp(aco_opcode::s_barrier
);
7586 if (ctx
.stage
== fragment_fs
)
7587 handle_bc_optimize(&ctx
);
7589 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7590 visit_cf_list(&ctx
, &func
->body
);
7592 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7593 emit_streamout(&ctx
, 0);
7595 if (ctx
.stage
== vertex_vs
)
7596 create_vs_exports(&ctx
);
7598 if (shader_count
>= 2) {
7599 begin_divergent_if_else(&ctx
, &ic
);
7600 end_divergent_if(&ctx
, &ic
);
7603 ralloc_free(ctx
.divergent_vals
);
7606 append_logical_end(ctx
.block
);
7607 ctx
.block
->kind
|= block_kind_uniform
;
7608 Builder
bld(ctx
.program
, ctx
.block
);
7609 if (ctx
.program
->wb_smem_l1_on_end
)
7610 bld
.smem(aco_opcode::s_dcache_wb
, false);
7611 bld
.sopp(aco_opcode::s_endpgm
);
7614 for (Block
& BB
: program
->blocks
) {
7615 for (unsigned idx
: BB
.linear_preds
)
7616 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7617 for (unsigned idx
: BB
.logical_preds
)
7618 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);