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
31 #include "aco_builder.h"
32 #include "aco_interface.h"
33 #include "aco_instruction_selection_setup.cpp"
34 #include "util/fast_idiv_by_const.h"
39 class loop_info_RAII
{
41 unsigned header_idx_old
;
43 bool divergent_cont_old
;
44 bool divergent_branch_old
;
45 bool divergent_if_old
;
48 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
50 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
51 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
52 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
53 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
55 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
56 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
57 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
58 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
59 ctx
->cf_info
.parent_if
.is_divergent
= false;
60 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
65 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
66 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
67 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
68 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
69 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
70 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
71 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
72 ctx
->cf_info
.exec_potentially_empty
= false;
80 bool exec_potentially_empty_old
;
84 bool then_branch_divergent
;
89 static void visit_cf_list(struct isel_context
*ctx
,
90 struct exec_list
*list
);
92 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
94 succ
->logical_preds
.emplace_back(pred_idx
);
98 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
100 succ
->linear_preds
.emplace_back(pred_idx
);
103 static void add_edge(unsigned pred_idx
, Block
*succ
)
105 add_logical_edge(pred_idx
, succ
);
106 add_linear_edge(pred_idx
, succ
);
109 static void append_logical_start(Block
*b
)
111 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
114 static void append_logical_end(Block
*b
)
116 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
119 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
121 assert(ctx
->allocated
[def
->index
].id());
122 return ctx
->allocated
[def
->index
];
125 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
127 Builder
bld(ctx
->program
, ctx
->block
);
130 dst
= bld
.tmp(src
.regClass());
132 if (ctx
->stage
!= fragment_fs
) {
136 if (src
.type() == RegType::vgpr
|| src
.size() > 1)
137 bld
.copy(Definition(dst
), src
);
139 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
143 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
144 ctx
->program
->needs_wqm
|= program_needs_wqm
;
148 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
150 if (val
.type() == RegType::sgpr
) {
151 Builder
bld(ctx
->program
, ctx
->block
);
152 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
154 assert(val
.type() == RegType::vgpr
);
158 //assumes a != 0xffffffff
159 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
162 Builder
bld(ctx
->program
, ctx
->block
);
164 if (util_is_power_of_two_or_zero(b
)) {
165 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
169 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
171 assert(info
.multiplier
<= 0xffffffff);
173 bool pre_shift
= info
.pre_shift
!= 0;
174 bool increment
= info
.increment
!= 0;
175 bool multiply
= true;
176 bool post_shift
= info
.post_shift
!= 0;
178 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
179 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
183 Temp pre_shift_dst
= a
;
185 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
186 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
189 Temp increment_dst
= pre_shift_dst
;
191 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
192 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
195 Temp multiply_dst
= increment_dst
;
197 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
198 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
199 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
203 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
207 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
209 Builder
bld(ctx
->program
, ctx
->block
);
210 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
214 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
216 /* no need to extract the whole vector */
217 if (src
.regClass() == dst_rc
) {
221 assert(src
.size() > idx
);
222 Builder
bld(ctx
->program
, ctx
->block
);
223 auto it
= ctx
->allocated_vec
.find(src
.id());
224 /* the size check needs to be early because elements other than 0 may be garbage */
225 if (it
!= ctx
->allocated_vec
.end() && it
->second
[0].size() == dst_rc
.size()) {
226 if (it
->second
[idx
].regClass() == dst_rc
) {
227 return it
->second
[idx
];
229 assert(dst_rc
.size() == it
->second
[idx
].regClass().size());
230 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
231 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
235 if (src
.size() == dst_rc
.size()) {
237 return bld
.copy(bld
.def(dst_rc
), src
);
239 Temp dst
= bld
.tmp(dst_rc
);
240 emit_extract_vector(ctx
, src
, idx
, dst
);
245 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
247 if (num_components
== 1)
249 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
251 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
252 split
->operands
[0] = Operand(vec_src
);
253 std::array
<Temp
,4> elems
;
254 for (unsigned i
= 0; i
< num_components
; i
++) {
255 elems
[i
] = {ctx
->program
->allocateId(), RegClass(vec_src
.type(), vec_src
.size() / num_components
)};
256 split
->definitions
[i
] = Definition(elems
[i
]);
258 ctx
->block
->instructions
.emplace_back(std::move(split
));
259 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
262 /* This vector expansion uses a mask to determine which elements in the new vector
263 * come from the original vector. The other elements are undefined. */
264 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
266 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
271 Builder
bld(ctx
->program
, ctx
->block
);
272 if (num_components
== 1) {
273 if (dst
.type() == RegType::sgpr
)
274 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
276 bld
.copy(Definition(dst
), vec_src
);
280 unsigned component_size
= dst
.size() / num_components
;
281 std::array
<Temp
,4> elems
;
283 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
284 vec
->definitions
[0] = Definition(dst
);
286 for (unsigned i
= 0; i
< num_components
; i
++) {
287 if (mask
& (1 << i
)) {
288 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
289 if (dst
.type() == RegType::sgpr
)
290 src
= bld
.as_uniform(src
);
291 vec
->operands
[i
] = Operand(src
);
293 vec
->operands
[i
] = Operand(0u);
295 elems
[i
] = vec
->operands
[i
].getTemp();
297 ctx
->block
->instructions
.emplace_back(std::move(vec
));
298 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
301 Temp
as_divergent_bool(isel_context
*ctx
, Temp val
, bool vcc_hint
)
303 if (val
.regClass() == s2
) {
306 assert(val
.regClass() == s1
);
307 Builder
bld(ctx
->program
, ctx
->block
);
308 Definition
& def
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
),
309 Operand((uint32_t) -1), Operand(0u), bld
.scc(val
)).def(0);
312 return def
.getTemp();
316 Temp
as_uniform_bool(isel_context
*ctx
, Temp val
)
318 if (val
.regClass() == s1
) {
321 assert(val
.regClass() == s2
);
322 Builder
bld(ctx
->program
, ctx
->block
);
323 return bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), Operand(0u), Operand(val
));
327 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
329 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
330 return get_ssa_temp(ctx
, src
.src
.ssa
);
332 if (src
.src
.ssa
->num_components
== size
) {
333 bool identity_swizzle
= true;
334 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
335 if (src
.swizzle
[i
] != i
)
336 identity_swizzle
= false;
338 if (identity_swizzle
)
339 return get_ssa_temp(ctx
, src
.src
.ssa
);
342 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
343 unsigned elem_size
= vec
.size() / src
.src
.ssa
->num_components
;
344 assert(elem_size
> 0); /* TODO: 8 and 16-bit vectors not supported */
345 assert(vec
.size() % elem_size
== 0);
347 RegClass elem_rc
= RegClass(vec
.type(), elem_size
);
349 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
352 std::array
<Temp
,4> elems
;
353 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
354 for (unsigned i
= 0; i
< size
; ++i
) {
355 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
356 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
358 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
)};
359 vec_instr
->definitions
[0] = Definition(dst
);
360 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
361 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
366 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
370 Builder
bld(ctx
->program
, ctx
->block
);
371 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
372 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
375 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
377 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
378 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
379 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
380 sop2
->definitions
[0] = Definition(dst
);
382 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
383 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
386 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool commutative
, bool swap_srcs
=false)
388 Builder
bld(ctx
->program
, ctx
->block
);
389 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
390 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
391 if (src1
.type() == RegType::sgpr
) {
392 if (commutative
&& src0
.type() == RegType::vgpr
) {
396 } else if (src0
.type() == RegType::vgpr
&&
397 op
!= aco_opcode::v_madmk_f32
&&
398 op
!= aco_opcode::v_madak_f32
&&
399 op
!= aco_opcode::v_madmk_f16
&&
400 op
!= aco_opcode::v_madak_f16
) {
401 /* If the instruction is not commutative, we emit a VOP3A instruction */
402 bld
.vop2_e64(op
, Definition(dst
), src0
, src1
);
405 src1
= bld
.copy(bld
.def(RegType::vgpr
, src1
.size()), src1
); //TODO: as_vgpr
408 bld
.vop2(op
, Definition(dst
), src0
, src1
);
411 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
413 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
414 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
415 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
417 /* ensure that the instruction has at most 1 sgpr operand
418 * The optimizer will inline constants for us */
419 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
420 src0
= as_vgpr(ctx
, src0
);
421 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
422 src1
= as_vgpr(ctx
, src1
);
423 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
424 src2
= as_vgpr(ctx
, src2
);
426 Builder
bld(ctx
->program
, ctx
->block
);
427 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
430 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
432 Builder
bld(ctx
->program
, ctx
->block
);
433 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
436 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
438 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
439 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
440 aco_ptr
<Instruction
> vopc
;
441 if (src1
.type() == RegType::sgpr
) {
442 if (src0
.type() == RegType::vgpr
) {
443 /* to swap the operands, we might also have to change the opcode */
445 case aco_opcode::v_cmp_lt_f32
:
446 op
= aco_opcode::v_cmp_gt_f32
;
448 case aco_opcode::v_cmp_ge_f32
:
449 op
= aco_opcode::v_cmp_le_f32
;
451 case aco_opcode::v_cmp_lt_i32
:
452 op
= aco_opcode::v_cmp_gt_i32
;
454 case aco_opcode::v_cmp_ge_i32
:
455 op
= aco_opcode::v_cmp_le_i32
;
457 case aco_opcode::v_cmp_lt_u32
:
458 op
= aco_opcode::v_cmp_gt_u32
;
460 case aco_opcode::v_cmp_ge_u32
:
461 op
= aco_opcode::v_cmp_le_u32
;
463 case aco_opcode::v_cmp_lt_f64
:
464 op
= aco_opcode::v_cmp_gt_f64
;
466 case aco_opcode::v_cmp_ge_f64
:
467 op
= aco_opcode::v_cmp_le_f64
;
469 case aco_opcode::v_cmp_lt_i64
:
470 op
= aco_opcode::v_cmp_gt_i64
;
472 case aco_opcode::v_cmp_ge_i64
:
473 op
= aco_opcode::v_cmp_le_i64
;
475 case aco_opcode::v_cmp_lt_u64
:
476 op
= aco_opcode::v_cmp_gt_u64
;
478 case aco_opcode::v_cmp_ge_u64
:
479 op
= aco_opcode::v_cmp_le_u64
;
481 default: /* eq and ne are commutative */
488 src1
= as_vgpr(ctx
, src1
);
491 Builder
bld(ctx
->program
, ctx
->block
);
492 bld
.vopc(op
, Definition(dst
), src0
, src1
).def(0).setHint(vcc
);
495 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
497 if (dst
.regClass() == s2
) {
498 emit_vopc_instruction(ctx
, instr
, op
, dst
);
499 if (!ctx
->divergent_vals
[instr
->dest
.dest
.ssa
.index
])
500 emit_split_vector(ctx
, dst
, 2);
501 } else if (dst
.regClass() == s1
) {
502 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
503 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
504 assert(src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
);
506 Builder
bld(ctx
->program
, ctx
->block
);
507 bld
.sopc(op
, bld
.scc(Definition(dst
)), src0
, src1
);
514 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op32
, aco_opcode op64
, Temp dst
)
516 Builder
bld(ctx
->program
, ctx
->block
);
517 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
518 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
519 if (dst
.regClass() == s2
) {
520 bld
.sop2(op64
, Definition(dst
), bld
.def(s1
, scc
),
521 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
523 assert(dst
.regClass() == s1
);
524 bld
.sop2(op32
, bld
.def(s1
), bld
.scc(Definition(dst
)),
525 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
530 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
532 Builder
bld(ctx
->program
, ctx
->block
);
533 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
534 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
535 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
537 if (dst
.type() == RegType::vgpr
) {
538 cond
= as_divergent_bool(ctx
, cond
, true);
540 aco_ptr
<Instruction
> bcsel
;
541 if (dst
.size() == 1) {
542 then
= as_vgpr(ctx
, then
);
543 els
= as_vgpr(ctx
, els
);
545 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
546 } else if (dst
.size() == 2) {
547 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
548 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
549 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
550 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
552 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
553 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
555 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
557 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
558 nir_print_instr(&instr
->instr
, stderr
);
559 fprintf(stderr
, "\n");
564 if (instr
->dest
.dest
.ssa
.bit_size
!= 1) { /* uniform condition and values in sgpr */
565 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
566 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
567 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
568 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(as_uniform_bool(ctx
, cond
)));
570 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
571 nir_print_instr(&instr
->instr
, stderr
);
572 fprintf(stderr
, "\n");
578 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
580 if (dst
.regClass() == s1
)
581 cond
= as_uniform_bool(ctx
, cond
);
583 if (cond
.regClass() == s1
) { /* uniform selection */
585 if (dst
.regClass() == s2
) {
586 op
= aco_opcode::s_cselect_b64
;
587 then
= as_divergent_bool(ctx
, then
, false);
588 els
= as_divergent_bool(ctx
, els
, false);
590 assert(dst
.regClass() == s1
);
591 op
= aco_opcode::s_cselect_b32
;
592 then
= as_uniform_bool(ctx
, then
);
593 els
= as_uniform_bool(ctx
, els
);
595 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(cond
));
599 /* divergent boolean bcsel
600 * this implements bcsel on bools: dst = s0 ? s1 : s2
601 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
602 assert (dst
.regClass() == s2
);
603 then
= as_divergent_bool(ctx
, then
, false);
604 els
= as_divergent_bool(ctx
, els
, false);
606 if (cond
.id() != then
.id())
607 then
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), cond
, then
);
609 if (cond
.id() == els
.id())
610 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), then
);
612 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), then
,
613 bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), els
, cond
));
616 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
618 if (!instr
->dest
.dest
.is_ssa
) {
619 fprintf(stderr
, "nir alu dst not in ssa: ");
620 nir_print_instr(&instr
->instr
, stderr
);
621 fprintf(stderr
, "\n");
624 Builder
bld(ctx
->program
, ctx
->block
);
625 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
630 std::array
<Temp
,4> elems
;
631 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
632 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
) {
633 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
634 vec
->operands
[i
] = Operand
{elems
[i
]};
636 vec
->definitions
[0] = Definition(dst
);
637 ctx
->block
->instructions
.emplace_back(std::move(vec
));
638 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
642 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
643 aco_ptr
<Instruction
> mov
;
644 if (dst
.type() == RegType::sgpr
) {
645 if (src
.type() == RegType::vgpr
)
646 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
647 else if (src
.regClass() == s1
)
648 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
649 else if (src
.regClass() == s2
)
650 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
652 unreachable("wrong src register class for nir_op_imov");
653 } else if (dst
.regClass() == v1
) {
654 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
655 } else if (dst
.regClass() == v2
) {
656 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
658 nir_print_instr(&instr
->instr
, stderr
);
659 unreachable("Should have been lowered to scalar.");
664 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
665 /* uniform booleans */
666 if (instr
->dest
.dest
.ssa
.bit_size
== 1 && dst
.regClass() == s1
) {
667 if (src
.regClass() == s1
) {
668 /* in this case, src is either 1 or 0 */
669 bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.scc(Definition(dst
)), Operand(1u), src
);
671 /* src is either exec_mask or 0 */
672 assert(src
.regClass() == s2
);
673 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
675 } else if (dst
.regClass() == v1
) {
676 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
677 } else if (dst
.type() == RegType::sgpr
) {
678 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
679 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
681 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
682 nir_print_instr(&instr
->instr
, stderr
);
683 fprintf(stderr
, "\n");
688 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
689 if (dst
.regClass() == v1
) {
690 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
691 } else if (dst
.regClass() == s1
) {
692 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
693 } else if (dst
.size() == 2) {
694 Temp src0
= bld
.tmp(dst
.type(), 1);
695 Temp src1
= bld
.tmp(dst
.type(), 1);
696 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
698 if (dst
.regClass() == s2
) {
699 Temp carry
= bld
.tmp(s1
);
700 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
701 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
702 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
704 Temp lower
= bld
.tmp(v1
);
705 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
706 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
707 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
710 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
711 nir_print_instr(&instr
->instr
, stderr
);
712 fprintf(stderr
, "\n");
717 if (dst
.regClass() == s1
) {
718 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
719 } else if (dst
.regClass() == v1
) {
720 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
721 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
723 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
724 nir_print_instr(&instr
->instr
, stderr
);
725 fprintf(stderr
, "\n");
730 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
731 if (dst
.regClass() == s1
) {
732 Temp tmp
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
733 Temp gtz
= bld
.sopc(aco_opcode::s_cmp_gt_i32
, bld
.def(s1
, scc
), src
, Operand(0u));
734 bld
.sop2(aco_opcode::s_add_i32
, Definition(dst
), bld
.def(s1
, scc
), gtz
, tmp
);
735 } else if (dst
.regClass() == s2
) {
736 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
737 Temp neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
738 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, neqz
);
739 } else if (dst
.regClass() == v1
) {
740 Temp tmp
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
741 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
742 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(1u), tmp
, gtz
);
743 } else if (dst
.regClass() == v2
) {
744 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
745 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
746 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
747 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
748 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
749 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
751 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
752 nir_print_instr(&instr
->instr
, stderr
);
753 fprintf(stderr
, "\n");
758 if (dst
.regClass() == v1
) {
759 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
760 } else if (dst
.regClass() == s1
) {
761 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
763 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
764 nir_print_instr(&instr
->instr
, stderr
);
765 fprintf(stderr
, "\n");
770 if (dst
.regClass() == v1
) {
771 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
772 } else if (dst
.regClass() == s1
) {
773 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
775 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
776 nir_print_instr(&instr
->instr
, stderr
);
777 fprintf(stderr
, "\n");
782 if (dst
.regClass() == v1
) {
783 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
784 } else if (dst
.regClass() == s1
) {
785 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
787 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
788 nir_print_instr(&instr
->instr
, stderr
);
789 fprintf(stderr
, "\n");
794 if (dst
.regClass() == v1
) {
795 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
796 } else if (dst
.regClass() == s1
) {
797 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
799 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
800 nir_print_instr(&instr
->instr
, stderr
);
801 fprintf(stderr
, "\n");
806 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
807 emit_boolean_logic(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::s_or_b64
, dst
);
808 } else if (dst
.regClass() == v1
) {
809 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
810 } else if (dst
.regClass() == s1
) {
811 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
812 } else if (dst
.regClass() == s2
) {
813 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
815 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
816 nir_print_instr(&instr
->instr
, stderr
);
817 fprintf(stderr
, "\n");
822 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
823 emit_boolean_logic(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::s_and_b64
, dst
);
824 } else if (dst
.regClass() == v1
) {
825 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
826 } else if (dst
.regClass() == s1
) {
827 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
828 } else if (dst
.regClass() == s2
) {
829 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
831 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
832 nir_print_instr(&instr
->instr
, stderr
);
833 fprintf(stderr
, "\n");
838 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
839 emit_boolean_logic(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::s_xor_b64
, dst
);
840 } else if (dst
.regClass() == v1
) {
841 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
842 } else if (dst
.regClass() == s1
) {
843 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
844 } else if (dst
.regClass() == s2
) {
845 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, 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_lshrrev_b32
, dst
, false, true);
856 } else if (dst
.regClass() == v2
) {
857 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
858 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
859 } else if (dst
.regClass() == s2
) {
860 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
861 } else if (dst
.regClass() == s1
) {
862 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, 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_lshlrev_b32
, dst
, false, true);
873 } else if (dst
.regClass() == v2
) {
874 bld
.vop3(aco_opcode::v_lshlrev_b64
, 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_lshl_b32
, dst
, true);
878 } else if (dst
.regClass() == s2
) {
879 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
881 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
882 nir_print_instr(&instr
->instr
, stderr
);
883 fprintf(stderr
, "\n");
888 if (dst
.regClass() == v1
) {
889 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
890 } else if (dst
.regClass() == v2
) {
891 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
892 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
893 } else if (dst
.regClass() == s1
) {
894 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
895 } else if (dst
.regClass() == s2
) {
896 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
898 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
899 nir_print_instr(&instr
->instr
, stderr
);
900 fprintf(stderr
, "\n");
904 case nir_op_find_lsb
: {
905 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
906 if (src
.regClass() == s1
) {
907 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
908 } else if (src
.regClass() == v1
) {
909 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
910 } else if (src
.regClass() == s2
) {
911 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
913 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
914 nir_print_instr(&instr
->instr
, stderr
);
915 fprintf(stderr
, "\n");
919 case nir_op_ufind_msb
:
920 case nir_op_ifind_msb
: {
921 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
922 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
923 aco_opcode op
= src
.regClass() == s2
?
924 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
925 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
926 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
928 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
929 Operand(src
.size() * 32u - 1u), msb_rev
);
930 Temp msb
= sub
.def(0).getTemp();
931 Temp carry
= sub
.def(1).getTemp();
933 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, carry
);
934 } else if (src
.regClass() == v1
) {
935 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
936 Temp msb_rev
= bld
.tmp(v1
);
937 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
938 Temp msb
= bld
.tmp(v1
);
939 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
940 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
942 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
943 nir_print_instr(&instr
->instr
, stderr
);
944 fprintf(stderr
, "\n");
948 case nir_op_bitfield_reverse
: {
949 if (dst
.regClass() == s1
) {
950 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
951 } else if (dst
.regClass() == v1
) {
952 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
954 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
955 nir_print_instr(&instr
->instr
, stderr
);
956 fprintf(stderr
, "\n");
961 if (dst
.regClass() == s1
) {
962 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
966 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
967 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
968 if (dst
.regClass() == v1
) {
969 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
973 assert(src0
.size() == 2 && src1
.size() == 2);
974 Temp src00
= bld
.tmp(src0
.type(), 1);
975 Temp src01
= bld
.tmp(dst
.type(), 1);
976 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
977 Temp src10
= bld
.tmp(src1
.type(), 1);
978 Temp src11
= bld
.tmp(dst
.type(), 1);
979 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
981 if (dst
.regClass() == s2
) {
982 Temp carry
= bld
.tmp(s1
);
983 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
984 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
985 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
986 } else if (dst
.regClass() == v2
) {
987 Temp dst0
= bld
.tmp(v1
);
988 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
989 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
990 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
992 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
993 nir_print_instr(&instr
->instr
, stderr
);
994 fprintf(stderr
, "\n");
998 case nir_op_uadd_sat
: {
999 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1000 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1001 if (dst
.regClass() == s1
) {
1002 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1003 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1005 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1006 } else if (dst
.regClass() == v1
) {
1007 if (ctx
->options
->chip_class
>= GFX9
) {
1008 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1009 add
->operands
[0] = Operand(src0
);
1010 add
->operands
[1] = Operand(src1
);
1011 add
->definitions
[0] = Definition(dst
);
1013 ctx
->block
->instructions
.emplace_back(std::move(add
));
1015 if (src1
.regClass() != v1
)
1016 std::swap(src0
, src1
);
1017 assert(src1
.regClass() == v1
);
1018 Temp tmp
= bld
.tmp(v1
);
1019 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1020 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1023 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1024 nir_print_instr(&instr
->instr
, stderr
);
1025 fprintf(stderr
, "\n");
1029 case nir_op_uadd_carry
: {
1030 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1031 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1032 if (dst
.regClass() == s1
) {
1033 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1036 if (dst
.regClass() == v1
) {
1037 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1038 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1042 Temp src00
= bld
.tmp(src0
.type(), 1);
1043 Temp src01
= bld
.tmp(dst
.type(), 1);
1044 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1045 Temp src10
= bld
.tmp(src1
.type(), 1);
1046 Temp src11
= bld
.tmp(dst
.type(), 1);
1047 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1048 if (dst
.regClass() == s2
) {
1049 Temp carry
= bld
.tmp(s1
);
1050 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1051 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1052 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1053 } else if (dst
.regClass() == v2
) {
1054 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1055 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1056 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1057 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1059 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1060 nir_print_instr(&instr
->instr
, stderr
);
1061 fprintf(stderr
, "\n");
1066 if (dst
.regClass() == s1
) {
1067 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1071 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1072 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1073 if (dst
.regClass() == v1
) {
1074 bld
.vsub32(Definition(dst
), src0
, src1
);
1078 Temp src00
= bld
.tmp(src0
.type(), 1);
1079 Temp src01
= bld
.tmp(dst
.type(), 1);
1080 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1081 Temp src10
= bld
.tmp(src1
.type(), 1);
1082 Temp src11
= bld
.tmp(dst
.type(), 1);
1083 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1084 if (dst
.regClass() == s2
) {
1085 Temp carry
= bld
.tmp(s1
);
1086 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1087 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1088 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1089 } else if (dst
.regClass() == v2
) {
1090 Temp lower
= bld
.tmp(v1
);
1091 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1092 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1093 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1095 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1096 nir_print_instr(&instr
->instr
, stderr
);
1097 fprintf(stderr
, "\n");
1101 case nir_op_usub_borrow
: {
1102 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1103 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1104 if (dst
.regClass() == s1
) {
1105 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1107 } else if (dst
.regClass() == v1
) {
1108 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1109 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1113 Temp src00
= bld
.tmp(src0
.type(), 1);
1114 Temp src01
= bld
.tmp(dst
.type(), 1);
1115 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1116 Temp src10
= bld
.tmp(src1
.type(), 1);
1117 Temp src11
= bld
.tmp(dst
.type(), 1);
1118 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1119 if (dst
.regClass() == s2
) {
1120 Temp borrow
= bld
.tmp(s1
);
1121 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1122 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1123 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1124 } else if (dst
.regClass() == v2
) {
1125 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1126 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1127 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1128 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1130 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1131 nir_print_instr(&instr
->instr
, stderr
);
1132 fprintf(stderr
, "\n");
1137 if (dst
.regClass() == v1
) {
1138 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1139 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1140 } else if (dst
.regClass() == s1
) {
1141 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1143 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1144 nir_print_instr(&instr
->instr
, stderr
);
1145 fprintf(stderr
, "\n");
1149 case nir_op_umul_high
: {
1150 if (dst
.regClass() == v1
) {
1151 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1152 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1153 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1154 } else if (dst
.regClass() == s1
) {
1155 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1156 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1157 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1159 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1160 nir_print_instr(&instr
->instr
, stderr
);
1161 fprintf(stderr
, "\n");
1165 case nir_op_imul_high
: {
1166 if (dst
.regClass() == v1
) {
1167 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1168 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1169 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1170 } else if (dst
.regClass() == s1
) {
1171 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1172 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1173 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1175 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1176 nir_print_instr(&instr
->instr
, stderr
);
1177 fprintf(stderr
, "\n");
1182 if (dst
.size() == 1) {
1183 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1184 } else if (dst
.size() == 2) {
1185 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1186 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1188 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1189 nir_print_instr(&instr
->instr
, stderr
);
1190 fprintf(stderr
, "\n");
1195 if (dst
.size() == 1) {
1196 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1197 } else if (dst
.size() == 2) {
1198 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]),
1199 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1201 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1202 nir_print_instr(&instr
->instr
, stderr
);
1203 fprintf(stderr
, "\n");
1208 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1209 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1210 if (dst
.size() == 1) {
1211 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1212 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1214 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1215 } else if (dst
.size() == 2) {
1216 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1217 get_alu_src(ctx
, instr
->src
[0]),
1218 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1219 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1222 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1223 nir_print_instr(&instr
->instr
, stderr
);
1224 fprintf(stderr
, "\n");
1230 if (dst
.size() == 1) {
1231 Temp rcp
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[1]));
1232 Temp mul
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]), rcp
);
1234 aco_opcode op
= instr
->op
== nir_op_fmod
? aco_opcode::v_floor_f32
: aco_opcode::v_trunc_f32
;
1235 Temp floor
= bld
.vop1(op
, bld
.def(v1
), mul
);
1237 mul
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[1]), floor
);
1238 bld
.vop2(aco_opcode::v_sub_f32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), mul
);
1239 } else if (dst
.size() == 2) {
1240 Temp rcp
= bld
.vop1(aco_opcode::v_rcp_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[1]));
1241 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[0]), rcp
);
1243 aco_opcode op
= instr
->op
== nir_op_fmod
? aco_opcode::v_floor_f64
: aco_opcode::v_trunc_f64
;
1244 Temp floor
= bld
.vop1(op
, bld
.def(v1
), mul
);
1246 mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), get_alu_src(ctx
, instr
->src
[1]), floor
);
1247 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), mul
);
1248 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1251 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1252 nir_print_instr(&instr
->instr
, stderr
);
1253 fprintf(stderr
, "\n");
1258 if (dst
.size() == 1) {
1259 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true);
1260 } else if (dst
.size() == 2) {
1261 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
),
1262 get_alu_src(ctx
, instr
->src
[0]),
1263 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1265 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1266 nir_print_instr(&instr
->instr
, stderr
);
1267 fprintf(stderr
, "\n");
1272 if (dst
.size() == 1) {
1273 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true);
1274 } else if (dst
.size() == 2) {
1275 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
),
1276 get_alu_src(ctx
, instr
->src
[0]),
1277 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1279 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1280 nir_print_instr(&instr
->instr
, stderr
);
1281 fprintf(stderr
, "\n");
1285 case nir_op_fmax3
: {
1286 if (dst
.size() == 1) {
1287 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
);
1289 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1290 nir_print_instr(&instr
->instr
, stderr
);
1291 fprintf(stderr
, "\n");
1295 case nir_op_fmin3
: {
1296 if (dst
.size() == 1) {
1297 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
);
1299 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1300 nir_print_instr(&instr
->instr
, stderr
);
1301 fprintf(stderr
, "\n");
1305 case nir_op_fmed3
: {
1306 if (dst
.size() == 1) {
1307 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
);
1309 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1310 nir_print_instr(&instr
->instr
, stderr
);
1311 fprintf(stderr
, "\n");
1315 case nir_op_umax3
: {
1316 if (dst
.size() == 1) {
1317 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1319 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1320 nir_print_instr(&instr
->instr
, stderr
);
1321 fprintf(stderr
, "\n");
1325 case nir_op_umin3
: {
1326 if (dst
.size() == 1) {
1327 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1329 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1330 nir_print_instr(&instr
->instr
, stderr
);
1331 fprintf(stderr
, "\n");
1335 case nir_op_umed3
: {
1336 if (dst
.size() == 1) {
1337 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1339 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1340 nir_print_instr(&instr
->instr
, stderr
);
1341 fprintf(stderr
, "\n");
1345 case nir_op_imax3
: {
1346 if (dst
.size() == 1) {
1347 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1349 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1350 nir_print_instr(&instr
->instr
, stderr
);
1351 fprintf(stderr
, "\n");
1355 case nir_op_imin3
: {
1356 if (dst
.size() == 1) {
1357 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1359 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1360 nir_print_instr(&instr
->instr
, stderr
);
1361 fprintf(stderr
, "\n");
1365 case nir_op_imed3
: {
1366 if (dst
.size() == 1) {
1367 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1369 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1370 nir_print_instr(&instr
->instr
, stderr
);
1371 fprintf(stderr
, "\n");
1375 case nir_op_cube_face_coord
: {
1376 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1377 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1378 emit_extract_vector(ctx
, in
, 1, v1
),
1379 emit_extract_vector(ctx
, in
, 2, v1
) };
1380 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1381 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1382 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1383 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1384 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1385 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1386 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1389 case nir_op_cube_face_index
: {
1390 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1391 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1392 emit_extract_vector(ctx
, in
, 1, v1
),
1393 emit_extract_vector(ctx
, in
, 2, v1
) };
1394 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1397 case nir_op_bcsel
: {
1398 emit_bcsel(ctx
, instr
, dst
);
1402 if (dst
.size() == 1) {
1403 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f32
, dst
);
1404 } else if (dst
.size() == 2) {
1405 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1407 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1408 nir_print_instr(&instr
->instr
, stderr
);
1409 fprintf(stderr
, "\n");
1414 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1415 if (dst
.size() == 1) {
1416 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1417 } else if (dst
.size() == 2) {
1418 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1419 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1420 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1421 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1423 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1424 nir_print_instr(&instr
->instr
, stderr
);
1425 fprintf(stderr
, "\n");
1430 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1431 if (dst
.size() == 1) {
1432 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1433 } else if (dst
.size() == 2) {
1434 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1435 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1436 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1437 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1439 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1440 nir_print_instr(&instr
->instr
, stderr
);
1441 fprintf(stderr
, "\n");
1446 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1447 if (dst
.size() == 1) {
1448 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1449 } else if (dst
.size() == 2) {
1450 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1451 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1454 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1455 nir_print_instr(&instr
->instr
, stderr
);
1456 fprintf(stderr
, "\n");
1460 case nir_op_flog2
: {
1461 if (dst
.size() == 1) {
1462 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f32
, dst
);
1464 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1465 nir_print_instr(&instr
->instr
, stderr
);
1466 fprintf(stderr
, "\n");
1471 if (dst
.size() == 1) {
1472 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f32
, dst
);
1473 } else if (dst
.size() == 2) {
1474 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
1476 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1477 nir_print_instr(&instr
->instr
, stderr
);
1478 fprintf(stderr
, "\n");
1482 case nir_op_fexp2
: {
1483 if (dst
.size() == 1) {
1484 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
1486 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1487 nir_print_instr(&instr
->instr
, stderr
);
1488 fprintf(stderr
, "\n");
1492 case nir_op_fsqrt
: {
1493 if (dst
.size() == 1) {
1494 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f32
, dst
);
1495 } else if (dst
.size() == 2) {
1496 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
1498 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1499 nir_print_instr(&instr
->instr
, stderr
);
1500 fprintf(stderr
, "\n");
1504 case nir_op_ffract
: {
1505 if (dst
.size() == 1) {
1506 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
1507 } else if (dst
.size() == 2) {
1508 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
1510 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1511 nir_print_instr(&instr
->instr
, stderr
);
1512 fprintf(stderr
, "\n");
1516 case nir_op_ffloor
: {
1517 if (dst
.size() == 1) {
1518 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
1519 } else if (dst
.size() == 2) {
1520 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f64
, dst
);
1522 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1523 nir_print_instr(&instr
->instr
, stderr
);
1524 fprintf(stderr
, "\n");
1528 case nir_op_fceil
: {
1529 if (dst
.size() == 1) {
1530 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
1531 } else if (dst
.size() == 2) {
1532 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
1534 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1535 nir_print_instr(&instr
->instr
, stderr
);
1536 fprintf(stderr
, "\n");
1540 case nir_op_ftrunc
: {
1541 if (dst
.size() == 1) {
1542 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
1543 } else if (dst
.size() == 2) {
1544 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f64
, dst
);
1546 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1547 nir_print_instr(&instr
->instr
, stderr
);
1548 fprintf(stderr
, "\n");
1552 case nir_op_fround_even
: {
1553 if (dst
.size() == 1) {
1554 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
1555 } else if (dst
.size() == 2) {
1556 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
1558 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1559 nir_print_instr(&instr
->instr
, stderr
);
1560 fprintf(stderr
, "\n");
1566 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1567 aco_ptr
<Instruction
> norm
;
1568 if (dst
.size() == 1) {
1570 Operand
half_pi(0x3e22f983u
);
1571 if (src
.type() == RegType::sgpr
)
1572 tmp
= bld
.vop2_e64(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1574 tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
1576 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
1577 if (ctx
->options
->chip_class
< GFX9
)
1578 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
1580 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
1581 bld
.vop1(opcode
, Definition(dst
), tmp
);
1583 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1584 nir_print_instr(&instr
->instr
, stderr
);
1585 fprintf(stderr
, "\n");
1589 case nir_op_ldexp
: {
1590 if (dst
.size() == 1) {
1591 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
),
1592 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1593 get_alu_src(ctx
, instr
->src
[1]));
1594 } else if (dst
.size() == 2) {
1595 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
),
1596 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0])),
1597 get_alu_src(ctx
, instr
->src
[1]));
1599 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1600 nir_print_instr(&instr
->instr
, stderr
);
1601 fprintf(stderr
, "\n");
1605 case nir_op_frexp_sig
: {
1606 if (dst
.size() == 1) {
1607 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
),
1608 get_alu_src(ctx
, instr
->src
[0]));
1609 } else if (dst
.size() == 2) {
1610 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
),
1611 get_alu_src(ctx
, instr
->src
[0]));
1613 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1614 nir_print_instr(&instr
->instr
, stderr
);
1615 fprintf(stderr
, "\n");
1619 case nir_op_frexp_exp
: {
1620 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1621 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
),
1622 get_alu_src(ctx
, instr
->src
[0]));
1623 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1624 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
),
1625 get_alu_src(ctx
, instr
->src
[0]));
1627 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1628 nir_print_instr(&instr
->instr
, stderr
);
1629 fprintf(stderr
, "\n");
1633 case nir_op_fsign
: {
1634 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
1635 if (dst
.size() == 1) {
1636 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1637 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
1638 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1639 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
1640 } else if (dst
.size() == 2) {
1641 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1642 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
1643 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, src
, cond
);
1645 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), src
);
1646 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
1647 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
1649 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1651 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1652 nir_print_instr(&instr
->instr
, stderr
);
1653 fprintf(stderr
, "\n");
1657 case nir_op_f2f32
: {
1658 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1659 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
1661 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1662 nir_print_instr(&instr
->instr
, stderr
);
1663 fprintf(stderr
, "\n");
1667 case nir_op_f2f64
: {
1668 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1669 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_f32
, dst
);
1671 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1672 nir_print_instr(&instr
->instr
, stderr
);
1673 fprintf(stderr
, "\n");
1677 case nir_op_i2f32
: {
1678 assert(dst
.size() == 1);
1679 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_i32
, dst
);
1682 case nir_op_i2f64
: {
1683 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1684 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_i32
, dst
);
1685 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1686 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1687 RegClass rc
= RegClass(src
.type(), 1);
1688 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1689 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1690 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1691 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
1692 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1693 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1696 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1697 nir_print_instr(&instr
->instr
, stderr
);
1698 fprintf(stderr
, "\n");
1702 case nir_op_u2f32
: {
1703 assert(dst
.size() == 1);
1704 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_u32
, dst
);
1707 case nir_op_u2f64
: {
1708 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1709 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f64_u32
, dst
);
1710 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1711 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1712 RegClass rc
= RegClass(src
.type(), 1);
1713 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
1714 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1715 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
1716 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
1717 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
1718 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
1720 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1721 nir_print_instr(&instr
->instr
, stderr
);
1722 fprintf(stderr
, "\n");
1726 case nir_op_f2i32
: {
1727 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1728 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1729 if (dst
.type() == RegType::vgpr
)
1730 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), src
);
1732 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1733 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), src
));
1735 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1736 if (dst
.type() == RegType::vgpr
)
1737 bld
.vop1(aco_opcode::v_cvt_i32_f64
, Definition(dst
), src
);
1739 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1740 bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), src
));
1743 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1744 nir_print_instr(&instr
->instr
, stderr
);
1745 fprintf(stderr
, "\n");
1749 case nir_op_f2u32
: {
1750 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1751 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1752 if (dst
.type() == RegType::vgpr
)
1753 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), src
);
1755 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1756 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), src
));
1758 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1759 if (dst
.type() == RegType::vgpr
)
1760 bld
.vop1(aco_opcode::v_cvt_u32_f64
, Definition(dst
), src
);
1762 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
1763 bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), src
));
1766 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1767 nir_print_instr(&instr
->instr
, stderr
);
1768 fprintf(stderr
, "\n");
1772 case nir_op_f2i64
: {
1773 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1774 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1775 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1776 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
1777 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1778 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
1779 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1780 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
1781 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1782 Temp new_exponent
= bld
.tmp(v1
);
1783 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
1784 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1785 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
1786 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1787 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1788 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
1789 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
1790 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
1791 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
1792 Temp new_lower
= bld
.tmp(v1
);
1793 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
1794 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
1795 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
1797 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1798 if (src
.type() == RegType::vgpr
)
1799 src
= bld
.as_uniform(src
);
1800 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1801 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1802 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1803 exponent
= bld
.sop2(aco_opcode::s_min_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
1804 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1805 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
1806 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1807 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
1808 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1809 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
1810 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
1811 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
1812 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
1813 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
1814 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1815 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1816 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
1817 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
1818 Temp borrow
= bld
.tmp(s1
);
1819 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
1820 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
1821 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1823 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1824 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1825 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1826 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1827 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1828 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1829 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1830 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1831 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
1832 if (dst
.type() == RegType::sgpr
) {
1833 lower
= bld
.as_uniform(lower
);
1834 upper
= bld
.as_uniform(upper
);
1836 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1839 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1840 nir_print_instr(&instr
->instr
, stderr
);
1841 fprintf(stderr
, "\n");
1845 case nir_op_f2u64
: {
1846 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1847 if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::vgpr
) {
1848 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
1849 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(s2
)), Operand(64u), exponent
);
1850 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
1851 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
1852 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
1853 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
1854 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
1855 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
1856 Temp new_exponent
= bld
.tmp(v1
);
1857 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
1858 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
1859 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
1860 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1861 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
1862 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
1863 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
1864 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
1865 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1867 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32 && dst
.type() == RegType::sgpr
) {
1868 if (src
.type() == RegType::vgpr
)
1869 src
= bld
.as_uniform(src
);
1870 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
1871 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
1872 exponent
= bld
.sop2(aco_opcode::s_max_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
1873 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
1874 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
1875 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
1876 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
1877 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
1878 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
1879 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
1880 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
1881 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
1882 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
1883 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
1884 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
1885 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
1886 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
1887 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1889 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1890 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
1891 Temp trunc
= bld
.vop1(aco_opcode::v_trunc_f64
, bld
.def(v2
), src
);
1892 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
1893 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
1894 Temp floor
= bld
.vop1(aco_opcode::v_floor_f64
, bld
.def(v2
), mul
);
1895 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
1896 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
1897 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
1898 if (dst
.type() == RegType::sgpr
) {
1899 lower
= bld
.as_uniform(lower
);
1900 upper
= bld
.as_uniform(upper
);
1902 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1905 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1906 nir_print_instr(&instr
->instr
, stderr
);
1907 fprintf(stderr
, "\n");
1911 case nir_op_b2f32
: {
1912 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1913 if (dst
.regClass() == s1
) {
1914 src
= as_uniform_bool(ctx
, src
);
1915 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
1916 } else if (dst
.regClass() == v1
) {
1917 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
),
1918 as_divergent_bool(ctx
, src
, true));
1920 unreachable("Wrong destination register class for nir_op_b2f32.");
1924 case nir_op_b2f64
: {
1925 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1926 if (dst
.regClass() == s2
) {
1927 src
= as_uniform_bool(ctx
, src
);
1928 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
1929 } else if (dst
.regClass() == v2
) {
1930 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
1931 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
,
1932 as_divergent_bool(ctx
, src
, true));
1933 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
1935 unreachable("Wrong destination register class for nir_op_b2f64.");
1939 case nir_op_i2i32
: {
1940 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1941 if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1942 /* we can actually just say dst = src, as it would map the lower register */
1943 emit_extract_vector(ctx
, src
, 0, dst
);
1945 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1946 nir_print_instr(&instr
->instr
, stderr
);
1947 fprintf(stderr
, "\n");
1951 case nir_op_u2u32
: {
1952 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1953 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
1954 if (dst
.regClass() == s1
) {
1955 bld
.sop2(aco_opcode::s_and_b32
, Definition(dst
), bld
.def(s1
, scc
), Operand(0xFFFFu
), src
);
1957 // TODO: do better with SDWA
1958 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0xFFFFu
), src
);
1960 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
1961 /* we can actually just say dst = src, as it would map the lower register */
1962 emit_extract_vector(ctx
, src
, 0, dst
);
1964 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1965 nir_print_instr(&instr
->instr
, stderr
);
1966 fprintf(stderr
, "\n");
1970 case nir_op_i2i64
: {
1971 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1972 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1973 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1975 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1976 nir_print_instr(&instr
->instr
, stderr
);
1977 fprintf(stderr
, "\n");
1981 case nir_op_u2u64
: {
1982 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1983 if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
1984 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
, Operand(0u));
1986 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1987 nir_print_instr(&instr
->instr
, stderr
);
1988 fprintf(stderr
, "\n");
1992 case nir_op_b2i32
: {
1993 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1994 if (dst
.regClass() == s1
) {
1995 if (src
.regClass() == s1
) {
1996 bld
.copy(Definition(dst
), src
);
1998 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
1999 assert(src
.regClass() == s2
);
2000 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2003 assert(dst
.regClass() == v1
&& src
.regClass() == s2
);
2004 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2009 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2010 if (dst
.regClass() == s2
) {
2011 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2012 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2013 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2015 assert(src
.regClass() == s1
&& dst
.regClass() == s1
);
2016 bld
.sopc(aco_opcode::s_cmp_lg_u32
, bld
.scc(Definition(dst
)), Operand(0u), src
);
2020 case nir_op_pack_64_2x32_split
: {
2021 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2022 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2024 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2027 case nir_op_unpack_64_2x32_split_x
:
2028 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2030 case nir_op_unpack_64_2x32_split_y
:
2031 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2033 case nir_op_pack_half_2x16
: {
2034 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2036 if (dst
.regClass() == v1
) {
2037 Temp src0
= bld
.tmp(v1
);
2038 Temp src1
= bld
.tmp(v1
);
2039 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2040 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2043 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2044 nir_print_instr(&instr
->instr
, stderr
);
2045 fprintf(stderr
, "\n");
2049 case nir_op_unpack_half_2x16_split_x
: {
2050 if (dst
.regClass() == v1
) {
2051 Builder
bld(ctx
->program
, ctx
->block
);
2052 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2054 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2055 nir_print_instr(&instr
->instr
, stderr
);
2056 fprintf(stderr
, "\n");
2060 case nir_op_unpack_half_2x16_split_y
: {
2061 if (dst
.regClass() == v1
) {
2062 Builder
bld(ctx
->program
, ctx
->block
);
2063 /* TODO: use SDWA here */
2064 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2065 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2067 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2068 nir_print_instr(&instr
->instr
, stderr
);
2069 fprintf(stderr
, "\n");
2073 case nir_op_fquantize2f16
: {
2074 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]));
2076 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2078 Temp cmp_res
= bld
.tmp(s2
);
2079 bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, Definition(cmp_res
), f16
, mask
).def(0).setHint(vcc
);
2081 Temp f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2083 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2087 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2088 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2090 if (dst
.regClass() == s1
) {
2091 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2092 } else if (dst
.regClass() == v1
) {
2093 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2095 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2096 nir_print_instr(&instr
->instr
, stderr
);
2097 fprintf(stderr
, "\n");
2101 case nir_op_bitfield_select
: {
2102 /* (mask & insert) | (~mask & base) */
2103 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2104 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2105 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2107 /* dst = (insert & bitmask) | (base & ~bitmask) */
2108 if (dst
.regClass() == s1
) {
2109 aco_ptr
<Instruction
> sop2
;
2110 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2111 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2113 if (const_insert
&& const_bitmask
) {
2114 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2116 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2117 lhs
= Operand(insert
);
2121 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2122 if (const_base
&& const_bitmask
) {
2123 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2125 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2126 rhs
= Operand(base
);
2129 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2131 } else if (dst
.regClass() == v1
) {
2132 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2133 base
= as_vgpr(ctx
, base
);
2134 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2135 insert
= as_vgpr(ctx
, insert
);
2137 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2140 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2141 nir_print_instr(&instr
->instr
, stderr
);
2142 fprintf(stderr
, "\n");
2148 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2149 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2150 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2152 if (dst
.type() == RegType::sgpr
) {
2154 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2155 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2156 if (const_offset
&& const_bits
) {
2157 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2158 extract
= Operand(const_extract
);
2162 width
= Operand(const_bits
->u32
<< 16);
2164 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2166 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2170 if (dst
.regClass() == s1
) {
2171 if (instr
->op
== nir_op_ubfe
)
2172 opcode
= aco_opcode::s_bfe_u32
;
2174 opcode
= aco_opcode::s_bfe_i32
;
2175 } else if (dst
.regClass() == s2
) {
2176 if (instr
->op
== nir_op_ubfe
)
2177 opcode
= aco_opcode::s_bfe_u64
;
2179 opcode
= aco_opcode::s_bfe_i64
;
2181 unreachable("Unsupported BFE bit size");
2184 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2188 if (dst
.regClass() == v1
) {
2189 if (instr
->op
== nir_op_ubfe
)
2190 opcode
= aco_opcode::v_bfe_u32
;
2192 opcode
= aco_opcode::v_bfe_i32
;
2194 unreachable("Unsupported BFE bit size");
2197 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2201 case nir_op_bit_count
: {
2202 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2203 if (src
.regClass() == s1
) {
2204 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2205 } else if (src
.regClass() == v1
) {
2206 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2207 } else if (src
.regClass() == v2
) {
2208 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2209 emit_extract_vector(ctx
, src
, 1, v1
),
2210 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2211 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2212 } else if (src
.regClass() == s2
) {
2213 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2215 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2216 nir_print_instr(&instr
->instr
, stderr
);
2217 fprintf(stderr
, "\n");
2222 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2223 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f32
, dst
);
2224 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2225 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_f64
, dst
);
2229 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2230 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f32
, dst
);
2231 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2232 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_f64
, dst
);
2236 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2237 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f32
, dst
);
2238 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2239 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_f64
, dst
);
2243 if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2244 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f32
, dst
);
2245 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2246 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_neq_f64
, dst
);
2250 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2251 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i32
, dst
);
2252 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2253 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_i32
, dst
);
2254 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2255 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_i64
, dst
);
2259 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2260 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i32
, dst
);
2261 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2262 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_i32
, dst
);
2263 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2264 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_i64
, dst
);
2268 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2269 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i32
, dst
);
2270 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2271 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_i32
, dst
);
2272 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2273 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_eq_i64
, dst
);
2274 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2275 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_eq_u64
, dst
);
2276 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2277 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2278 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2279 bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.scc(Definition(dst
)),
2280 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2281 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2282 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2283 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2284 bld
.sop2(aco_opcode::s_xnor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2285 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2287 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2288 nir_print_instr(&instr
->instr
, stderr
);
2289 fprintf(stderr
, "\n");
2294 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2295 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i32
, dst
);
2296 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2297 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lg_i64
, dst
);
2298 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32) {
2299 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_i32
, dst
);
2300 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 64) {
2301 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lg_u64
, dst
);
2302 } else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2303 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2304 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2305 bld
.sopc(aco_opcode::s_cmp_lg_i32
, bld
.scc(Definition(dst
)),
2306 as_uniform_bool(ctx
, src0
), as_uniform_bool(ctx
, src1
));
2307 } else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 1) {
2308 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2309 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2310 bld
.sop2(aco_opcode::s_xor_b64
, Definition(dst
), bld
.def(s1
, scc
),
2311 as_divergent_bool(ctx
, src0
, false), as_divergent_bool(ctx
, src1
, false));
2313 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2314 nir_print_instr(&instr
->instr
, stderr
);
2315 fprintf(stderr
, "\n");
2320 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2321 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u32
, dst
);
2322 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2323 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_lt_u32
, dst
);
2324 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2325 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_lt_u64
, dst
);
2329 if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2330 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u32
, dst
);
2331 else if (dst
.regClass() == s1
&& instr
->src
[0].src
.ssa
->bit_size
== 32)
2332 emit_comparison(ctx
, instr
, aco_opcode::s_cmp_ge_u32
, dst
);
2333 else if (dst
.regClass() == s2
&& instr
->src
[0].src
.ssa
->bit_size
== 64)
2334 emit_comparison(ctx
, instr
, aco_opcode::v_cmp_ge_u64
, dst
);
2339 case nir_op_fddx_fine
:
2340 case nir_op_fddy_fine
:
2341 case nir_op_fddx_coarse
:
2342 case nir_op_fddy_coarse
: {
2343 Definition tl
= bld
.def(v1
);
2345 if (instr
->op
== nir_op_fddx_fine
) {
2346 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 2, 2));
2347 dpp_ctrl
= dpp_quad_perm(1, 1, 3, 3);
2348 } else if (instr
->op
== nir_op_fddy_fine
) {
2349 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 1, 0, 1));
2350 dpp_ctrl
= dpp_quad_perm(2, 3, 2, 3);
2352 bld
.vop1_dpp(aco_opcode::v_mov_b32
, tl
, get_alu_src(ctx
, instr
->src
[0]), dpp_quad_perm(0, 0, 0, 0));
2353 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2354 dpp_ctrl
= dpp_quad_perm(1, 1, 1, 1);
2356 dpp_ctrl
= dpp_quad_perm(2, 2, 2, 2);
2359 Definition tmp
= bld
.def(v1
);
2360 bld
.vop2_dpp(aco_opcode::v_sub_f32
, tmp
, get_alu_src(ctx
, instr
->src
[0]), tl
.getTemp(), dpp_ctrl
);
2361 emit_wqm(ctx
, tmp
.getTemp(), dst
, true);
2365 fprintf(stderr
, "Unknown NIR ALU instr: ");
2366 nir_print_instr(&instr
->instr
, stderr
);
2367 fprintf(stderr
, "\n");
2371 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
2373 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
2375 // TODO: we really want to have the resulting type as this would allow for 64bit literals
2376 // which get truncated the lsb if double and msb if int
2377 // for now, we only use s_mov_b64 with 64bit inline constants
2378 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
2379 assert(dst
.type() == RegType::sgpr
);
2381 if (dst
.size() == 1)
2383 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(instr
->value
[0].u32
));
2385 assert(dst
.size() != 1);
2386 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2387 if (instr
->def
.bit_size
== 64)
2388 for (unsigned i
= 0; i
< dst
.size(); i
++)
2389 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
2391 for (unsigned i
= 0; i
< dst
.size(); i
++)
2392 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
2394 vec
->definitions
[0] = Definition(dst
);
2395 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2399 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
2401 uint32_t new_mask
= 0;
2402 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
2403 if (mask
& (1u << i
))
2404 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
2408 void visit_store_vs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2410 /* This wouldn't work inside control flow or with indirect offsets but
2411 * that doesn't happen because of nir_lower_io_to_temporaries(). */
2413 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2414 unsigned component
= nir_intrinsic_component(instr
);
2415 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2416 unsigned idx
= nir_intrinsic_base(instr
) + component
;
2418 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
2419 if (off_instr
->type
!= nir_instr_type_load_const
) {
2420 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2421 nir_print_instr(off_instr
, stderr
);
2422 fprintf(stderr
, "\n");
2424 idx
+= nir_instr_as_load_const(off_instr
)->value
[0].u32
* 4u;
2426 if (instr
->src
[0].ssa
->bit_size
== 64)
2427 write_mask
= widen_mask(write_mask
, 2);
2429 for (unsigned i
= 0; i
< 8; ++i
) {
2430 if (write_mask
& (1 << i
)) {
2431 ctx
->vs_output
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
2432 ctx
->vs_output
.outputs
[idx
/ 4u][idx
% 4u] = emit_extract_vector(ctx
, src
, i
, v1
);
2438 void visit_store_fs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2440 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
2442 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2443 for (unsigned i
= 0; i
< 4; ++i
) {
2444 if (write_mask
& (1 << i
)) {
2445 Temp tmp
= emit_extract_vector(ctx
, src
, i
, v1
);
2446 values
[i
] = Operand(tmp
);
2448 values
[i
] = Operand(v1
);
2452 unsigned index
= nir_intrinsic_base(instr
) / 4;
2453 unsigned target
, col_format
;
2454 unsigned enabled_channels
= 0xF;
2455 aco_opcode compr_op
= (aco_opcode
)0;
2457 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2458 assert(offset
&& "Non-const offsets on exports not yet supported");
2459 index
+= offset
->u32
;
2461 assert(index
!= FRAG_RESULT_COLOR
);
2463 /* Unlike vertex shader exports, it's fine to use multiple exports to
2464 * export separate channels of one target. So shaders which export both
2465 * FRAG_RESULT_SAMPLE_MASK and FRAG_RESULT_DEPTH should work fine.
2466 * TODO: combine the exports in those cases and create better code
2469 if (index
== FRAG_RESULT_SAMPLE_MASK
) {
2471 if (ctx
->program
->info
->ps
.writes_z
) {
2472 target
= V_008DFC_SQ_EXP_MRTZ
;
2473 enabled_channels
= 0x4;
2474 col_format
= (unsigned) -1;
2476 values
[2] = values
[0];
2477 values
[0] = Operand(v1
);
2479 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2480 exp
->valid_mask
= false;
2482 exp
->compressed
= true;
2483 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2484 exp
->enabled_mask
= 0xc;
2485 for (int i
= 0; i
< 4; i
++)
2486 exp
->operands
[i
] = Operand(v1
);
2487 exp
->operands
[1] = Operand(values
[0]);
2488 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2492 } else if (index
== FRAG_RESULT_DEPTH
) {
2494 target
= V_008DFC_SQ_EXP_MRTZ
;
2495 enabled_channels
= 0x1;
2496 col_format
= (unsigned) -1;
2498 } else if (index
== FRAG_RESULT_STENCIL
) {
2500 if (ctx
->program
->info
->ps
.writes_z
) {
2501 target
= V_008DFC_SQ_EXP_MRTZ
;
2502 enabled_channels
= 0x2;
2503 col_format
= (unsigned) -1;
2505 values
[1] = values
[0];
2506 values
[0] = Operand(v1
);
2508 aco_ptr
<Instruction
> shift
{create_instruction
<VOP2_instruction
>(aco_opcode::v_lshlrev_b32
, Format::VOP2
, 2, 1)};
2509 shift
->operands
[0] = Operand((uint32_t) 16);
2510 shift
->operands
[1] = values
[0];
2511 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2512 shift
->definitions
[0] = Definition(tmp
);
2513 ctx
->block
->instructions
.emplace_back(std::move(shift
));
2515 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2516 exp
->valid_mask
= false;
2518 exp
->compressed
= true;
2519 exp
->dest
= V_008DFC_SQ_EXP_MRTZ
;
2520 exp
->enabled_mask
= 0x3;
2521 exp
->operands
[0] = Operand(tmp
);
2522 for (int i
= 1; i
< 4; i
++)
2523 exp
->operands
[i
] = Operand(v1
);
2524 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2529 index
-= FRAG_RESULT_DATA0
;
2530 target
= V_008DFC_SQ_EXP_MRT
+ index
;
2531 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2533 ASSERTED
bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2534 ASSERTED
bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2535 assert(!is_int8
&& !is_int10
);
2539 case V_028714_SPI_SHADER_ZERO
:
2540 enabled_channels
= 0; /* writemask */
2541 target
= V_008DFC_SQ_EXP_NULL
;
2544 case V_028714_SPI_SHADER_32_R
:
2545 enabled_channels
= 1;
2548 case V_028714_SPI_SHADER_32_GR
:
2549 enabled_channels
= 0x3;
2552 case V_028714_SPI_SHADER_32_AR
:
2553 enabled_channels
= 0x9;
2556 case V_028714_SPI_SHADER_FP16_ABGR
:
2557 enabled_channels
= 0x5;
2558 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
2561 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2562 enabled_channels
= 0x5;
2563 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
2566 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2567 enabled_channels
= 0x5;
2568 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
2571 case V_028714_SPI_SHADER_UINT16_ABGR
:
2572 enabled_channels
= 0x5;
2573 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
2576 case V_028714_SPI_SHADER_SINT16_ABGR
:
2577 enabled_channels
= 0x5;
2578 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
2581 case V_028714_SPI_SHADER_32_ABGR
:
2582 enabled_channels
= 0xF;
2589 if (target
== V_008DFC_SQ_EXP_NULL
)
2594 for (int i
= 0; i
< 2; i
++)
2596 /* check if at least one of the values to be compressed is enabled */
2597 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
2599 enabled_channels
|= enabled
<< (i
*2);
2600 aco_ptr
<VOP3A_instruction
> compr
{create_instruction
<VOP3A_instruction
>(compr_op
, Format::VOP3A
, 2, 1)};
2601 Temp tmp
{ctx
->program
->allocateId(), v1
};
2602 compr
->operands
[0] = values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2];
2603 compr
->operands
[1] = values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1];
2604 compr
->definitions
[0] = Definition(tmp
);
2605 values
[i
] = Operand(tmp
);
2606 ctx
->block
->instructions
.emplace_back(std::move(compr
));
2608 values
[i
] = Operand(v1
);
2613 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
2614 exp
->valid_mask
= false;
2616 exp
->compressed
= (bool) compr_op
;
2618 exp
->enabled_mask
= enabled_channels
;
2619 if ((bool) compr_op
) {
2620 for (int i
= 0; i
< 2; i
++)
2621 exp
->operands
[i
] = enabled_channels
& (3 << (i
* 2)) ? values
[i
] : Operand(v1
);
2622 exp
->operands
[2] = Operand(v1
);
2623 exp
->operands
[3] = Operand(v1
);
2625 for (int i
= 0; i
< 4; i
++)
2626 exp
->operands
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
2629 ctx
->block
->instructions
.emplace_back(std::move(exp
));
2632 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2634 if (ctx
->stage
== vertex_vs
) {
2635 visit_store_vs_output(ctx
, instr
);
2636 } else if (ctx
->stage
== fragment_fs
) {
2637 visit_store_fs_output(ctx
, instr
);
2639 unreachable("Shader stage not implemented");
2643 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
2645 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
2646 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
2648 Builder
bld(ctx
->program
, ctx
->block
);
2649 Temp tmp
= bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
, bld
.m0(prim_mask
), idx
, component
);
2650 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
, bld
.m0(prim_mask
), tmp
, idx
, component
);
2653 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
2655 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
2656 for (unsigned i
= 0; i
< num_components
; i
++)
2657 vec
->operands
[i
] = Operand(ctx
->fs_inputs
[fs_input::frag_pos_0
+ i
]);
2659 if (ctx
->fs_vgpr_args
[fs_input::frag_pos_3
]) {
2660 assert(num_components
== 4);
2661 Builder
bld(ctx
->program
, ctx
->block
);
2662 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::frag_pos_3
]);
2665 for (Operand
& op
: vec
->operands
)
2666 op
= op
.isUndefined() ? Operand(0u) : op
;
2668 vec
->definitions
[0] = Definition(dst
);
2669 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2670 emit_split_vector(ctx
, dst
, num_components
);
2674 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2676 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2677 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2678 unsigned idx
= nir_intrinsic_base(instr
);
2679 unsigned component
= nir_intrinsic_component(instr
);
2680 Temp prim_mask
= ctx
->prim_mask
;
2682 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
2684 assert(offset
->u32
== 0);
2686 /* the lower 15bit of the prim_mask contain the offset into LDS
2687 * while the upper bits contain the number of prims */
2688 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
2689 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2690 Builder
bld(ctx
->program
, ctx
->block
);
2691 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2692 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2693 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2694 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2695 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2698 if (instr
->dest
.ssa
.num_components
== 1) {
2699 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
2701 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
2702 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
2704 Temp tmp
= {ctx
->program
->allocateId(), v1
};
2705 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
2706 vec
->operands
[i
] = Operand(tmp
);
2708 vec
->definitions
[0] = Definition(dst
);
2709 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2713 unsigned get_num_channels_from_data_format(unsigned data_format
)
2715 switch (data_format
) {
2716 case V_008F0C_BUF_DATA_FORMAT_8
:
2717 case V_008F0C_BUF_DATA_FORMAT_16
:
2718 case V_008F0C_BUF_DATA_FORMAT_32
:
2720 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2721 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2722 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2724 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2725 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2726 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2728 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2729 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2730 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2731 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2732 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2741 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2742 * so we may need to fix it up. */
2743 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
2745 Builder
bld(ctx
->program
, ctx
->block
);
2747 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
2748 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
2750 /* For the integer-like cases, do a natural sign extension.
2752 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
2753 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
2756 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
2757 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
2759 /* Convert back to the right type. */
2760 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
2761 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
2762 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0xbf800000u
), alpha
);
2763 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
2764 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
2765 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
2771 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2773 Builder
bld(ctx
->program
, ctx
->block
);
2774 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
2775 if (ctx
->stage
& sw_vs
) {
2777 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
2778 if (off_instr
->type
!= nir_instr_type_load_const
) {
2779 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2780 nir_print_instr(off_instr
, stderr
);
2781 fprintf(stderr
, "\n");
2783 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
2785 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, ctx
->vertex_buffers
);
2787 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
2788 unsigned component
= nir_intrinsic_component(instr
);
2789 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
2790 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
2791 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
2792 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
2794 unsigned dfmt
= attrib_format
& 0xf;
2796 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
2797 unsigned num_dfmt_channels
= get_num_channels_from_data_format(dfmt
);
2798 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
2799 unsigned num_channels
= MIN2(util_last_bit(mask
), num_dfmt_channels
);
2800 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
2801 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
2803 num_channels
= MAX2(num_channels
, 3);
2805 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, Operand(attrib_binding
* 16u));
2808 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
2809 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
2811 ctx
->needs_instance_id
= true;
2814 Temp divided
= bld
.tmp(v1
);
2815 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, ctx
->instance_id
), divisor
);
2816 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, divided
);
2818 index
= bld
.vadd32(bld
.def(v1
), ctx
->start_instance
, ctx
->instance_id
);
2821 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), ctx
->start_instance
);
2824 index
= bld
.vadd32(bld
.def(v1
), ctx
->base_vertex
, ctx
->vertex_id
);
2827 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
2828 index
= bld
.vadd32(bld
.def(v1
), Operand(attrib_offset
/ attrib_stride
), index
);
2829 attrib_offset
= attrib_offset
% attrib_stride
;
2832 Operand
soffset(0u);
2833 if (attrib_offset
>= 4096) {
2834 soffset
= bld
.copy(bld
.def(s1
), Operand(attrib_offset
));
2839 switch (num_channels
) {
2841 opcode
= aco_opcode::tbuffer_load_format_x
;
2844 opcode
= aco_opcode::tbuffer_load_format_xy
;
2847 opcode
= aco_opcode::tbuffer_load_format_xyz
;
2850 opcode
= aco_opcode::tbuffer_load_format_xyzw
;
2853 unreachable("Unimplemented load_input vector size");
2856 Temp tmp
= post_shuffle
|| num_channels
!= dst
.size() || alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
|| component
? bld
.tmp(RegType::vgpr
, num_channels
) : dst
;
2858 aco_ptr
<MTBUF_instruction
> mubuf
{create_instruction
<MTBUF_instruction
>(opcode
, Format::MTBUF
, 3, 1)};
2859 mubuf
->operands
[0] = Operand(index
);
2860 mubuf
->operands
[1] = Operand(list
);
2861 mubuf
->operands
[2] = soffset
;
2862 mubuf
->definitions
[0] = Definition(tmp
);
2863 mubuf
->idxen
= true;
2864 mubuf
->can_reorder
= true;
2867 assert(attrib_offset
< 4096);
2868 mubuf
->offset
= attrib_offset
;
2869 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
2871 emit_split_vector(ctx
, tmp
, tmp
.size());
2873 if (tmp
.id() != dst
.id()) {
2874 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
2875 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
2877 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
2878 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
2879 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
2881 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2882 for (unsigned i
= 0; i
< dst
.size(); i
++) {
2883 unsigned idx
= i
+ component
;
2884 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
&& num_channels
>= 4) {
2885 Temp alpha
= emit_extract_vector(ctx
, tmp
, swizzle
[3], v1
);
2886 vec
->operands
[3] = Operand(adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, alpha
));
2887 } else if (idx
< num_channels
) {
2888 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, tmp
, swizzle
[idx
], v1
));
2889 } else if (is_float
&& idx
== 3) {
2890 vec
->operands
[i
] = Operand(0x3f800000u
);
2891 } else if (!is_float
&& idx
== 3) {
2892 vec
->operands
[i
] = Operand(1u);
2894 vec
->operands
[i
] = Operand(0u);
2897 vec
->definitions
[0] = Definition(dst
);
2898 ctx
->block
->instructions
.emplace_back(std::move(vec
));
2899 emit_split_vector(ctx
, dst
, dst
.size());
2902 } else if (ctx
->stage
== fragment_fs
) {
2903 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
2904 if (off_instr
->type
!= nir_instr_type_load_const
||
2905 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
2906 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
2907 nir_print_instr(off_instr
, stderr
);
2908 fprintf(stderr
, "\n");
2911 Temp prim_mask
= ctx
->prim_mask
;
2912 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[0]);
2914 assert(offset
->u32
== 0);
2916 /* the lower 15bit of the prim_mask contain the offset into LDS
2917 * while the upper bits contain the number of prims */
2918 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
2919 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
2920 Builder
bld(ctx
->program
, ctx
->block
);
2921 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
2922 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
2923 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
2924 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
2925 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
2928 unsigned idx
= nir_intrinsic_base(instr
);
2929 unsigned component
= nir_intrinsic_component(instr
);
2931 if (dst
.size() == 1) {
2932 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(2u), bld
.m0(prim_mask
), idx
, component
);
2934 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
2935 for (unsigned i
= 0; i
< dst
.size(); i
++)
2936 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(2u), bld
.m0(prim_mask
), idx
, component
+ i
);
2937 vec
->definitions
[0] = Definition(dst
);
2938 bld
.insert(std::move(vec
));
2942 unreachable("Shader stage not implemented");
2946 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
2948 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
2949 Builder
bld(ctx
->program
, ctx
->block
);
2950 Temp ptr64
= convert_pointer_to_64_bit(ctx
, ctx
->descriptor_sets
[0]);
2951 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, Operand(desc_set
<< 2));//, false, false, false);
2954 return ctx
->descriptor_sets
[desc_set
];
2958 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
2960 Builder
bld(ctx
->program
, ctx
->block
);
2961 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
2962 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2963 unsigned binding
= nir_intrinsic_binding(instr
);
2966 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
2967 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
2968 unsigned offset
= layout
->binding
[binding
].offset
;
2970 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
2971 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
2972 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
2973 desc_ptr
= ctx
->push_constants
;
2974 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
2977 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
2978 stride
= layout
->binding
[binding
].size
;
2981 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
2982 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
2984 if (nir_const_index
) {
2985 const_index
= const_index
* stride
;
2987 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
2991 if (nir_const_index
) {
2992 const_index
= const_index
+ offset
;
2994 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
2998 if (nir_const_index
&& const_index
== 0) {
3001 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3002 nir_const_index
? Operand(const_index
) : Operand(index
),
3006 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3007 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), index
);
3010 void load_buffer(isel_context
*ctx
, unsigned num_components
, Temp dst
, Temp rsrc
, Temp offset
, bool glc
=false)
3012 Builder
bld(ctx
->program
, ctx
->block
);
3014 unsigned num_bytes
= dst
.size() * 4;
3017 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
3018 if (ctx
->options
->chip_class
< GFX8
)
3019 offset
= as_vgpr(ctx
, offset
);
3021 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3022 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3023 unsigned const_offset
= 0;
3025 Temp lower
= Temp();
3026 if (num_bytes
> 16) {
3027 assert(num_components
== 3 || num_components
== 4);
3028 op
= aco_opcode::buffer_load_dwordx4
;
3029 lower
= bld
.tmp(v4
);
3030 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3031 mubuf
->definitions
[0] = Definition(lower
);
3032 mubuf
->operands
[0] = vaddr
;
3033 mubuf
->operands
[1] = Operand(rsrc
);
3034 mubuf
->operands
[2] = soffset
;
3035 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3037 mubuf
->barrier
= barrier_buffer
;
3038 bld
.insert(std::move(mubuf
));
3039 emit_split_vector(ctx
, lower
, 2);
3044 switch (num_bytes
) {
3046 op
= aco_opcode::buffer_load_dword
;
3049 op
= aco_opcode::buffer_load_dwordx2
;
3052 op
= aco_opcode::buffer_load_dwordx3
;
3055 op
= aco_opcode::buffer_load_dwordx4
;
3058 unreachable("Load SSBO not implemented for this size.");
3060 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3061 mubuf
->operands
[0] = vaddr
;
3062 mubuf
->operands
[1] = Operand(rsrc
);
3063 mubuf
->operands
[2] = soffset
;
3064 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3066 mubuf
->barrier
= barrier_buffer
;
3067 mubuf
->offset
= const_offset
;
3068 aco_ptr
<Instruction
> instr
= std::move(mubuf
);
3070 if (dst
.size() > 4) {
3071 assert(lower
!= Temp());
3072 Temp upper
= bld
.tmp(RegType::vgpr
, dst
.size() - lower
.size());
3073 instr
->definitions
[0] = Definition(upper
);
3074 bld
.insert(std::move(instr
));
3075 if (dst
.size() == 8)
3076 emit_split_vector(ctx
, upper
, 2);
3077 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size() / 2, 1));
3078 instr
->operands
[0] = Operand(emit_extract_vector(ctx
, lower
, 0, v2
));
3079 instr
->operands
[1] = Operand(emit_extract_vector(ctx
, lower
, 1, v2
));
3080 instr
->operands
[2] = Operand(emit_extract_vector(ctx
, upper
, 0, v2
));
3081 if (dst
.size() == 8)
3082 instr
->operands
[3] = Operand(emit_extract_vector(ctx
, upper
, 1, v2
));
3085 if (dst
.type() == RegType::sgpr
) {
3086 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
3087 instr
->definitions
[0] = Definition(vec
);
3088 bld
.insert(std::move(instr
));
3089 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
3091 instr
->definitions
[0] = Definition(dst
);
3092 bld
.insert(std::move(instr
));
3095 switch (num_bytes
) {
3097 op
= aco_opcode::s_buffer_load_dword
;
3100 op
= aco_opcode::s_buffer_load_dwordx2
;
3104 op
= aco_opcode::s_buffer_load_dwordx4
;
3108 op
= aco_opcode::s_buffer_load_dwordx8
;
3111 unreachable("Load SSBO not implemented for this size.");
3113 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3114 load
->operands
[0] = Operand(rsrc
);
3115 load
->operands
[1] = Operand(bld
.as_uniform(offset
));
3116 assert(load
->operands
[1].getTemp().type() == RegType::sgpr
);
3117 load
->definitions
[0] = Definition(dst
);
3119 load
->barrier
= barrier_buffer
;
3120 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
3123 if (dst
.size() == 3) {
3124 Temp vec
= bld
.tmp(s4
);
3125 load
->definitions
[0] = Definition(vec
);
3126 bld
.insert(std::move(load
));
3127 emit_split_vector(ctx
, vec
, 4);
3129 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3130 emit_extract_vector(ctx
, vec
, 0, s1
),
3131 emit_extract_vector(ctx
, vec
, 1, s1
),
3132 emit_extract_vector(ctx
, vec
, 2, s1
));
3133 } else if (dst
.size() == 6) {
3134 Temp vec
= bld
.tmp(s8
);
3135 load
->definitions
[0] = Definition(vec
);
3136 bld
.insert(std::move(load
));
3137 emit_split_vector(ctx
, vec
, 4);
3139 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3140 emit_extract_vector(ctx
, vec
, 0, s2
),
3141 emit_extract_vector(ctx
, vec
, 1, s2
),
3142 emit_extract_vector(ctx
, vec
, 2, s2
));
3144 bld
.insert(std::move(load
));
3148 emit_split_vector(ctx
, dst
, num_components
);
3151 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3153 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3154 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3156 Builder
bld(ctx
->program
, ctx
->block
);
3158 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3159 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
3160 unsigned binding
= nir_intrinsic_binding(idx_instr
);
3161 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
3163 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
3164 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3165 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3166 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3167 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
3168 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3169 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3170 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
3171 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
3172 Operand(0xFFFFFFFFu
),
3173 Operand(desc_type
));
3174 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3175 rsrc
, upper_dwords
);
3177 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
3178 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
3181 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
3184 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3186 Builder
bld(ctx
->program
, ctx
->block
);
3187 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3189 unsigned offset
= nir_intrinsic_base(instr
);
3190 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
3191 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
3193 unsigned count
= instr
->dest
.ssa
.num_components
;
3194 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
3195 start
-= ctx
->base_inline_push_consts
;
3196 if (start
+ count
<= ctx
->num_inline_push_consts
) {
3197 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
3198 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
3199 for (unsigned i
= 0; i
< count
; ++i
) {
3200 elems
[i
] = ctx
->inline_push_consts
[start
+ i
];
3201 vec
->operands
[i
] = Operand
{elems
[i
]};
3203 vec
->definitions
[0] = Definition(dst
);
3204 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3205 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
3210 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
3211 if (offset
!= 0) // TODO check if index != 0 as well
3212 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
3213 Temp ptr
= convert_pointer_to_64_bit(ctx
, ctx
->push_constants
);
3218 switch (dst
.size()) {
3220 op
= aco_opcode::s_load_dword
;
3223 op
= aco_opcode::s_load_dwordx2
;
3229 op
= aco_opcode::s_load_dwordx4
;
3235 op
= aco_opcode::s_load_dwordx8
;
3238 unreachable("unimplemented or forbidden load_push_constant.");
3241 bld
.smem(op
, Definition(vec
), ptr
, index
);
3244 emit_split_vector(ctx
, vec
, 4);
3245 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
3246 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
3247 emit_extract_vector(ctx
, vec
, 0, rc
),
3248 emit_extract_vector(ctx
, vec
, 1, rc
),
3249 emit_extract_vector(ctx
, vec
, 2, rc
));
3252 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
3255 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3257 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3259 Builder
bld(ctx
->program
, ctx
->block
);
3261 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3262 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3263 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3264 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3265 if (ctx
->options
->chip_class
>= GFX10
) {
3266 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3267 S_008F0C_OOB_SELECT(3) |
3268 S_008F0C_RESOURCE_LEVEL(1);
3270 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3271 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3274 unsigned base
= nir_intrinsic_base(instr
) + ctx
->constant_data_offset
;
3275 unsigned range
= nir_intrinsic_range(instr
);
3277 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
3278 if (base
&& offset
.type() == RegType::sgpr
)
3279 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
3280 else if (base
&& offset
.type() == RegType::vgpr
)
3281 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
3283 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3284 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u)),
3285 Operand(MIN2(range
, ctx
->shader
->constant_data_size
- nir_intrinsic_base(instr
))),
3286 Operand(desc_type
));
3288 load_buffer(ctx
, instr
->num_components
, dst
, rsrc
, offset
);
3291 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3293 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3294 ctx
->cf_info
.exec_potentially_empty
= true;
3296 ctx
->program
->needs_exact
= true;
3298 Builder
bld(ctx
->program
, ctx
->block
);
3299 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
3300 src
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
3301 bld
.pseudo(aco_opcode::p_discard_if
, src
);
3302 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3306 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
3308 Builder
bld(ctx
->program
, ctx
->block
);
3310 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
3311 ctx
->cf_info
.exec_potentially_empty
= true;
3313 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
3314 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
3316 if (ctx
->block
->loop_nest_depth
&&
3317 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
3318 /* we handle discards the same way as jump instructions */
3319 append_logical_end(ctx
->block
);
3321 /* in loops, discard behaves like break */
3322 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
3323 ctx
->block
->kind
|= block_kind_discard
;
3326 /* uniform discard - loop ends here */
3327 assert(nir_instr_is_last(&instr
->instr
));
3328 ctx
->block
->kind
|= block_kind_uniform
;
3329 ctx
->cf_info
.has_branch
= true;
3330 bld
.branch(aco_opcode::p_branch
);
3331 add_linear_edge(ctx
->block
->index
, linear_target
);
3335 /* we add a break right behind the discard() instructions */
3336 ctx
->block
->kind
|= block_kind_break
;
3337 unsigned idx
= ctx
->block
->index
;
3339 /* remove critical edges from linear CFG */
3340 bld
.branch(aco_opcode::p_branch
);
3341 Block
* break_block
= ctx
->program
->create_and_insert_block();
3342 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3343 break_block
->kind
|= block_kind_uniform
;
3344 add_linear_edge(idx
, break_block
);
3345 add_linear_edge(break_block
->index
, linear_target
);
3346 bld
.reset(break_block
);
3347 bld
.branch(aco_opcode::p_branch
);
3349 Block
* continue_block
= ctx
->program
->create_and_insert_block();
3350 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
3351 add_linear_edge(idx
, continue_block
);
3352 append_logical_start(continue_block
);
3353 ctx
->block
= continue_block
;
3358 /* it can currently happen that NIR doesn't remove the unreachable code */
3359 if (!nir_instr_is_last(&instr
->instr
)) {
3360 ctx
->program
->needs_exact
= true;
3361 /* save exec somewhere temporarily so that it doesn't get
3362 * overwritten before the discard from outer exec masks */
3363 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, s2
));
3364 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
3365 ctx
->block
->kind
|= block_kind_uses_discard_if
;
3369 /* This condition is incorrect for uniformly branched discards in a loop
3370 * predicated by a divergent condition, but the above code catches that case
3371 * and the discard would end up turning into a discard_if.
3381 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
3382 /* program just ends here */
3383 ctx
->block
->kind
|= block_kind_uniform
;
3384 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
3385 0 /* enabled mask */, 9 /* dest */,
3386 false /* compressed */, true/* done */, true /* valid mask */);
3387 bld
.sopp(aco_opcode::s_endpgm
);
3388 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
3390 ctx
->block
->kind
|= block_kind_discard
;
3391 /* branch and linear edge is added by visit_if() */
3395 enum aco_descriptor_type
{
3405 enum aco_image_dim
{
3409 aco_image_cube
, // includes cube arrays
3413 aco_image_2darraymsaa
,
3416 static enum aco_image_dim
3417 get_sampler_dim(isel_context
*ctx
, enum glsl_sampler_dim dim
, bool is_array
)
3420 case GLSL_SAMPLER_DIM_1D
:
3421 if (ctx
->options
->chip_class
>= GFX9
)
3422 return is_array
? aco_image_2darray
: aco_image_2d
;
3423 return is_array
? aco_image_1darray
: aco_image_1d
;
3424 case GLSL_SAMPLER_DIM_2D
:
3425 case GLSL_SAMPLER_DIM_RECT
:
3426 case GLSL_SAMPLER_DIM_EXTERNAL
:
3427 return is_array
? aco_image_2darray
: aco_image_2d
;
3428 case GLSL_SAMPLER_DIM_3D
:
3429 return aco_image_3d
;
3430 case GLSL_SAMPLER_DIM_CUBE
:
3431 return aco_image_cube
;
3432 case GLSL_SAMPLER_DIM_MS
:
3433 return is_array
? aco_image_2darraymsaa
: aco_image_2dmsaa
;
3434 case GLSL_SAMPLER_DIM_SUBPASS
:
3435 return aco_image_2darray
;
3436 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3437 return aco_image_2darraymsaa
;
3439 unreachable("bad sampler dim");
3444 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
3445 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3447 aco_image_dim dim
= get_sampler_dim(ctx
, sampler_dim
, is_array
);
3448 return dim
== aco_image_cube
||
3449 dim
== aco_image_1darray
||
3450 dim
== aco_image_2darray
||
3451 dim
== aco_image_2darraymsaa
;
3454 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
3455 enum aco_descriptor_type desc_type
,
3456 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
3458 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
3459 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
3460 if (it != ctx->tex_desc.end())
3463 Temp index
= Temp();
3464 bool index_set
= false;
3465 unsigned constant_index
= 0;
3466 unsigned descriptor_set
;
3467 unsigned base_index
;
3468 Builder
bld(ctx
->program
, ctx
->block
);
3471 assert(tex_instr
&& !image
);
3473 base_index
= tex_instr
->sampler_index
;
3475 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3476 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3480 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3481 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3483 constant_index
+= array_size
* const_value
->u32
;
3485 Temp indirect
= bld
.as_uniform(get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
));
3487 if (array_size
!= 1)
3488 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
3494 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
3498 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3500 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3501 base_index
= deref_instr
->var
->data
.binding
;
3504 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
3505 list
= convert_pointer_to_64_bit(ctx
, list
);
3507 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
3508 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
3509 unsigned offset
= binding
->offset
;
3510 unsigned stride
= binding
->size
;
3514 assert(base_index
< layout
->binding_count
);
3516 switch (desc_type
) {
3517 case ACO_DESC_IMAGE
:
3519 opcode
= aco_opcode::s_load_dwordx8
;
3521 case ACO_DESC_FMASK
:
3523 opcode
= aco_opcode::s_load_dwordx8
;
3526 case ACO_DESC_SAMPLER
:
3528 opcode
= aco_opcode::s_load_dwordx4
;
3529 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
3530 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
3532 case ACO_DESC_BUFFER
:
3534 opcode
= aco_opcode::s_load_dwordx4
;
3536 case ACO_DESC_PLANE_0
:
3537 case ACO_DESC_PLANE_1
:
3539 opcode
= aco_opcode::s_load_dwordx8
;
3540 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
3542 case ACO_DESC_PLANE_2
:
3544 opcode
= aco_opcode::s_load_dwordx4
;
3548 unreachable("invalid desc_type\n");
3551 offset
+= constant_index
* stride
;
3553 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
3554 (!index_set
|| binding
->immutable_samplers_equal
)) {
3555 if (binding
->immutable_samplers_equal
)
3558 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
3559 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
3560 Operand(samplers
[constant_index
* 4 + 0]),
3561 Operand(samplers
[constant_index
* 4 + 1]),
3562 Operand(samplers
[constant_index
* 4 + 2]),
3563 Operand(samplers
[constant_index
* 4 + 3]));
3568 off
= Operand(offset
);
3570 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
3571 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
3574 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
3576 if (desc_type
== ACO_DESC_PLANE_2
) {
3578 for (unsigned i
= 0; i
< 8; i
++)
3579 components
[i
] = bld
.tmp(s1
);
3580 bld
.pseudo(aco_opcode::p_split_vector
,
3581 Definition(components
[0]),
3582 Definition(components
[1]),
3583 Definition(components
[2]),
3584 Definition(components
[3]),
3587 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
3588 bld
.pseudo(aco_opcode::p_split_vector
,
3589 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
3590 Definition(components
[4]),
3591 Definition(components
[5]),
3592 Definition(components
[6]),
3593 Definition(components
[7]),
3596 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
3597 components
[0], components
[1], components
[2], components
[3],
3598 components
[4], components
[5], components
[6], components
[7]);
3604 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
3607 case GLSL_SAMPLER_DIM_BUF
:
3609 case GLSL_SAMPLER_DIM_1D
:
3610 return array
? 2 : 1;
3611 case GLSL_SAMPLER_DIM_2D
:
3612 return array
? 3 : 2;
3613 case GLSL_SAMPLER_DIM_MS
:
3614 return array
? 4 : 3;
3615 case GLSL_SAMPLER_DIM_3D
:
3616 case GLSL_SAMPLER_DIM_CUBE
:
3618 case GLSL_SAMPLER_DIM_RECT
:
3619 case GLSL_SAMPLER_DIM_SUBPASS
:
3621 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3630 /* Adjust the sample index according to FMASK.
3632 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3633 * which is the identity mapping. Each nibble says which physical sample
3634 * should be fetched to get that sample.
3636 * For example, 0x11111100 means there are only 2 samples stored and
3637 * the second sample covers 3/4 of the pixel. When reading samples 0
3638 * and 1, return physical sample 0 (determined by the first two 0s
3639 * in FMASK), otherwise return physical sample 1.
3641 * The sample index should be adjusted as follows:
3642 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3644 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, Temp coords
, Operand sample_index
, Temp fmask_desc_ptr
)
3646 Builder
bld(ctx
->program
, ctx
->block
);
3647 Temp fmask
= bld
.tmp(v1
);
3649 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3650 load
->operands
[0] = Operand(coords
);
3651 load
->operands
[1] = Operand(fmask_desc_ptr
);
3652 load
->definitions
[0] = Definition(fmask
);
3657 load
->can_reorder
= true; /* fmask images shouldn't be modified */
3658 ctx
->block
->instructions
.emplace_back(std::move(load
));
3660 Operand sample_index4
;
3661 if (sample_index
.isConstant() && sample_index
.constantValue() < 16) {
3662 sample_index4
= Operand(sample_index
.constantValue() << 2);
3663 } else if (sample_index
.regClass() == s1
) {
3664 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
3666 assert(sample_index
.regClass() == v1
);
3667 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
3671 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
3672 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
3673 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
3674 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
3676 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
3678 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3679 * resource descriptor is 0 (invalid),
3681 Temp compare
= bld
.tmp(s2
);
3682 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
3683 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
3685 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
3687 /* Replace the MSAA sample index. */
3688 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
3691 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
3694 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
3695 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3696 bool is_array
= glsl_sampler_type_is_array(type
);
3697 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3698 assert(!add_frag_pos
&& "Input attachments should be lowered.");
3699 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
3700 bool gfx9_1d
= ctx
->options
->chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
3701 int count
= image_type_to_components_count(dim
, is_array
);
3702 std::vector
<Operand
> coords(count
);
3705 Operand sample_index
;
3706 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
3708 sample_index
= Operand(sample_cv
->u32
);
3710 sample_index
= Operand(emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
), 0, v1
));
3712 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
3713 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, is_array
? 3 : 2, 1)};
3714 for (unsigned i
= 0; i
< vec
->operands
.size(); i
++)
3715 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3716 Temp fmask_load_address
= {ctx
->program
->allocateId(), is_array
? v3
: v2
};
3717 vec
->definitions
[0] = Definition(fmask_load_address
);
3718 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3720 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
3721 sample_index
= Operand(adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
));
3724 coords
[count
] = sample_index
;
3727 if (count
== 1 && !gfx9_1d
)
3728 return emit_extract_vector(ctx
, src0
, 0, v1
);
3731 coords
[0] = Operand(emit_extract_vector(ctx
, src0
, 0, v1
));
3732 coords
.resize(coords
.size() + 1);
3733 coords
[1] = Operand((uint32_t) 0);
3735 coords
[2] = Operand(emit_extract_vector(ctx
, src0
, 1, v1
));
3737 for (int i
= 0; i
< count
; i
++)
3738 coords
[i
] = Operand(emit_extract_vector(ctx
, src0
, i
, v1
));
3741 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
3742 for (unsigned i
= 0; i
< coords
.size(); i
++)
3743 vec
->operands
[i
] = coords
[i
];
3744 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
3745 vec
->definitions
[0] = Definition(res
);
3746 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3751 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3753 Builder
bld(ctx
->program
, ctx
->block
);
3754 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3755 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3756 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3757 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3759 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3760 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
3761 unsigned num_channels
= util_last_bit(mask
);
3762 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3763 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3766 switch (num_channels
) {
3768 opcode
= aco_opcode::buffer_load_format_x
;
3771 opcode
= aco_opcode::buffer_load_format_xy
;
3774 opcode
= aco_opcode::buffer_load_format_xyz
;
3777 opcode
= aco_opcode::buffer_load_format_xyzw
;
3780 unreachable(">4 channel buffer image load");
3782 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
3783 load
->operands
[0] = Operand(vindex
);
3784 load
->operands
[1] = Operand(rsrc
);
3785 load
->operands
[2] = Operand((uint32_t) 0);
3787 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
3790 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
3791 load
->definitions
[0] = Definition(tmp
);
3793 load
->barrier
= barrier_image
;
3794 ctx
->block
->instructions
.emplace_back(std::move(load
));
3796 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
3800 Temp coords
= get_image_coords(ctx
, instr
, type
);
3801 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3802 //aco_image_dim img_dim = get_image_dim(ctx, glsl_get_sampler_dim(type), glsl_sampler_type_is_array(type));
3804 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
3805 unsigned num_components
= util_bitcount(dmask
);
3807 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
3810 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
3812 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 2, 1)};
3813 load
->operands
[0] = Operand(coords
);
3814 load
->operands
[1] = Operand(resource
);
3815 load
->definitions
[0] = Definition(tmp
);
3816 load
->glc
= var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
3817 load
->dmask
= dmask
;
3819 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3820 load
->barrier
= barrier_image
;
3821 ctx
->block
->instructions
.emplace_back(std::move(load
));
3823 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
3827 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3829 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3830 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3831 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3832 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
3834 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
3836 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3837 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3838 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3840 switch (data
.size()) {
3842 opcode
= aco_opcode::buffer_store_format_x
;
3845 opcode
= aco_opcode::buffer_store_format_xy
;
3848 opcode
= aco_opcode::buffer_store_format_xyz
;
3851 opcode
= aco_opcode::buffer_store_format_xyzw
;
3854 unreachable(">4 channel buffer image store");
3856 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
3857 store
->operands
[0] = Operand(vindex
);
3858 store
->operands
[1] = Operand(rsrc
);
3859 store
->operands
[2] = Operand((uint32_t) 0);
3860 store
->operands
[3] = Operand(data
);
3861 store
->idxen
= true;
3863 store
->disable_wqm
= true;
3864 store
->barrier
= barrier_image
;
3865 ctx
->program
->needs_exact
= true;
3866 ctx
->block
->instructions
.emplace_back(std::move(store
));
3870 assert(data
.type() == RegType::vgpr
);
3871 Temp coords
= get_image_coords(ctx
, instr
, type
);
3872 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3874 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(aco_opcode::image_store
, Format::MIMG
, 4, 0)};
3875 store
->operands
[0] = Operand(coords
);
3876 store
->operands
[1] = Operand(resource
);
3877 store
->operands
[2] = Operand(s4
);
3878 store
->operands
[3] = Operand(data
);
3880 store
->dmask
= (1 << data
.size()) - 1;
3882 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3883 store
->disable_wqm
= true;
3884 store
->barrier
= barrier_image
;
3885 ctx
->program
->needs_exact
= true;
3886 ctx
->block
->instructions
.emplace_back(std::move(store
));
3890 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
3892 /* return the previous value if dest is ever used */
3893 bool return_previous
= false;
3894 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
3895 return_previous
= true;
3898 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
3899 return_previous
= true;
3903 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
3904 const struct glsl_type
*type
= glsl_without_array(var
->type
);
3905 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
3906 Builder
bld(ctx
->program
, ctx
->block
);
3908 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
3909 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
3911 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
3912 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
3914 aco_opcode buf_op
, image_op
;
3915 switch (instr
->intrinsic
) {
3916 case nir_intrinsic_image_deref_atomic_add
:
3917 buf_op
= aco_opcode::buffer_atomic_add
;
3918 image_op
= aco_opcode::image_atomic_add
;
3920 case nir_intrinsic_image_deref_atomic_umin
:
3921 buf_op
= aco_opcode::buffer_atomic_umin
;
3922 image_op
= aco_opcode::image_atomic_umin
;
3924 case nir_intrinsic_image_deref_atomic_imin
:
3925 buf_op
= aco_opcode::buffer_atomic_smin
;
3926 image_op
= aco_opcode::image_atomic_smin
;
3928 case nir_intrinsic_image_deref_atomic_umax
:
3929 buf_op
= aco_opcode::buffer_atomic_umax
;
3930 image_op
= aco_opcode::image_atomic_umax
;
3932 case nir_intrinsic_image_deref_atomic_imax
:
3933 buf_op
= aco_opcode::buffer_atomic_smax
;
3934 image_op
= aco_opcode::image_atomic_smax
;
3936 case nir_intrinsic_image_deref_atomic_and
:
3937 buf_op
= aco_opcode::buffer_atomic_and
;
3938 image_op
= aco_opcode::image_atomic_and
;
3940 case nir_intrinsic_image_deref_atomic_or
:
3941 buf_op
= aco_opcode::buffer_atomic_or
;
3942 image_op
= aco_opcode::image_atomic_or
;
3944 case nir_intrinsic_image_deref_atomic_xor
:
3945 buf_op
= aco_opcode::buffer_atomic_xor
;
3946 image_op
= aco_opcode::image_atomic_xor
;
3948 case nir_intrinsic_image_deref_atomic_exchange
:
3949 buf_op
= aco_opcode::buffer_atomic_swap
;
3950 image_op
= aco_opcode::image_atomic_swap
;
3952 case nir_intrinsic_image_deref_atomic_comp_swap
:
3953 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
3954 image_op
= aco_opcode::image_atomic_cmpswap
;
3957 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
3960 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
3962 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3963 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
3964 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
3965 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
3966 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
3967 mubuf
->operands
[0] = Operand(vindex
);
3968 mubuf
->operands
[1] = Operand(resource
);
3969 mubuf
->operands
[2] = Operand((uint32_t)0);
3970 mubuf
->operands
[3] = Operand(data
);
3971 if (return_previous
)
3972 mubuf
->definitions
[0] = Definition(dst
);
3974 mubuf
->idxen
= true;
3975 mubuf
->glc
= return_previous
;
3976 mubuf
->disable_wqm
= true;
3977 mubuf
->barrier
= barrier_image
;
3978 ctx
->program
->needs_exact
= true;
3979 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
3983 Temp coords
= get_image_coords(ctx
, instr
, type
);
3984 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
3985 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 4, return_previous
? 1 : 0)};
3986 mimg
->operands
[0] = Operand(coords
);
3987 mimg
->operands
[1] = Operand(resource
);
3988 mimg
->operands
[2] = Operand(s4
); /* no sampler */
3989 mimg
->operands
[3] = Operand(data
);
3990 if (return_previous
)
3991 mimg
->definitions
[0] = Definition(dst
);
3992 mimg
->glc
= return_previous
;
3993 mimg
->dmask
= (1 << data
.size()) - 1;
3995 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
3996 mimg
->disable_wqm
= true;
3997 mimg
->barrier
= barrier_image
;
3998 ctx
->program
->needs_exact
= true;
3999 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4003 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
4005 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
4006 Builder
bld(ctx
->program
, ctx
->block
);
4008 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
4009 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
4010 stride
= bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, bld
.def(v1
), stride
);
4011 stride
= bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), stride
);
4013 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
4014 size
= bld
.vop1(aco_opcode::v_cvt_f32_u32
, bld
.def(v1
), size
);
4016 Temp res
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), size
, stride
);
4017 res
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), res
);
4018 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4020 // TODO: we can probably calculate this faster on the scalar unit to do: size / stride{1,2,4,8,12,16}
4022 * for 1,2,4,8,16, the result is just (stride >> S_FF1_I32_B32)
4023 * in case 12 (or 3?), we have to divide by 3:
4024 * set v_skip in case it's 12 (if we also have to take care of 3, shift first)
4025 * use v_mul_hi_u32 with magic number to divide
4026 * we need some pseudo merge opcode to overwrite the original SALU result with readfirstlane
4028 * total: 6 SALU + 2 VALU instructions vs 1 SALU + 6 VALU instructions
4032 emit_extract_vector(ctx
, desc
, 2, dst
);
4036 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4038 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
4039 const struct glsl_type
*type
= glsl_without_array(var
->type
);
4040 Builder
bld(ctx
->program
, ctx
->block
);
4042 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
4043 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
4044 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
4048 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
4051 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
4053 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4055 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1)};
4056 mimg
->operands
[0] = Operand(lod
);
4057 mimg
->operands
[1] = Operand(resource
);
4058 unsigned& dmask
= mimg
->dmask
;
4059 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
4060 mimg
->da
= glsl_sampler_type_is_array(type
);
4061 mimg
->can_reorder
= true;
4062 Definition
& def
= mimg
->definitions
[0];
4063 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
4065 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
4066 glsl_sampler_type_is_array(type
)) {
4068 assert(instr
->dest
.ssa
.num_components
== 3);
4069 Temp tmp
= {ctx
->program
->allocateId(), v3
};
4070 def
= Definition(tmp
);
4071 emit_split_vector(ctx
, tmp
, 3);
4073 /* divide 3rd value by 6 by multiplying with magic number */
4074 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
4075 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
4077 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4078 emit_extract_vector(ctx
, tmp
, 0, v1
),
4079 emit_extract_vector(ctx
, tmp
, 1, v1
),
4082 } else if (ctx
->options
->chip_class
>= GFX9
&&
4083 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
4084 glsl_sampler_type_is_array(type
)) {
4085 assert(instr
->dest
.ssa
.num_components
== 2);
4086 def
= Definition(dst
);
4089 def
= Definition(dst
);
4092 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
4095 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4097 Builder
bld(ctx
->program
, ctx
->block
);
4098 unsigned num_components
= instr
->num_components
;
4100 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4101 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4102 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4104 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4105 load_buffer(ctx
, num_components
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), glc
);
4108 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4110 Builder
bld(ctx
->program
, ctx
->block
);
4111 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4112 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4113 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4116 if (ctx
->options
->chip_class
< GFX8
)
4117 offset
= as_vgpr(ctx
,get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4119 offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
4121 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4122 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4124 bool smem
= !ctx
->divergent_vals
[instr
->src
[2].ssa
->index
] &&
4125 ctx
->options
->chip_class
>= GFX8
;
4127 offset
= bld
.as_uniform(offset
);
4128 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
4132 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4133 if (count
== 3 && smem
) {
4134 writemask
|= 1u << (start
+ 2);
4137 int num_bytes
= count
* elem_size_bytes
;
4139 if (num_bytes
> 16) {
4140 assert(elem_size_bytes
== 8);
4141 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4146 // TODO: check alignment of sub-dword stores
4147 // TODO: split 3 bytes. there is no store instruction for that
4150 if (count
!= instr
->num_components
) {
4151 emit_split_vector(ctx
, data
, instr
->num_components
);
4152 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4153 for (int i
= 0; i
< count
; i
++) {
4154 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(data
.type(), elem_size_bytes
/ 4));
4155 vec
->operands
[i
] = Operand(smem_nonfs
? bld
.as_uniform(elem
) : elem
);
4157 write_data
= bld
.tmp(smem_nonfs
? RegType::sgpr
: data
.type(), count
* elem_size_bytes
/ 4);
4158 vec
->definitions
[0] = Definition(write_data
);
4159 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4160 } else if (!smem
&& data
.type() != RegType::vgpr
) {
4161 assert(num_bytes
% 4 == 0);
4162 write_data
= bld
.copy(bld
.def(RegType::vgpr
, num_bytes
/ 4), data
);
4163 } else if (smem_nonfs
&& data
.type() == RegType::vgpr
) {
4164 assert(num_bytes
% 4 == 0);
4165 write_data
= bld
.as_uniform(data
);
4170 aco_opcode vmem_op
, smem_op
;
4171 switch (num_bytes
) {
4173 vmem_op
= aco_opcode::buffer_store_dword
;
4174 smem_op
= aco_opcode::s_buffer_store_dword
;
4177 vmem_op
= aco_opcode::buffer_store_dwordx2
;
4178 smem_op
= aco_opcode::s_buffer_store_dwordx2
;
4181 vmem_op
= aco_opcode::buffer_store_dwordx3
;
4182 smem_op
= aco_opcode::last_opcode
;
4186 vmem_op
= aco_opcode::buffer_store_dwordx4
;
4187 smem_op
= aco_opcode::s_buffer_store_dwordx4
;
4190 unreachable("Store SSBO not implemented for this size.");
4192 if (ctx
->stage
== fragment_fs
)
4193 smem_op
= aco_opcode::p_fs_buffer_store_smem
;
4196 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(smem_op
, Format::SMEM
, 3, 0)};
4197 store
->operands
[0] = Operand(rsrc
);
4199 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
4200 offset
, Operand(start
* elem_size_bytes
));
4201 store
->operands
[1] = Operand(off
);
4203 store
->operands
[1] = Operand(offset
);
4205 if (smem_op
!= aco_opcode::p_fs_buffer_store_smem
)
4206 store
->operands
[1].setFixed(m0
);
4207 store
->operands
[2] = Operand(write_data
);
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
->block
->instructions
.emplace_back(std::move(store
));
4212 ctx
->program
->wb_smem_l1_on_end
= true;
4213 if (smem_op
== aco_opcode::p_fs_buffer_store_smem
) {
4214 ctx
->block
->kind
|= block_kind_needs_lowering
;
4215 ctx
->program
->needs_exact
= true;
4218 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(vmem_op
, Format::MUBUF
, 4, 0)};
4219 store
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4220 store
->operands
[1] = Operand(rsrc
);
4221 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4222 store
->operands
[3] = Operand(write_data
);
4223 store
->offset
= start
* elem_size_bytes
;
4224 store
->offen
= (offset
.type() == RegType::vgpr
);
4225 store
->glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4226 store
->disable_wqm
= true;
4227 store
->barrier
= barrier_buffer
;
4228 ctx
->program
->needs_exact
= true;
4229 ctx
->block
->instructions
.emplace_back(std::move(store
));
4234 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4236 /* return the previous value if dest is ever used */
4237 bool return_previous
= false;
4238 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4239 return_previous
= true;
4242 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4243 return_previous
= true;
4247 Builder
bld(ctx
->program
, ctx
->block
);
4248 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4250 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
4251 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
4252 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
4255 if (ctx
->options
->chip_class
< GFX8
)
4256 offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4258 offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4260 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4261 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
4263 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4265 aco_opcode op32
, op64
;
4266 switch (instr
->intrinsic
) {
4267 case nir_intrinsic_ssbo_atomic_add
:
4268 op32
= aco_opcode::buffer_atomic_add
;
4269 op64
= aco_opcode::buffer_atomic_add_x2
;
4271 case nir_intrinsic_ssbo_atomic_imin
:
4272 op32
= aco_opcode::buffer_atomic_smin
;
4273 op64
= aco_opcode::buffer_atomic_smin_x2
;
4275 case nir_intrinsic_ssbo_atomic_umin
:
4276 op32
= aco_opcode::buffer_atomic_umin
;
4277 op64
= aco_opcode::buffer_atomic_umin_x2
;
4279 case nir_intrinsic_ssbo_atomic_imax
:
4280 op32
= aco_opcode::buffer_atomic_smax
;
4281 op64
= aco_opcode::buffer_atomic_smax_x2
;
4283 case nir_intrinsic_ssbo_atomic_umax
:
4284 op32
= aco_opcode::buffer_atomic_umax
;
4285 op64
= aco_opcode::buffer_atomic_umax_x2
;
4287 case nir_intrinsic_ssbo_atomic_and
:
4288 op32
= aco_opcode::buffer_atomic_and
;
4289 op64
= aco_opcode::buffer_atomic_and_x2
;
4291 case nir_intrinsic_ssbo_atomic_or
:
4292 op32
= aco_opcode::buffer_atomic_or
;
4293 op64
= aco_opcode::buffer_atomic_or_x2
;
4295 case nir_intrinsic_ssbo_atomic_xor
:
4296 op32
= aco_opcode::buffer_atomic_xor
;
4297 op64
= aco_opcode::buffer_atomic_xor_x2
;
4299 case nir_intrinsic_ssbo_atomic_exchange
:
4300 op32
= aco_opcode::buffer_atomic_swap
;
4301 op64
= aco_opcode::buffer_atomic_swap_x2
;
4303 case nir_intrinsic_ssbo_atomic_comp_swap
:
4304 op32
= aco_opcode::buffer_atomic_cmpswap
;
4305 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
4308 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
4310 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
4311 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
4312 mubuf
->operands
[0] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
4313 mubuf
->operands
[1] = Operand(rsrc
);
4314 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
4315 mubuf
->operands
[3] = Operand(data
);
4316 if (return_previous
)
4317 mubuf
->definitions
[0] = Definition(dst
);
4319 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
4320 mubuf
->glc
= return_previous
;
4321 mubuf
->disable_wqm
= true;
4322 mubuf
->barrier
= barrier_buffer
;
4323 ctx
->program
->needs_exact
= true;
4324 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
4327 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4329 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4330 Builder
bld(ctx
->program
, ctx
->block
);
4331 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
4332 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
4335 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4337 Builder
bld(ctx
->program
, ctx
->block
);
4338 unsigned num_components
= instr
->num_components
;
4339 unsigned num_bytes
= num_components
* instr
->dest
.ssa
.bit_size
/ 8;
4341 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4342 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4344 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
4346 if (dst
.type() == RegType::vgpr
|| (glc
&& ctx
->options
->chip_class
< GFX8
)) {
4347 bool global
= ctx
->options
->chip_class
>= GFX9
;
4349 switch (num_bytes
) {
4351 op
= global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
4354 op
= global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
4357 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
4360 op
= global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
4363 unreachable("load_global not implemented for this size.");
4365 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
4366 flat
->operands
[0] = Operand(addr
);
4367 flat
->operands
[1] = Operand(s1
);
4370 if (dst
.type() == RegType::sgpr
) {
4371 Temp vec
= bld
.tmp(RegType::vgpr
, dst
.size());
4372 flat
->definitions
[0] = Definition(vec
);
4373 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4374 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
4376 flat
->definitions
[0] = Definition(dst
);
4377 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4379 emit_split_vector(ctx
, dst
, num_components
);
4381 switch (num_bytes
) {
4383 op
= aco_opcode::s_load_dword
;
4386 op
= aco_opcode::s_load_dwordx2
;
4390 op
= aco_opcode::s_load_dwordx4
;
4393 unreachable("load_global not implemented for this size.");
4395 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
4396 load
->operands
[0] = Operand(addr
);
4397 load
->operands
[1] = Operand(0u);
4398 load
->definitions
[0] = Definition(dst
);
4400 load
->barrier
= barrier_buffer
;
4401 assert(ctx
->options
->chip_class
>= GFX8
|| !glc
);
4403 if (dst
.size() == 3) {
4405 Temp vec
= bld
.tmp(s4
);
4406 load
->definitions
[0] = Definition(vec
);
4407 ctx
->block
->instructions
.emplace_back(std::move(load
));
4408 emit_split_vector(ctx
, vec
, 4);
4410 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
4411 emit_extract_vector(ctx
, vec
, 0, s1
),
4412 emit_extract_vector(ctx
, vec
, 1, s1
),
4413 emit_extract_vector(ctx
, vec
, 2, s1
));
4415 ctx
->block
->instructions
.emplace_back(std::move(load
));
4420 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4422 Builder
bld(ctx
->program
, ctx
->block
);
4423 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4425 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4426 Temp addr
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4428 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4431 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4432 unsigned num_bytes
= count
* elem_size_bytes
;
4434 Temp write_data
= data
;
4435 if (count
!= instr
->num_components
) {
4436 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4437 for (int i
= 0; i
< count
; i
++)
4438 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, data
, start
+ i
, v1
));
4439 write_data
= bld
.tmp(RegType::vgpr
, count
);
4440 vec
->definitions
[0] = Definition(write_data
);
4441 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4444 unsigned offset
= start
* elem_size_bytes
;
4445 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
4446 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
4447 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
4448 Temp carry
= bld
.tmp(s2
);
4449 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
4451 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
4452 Operand(offset
), addr0
);
4453 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(s2
),
4455 carry
).def(1).setHint(vcc
);
4457 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
4462 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
4463 bool global
= ctx
->options
->chip_class
>= GFX9
;
4465 switch (num_bytes
) {
4467 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
4470 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
4473 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
4476 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
4479 unreachable("store_global not implemented for this size.");
4481 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
4482 flat
->operands
[0] = Operand(addr
);
4483 flat
->operands
[1] = Operand(s1
);
4484 flat
->operands
[2] = Operand(data
);
4486 flat
->offset
= offset
;
4487 ctx
->block
->instructions
.emplace_back(std::move(flat
));
4491 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4492 Builder
bld(ctx
->program
, ctx
->block
);
4493 switch(instr
->intrinsic
) {
4494 case nir_intrinsic_group_memory_barrier
:
4495 case nir_intrinsic_memory_barrier
:
4496 bld
.barrier(aco_opcode::p_memory_barrier_all
);
4498 case nir_intrinsic_memory_barrier_atomic_counter
:
4499 bld
.barrier(aco_opcode::p_memory_barrier_atomic
);
4501 case nir_intrinsic_memory_barrier_buffer
:
4502 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
4504 case nir_intrinsic_memory_barrier_image
:
4505 bld
.barrier(aco_opcode::p_memory_barrier_image
);
4507 case nir_intrinsic_memory_barrier_shared
:
4508 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
4511 unreachable("Unimplemented memory barrier intrinsic");
4516 Operand
load_lds_size_m0(isel_context
*ctx
)
4518 /* TODO: m0 does not need to be initialized on GFX9+ */
4519 Builder
bld(ctx
->program
, ctx
->block
);
4520 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
4524 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4526 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
4527 Operand m
= load_lds_size_m0(ctx
);
4528 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4529 assert(instr
->dest
.ssa
.bit_size
>= 32 && "Bitsize not supported in load_shared.");
4530 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4531 Builder
bld(ctx
->program
, ctx
->block
);
4533 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4534 unsigned bytes_read
= 0;
4535 unsigned result_size
= 0;
4536 unsigned total_bytes
= instr
->num_components
* elem_size_bytes
;
4537 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : instr
->dest
.ssa
.bit_size
/ 8;
4538 std::array
<Temp
, 4> result
;
4540 while (bytes_read
< total_bytes
) {
4541 unsigned todo
= total_bytes
- bytes_read
;
4542 bool aligned8
= bytes_read
% 8 == 0 && align
% 8 == 0;
4543 bool aligned16
= bytes_read
% 16 == 0 && align
% 16 == 0;
4545 aco_opcode op
= aco_opcode::last_opcode
;
4546 if (todo
>= 16 && aligned16
) {
4547 op
= aco_opcode::ds_read_b128
;
4549 } else if (todo
>= 12 && aligned16
) {
4550 op
= aco_opcode::ds_read_b96
;
4552 } else if (todo
>= 8) {
4553 op
= aligned8
? aco_opcode::ds_read_b64
: aco_opcode::ds_read2_b32
;
4555 } else if (todo
>= 4) {
4556 op
= aco_opcode::ds_read_b32
;
4561 assert(todo
% elem_size_bytes
== 0);
4562 unsigned num_elements
= todo
/ elem_size_bytes
;
4563 unsigned offset
= nir_intrinsic_base(instr
) + bytes_read
;
4564 unsigned max_offset
= op
== aco_opcode::ds_read2_b32
? 1019 : 65535;
4566 Temp address_offset
= address
;
4567 if (offset
> max_offset
) {
4568 address_offset
= bld
.vadd32(bld
.def(v1
), Operand((uint32_t)nir_intrinsic_base(instr
)), address_offset
);
4569 offset
= bytes_read
;
4571 assert(offset
<= max_offset
); /* bytes_read shouldn't be large enough for this to happen */
4574 if (instr
->num_components
== 1 && dst
.type() == RegType::vgpr
)
4577 res
= bld
.tmp(RegClass(RegType::vgpr
, todo
/ 4));
4579 if (op
== aco_opcode::ds_read2_b32
)
4580 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
>> 2, (offset
>> 2) + 1);
4582 res
= bld
.ds(op
, Definition(res
), address_offset
, m
, offset
);
4584 if (instr
->num_components
== 1) {
4585 assert(todo
== total_bytes
);
4586 if (dst
.type() == RegType::sgpr
)
4587 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), res
);
4591 if (dst
.type() == RegType::sgpr
)
4592 res
= bld
.as_uniform(res
);
4594 if (num_elements
== 1) {
4595 result
[result_size
++] = res
;
4597 assert(res
!= dst
&& res
.size() % num_elements
== 0);
4598 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_elements
)};
4599 split
->operands
[0] = Operand(res
);
4600 for (unsigned i
= 0; i
< num_elements
; i
++)
4601 split
->definitions
[i
] = Definition(result
[result_size
++] = bld
.tmp(res
.type(), elem_size_bytes
/ 4));
4602 ctx
->block
->instructions
.emplace_back(std::move(split
));
4608 assert(result_size
== instr
->num_components
&& result_size
> 1);
4609 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, result_size
, 1)};
4610 for (unsigned i
= 0; i
< result_size
; i
++)
4611 vec
->operands
[i
] = Operand(result
[i
]);
4612 vec
->definitions
[0] = Definition(dst
);
4613 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4614 ctx
->allocated_vec
.emplace(dst
.id(), result
);
4617 void ds_write_helper(isel_context
*ctx
, Operand m
, Temp address
, Temp data
, unsigned offset0
, unsigned offset1
, unsigned align
)
4619 Builder
bld(ctx
->program
, ctx
->block
);
4620 unsigned bytes_written
= 0;
4621 while (bytes_written
< data
.size() * 4) {
4622 unsigned todo
= data
.size() * 4 - bytes_written
;
4623 bool aligned8
= bytes_written
% 8 == 0 && align
% 8 == 0;
4624 bool aligned16
= bytes_written
% 16 == 0 && align
% 16 == 0;
4626 aco_opcode op
= aco_opcode::last_opcode
;
4628 if (todo
>= 16 && aligned16
) {
4629 op
= aco_opcode::ds_write_b128
;
4631 } else if (todo
>= 12 && aligned16
) {
4632 op
= aco_opcode::ds_write_b96
;
4634 } else if (todo
>= 8) {
4635 op
= aligned8
? aco_opcode::ds_write_b64
: aco_opcode::ds_write2_b32
;
4637 } else if (todo
>= 4) {
4638 op
= aco_opcode::ds_write_b32
;
4644 bool write2
= op
== aco_opcode::ds_write2_b32
;
4645 unsigned offset
= offset0
+ offset1
+ bytes_written
;
4646 unsigned max_offset
= write2
? 1020 : 65535;
4647 Temp address_offset
= address
;
4648 if (offset
> max_offset
) {
4649 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset0
), address_offset
);
4650 offset
= offset1
+ bytes_written
;
4652 assert(offset
<= max_offset
); /* offset1 shouldn't be large enough for this to happen */
4655 Temp val0
= emit_extract_vector(ctx
, data
, bytes_written
>> 2, v1
);
4656 Temp val1
= emit_extract_vector(ctx
, data
, (bytes_written
>> 2) + 1, v1
);
4657 bld
.ds(op
, address_offset
, val0
, val1
, m
, offset
>> 2, (offset
>> 2) + 1);
4659 Temp val
= emit_extract_vector(ctx
, data
, bytes_written
>> 2, RegClass(RegType::vgpr
, size
));
4660 bld
.ds(op
, address_offset
, val
, m
, offset
);
4663 bytes_written
+= size
* 4;
4667 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4669 unsigned offset
= nir_intrinsic_base(instr
);
4670 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4671 Operand m
= load_lds_size_m0(ctx
);
4672 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4673 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4674 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4675 assert(elem_size_bytes
>= 4 && "Only 32bit & 64bit store_shared currently supported.");
4677 /* we need at most two stores for 32bit variables */
4678 int start
[2], count
[2];
4679 u_bit_scan_consecutive_range(&writemask
, &start
[0], &count
[0]);
4680 u_bit_scan_consecutive_range(&writemask
, &start
[1], &count
[1]);
4681 assert(writemask
== 0);
4683 /* one combined store is sufficient */
4684 if (count
[0] == count
[1]) {
4685 Builder
bld(ctx
->program
, ctx
->block
);
4687 Temp address_offset
= address
;
4688 if ((offset
>> 2) + start
[1] > 255) {
4689 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address_offset
);
4693 assert(count
[0] == 1);
4694 Temp val0
= emit_extract_vector(ctx
, data
, start
[0], v1
);
4695 Temp val1
= emit_extract_vector(ctx
, data
, start
[1], v1
);
4696 aco_opcode op
= elem_size_bytes
== 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
4697 offset
= offset
/ elem_size_bytes
;
4698 bld
.ds(op
, address_offset
, val0
, val1
, m
,
4699 offset
+ start
[0], offset
+ start
[1]);
4703 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
4704 for (unsigned i
= 0; i
< 2; i
++) {
4708 Temp write_data
= emit_extract_vector(ctx
, data
, start
[i
], RegClass(RegType::vgpr
, count
[i
] * elem_size_bytes
/ 4));
4709 ds_write_helper(ctx
, m
, address
, write_data
, offset
, start
[i
] * elem_size_bytes
, align
);
4714 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4716 unsigned offset
= nir_intrinsic_base(instr
);
4717 Operand m
= load_lds_size_m0(ctx
);
4718 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4719 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4721 unsigned num_operands
= 3;
4722 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
4723 switch(instr
->intrinsic
) {
4724 case nir_intrinsic_shared_atomic_add
:
4725 op32
= aco_opcode::ds_add_u32
;
4726 op64
= aco_opcode::ds_add_u64
;
4727 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
4728 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
4730 case nir_intrinsic_shared_atomic_imin
:
4731 op32
= aco_opcode::ds_min_i32
;
4732 op64
= aco_opcode::ds_min_i64
;
4733 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
4734 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
4736 case nir_intrinsic_shared_atomic_umin
:
4737 op32
= aco_opcode::ds_min_u32
;
4738 op64
= aco_opcode::ds_min_u64
;
4739 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
4740 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
4742 case nir_intrinsic_shared_atomic_imax
:
4743 op32
= aco_opcode::ds_max_i32
;
4744 op64
= aco_opcode::ds_max_i64
;
4745 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
4746 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
4748 case nir_intrinsic_shared_atomic_umax
:
4749 op32
= aco_opcode::ds_max_u32
;
4750 op64
= aco_opcode::ds_max_u64
;
4751 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
4752 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
4754 case nir_intrinsic_shared_atomic_and
:
4755 op32
= aco_opcode::ds_and_b32
;
4756 op64
= aco_opcode::ds_and_b64
;
4757 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
4758 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
4760 case nir_intrinsic_shared_atomic_or
:
4761 op32
= aco_opcode::ds_or_b32
;
4762 op64
= aco_opcode::ds_or_b64
;
4763 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
4764 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
4766 case nir_intrinsic_shared_atomic_xor
:
4767 op32
= aco_opcode::ds_xor_b32
;
4768 op64
= aco_opcode::ds_xor_b64
;
4769 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
4770 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
4772 case nir_intrinsic_shared_atomic_exchange
:
4773 op32
= aco_opcode::ds_write_b32
;
4774 op64
= aco_opcode::ds_write_b64
;
4775 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
4776 op64_rtn
= aco_opcode::ds_wrxchg2_rtn_b64
;
4778 case nir_intrinsic_shared_atomic_comp_swap
:
4779 op32
= aco_opcode::ds_cmpst_b32
;
4780 op64
= aco_opcode::ds_cmpst_b64
;
4781 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
4782 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
4786 unreachable("Unhandled shared atomic intrinsic");
4789 /* return the previous value if dest is ever used */
4790 bool return_previous
= false;
4791 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
4792 return_previous
= true;
4795 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
4796 return_previous
= true;
4801 if (data
.size() == 1) {
4802 assert(instr
->dest
.ssa
.bit_size
== 32);
4803 op
= return_previous
? op32_rtn
: op32
;
4805 assert(instr
->dest
.ssa
.bit_size
== 64);
4806 op
= return_previous
? op64_rtn
: op64
;
4809 if (offset
> 65535) {
4810 Builder
bld(ctx
->program
, ctx
->block
);
4811 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
4815 aco_ptr
<DS_instruction
> ds
;
4816 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
4817 ds
->operands
[0] = Operand(address
);
4818 ds
->operands
[1] = Operand(data
);
4819 if (num_operands
== 4)
4820 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
4821 ds
->operands
[num_operands
- 1] = m
;
4822 ds
->offset0
= offset
;
4823 if (return_previous
)
4824 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
4825 ctx
->block
->instructions
.emplace_back(std::move(ds
));
4828 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4829 assert(instr
->dest
.ssa
.bit_size
== 32 || instr
->dest
.ssa
.bit_size
== 64);
4830 Builder
bld(ctx
->program
, ctx
->block
);
4831 Temp scratch_addr
= ctx
->private_segment_buffer
;
4832 if (ctx
->stage
!= MESA_SHADER_COMPUTE
)
4833 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), ctx
->private_segment_buffer
, Operand(0u));
4835 /* older generations need element size = 16 bytes */
4836 if (ctx
->program
->chip_class
>= GFX9
)
4837 rsrc_conf
= 0x00E00000u
;
4839 rsrc_conf
= 0x00F80000u
;
4840 /* buffer res = addr + num_records = -1, index_stride = 64, add_tid_enable = true */
4841 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4842 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4843 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4846 switch (dst
.size()) {
4848 op
= aco_opcode::buffer_load_dword
;
4851 op
= aco_opcode::buffer_load_dwordx2
;
4854 op
= aco_opcode::buffer_load_dwordx3
;
4857 op
= aco_opcode::buffer_load_dwordx4
;
4861 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4862 Temp lower
= bld
.mubuf(aco_opcode::buffer_load_dwordx4
,
4863 bld
.def(v4
), offset
, rsrc
,
4864 ctx
->scratch_offset
, 0, true);
4865 Temp upper
= bld
.mubuf(dst
.size() == 6 ? aco_opcode::buffer_load_dwordx2
:
4866 aco_opcode::buffer_load_dwordx4
,
4867 dst
.size() == 6 ? bld
.def(v2
) : bld
.def(v4
),
4868 offset
, rsrc
, ctx
->scratch_offset
, 16, true);
4869 emit_split_vector(ctx
, lower
, 2);
4870 elems
[0] = emit_extract_vector(ctx
, lower
, 0, v2
);
4871 elems
[1] = emit_extract_vector(ctx
, lower
, 1, v2
);
4872 if (dst
.size() == 8) {
4873 emit_split_vector(ctx
, upper
, 2);
4874 elems
[2] = emit_extract_vector(ctx
, upper
, 0, v2
);
4875 elems
[3] = emit_extract_vector(ctx
, upper
, 1, v2
);
4880 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
4881 Format::PSEUDO
, dst
.size() / 2, 1)};
4882 for (unsigned i
= 0; i
< dst
.size() / 2; i
++)
4883 vec
->operands
[i
] = Operand(elems
[i
]);
4884 vec
->definitions
[0] = Definition(dst
);
4885 bld
.insert(std::move(vec
));
4886 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4890 unreachable("Wrong dst size for nir_intrinsic_load_scratch");
4893 bld
.mubuf(op
, Definition(dst
), offset
, rsrc
, ctx
->scratch_offset
, 0, true);
4894 emit_split_vector(ctx
, dst
, instr
->num_components
);
4897 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4898 assert(instr
->src
[0].ssa
->bit_size
== 32 || instr
->src
[0].ssa
->bit_size
== 64);
4899 Builder
bld(ctx
->program
, ctx
->block
);
4900 Temp scratch_addr
= ctx
->private_segment_buffer
;
4901 if (ctx
->stage
!= MESA_SHADER_COMPUTE
)
4902 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), ctx
->private_segment_buffer
, Operand(0u));
4904 /* older generations need element size = 16 bytes */
4905 if (ctx
->program
->chip_class
>= GFX9
)
4906 rsrc_conf
= 0x00E00000u
;
4908 rsrc_conf
= 0x00F80000u
;
4909 /* buffer res = addr + num_records = -1, index_stride = 64, add_tid_enable = true */
4910 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
4911 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
4912 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
4914 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4915 unsigned writemask
= nir_intrinsic_write_mask(instr
);
4919 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
4920 int num_bytes
= count
* elem_size_bytes
;
4922 if (num_bytes
> 16) {
4923 assert(elem_size_bytes
== 8);
4924 writemask
|= (((count
- 2) << 1) - 1) << (start
+ 2);
4929 // TODO: check alignment of sub-dword stores
4930 // TODO: split 3 bytes. there is no store instruction for that
4933 if (count
!= instr
->num_components
) {
4934 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
4935 for (int i
= 0; i
< count
; i
++) {
4936 Temp elem
= emit_extract_vector(ctx
, data
, start
+ i
, RegClass(RegType::vgpr
, elem_size_bytes
/ 4));
4937 vec
->operands
[i
] = Operand(elem
);
4939 write_data
= bld
.tmp(RegClass(RegType::vgpr
, count
* elem_size_bytes
/ 4));
4940 vec
->definitions
[0] = Definition(write_data
);
4941 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4947 switch (num_bytes
) {
4949 op
= aco_opcode::buffer_store_dword
;
4952 op
= aco_opcode::buffer_store_dwordx2
;
4955 op
= aco_opcode::buffer_store_dwordx3
;
4958 op
= aco_opcode::buffer_store_dwordx4
;
4961 unreachable("Invalid data size for nir_intrinsic_store_scratch.");
4964 bld
.mubuf(op
, offset
, rsrc
, ctx
->scratch_offset
, write_data
, start
* elem_size_bytes
, true);
4968 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
4969 uint8_t log2_ps_iter_samples
;
4970 if (ctx
->program
->info
->ps
.force_persample
) {
4971 log2_ps_iter_samples
=
4972 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
4974 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
4977 /* The bit pattern matches that used by fixed function fragment
4979 static const unsigned ps_iter_masks
[] = {
4980 0xffff, /* not used */
4986 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
4988 Builder
bld(ctx
->program
, ctx
->block
);
4990 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), ctx
->fs_inputs
[fs_input::ancillary
], Operand(8u), Operand(4u));
4991 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
4992 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
4993 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4994 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, ctx
->fs_inputs
[fs_input::sample_coverage
]);
4997 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
4999 Builder
bld(ctx
->program
, ctx
->block
);
5001 if (cluster_size
== 1) {
5003 } if (op
== nir_op_iand
&& cluster_size
== 4) {
5004 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
5005 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5006 return bld
.sop1(aco_opcode::s_not_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5007 bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
));
5008 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
5009 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
5010 return bld
.sop1(aco_opcode::s_wqm_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5011 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)));
5012 } else if (op
== nir_op_iand
&& cluster_size
== 64) {
5013 //subgroupAnd(val) -> (exec & ~val) == 0
5014 Temp tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
).def(1).getTemp();
5015 return bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), tmp
, Operand(0u));
5016 } else if (op
== nir_op_ior
&& cluster_size
== 64) {
5017 //subgroupOr(val) -> (val & exec) != 0
5018 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)).def(1).getTemp();
5019 } else if (op
== nir_op_ixor
&& cluster_size
== 64) {
5020 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
5021 Temp tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5022 tmp
= bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
);
5023 return bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
5025 //subgroupClustered{And,Or,Xor}(val, n) ->
5026 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0))
5027 //cluster_offset = ~(n - 1) & lane_id
5028 //cluster_mask = ((1 << n) - 1)
5029 //subgroupClusteredAnd():
5030 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
5031 //subgroupClusteredOr():
5032 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
5033 //subgroupClusteredXor():
5034 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
5035 Temp lane_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5036 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5037 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
5040 if (op
== nir_op_iand
)
5041 tmp
= bld
.sop2(aco_opcode::s_orn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5043 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5045 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
5046 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
5047 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5048 if (cluster_mask
!= 0xffffffff)
5049 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
5051 Definition cmp_def
= Definition();
5052 if (op
== nir_op_iand
) {
5053 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(cluster_mask
), tmp
).def(0);
5054 } else if (op
== nir_op_ior
) {
5055 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5056 } else if (op
== nir_op_ixor
) {
5057 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
5058 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
5059 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
).def(0);
5061 cmp_def
.setHint(vcc
);
5062 return cmp_def
.getTemp();
5066 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5068 Builder
bld(ctx
->program
, ctx
->block
);
5070 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
5071 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
5072 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
5074 if (op
== nir_op_iand
)
5075 tmp
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5077 tmp
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
));
5079 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
5080 Temp lo
= lohi
.def(0).getTemp();
5081 Temp hi
= lohi
.def(1).getTemp();
5082 Temp mbcnt
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), hi
,
5083 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), lo
, Operand(0u)));
5085 Definition cmp_def
= Definition();
5086 if (op
== nir_op_iand
)
5087 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5088 else if (op
== nir_op_ior
)
5089 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), mbcnt
).def(0);
5090 else if (op
== nir_op_ixor
)
5091 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u),
5092 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
5093 cmp_def
.setHint(vcc
);
5094 return cmp_def
.getTemp();
5097 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
5099 Builder
bld(ctx
->program
, ctx
->block
);
5101 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
5102 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
5103 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
5104 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
5105 if (op
== nir_op_iand
)
5106 return bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5107 else if (op
== nir_op_ior
)
5108 return bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5109 else if (op
== nir_op_ixor
)
5110 return bld
.sop2(aco_opcode::s_xor_b64
, bld
.def(s2
), bld
.def(s1
, scc
), tmp
, src
);
5116 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
5118 Builder
bld(ctx
->program
, ctx
->block
);
5119 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5120 if (src
.regClass().type() == RegType::vgpr
) {
5121 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
5122 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5123 bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.scc(dst
), Operand(0u), Operand(src
));
5124 } else if (src
.regClass() == s1
) {
5125 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
5126 } else if (src
.regClass() == s2
) {
5127 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
5129 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5130 nir_print_instr(&instr
->instr
, stderr
);
5131 fprintf(stderr
, "\n");
5135 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
5137 Builder
bld(ctx
->program
, ctx
->block
);
5138 Temp p1
= ctx
->fs_inputs
[fs_input::persp_center_p1
];
5139 Temp p2
= ctx
->fs_inputs
[fs_input::persp_center_p2
];
5142 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_quad_perm(0, 0, 0, 0));
5143 Temp ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(1, 1, 1, 1));
5144 Temp ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_quad_perm(2, 2, 2, 2));
5145 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_quad_perm(0, 0, 0, 0));
5146 Temp ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(1, 1, 1, 1));
5147 Temp ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_quad_perm(2, 2, 2, 2));
5149 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
5150 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
5151 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
5152 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
5153 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
5154 Temp wqm1
= bld
.tmp(v1
);
5155 emit_wqm(ctx
, tmp1
, wqm1
, true);
5156 Temp wqm2
= bld
.tmp(v1
);
5157 emit_wqm(ctx
, tmp2
, wqm2
, true);
5158 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
5162 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5164 Builder
bld(ctx
->program
, ctx
->block
);
5165 switch(instr
->intrinsic
) {
5166 case nir_intrinsic_load_barycentric_sample
:
5167 case nir_intrinsic_load_barycentric_pixel
:
5168 case nir_intrinsic_load_barycentric_centroid
: {
5169 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
5170 fs_input input
= get_interp_input(instr
->intrinsic
, mode
);
5172 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5173 if (input
== fs_input::max_inputs
) {
5174 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5175 Operand(0u), Operand(0u));
5177 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5178 ctx
->fs_inputs
[input
],
5179 ctx
->fs_inputs
[input
+ 1]);
5181 emit_split_vector(ctx
, dst
, 2);
5184 case nir_intrinsic_load_barycentric_at_sample
: {
5185 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
5186 switch (ctx
->options
->key
.fs
.num_samples
) {
5187 case 2: sample_pos_offset
+= 1 << 3; break;
5188 case 4: sample_pos_offset
+= 3 << 3; break;
5189 case 8: sample_pos_offset
+= 7 << 3; break;
5193 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5194 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
5195 if (addr
.type() == RegType::sgpr
) {
5198 sample_pos_offset
+= const_addr
->u32
<< 3;
5199 offset
= Operand(sample_pos_offset
);
5200 } else if (ctx
->options
->chip_class
>= GFX9
) {
5201 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5203 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
5204 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
5206 addr
= ctx
->private_segment_buffer
;
5207 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), addr
, Operand(offset
));
5209 } else if (ctx
->options
->chip_class
>= GFX9
) {
5210 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5211 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, ctx
->private_segment_buffer
, sample_pos_offset
);
5213 /* addr += ctx->private_segment_buffer + sample_pos_offset */
5214 Temp tmp0
= bld
.tmp(s1
);
5215 Temp tmp1
= bld
.tmp(s1
);
5216 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), ctx
->private_segment_buffer
);
5217 Definition scc_tmp
= bld
.def(s1
, scc
);
5218 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
5219 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), scc_tmp
.getTemp());
5220 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
5221 Temp pck0
= bld
.tmp(v1
);
5222 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
5223 tmp1
= as_vgpr(ctx
, tmp1
);
5224 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(s2
)), tmp1
, Operand(0u), carry
);
5225 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
5227 /* sample_pos = flat_load_dwordx2 addr */
5228 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
5231 /* sample_pos -= 0.5 */
5232 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5233 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
5234 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
5235 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
5236 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
5238 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5241 case nir_intrinsic_load_barycentric_at_offset
: {
5242 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5243 RegClass rc
= RegClass(offset
.type(), 1);
5244 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
5245 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
5246 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
5249 case nir_intrinsic_load_front_face
: {
5250 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5251 Operand(0u), ctx
->fs_inputs
[fs_input::front_face
]).def(0).setHint(vcc
);
5254 case nir_intrinsic_load_view_index
:
5255 case nir_intrinsic_load_layer_id
: {
5256 if (instr
->intrinsic
== nir_intrinsic_load_view_index
&& (ctx
->stage
& sw_vs
)) {
5257 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5258 bld
.copy(Definition(dst
), Operand(ctx
->view_index
));
5262 unsigned idx
= nir_intrinsic_base(instr
);
5263 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5264 Operand(2u), bld
.m0(ctx
->prim_mask
), idx
, 0);
5267 case nir_intrinsic_load_frag_coord
: {
5268 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
5271 case nir_intrinsic_load_sample_pos
: {
5272 Temp posx
= ctx
->fs_inputs
[fs_input::frag_pos_0
];
5273 Temp posy
= ctx
->fs_inputs
[fs_input::frag_pos_1
];
5274 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5275 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
5276 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
5279 case nir_intrinsic_load_interpolated_input
:
5280 visit_load_interpolated_input(ctx
, instr
);
5282 case nir_intrinsic_store_output
:
5283 visit_store_output(ctx
, instr
);
5285 case nir_intrinsic_load_input
:
5286 visit_load_input(ctx
, instr
);
5288 case nir_intrinsic_load_ubo
:
5289 visit_load_ubo(ctx
, instr
);
5291 case nir_intrinsic_load_push_constant
:
5292 visit_load_push_constant(ctx
, instr
);
5294 case nir_intrinsic_load_constant
:
5295 visit_load_constant(ctx
, instr
);
5297 case nir_intrinsic_vulkan_resource_index
:
5298 visit_load_resource(ctx
, instr
);
5300 case nir_intrinsic_discard
:
5301 visit_discard(ctx
, instr
);
5303 case nir_intrinsic_discard_if
:
5304 visit_discard_if(ctx
, instr
);
5306 case nir_intrinsic_load_shared
:
5307 visit_load_shared(ctx
, instr
);
5309 case nir_intrinsic_store_shared
:
5310 visit_store_shared(ctx
, instr
);
5312 case nir_intrinsic_shared_atomic_add
:
5313 case nir_intrinsic_shared_atomic_imin
:
5314 case nir_intrinsic_shared_atomic_umin
:
5315 case nir_intrinsic_shared_atomic_imax
:
5316 case nir_intrinsic_shared_atomic_umax
:
5317 case nir_intrinsic_shared_atomic_and
:
5318 case nir_intrinsic_shared_atomic_or
:
5319 case nir_intrinsic_shared_atomic_xor
:
5320 case nir_intrinsic_shared_atomic_exchange
:
5321 case nir_intrinsic_shared_atomic_comp_swap
:
5322 visit_shared_atomic(ctx
, instr
);
5324 case nir_intrinsic_image_deref_load
:
5325 visit_image_load(ctx
, instr
);
5327 case nir_intrinsic_image_deref_store
:
5328 visit_image_store(ctx
, instr
);
5330 case nir_intrinsic_image_deref_atomic_add
:
5331 case nir_intrinsic_image_deref_atomic_umin
:
5332 case nir_intrinsic_image_deref_atomic_imin
:
5333 case nir_intrinsic_image_deref_atomic_umax
:
5334 case nir_intrinsic_image_deref_atomic_imax
:
5335 case nir_intrinsic_image_deref_atomic_and
:
5336 case nir_intrinsic_image_deref_atomic_or
:
5337 case nir_intrinsic_image_deref_atomic_xor
:
5338 case nir_intrinsic_image_deref_atomic_exchange
:
5339 case nir_intrinsic_image_deref_atomic_comp_swap
:
5340 visit_image_atomic(ctx
, instr
);
5342 case nir_intrinsic_image_deref_size
:
5343 visit_image_size(ctx
, instr
);
5345 case nir_intrinsic_load_ssbo
:
5346 visit_load_ssbo(ctx
, instr
);
5348 case nir_intrinsic_store_ssbo
:
5349 visit_store_ssbo(ctx
, instr
);
5351 case nir_intrinsic_load_global
:
5352 visit_load_global(ctx
, instr
);
5354 case nir_intrinsic_store_global
:
5355 visit_store_global(ctx
, instr
);
5357 case nir_intrinsic_ssbo_atomic_add
:
5358 case nir_intrinsic_ssbo_atomic_imin
:
5359 case nir_intrinsic_ssbo_atomic_umin
:
5360 case nir_intrinsic_ssbo_atomic_imax
:
5361 case nir_intrinsic_ssbo_atomic_umax
:
5362 case nir_intrinsic_ssbo_atomic_and
:
5363 case nir_intrinsic_ssbo_atomic_or
:
5364 case nir_intrinsic_ssbo_atomic_xor
:
5365 case nir_intrinsic_ssbo_atomic_exchange
:
5366 case nir_intrinsic_ssbo_atomic_comp_swap
:
5367 visit_atomic_ssbo(ctx
, instr
);
5369 case nir_intrinsic_load_scratch
:
5370 visit_load_scratch(ctx
, instr
);
5372 case nir_intrinsic_store_scratch
:
5373 visit_store_scratch(ctx
, instr
);
5375 case nir_intrinsic_get_buffer_size
:
5376 visit_get_buffer_size(ctx
, instr
);
5378 case nir_intrinsic_barrier
: {
5379 unsigned* bsize
= ctx
->program
->info
->cs
.block_size
;
5380 unsigned workgroup_size
= bsize
[0] * bsize
[1] * bsize
[2];
5381 if (workgroup_size
> 64)
5382 bld
.sopp(aco_opcode::s_barrier
);
5385 case nir_intrinsic_group_memory_barrier
:
5386 case nir_intrinsic_memory_barrier
:
5387 case nir_intrinsic_memory_barrier_atomic_counter
:
5388 case nir_intrinsic_memory_barrier_buffer
:
5389 case nir_intrinsic_memory_barrier_image
:
5390 case nir_intrinsic_memory_barrier_shared
:
5391 emit_memory_barrier(ctx
, instr
);
5393 case nir_intrinsic_load_num_work_groups
:
5394 case nir_intrinsic_load_work_group_id
:
5395 case nir_intrinsic_load_local_invocation_id
: {
5396 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5398 if (instr
->intrinsic
== nir_intrinsic_load_num_work_groups
)
5399 ids
= ctx
->num_workgroups
;
5400 else if (instr
->intrinsic
== nir_intrinsic_load_work_group_id
)
5401 ids
= ctx
->workgroup_ids
;
5403 ids
= ctx
->local_invocation_ids
;
5404 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5405 ids
[0].id() ? Operand(ids
[0]) : Operand(1u),
5406 ids
[1].id() ? Operand(ids
[1]) : Operand(1u),
5407 ids
[2].id() ? Operand(ids
[2]) : Operand(1u));
5408 emit_split_vector(ctx
, dst
, 3);
5411 case nir_intrinsic_load_local_invocation_index
: {
5412 Temp id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
5413 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5414 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5415 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
5418 case nir_intrinsic_load_subgroup_id
: {
5419 if (ctx
->stage
== compute_cs
) {
5420 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
), ctx
->tg_size
);
5421 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), tg_num
, Operand(0x6u
));
5423 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
5427 case nir_intrinsic_load_subgroup_invocation
: {
5428 bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand((uint32_t) -1),
5429 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
5432 case nir_intrinsic_load_num_subgroups
: {
5433 if (ctx
->stage
== compute_cs
)
5434 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
), ctx
->tg_size
);
5436 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
5439 case nir_intrinsic_ballot
: {
5440 Definition tmp
= bld
.def(s2
);
5441 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5442 if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s2
) {
5443 bld
.sop2(aco_opcode::s_and_b64
, tmp
, bld
.def(s1
, scc
), Operand(exec
, s2
), src
);
5444 } else if (instr
->src
[0].ssa
->bit_size
== 1 && src
.regClass() == s1
) {
5445 bld
.sop2(aco_opcode::s_cselect_b64
, tmp
, Operand(exec
, s2
), Operand(0u), bld
.scc(src
));
5446 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
5447 bld
.vopc(aco_opcode::v_cmp_lg_u32
, tmp
, Operand(0u), src
);
5448 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
5449 bld
.vopc(aco_opcode::v_cmp_lg_u64
, tmp
, Operand(0u), src
);
5451 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5452 nir_print_instr(&instr
->instr
, stderr
);
5453 fprintf(stderr
, "\n");
5455 emit_wqm(ctx
, tmp
.getTemp(), get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5458 case nir_intrinsic_shuffle
: {
5459 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5460 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5461 emit_uniform_subgroup(ctx
, instr
, src
);
5463 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5464 assert(tid
.regClass() == v1
);
5465 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5466 if (src
.regClass() == v1
) {
5467 tid
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), tid
);
5468 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, src
), dst
);
5469 } else if (src
.regClass() == v2
) {
5470 tid
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), tid
);
5472 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5473 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5474 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, lo
));
5475 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), tid
, hi
));
5476 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5477 emit_split_vector(ctx
, dst
, 2);
5478 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5479 Temp tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
5480 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
5481 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
5482 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), tmp
), dst
);
5484 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5485 nir_print_instr(&instr
->instr
, stderr
);
5486 fprintf(stderr
, "\n");
5491 case nir_intrinsic_load_sample_id
: {
5492 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
5493 ctx
->fs_inputs
[ancillary
], Operand(8u), Operand(4u));
5496 case nir_intrinsic_load_sample_mask_in
: {
5497 visit_load_sample_mask_in(ctx
, instr
);
5500 case nir_intrinsic_read_first_invocation
: {
5501 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5502 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5503 if (src
.regClass() == v1
) {
5505 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
5507 } else if (src
.regClass() == v2
) {
5508 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5509 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5510 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
5511 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
5512 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5513 emit_split_vector(ctx
, dst
, 2);
5514 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5516 bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
,
5517 bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
))),
5519 } else if (src
.regClass() == s1
) {
5520 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5521 } else if (src
.regClass() == s2
) {
5522 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5524 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5525 nir_print_instr(&instr
->instr
, stderr
);
5526 fprintf(stderr
, "\n");
5530 case nir_intrinsic_read_invocation
: {
5531 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5532 Temp lane
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5533 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5534 assert(lane
.regClass() == s1
);
5535 if (src
.regClass() == v1
) {
5536 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), src
, lane
), dst
);
5537 } else if (src
.regClass() == v2
) {
5538 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5539 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5540 lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), lo
, lane
));
5541 hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_readlane_b32
, bld
.def(s1
), hi
, lane
));
5542 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5543 emit_split_vector(ctx
, dst
, 2);
5544 } else if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5545 emit_wqm(ctx
, bld
.sopc(aco_opcode::s_bitcmp1_b64
, bld
.def(s1
, scc
), src
, lane
), dst
);
5546 } else if (src
.regClass() == s1
) {
5547 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
5548 } else if (src
.regClass() == s2
) {
5549 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
5551 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5552 nir_print_instr(&instr
->instr
, stderr
);
5553 fprintf(stderr
, "\n");
5557 case nir_intrinsic_vote_all
: {
5558 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5559 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5560 assert(src
.regClass() == s2
);
5561 assert(dst
.regClass() == s1
);
5563 Definition tmp
= bld
.def(s1
);
5564 bld
.sopc(aco_opcode::s_cmp_eq_u64
, bld
.scc(tmp
),
5565 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
)),
5567 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5570 case nir_intrinsic_vote_any
: {
5571 Temp src
= as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false);
5572 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5573 assert(src
.regClass() == s2
);
5574 assert(dst
.regClass() == s1
);
5576 Definition tmp
= bld
.def(s1
);
5577 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.scc(tmp
), src
, Operand(exec
, s2
));
5578 emit_wqm(ctx
, tmp
.getTemp(), dst
);
5581 case nir_intrinsic_reduce
:
5582 case nir_intrinsic_inclusive_scan
:
5583 case nir_intrinsic_exclusive_scan
: {
5584 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5585 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5586 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
5587 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
5588 nir_intrinsic_cluster_size(instr
) : 0;
5589 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: 64, 64));
5591 if (!ctx
->divergent_vals
[instr
->src
[0].ssa
->index
] && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
5592 emit_uniform_subgroup(ctx
, instr
, src
);
5593 } else if (instr
->dest
.ssa
.bit_size
== 1) {
5594 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
5596 else if (op
== nir_op_iadd
)
5598 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
5600 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
5602 switch (instr
->intrinsic
) {
5603 case nir_intrinsic_reduce
:
5604 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
5606 case nir_intrinsic_exclusive_scan
:
5607 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
5609 case nir_intrinsic_inclusive_scan
:
5610 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
5615 } else if (cluster_size
== 1) {
5616 bld
.copy(Definition(dst
), src
);
5618 src
= as_vgpr(ctx
, src
);
5622 #define CASE(name) case nir_op_##name: reduce_op = (src.regClass() == v1) ? name##32 : name##64; break;
5637 unreachable("unknown reduction op");
5642 switch (instr
->intrinsic
) {
5643 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
5644 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
5645 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
5647 unreachable("unknown reduce intrinsic");
5650 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
5651 reduce
->operands
[0] = Operand(src
);
5652 // filled in by aco_reduce_assign.cpp, used internally as part of the
5654 assert(dst
.size() == 1 || dst
.size() == 2);
5655 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
5656 reduce
->operands
[2] = Operand(v1
.as_linear());
5658 Temp tmp_dst
= bld
.tmp(dst
.regClass());
5659 reduce
->definitions
[0] = Definition(tmp_dst
);
5660 reduce
->definitions
[1] = bld
.def(s2
); // used internally
5661 reduce
->definitions
[2] = Definition();
5662 reduce
->definitions
[3] = Definition(scc
, s1
);
5663 reduce
->definitions
[4] = Definition();
5664 reduce
->reduce_op
= reduce_op
;
5665 reduce
->cluster_size
= cluster_size
;
5666 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
5668 emit_wqm(ctx
, tmp_dst
, dst
);
5672 case nir_intrinsic_quad_broadcast
: {
5673 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5674 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5675 emit_uniform_subgroup(ctx
, instr
, src
);
5677 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5678 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
5679 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5680 uint32_t half_mask
= 0x11111111u
<< lane
;
5681 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
5682 Temp tmp
= bld
.tmp(s2
);
5683 bld
.sop1(aco_opcode::s_wqm_b64
, Definition(tmp
),
5684 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mask_tmp
,
5685 bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, s2
))));
5686 emit_wqm(ctx
, tmp
, dst
);
5687 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5689 bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
,
5690 dpp_quad_perm(lane
, lane
, lane
, lane
)),
5692 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5693 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5694 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5695 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5696 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_quad_perm(lane
, lane
, lane
, lane
)));
5697 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5698 emit_split_vector(ctx
, dst
, 2);
5700 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5701 nir_print_instr(&instr
->instr
, stderr
);
5702 fprintf(stderr
, "\n");
5707 case nir_intrinsic_quad_swap_horizontal
:
5708 case nir_intrinsic_quad_swap_vertical
:
5709 case nir_intrinsic_quad_swap_diagonal
:
5710 case nir_intrinsic_quad_swizzle_amd
: {
5711 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5712 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5713 emit_uniform_subgroup(ctx
, instr
, src
);
5716 uint16_t dpp_ctrl
= 0;
5717 switch (instr
->intrinsic
) {
5718 case nir_intrinsic_quad_swap_horizontal
:
5719 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
5721 case nir_intrinsic_quad_swap_vertical
:
5722 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
5724 case nir_intrinsic_quad_swap_diagonal
:
5725 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
5727 case nir_intrinsic_quad_swizzle_amd
: {
5728 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
5735 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5736 if (instr
->dest
.ssa
.bit_size
== 1 && src
.regClass() == s2
) {
5737 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
5738 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5739 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(s2
), Operand(0u), src
);
5740 emit_wqm(ctx
, tmp
, dst
);
5741 } else if (instr
->dest
.ssa
.bit_size
== 32) {
5742 Temp tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
5743 emit_wqm(ctx
, tmp
, dst
);
5744 } else if (instr
->dest
.ssa
.bit_size
== 64) {
5745 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5746 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5747 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
5748 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
5749 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5750 emit_split_vector(ctx
, dst
, 2);
5752 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5753 nir_print_instr(&instr
->instr
, stderr
);
5754 fprintf(stderr
, "\n");
5758 case nir_intrinsic_masked_swizzle_amd
: {
5759 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5760 if (!ctx
->divergent_vals
[instr
->dest
.ssa
.index
]) {
5761 emit_uniform_subgroup(ctx
, instr
, src
);
5764 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5765 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
5766 if (dst
.regClass() == v1
) {
5768 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
5770 } else if (dst
.regClass() == v2
) {
5771 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
5772 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
5773 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
5774 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
5775 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5776 emit_split_vector(ctx
, dst
, 2);
5778 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5779 nir_print_instr(&instr
->instr
, stderr
);
5780 fprintf(stderr
, "\n");
5784 case nir_intrinsic_write_invocation_amd
: {
5785 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5786 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
5787 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
5788 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5789 if (dst
.regClass() == v1
) {
5790 /* src2 is ignored for writelane. RA assigns the same reg for dst */
5791 emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val
, lane
, src
), dst
);
5792 } else if (dst
.regClass() == v2
) {
5793 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
5794 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
5795 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
5796 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
5797 Temp lo
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_lo
, lane
, src_hi
));
5798 Temp hi
= emit_wqm(ctx
, bld
.vop3(aco_opcode::v_writelane_b32
, bld
.def(v1
), val_hi
, lane
, src_hi
));
5799 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
5800 emit_split_vector(ctx
, dst
, 2);
5802 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
5803 nir_print_instr(&instr
->instr
, stderr
);
5804 fprintf(stderr
, "\n");
5808 case nir_intrinsic_mbcnt_amd
: {
5809 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5810 RegClass rc
= RegClass(src
.type(), 1);
5811 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
5812 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
5813 Temp tmp
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), mask_lo
, Operand(0u));
5814 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5815 Temp wqm_tmp
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), mask_hi
, tmp
);
5816 emit_wqm(ctx
, wqm_tmp
, dst
);
5819 case nir_intrinsic_load_helper_invocation
: {
5820 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5821 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
5822 ctx
->block
->kind
|= block_kind_needs_lowering
;
5823 ctx
->program
->needs_exact
= true;
5826 case nir_intrinsic_is_helper_invocation
: {
5827 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5828 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
5829 ctx
->block
->kind
|= block_kind_needs_lowering
;
5830 ctx
->program
->needs_exact
= true;
5833 case nir_intrinsic_demote
:
5834 bld
.pseudo(aco_opcode::p_demote_to_helper
);
5835 ctx
->block
->kind
|= block_kind_uses_demote
;
5836 ctx
->program
->needs_exact
= true;
5838 case nir_intrinsic_demote_if
: {
5839 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
),
5840 as_divergent_bool(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
), false),
5842 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
5843 ctx
->block
->kind
|= block_kind_uses_demote
;
5844 ctx
->program
->needs_exact
= true;
5847 case nir_intrinsic_first_invocation
: {
5848 emit_wqm(ctx
, bld
.sop1(aco_opcode::s_ff1_i32_b64
, bld
.def(s1
), Operand(exec
, s2
)),
5849 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
5852 case nir_intrinsic_shader_clock
:
5853 bld
.smem(aco_opcode::s_memtime
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
5855 case nir_intrinsic_load_vertex_id_zero_base
: {
5856 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5857 bld
.copy(Definition(dst
), ctx
->vertex_id
);
5860 case nir_intrinsic_load_first_vertex
: {
5861 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5862 bld
.copy(Definition(dst
), ctx
->base_vertex
);
5865 case nir_intrinsic_load_base_instance
: {
5866 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5867 bld
.copy(Definition(dst
), ctx
->start_instance
);
5870 case nir_intrinsic_load_instance_id
: {
5871 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5872 bld
.copy(Definition(dst
), ctx
->instance_id
);
5875 case nir_intrinsic_load_draw_id
: {
5876 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5877 bld
.copy(Definition(dst
), ctx
->draw_id
);
5881 fprintf(stderr
, "Unimplemented intrinsic instr: ");
5882 nir_print_instr(&instr
->instr
, stderr
);
5883 fprintf(stderr
, "\n");
5891 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
5892 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
5893 enum glsl_base_type
*stype
)
5895 nir_deref_instr
*texture_deref_instr
= NULL
;
5896 nir_deref_instr
*sampler_deref_instr
= NULL
;
5899 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5900 switch (instr
->src
[i
].src_type
) {
5901 case nir_tex_src_texture_deref
:
5902 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5904 case nir_tex_src_sampler_deref
:
5905 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
5907 case nir_tex_src_plane
:
5908 plane
= nir_src_as_int(instr
->src
[i
].src
);
5915 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
5917 if (!sampler_deref_instr
)
5918 sampler_deref_instr
= texture_deref_instr
;
5921 assert(instr
->op
!= nir_texop_txf_ms
&&
5922 instr
->op
!= nir_texop_samples_identical
);
5923 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
5924 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
5925 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
5926 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
5928 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
5931 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
5932 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
5933 fprintf(stderr
, "Unimplemented sampler descriptor: ");
5934 nir_print_instr(&instr
->instr
, stderr
);
5935 fprintf(stderr
, "\n");
5937 // TODO: build samp_ptr = and(samp_ptr, res_ptr)
5940 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
5941 instr
->op
== nir_texop_samples_identical
))
5942 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
5945 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
5946 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
5948 Builder
bld(ctx
->program
, ctx
->block
);
5950 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
5951 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
5952 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
5954 Operand
neg_one(0xbf800000u
);
5955 Operand
one(0x3f800000u
);
5956 Operand
two(0x40000000u
);
5957 Operand
four(0x40800000u
);
5959 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), Operand(0u), ma
);
5960 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
5961 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
5963 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(s2
)), four
, id
);
5964 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(s2
), two
, id
);
5965 is_ma_y
= bld
.sop2(aco_opcode::s_andn2_b64
, bld
.hint_vcc(bld
.def(s2
)), is_ma_y
, is_ma_z
);
5966 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
);
5969 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
5970 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
5971 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
5973 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
5976 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
5977 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
5978 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
5981 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
5982 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
5984 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
5985 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
5988 void prepare_cube_coords(isel_context
*ctx
, Temp
* coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
5990 Builder
bld(ctx
->program
, ctx
->block
);
5991 Temp coord_args
[4], ma
, tc
, sc
, id
;
5992 for (unsigned i
= 0; i
< (is_array
? 4 : 3); i
++)
5993 coord_args
[i
] = emit_extract_vector(ctx
, *coords
, i
, v1
);
5996 coord_args
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_args
[3]);
5998 // see comment in ac_prepare_cube_coords()
5999 if (ctx
->options
->chip_class
<= GFX8
)
6000 coord_args
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coord_args
[3]);
6003 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6005 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
6006 vop3a
->operands
[0] = Operand(ma
);
6007 vop3a
->abs
[0] = true;
6008 Temp invma
= bld
.tmp(v1
);
6009 vop3a
->definitions
[0] = Definition(invma
);
6010 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
6012 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6014 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6016 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6018 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
6020 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coord_args
[0], coord_args
[1], coord_args
[2]);
6023 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
6024 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
6026 for (unsigned i
= 0; i
< 2; i
++) {
6027 // see comment in ac_prepare_cube_coords()
6029 Temp deriv_sc
, deriv_tc
;
6030 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
6031 &deriv_ma
, &deriv_sc
, &deriv_tc
);
6033 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
6035 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6036 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
6037 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
6038 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
6039 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
6040 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
6041 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
6044 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
6045 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
6049 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coord_args
[3], id
, Operand(0x41000000u
/*8.0*/));
6050 *coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), sc
, tc
, id
);
6054 Temp
apply_round_slice(isel_context
*ctx
, Temp coords
, unsigned idx
)
6057 for (unsigned i
= 0; i
< coords
.size(); i
++)
6058 coord_vec
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6060 Builder
bld(ctx
->program
, ctx
->block
);
6061 coord_vec
[idx
] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coord_vec
[idx
]);
6063 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6064 for (unsigned i
= 0; i
< coords
.size(); i
++)
6065 vec
->operands
[i
] = Operand(coord_vec
[i
]);
6066 Temp res
= bld
.tmp(RegType::vgpr
, coords
.size());
6067 vec
->definitions
[0] = Definition(res
);
6068 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6072 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
6074 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
6076 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
6077 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
6080 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
6081 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
6082 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
6086 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
6088 Builder
bld(ctx
->program
, ctx
->block
);
6089 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
6090 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false;
6091 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), coords
, compare
= Temp(), sample_index
= Temp(),
6092 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(), derivs
= Temp();
6093 nir_const_value
*sample_index_cv
= NULL
;
6094 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
6095 enum glsl_base_type stype
;
6096 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
6098 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
6099 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
6100 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
6101 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
6103 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
6104 switch (instr
->src
[i
].src_type
) {
6105 case nir_tex_src_coord
:
6106 coords
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
));
6108 case nir_tex_src_bias
:
6109 if (instr
->op
== nir_texop_txb
) {
6110 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6114 case nir_tex_src_lod
: {
6115 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
6117 if (val
&& val
->f32
<= 0.0) {
6120 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6125 case nir_tex_src_comparator
:
6126 if (instr
->is_shadow
) {
6127 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6131 case nir_tex_src_offset
:
6132 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6133 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
6136 case nir_tex_src_ddx
:
6137 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6140 case nir_tex_src_ddy
:
6141 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6144 case nir_tex_src_ms_index
:
6145 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
6146 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
6147 has_sample_index
= true;
6149 case nir_tex_src_texture_offset
:
6150 case nir_tex_src_sampler_offset
:
6155 // TODO: all other cases: structure taken from ac_nir_to_llvm.c
6156 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
6157 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6159 if (instr
->op
== nir_texop_texture_samples
) {
6160 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
6162 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
6163 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
6164 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 */));
6165 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
6167 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
6168 samples
, Operand(1u), bld
.scc(is_msaa
));
6172 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
6173 aco_ptr
<Instruction
> tmp_instr
;
6174 Temp acc
, pack
= Temp();
6176 uint32_t pack_const
= 0;
6177 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6178 if (!const_offset
[i
])
6180 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
6183 if (offset
.type() == RegType::sgpr
) {
6184 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6185 if (const_offset
[i
])
6188 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
6189 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
6192 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
6195 if (pack
== Temp()) {
6198 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
6202 if (pack_const
&& pack
!= Temp())
6203 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
6205 for (unsigned i
= 0; i
< offset
.size(); i
++) {
6206 if (const_offset
[i
])
6209 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
6210 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
6213 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
6216 if (pack
== Temp()) {
6219 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
6223 if (pack_const
&& pack
!= Temp())
6224 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
6226 if (pack_const
&& pack
== Temp())
6227 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
6228 else if (pack
== Temp())
6234 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
6235 prepare_cube_coords(ctx
, &coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
6237 /* pack derivatives */
6238 if (has_ddx
|| has_ddy
) {
6239 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
>= GFX9
) {
6240 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v4
),
6241 ddx
, Operand(0u), ddy
, Operand(0u));
6243 derivs
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, ddx
.size() + ddy
.size()), ddx
, ddy
);
6248 if (instr
->coord_components
> 1 &&
6249 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6251 instr
->op
!= nir_texop_txf
)
6252 coords
= apply_round_slice(ctx
, coords
, 1);
6254 if (instr
->coord_components
> 2 &&
6255 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
6256 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6257 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
6258 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6260 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
)
6261 coords
= apply_round_slice(ctx
, coords
, 2);
6263 if (ctx
->options
->chip_class
>= GFX9
&&
6264 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6265 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
6266 assert(coords
.size() > 0 && coords
.size() < 3);
6268 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size() + 1, 1)};
6269 vec
->operands
[0] = Operand(emit_extract_vector(ctx
, coords
, 0, v1
));
6270 vec
->operands
[1] = instr
->op
== nir_texop_txf
? Operand((uint32_t) 0) : Operand((uint32_t) 0x3f000000);
6271 if (coords
.size() > 1)
6272 vec
->operands
[2] = Operand(emit_extract_vector(ctx
, coords
, 1, v1
));
6273 coords
= bld
.tmp(RegType::vgpr
, coords
.size() + 1);
6274 vec
->definitions
[0] = Definition(coords
);
6275 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6278 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
6280 if (instr
->op
== nir_texop_samples_identical
)
6281 resource
= fmask_ptr
;
6283 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
6284 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
6285 instr
->op
!= nir_texop_txs
) {
6286 assert(has_sample_index
);
6287 Operand
op(sample_index
);
6288 if (sample_index_cv
)
6289 op
= Operand(sample_index_cv
->u32
);
6290 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
6293 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
6294 Temp split_coords
[coords
.size()];
6295 emit_split_vector(ctx
, coords
, coords
.size());
6296 for (unsigned i
= 0; i
< coords
.size(); i
++)
6297 split_coords
[i
] = emit_extract_vector(ctx
, coords
, i
, v1
);
6300 for (; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
6301 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
6302 split_coords
[i
] = bld
.vadd32(bld
.def(v1
), split_coords
[i
], off
);
6305 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
6306 for (unsigned i
= 0; i
< coords
.size(); i
++)
6307 vec
->operands
[i
] = Operand(split_coords
[i
]);
6308 coords
= bld
.tmp(coords
.regClass());
6309 vec
->definitions
[0] = Definition(coords
);
6310 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6315 /* Build tex instruction */
6316 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
6317 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6320 /* gather4 selects the component by dmask and always returns vec4 */
6321 if (instr
->op
== nir_texop_tg4
) {
6322 assert(instr
->dest
.ssa
.num_components
== 4);
6323 if (instr
->is_shadow
)
6326 dmask
= 1 << instr
->component
;
6327 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
6328 tmp_dst
= bld
.tmp(v4
);
6329 } else if (instr
->op
== nir_texop_samples_identical
) {
6330 tmp_dst
= bld
.tmp(v1
);
6331 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
6332 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
6335 aco_ptr
<MIMG_instruction
> tex
;
6336 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
6338 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6340 bool div_by_6
= instr
->op
== nir_texop_txs
&&
6341 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
6344 if (tmp_dst
.id() == dst
.id() && div_by_6
)
6345 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
6347 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6348 tex
->operands
[0] = Operand(as_vgpr(ctx
,lod
));
6349 tex
->operands
[1] = Operand(resource
);
6350 if (ctx
->options
->chip_class
>= GFX9
&&
6351 instr
->op
== nir_texop_txs
&&
6352 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
6354 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
6355 } else if (instr
->op
== nir_texop_query_levels
) {
6356 tex
->dmask
= 1 << 3;
6361 tex
->definitions
[0] = Definition(tmp_dst
);
6362 tex
->can_reorder
= true;
6363 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6366 /* divide 3rd value by 6 by multiplying with magic number */
6367 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6368 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6369 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
6370 assert(instr
->dest
.ssa
.num_components
== 3);
6371 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
6372 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6373 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
6374 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
6379 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6383 Temp tg4_compare_cube_wa64
= Temp();
6385 if (tg4_integer_workarounds
) {
6386 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 2, 1));
6387 tex
->operands
[0] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6388 tex
->operands
[1] = Operand(resource
);
6391 Temp size
= bld
.tmp(v2
);
6392 tex
->definitions
[0] = Definition(size
);
6393 tex
->can_reorder
= true;
6394 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6395 emit_split_vector(ctx
, size
, size
.size());
6398 for (unsigned i
= 0; i
< 2; i
++) {
6399 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
6400 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
6401 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
6402 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
6405 Temp orig_coords
[2] = {
6406 emit_extract_vector(ctx
, coords
, 0, v1
),
6407 emit_extract_vector(ctx
, coords
, 1, v1
)};
6408 Temp new_coords
[2] = {
6409 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[0], half_texel
[0]),
6410 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), orig_coords
[1], half_texel
[1])
6413 if (tg4_integer_cube_workaround
) {
6414 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
6415 Temp desc
[resource
.size()];
6416 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
6417 Format::PSEUDO
, 1, resource
.size())};
6418 split
->operands
[0] = Operand(resource
);
6419 for (unsigned i
= 0; i
< resource
.size(); i
++) {
6420 desc
[i
] = bld
.tmp(s1
);
6421 split
->definitions
[i
] = Definition(desc
[i
]);
6423 ctx
->block
->instructions
.emplace_back(std::move(split
));
6425 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
6426 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
6427 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
6430 if (stype
== GLSL_TYPE_UINT
) {
6431 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6432 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
6433 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
6434 bld
.scc(compare_cube_wa
));
6436 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
6437 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
6438 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
6439 bld
.scc(compare_cube_wa
));
6441 tg4_compare_cube_wa64
= as_divergent_bool(ctx
, compare_cube_wa
, true);
6442 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
6444 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
6445 Operand((uint32_t)C_008F14_NUM_FORMAT
));
6446 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
6448 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
6449 Format::PSEUDO
, resource
.size(), 1)};
6450 for (unsigned i
= 0; i
< resource
.size(); i
++)
6451 vec
->operands
[i
] = Operand(desc
[i
]);
6452 resource
= bld
.tmp(resource
.regClass());
6453 vec
->definitions
[0] = Definition(resource
);
6454 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6456 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6457 new_coords
[0], orig_coords
[0], tg4_compare_cube_wa64
);
6458 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
6459 new_coords
[1], orig_coords
[1], tg4_compare_cube_wa64
);
6462 if (coords
.size() == 3) {
6463 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
),
6464 new_coords
[0], new_coords
[1],
6465 emit_extract_vector(ctx
, coords
, 2, v1
));
6467 assert(coords
.size() == 2);
6468 coords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
),
6469 new_coords
[0], new_coords
[1]);
6473 if (!(has_ddx
&& has_ddy
) && !has_lod
&& !level_zero
&&
6474 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
6475 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
6476 coords
= emit_wqm(ctx
, coords
, bld
.tmp(coords
.regClass()), true);
6478 std::vector
<Operand
> args
;
6480 args
.emplace_back(Operand(offset
));
6482 args
.emplace_back(Operand(bias
));
6484 args
.emplace_back(Operand(compare
));
6486 args
.emplace_back(Operand(derivs
));
6487 args
.emplace_back(Operand(coords
));
6488 if (has_sample_index
)
6489 args
.emplace_back(Operand(sample_index
));
6491 args
.emplace_back(lod
);
6494 if (args
.size() > 1) {
6495 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
6497 for (unsigned i
= 0; i
< args
.size(); i
++) {
6498 size
+= args
[i
].size();
6499 vec
->operands
[i
] = args
[i
];
6501 RegClass rc
= RegClass(RegType::vgpr
, size
);
6502 Temp tmp
= bld
.tmp(rc
);
6503 vec
->definitions
[0] = Definition(tmp
);
6504 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6507 assert(args
[0].isTemp());
6508 arg
= Operand(as_vgpr(ctx
, args
[0].getTemp()));
6511 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
6512 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
6514 assert(coords
.size() == 1);
6515 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
6519 op
= aco_opcode::buffer_load_format_x
; break;
6521 op
= aco_opcode::buffer_load_format_xy
; break;
6523 op
= aco_opcode::buffer_load_format_xyz
; break;
6525 op
= aco_opcode::buffer_load_format_xyzw
; break;
6527 unreachable("Tex instruction loads more than 4 components.");
6530 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
6531 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
6534 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
6536 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
6537 mubuf
->operands
[0] = Operand(coords
);
6538 mubuf
->operands
[1] = Operand(resource
);
6539 mubuf
->operands
[2] = Operand((uint32_t) 0);
6540 mubuf
->definitions
[0] = Definition(tmp_dst
);
6541 mubuf
->idxen
= true;
6542 mubuf
->can_reorder
= true;
6543 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6545 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
6550 if (instr
->op
== nir_texop_txf
||
6551 instr
->op
== nir_texop_txf_ms
||
6552 instr
->op
== nir_texop_samples_identical
) {
6553 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
6554 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 2, 1));
6555 tex
->operands
[0] = Operand(arg
);
6556 tex
->operands
[1] = Operand(resource
);
6560 tex
->definitions
[0] = Definition(tmp_dst
);
6561 tex
->can_reorder
= true;
6562 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6564 if (instr
->op
== nir_texop_samples_identical
) {
6565 assert(dmask
== 1 && dst
.regClass() == v1
);
6566 assert(dst
.id() != tmp_dst
.id());
6568 Temp tmp
= bld
.tmp(s2
);
6569 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
6570 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
6573 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
6578 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
6579 aco_opcode opcode
= aco_opcode::image_sample
;
6580 if (has_offset
) { /* image_sample_*_o */
6582 opcode
= aco_opcode::image_sample_c_o
;
6584 opcode
= aco_opcode::image_sample_c_d_o
;
6586 opcode
= aco_opcode::image_sample_c_b_o
;
6588 opcode
= aco_opcode::image_sample_c_lz_o
;
6590 opcode
= aco_opcode::image_sample_c_l_o
;
6592 opcode
= aco_opcode::image_sample_o
;
6594 opcode
= aco_opcode::image_sample_d_o
;
6596 opcode
= aco_opcode::image_sample_b_o
;
6598 opcode
= aco_opcode::image_sample_lz_o
;
6600 opcode
= aco_opcode::image_sample_l_o
;
6602 } else { /* no offset */
6604 opcode
= aco_opcode::image_sample_c
;
6606 opcode
= aco_opcode::image_sample_c_d
;
6608 opcode
= aco_opcode::image_sample_c_b
;
6610 opcode
= aco_opcode::image_sample_c_lz
;
6612 opcode
= aco_opcode::image_sample_c_l
;
6614 opcode
= aco_opcode::image_sample
;
6616 opcode
= aco_opcode::image_sample_d
;
6618 opcode
= aco_opcode::image_sample_b
;
6620 opcode
= aco_opcode::image_sample_lz
;
6622 opcode
= aco_opcode::image_sample_l
;
6626 if (instr
->op
== nir_texop_tg4
) {
6628 opcode
= aco_opcode::image_gather4_lz_o
;
6630 opcode
= aco_opcode::image_gather4_c_lz_o
;
6632 opcode
= aco_opcode::image_gather4_lz
;
6634 opcode
= aco_opcode::image_gather4_c_lz
;
6636 } else if (instr
->op
== nir_texop_lod
) {
6637 opcode
= aco_opcode::image_get_lod
;
6640 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
6641 tex
->operands
[0] = arg
;
6642 tex
->operands
[1] = Operand(resource
);
6643 tex
->operands
[2] = Operand(sampler
);
6646 tex
->definitions
[0] = Definition(tmp_dst
);
6647 tex
->can_reorder
= true;
6648 ctx
->block
->instructions
.emplace_back(std::move(tex
));
6650 if (tg4_integer_cube_workaround
) {
6651 assert(tmp_dst
.id() != dst
.id());
6652 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
6654 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
6656 for (unsigned i
= 0; i
< dst
.size(); i
++) {
6657 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
6659 if (stype
== GLSL_TYPE_UINT
)
6660 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
6662 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
6663 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
6665 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
6666 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
6667 val
[0], val
[1], val
[2], val
[3]);
6669 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
6670 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
6675 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
)
6677 Temp tmp
= get_ssa_temp(ctx
, ssa
);
6678 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
6679 return Operand(tmp
.regClass());
6681 return Operand(tmp
);
6684 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
6686 aco_ptr
<Pseudo_instruction
> phi
;
6687 unsigned num_src
= exec_list_length(&instr
->srcs
);
6688 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6690 aco_opcode opcode
= !dst
.is_linear() || ctx
->divergent_vals
[instr
->dest
.ssa
.index
] ? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
6692 std::map
<unsigned, nir_ssa_def
*> phi_src
;
6693 bool all_undef
= true;
6694 nir_foreach_phi_src(src
, instr
) {
6695 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
6696 if (src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_ssa_undef
)
6700 Builder
bld(ctx
->program
, ctx
->block
);
6701 if (dst
.regClass() == s1
) {
6702 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
6703 } else if (dst
.regClass() == v1
) {
6704 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
6706 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6707 for (unsigned i
= 0; i
< dst
.size(); i
++)
6708 vec
->operands
[i
] = Operand(0u);
6709 vec
->definitions
[0] = Definition(dst
);
6710 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6715 /* try to scalarize vector phis */
6716 if (dst
.size() > 1) {
6717 // TODO: scalarize linear phis on divergent ifs
6718 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
6719 std::array
<Temp
, 4> new_vec
;
6720 for (std::pair
<const unsigned, nir_ssa_def
*>& pair
: phi_src
) {
6721 Operand src
= get_phi_operand(ctx
, pair
.second
);
6722 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end()) {
6723 can_scalarize
= false;
6727 if (can_scalarize
) {
6728 unsigned num_components
= instr
->dest
.ssa
.num_components
;
6729 assert(dst
.size() % num_components
== 0);
6730 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
6732 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
6733 for (unsigned k
= 0; k
< num_components
; k
++) {
6734 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
, 1));
6735 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6736 for (unsigned i
= 0; i
< num_src
; i
++) {
6737 Operand src
= get_phi_operand(ctx
, it
->second
);
6738 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
6741 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
6742 phi
->definitions
[0] = Definition(phi_dst
);
6743 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6744 new_vec
[k
] = phi_dst
;
6745 vec
->operands
[k
] = Operand(phi_dst
);
6747 vec
->definitions
[0] = Definition(dst
);
6748 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6749 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
6754 unsigned extra_src
= 0;
6755 if (opcode
== aco_opcode::p_linear_phi
&& (ctx
->block
->kind
& block_kind_loop_exit
) &&
6756 ctx
->program
->blocks
[ctx
->block
->index
-2].kind
& block_kind_continue_or_break
) {
6760 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_src
+ extra_src
, 1));
6762 /* if we have a linear phi on a divergent if, we know that one src is undef */
6763 if (opcode
== aco_opcode::p_linear_phi
&& ctx
->block
->kind
& block_kind_merge
) {
6764 assert(extra_src
== 0);
6766 /* we place the phi either in the invert-block or in the current block */
6767 if (phi_src
.begin()->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
) {
6768 assert((++phi_src
.begin())->second
->parent_instr
->type
== nir_instr_type_ssa_undef
);
6769 Block
& linear_else
= ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
6770 block
= &ctx
->program
->blocks
[linear_else
.linear_preds
[0]];
6771 assert(block
->kind
& block_kind_invert
);
6772 phi
->operands
[0] = get_phi_operand(ctx
, phi_src
.begin()->second
);
6774 assert((++phi_src
.begin())->second
->parent_instr
->type
!= nir_instr_type_ssa_undef
);
6776 phi
->operands
[0] = get_phi_operand(ctx
, (++phi_src
.begin())->second
);
6778 phi
->operands
[1] = Operand(dst
.regClass());
6779 phi
->definitions
[0] = Definition(dst
);
6780 block
->instructions
.emplace(block
->instructions
.begin(), std::move(phi
));
6784 std::map
<unsigned, nir_ssa_def
*>::iterator it
= phi_src
.begin();
6785 for (unsigned i
= 0; i
< num_src
; i
++) {
6786 phi
->operands
[i
] = get_phi_operand(ctx
, it
->second
);
6789 for (unsigned i
= 0; i
< extra_src
; i
++)
6790 phi
->operands
[num_src
+ i
] = Operand(dst
.regClass());
6791 phi
->definitions
[0] = Definition(dst
);
6792 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
6796 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
6798 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
6800 assert(dst
.type() == RegType::sgpr
);
6802 if (dst
.size() == 1) {
6803 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
6805 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
6806 for (unsigned i
= 0; i
< dst
.size(); i
++)
6807 vec
->operands
[i
] = Operand(0u);
6808 vec
->definitions
[0] = Definition(dst
);
6809 ctx
->block
->instructions
.emplace_back(std::move(vec
));
6813 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
6815 Builder
bld(ctx
->program
, ctx
->block
);
6816 Block
*logical_target
;
6817 append_logical_end(ctx
->block
);
6818 unsigned idx
= ctx
->block
->index
;
6820 switch (instr
->type
) {
6821 case nir_jump_break
:
6822 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
6823 add_logical_edge(idx
, logical_target
);
6824 ctx
->block
->kind
|= block_kind_break
;
6826 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
6827 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
6828 /* uniform break - directly jump out of the loop */
6829 ctx
->block
->kind
|= block_kind_uniform
;
6830 ctx
->cf_info
.has_branch
= true;
6831 bld
.branch(aco_opcode::p_branch
);
6832 add_linear_edge(idx
, logical_target
);
6835 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6837 case nir_jump_continue
:
6838 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6839 add_logical_edge(idx
, logical_target
);
6840 ctx
->block
->kind
|= block_kind_continue
;
6842 if (ctx
->cf_info
.parent_if
.is_divergent
) {
6843 /* for potential uniform breaks after this continue,
6844 we must ensure that they are handled correctly */
6845 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
6846 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
6848 /* uniform continue - directly jump to the loop header */
6849 ctx
->block
->kind
|= block_kind_uniform
;
6850 ctx
->cf_info
.has_branch
= true;
6851 bld
.branch(aco_opcode::p_branch
);
6852 add_linear_edge(idx
, logical_target
);
6857 fprintf(stderr
, "Unknown NIR jump instr: ");
6858 nir_print_instr(&instr
->instr
, stderr
);
6859 fprintf(stderr
, "\n");
6863 /* remove critical edges from linear CFG */
6864 bld
.branch(aco_opcode::p_branch
);
6865 Block
* break_block
= ctx
->program
->create_and_insert_block();
6866 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6867 break_block
->kind
|= block_kind_uniform
;
6868 add_linear_edge(idx
, break_block
);
6869 /* the loop_header pointer might be invalidated by this point */
6870 if (instr
->type
== nir_jump_continue
)
6871 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
6872 add_linear_edge(break_block
->index
, logical_target
);
6873 bld
.reset(break_block
);
6874 bld
.branch(aco_opcode::p_branch
);
6876 Block
* continue_block
= ctx
->program
->create_and_insert_block();
6877 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6878 add_linear_edge(idx
, continue_block
);
6879 append_logical_start(continue_block
);
6880 ctx
->block
= continue_block
;
6884 void visit_block(isel_context
*ctx
, nir_block
*block
)
6886 nir_foreach_instr(instr
, block
) {
6887 switch (instr
->type
) {
6888 case nir_instr_type_alu
:
6889 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
6891 case nir_instr_type_load_const
:
6892 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
6894 case nir_instr_type_intrinsic
:
6895 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
6897 case nir_instr_type_tex
:
6898 visit_tex(ctx
, nir_instr_as_tex(instr
));
6900 case nir_instr_type_phi
:
6901 visit_phi(ctx
, nir_instr_as_phi(instr
));
6903 case nir_instr_type_ssa_undef
:
6904 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
6906 case nir_instr_type_deref
:
6908 case nir_instr_type_jump
:
6909 visit_jump(ctx
, nir_instr_as_jump(instr
));
6912 fprintf(stderr
, "Unknown NIR instr type: ");
6913 nir_print_instr(instr
, stderr
);
6914 fprintf(stderr
, "\n");
6922 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
6924 append_logical_end(ctx
->block
);
6925 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
6926 Builder
bld(ctx
->program
, ctx
->block
);
6927 bld
.branch(aco_opcode::p_branch
);
6928 unsigned loop_preheader_idx
= ctx
->block
->index
;
6930 Block loop_exit
= Block();
6931 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6932 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
6934 Block
* loop_header
= ctx
->program
->create_and_insert_block();
6935 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
6936 loop_header
->kind
|= block_kind_loop_header
;
6937 add_edge(loop_preheader_idx
, loop_header
);
6938 ctx
->block
= loop_header
;
6940 /* emit loop body */
6941 unsigned loop_header_idx
= loop_header
->index
;
6942 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
6943 append_logical_start(ctx
->block
);
6944 visit_cf_list(ctx
, &loop
->body
);
6946 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
6947 if (!ctx
->cf_info
.has_branch
) {
6948 append_logical_end(ctx
->block
);
6949 if (ctx
->cf_info
.exec_potentially_empty
) {
6950 /* Discards can result in code running with an empty exec mask.
6951 * This would result in divergent breaks not ever being taken. As a
6952 * workaround, break the loop when the loop mask is empty instead of
6953 * always continuing. */
6954 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
6956 /* create "loop_almost_exit" to avoid critical edges */
6957 unsigned block_idx
= ctx
->block
->index
;
6958 Block
*loop_almost_exit
= ctx
->program
->create_and_insert_block();
6959 loop_almost_exit
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
6960 loop_almost_exit
->kind
= block_kind_uniform
;
6961 bld
.reset(loop_almost_exit
);
6962 bld
.branch(aco_opcode::p_branch
);
6964 add_linear_edge(block_idx
, loop_almost_exit
);
6965 add_linear_edge(loop_almost_exit
->index
, &loop_exit
);
6967 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
6969 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
6971 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
6972 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
6974 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
6975 bld
.reset(ctx
->block
);
6976 bld
.branch(aco_opcode::p_branch
);
6979 /* fixup phis in loop header from unreachable blocks */
6980 if (ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
) {
6981 bool linear
= ctx
->cf_info
.has_branch
;
6982 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
6983 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
6984 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
6985 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
6986 /* the last operand should be the one that needs to be removed */
6987 instr
->operands
.pop_back();
6988 } else if (!is_phi(instr
)) {
6994 ctx
->cf_info
.has_branch
= false;
6996 // TODO: if the loop has not a single exit, we must add one °°
6997 /* emit loop successor block */
6998 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
6999 append_logical_start(ctx
->block
);
7002 // TODO: check if it is beneficial to not branch on continues
7003 /* trim linear phis in loop header */
7004 for (auto&& instr
: loop_entry
->instructions
) {
7005 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
7006 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
7007 new_phi
->definitions
[0] = instr
->definitions
[0];
7008 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
7009 new_phi
->operands
[i
] = instr
->operands
[i
];
7010 /* check that the remaining operands are all the same */
7011 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
7012 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
7013 instr
.swap(new_phi
);
7014 } else if (instr
->opcode
== aco_opcode::p_phi
) {
7023 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
7027 append_logical_end(ctx
->block
);
7028 ctx
->block
->kind
|= block_kind_branch
;
7030 /* branch to linear then block */
7031 assert(cond
.regClass() == s2
);
7032 aco_ptr
<Pseudo_branch_instruction
> branch
;
7033 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7034 branch
->operands
[0] = Operand(cond
);
7035 ctx
->block
->instructions
.push_back(std::move(branch
));
7037 ic
->BB_if_idx
= ctx
->block
->index
;
7038 ic
->BB_invert
= Block();
7039 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7040 /* Invert blocks are intentionally not marked as top level because they
7041 * are not part of the logical cfg. */
7042 ic
->BB_invert
.kind
|= block_kind_invert
;
7043 ic
->BB_endif
= Block();
7044 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7045 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
7047 ic
->exec_potentially_empty_old
= ctx
->cf_info
.exec_potentially_empty
;
7048 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
7049 ctx
->cf_info
.parent_if
.is_divergent
= true;
7050 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7052 /** emit logical then block */
7053 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
7054 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7055 add_edge(ic
->BB_if_idx
, BB_then_logical
);
7056 ctx
->block
= BB_then_logical
;
7057 append_logical_start(BB_then_logical
);
7060 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
7062 Block
*BB_then_logical
= ctx
->block
;
7063 append_logical_end(BB_then_logical
);
7064 /* branch from logical then block to invert block */
7065 aco_ptr
<Pseudo_branch_instruction
> branch
;
7066 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7067 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
7068 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
7069 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7070 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
7071 BB_then_logical
->kind
|= block_kind_uniform
;
7072 assert(!ctx
->cf_info
.has_branch
);
7073 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7074 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7076 /** emit linear then block */
7077 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
7078 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7079 BB_then_linear
->kind
|= block_kind_uniform
;
7080 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
7081 /* branch from linear then block to invert block */
7082 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7083 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
7084 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
7086 /** emit invert merge block */
7087 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
7088 ic
->invert_idx
= ctx
->block
->index
;
7090 /* branch to linear else block (skip else) */
7091 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
7092 branch
->operands
[0] = Operand(ic
->cond
);
7093 ctx
->block
->instructions
.push_back(std::move(branch
));
7095 ic
->exec_potentially_empty_old
|= ctx
->cf_info
.exec_potentially_empty
;
7096 ctx
->cf_info
.exec_potentially_empty
= false; /* divergent branches use cbranch_execz */
7098 /** emit logical else block */
7099 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
7100 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7101 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
7102 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
7103 ctx
->block
= BB_else_logical
;
7104 append_logical_start(BB_else_logical
);
7107 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
7109 Block
*BB_else_logical
= ctx
->block
;
7110 append_logical_end(BB_else_logical
);
7112 /* branch from logical else block to endif block */
7113 aco_ptr
<Pseudo_branch_instruction
> branch
;
7114 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7115 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
7116 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7117 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7118 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
7119 BB_else_logical
->kind
|= block_kind_uniform
;
7121 assert(!ctx
->cf_info
.has_branch
);
7122 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
7125 /** emit linear else block */
7126 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
7127 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7128 BB_else_linear
->kind
|= block_kind_uniform
;
7129 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
7131 /* branch from linear else block to endif block */
7132 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7133 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
7134 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
7137 /** emit endif merge block */
7138 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
7139 append_logical_start(ctx
->block
);
7142 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
7143 ctx
->cf_info
.exec_potentially_empty
|= ic
->exec_potentially_empty_old
;
7144 /* uniform control flow never has an empty exec-mask */
7145 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
7146 ctx
->cf_info
.exec_potentially_empty
= false;
7149 static void visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
7151 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
7152 Builder
bld(ctx
->program
, ctx
->block
);
7153 aco_ptr
<Pseudo_branch_instruction
> branch
;
7155 if (!ctx
->divergent_vals
[if_stmt
->condition
.ssa
->index
]) { /* uniform condition */
7157 * Uniform conditionals are represented in the following way*) :
7159 * The linear and logical CFG:
7162 * BB_THEN (logical) BB_ELSE (logical)
7166 * *) Exceptions may be due to break and continue statements within loops
7167 * If a break/continue happens within uniform control flow, it branches
7168 * to the loop exit/entry block. Otherwise, it branches to the next
7171 append_logical_end(ctx
->block
);
7172 ctx
->block
->kind
|= block_kind_uniform
;
7175 if (cond
.regClass() == s2
) {
7176 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
7177 cond
= as_uniform_bool(ctx
, cond
);
7179 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
7180 branch
->operands
[0] = Operand(cond
);
7181 branch
->operands
[0].setFixed(scc
);
7182 ctx
->block
->instructions
.emplace_back(std::move(branch
));
7184 unsigned BB_if_idx
= ctx
->block
->index
;
7185 Block BB_endif
= Block();
7186 BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7187 BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
7189 /** emit then block */
7190 Block
* BB_then
= ctx
->program
->create_and_insert_block();
7191 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7192 add_edge(BB_if_idx
, BB_then
);
7193 append_logical_start(BB_then
);
7194 ctx
->block
= BB_then
;
7195 visit_cf_list(ctx
, &if_stmt
->then_list
);
7196 BB_then
= ctx
->block
;
7197 bool then_branch
= ctx
->cf_info
.has_branch
;
7198 bool then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
7201 append_logical_end(BB_then
);
7202 /* branch from then block to endif block */
7203 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7204 BB_then
->instructions
.emplace_back(std::move(branch
));
7205 add_linear_edge(BB_then
->index
, &BB_endif
);
7206 if (!then_branch_divergent
)
7207 add_logical_edge(BB_then
->index
, &BB_endif
);
7208 BB_then
->kind
|= block_kind_uniform
;
7211 ctx
->cf_info
.has_branch
= false;
7212 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
7214 /** emit else block */
7215 Block
* BB_else
= ctx
->program
->create_and_insert_block();
7216 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
7217 add_edge(BB_if_idx
, BB_else
);
7218 append_logical_start(BB_else
);
7219 ctx
->block
= BB_else
;
7220 visit_cf_list(ctx
, &if_stmt
->else_list
);
7221 BB_else
= ctx
->block
;
7223 if (!ctx
->cf_info
.has_branch
) {
7224 append_logical_end(BB_else
);
7225 /* branch from then block to endif block */
7226 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
7227 BB_else
->instructions
.emplace_back(std::move(branch
));
7228 add_linear_edge(BB_else
->index
, &BB_endif
);
7229 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
7230 add_logical_edge(BB_else
->index
, &BB_endif
);
7231 BB_else
->kind
|= block_kind_uniform
;
7234 ctx
->cf_info
.has_branch
&= then_branch
;
7235 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= then_branch_divergent
;
7237 /** emit endif merge block */
7238 if (!ctx
->cf_info
.has_branch
) {
7239 ctx
->block
= ctx
->program
->insert_block(std::move(BB_endif
));
7240 append_logical_start(ctx
->block
);
7242 } else { /* non-uniform condition */
7244 * To maintain a logical and linear CFG without critical edges,
7245 * non-uniform conditionals are represented in the following way*) :
7250 * BB_THEN (logical) BB_THEN (linear)
7252 * BB_INVERT (linear)
7254 * BB_ELSE (logical) BB_ELSE (linear)
7261 * BB_THEN (logical) BB_ELSE (logical)
7265 * *) Exceptions may be due to break and continue statements within loops
7270 begin_divergent_if_then(ctx
, &ic
, cond
);
7271 visit_cf_list(ctx
, &if_stmt
->then_list
);
7273 begin_divergent_if_else(ctx
, &ic
);
7274 visit_cf_list(ctx
, &if_stmt
->else_list
);
7276 end_divergent_if(ctx
, &ic
);
7280 static void visit_cf_list(isel_context
*ctx
,
7281 struct exec_list
*list
)
7283 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
7284 switch (node
->type
) {
7285 case nir_cf_node_block
:
7286 visit_block(ctx
, nir_cf_node_as_block(node
));
7288 case nir_cf_node_if
:
7289 visit_if(ctx
, nir_cf_node_as_if(node
));
7291 case nir_cf_node_loop
:
7292 visit_loop(ctx
, nir_cf_node_as_loop(node
));
7295 unreachable("unimplemented cf list type");
7300 static void export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
7302 int offset
= ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
7303 uint64_t mask
= ctx
->vs_output
.mask
[slot
];
7304 if (!is_pos
&& !mask
)
7306 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
7308 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7309 exp
->enabled_mask
= mask
;
7310 for (unsigned i
= 0; i
< 4; ++i
) {
7311 if (mask
& (1 << i
))
7312 exp
->operands
[i
] = Operand(ctx
->vs_output
.outputs
[slot
][i
]);
7314 exp
->operands
[i
] = Operand(v1
);
7316 exp
->valid_mask
= false;
7318 exp
->compressed
= false;
7320 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7322 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
7323 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7326 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
7328 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
7329 exp
->enabled_mask
= 0;
7330 for (unsigned i
= 0; i
< 4; ++i
)
7331 exp
->operands
[i
] = Operand(v1
);
7332 if (ctx
->vs_output
.mask
[VARYING_SLOT_PSIZ
]) {
7333 exp
->operands
[0] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_PSIZ
][0]);
7334 exp
->enabled_mask
|= 0x1;
7336 if (ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
]) {
7337 exp
->operands
[2] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0]);
7338 exp
->enabled_mask
|= 0x4;
7340 if (ctx
->vs_output
.mask
[VARYING_SLOT_VIEWPORT
]) {
7341 if (ctx
->options
->chip_class
< GFX9
) {
7342 exp
->operands
[3] = Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]);
7343 exp
->enabled_mask
|= 0x8;
7345 Builder
bld(ctx
->program
, ctx
->block
);
7347 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
7348 Operand(ctx
->vs_output
.outputs
[VARYING_SLOT_VIEWPORT
][0]));
7349 if (exp
->operands
[2].isTemp())
7350 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
7352 exp
->operands
[2] = Operand(out
);
7353 exp
->enabled_mask
|= 0x4;
7356 exp
->valid_mask
= false;
7358 exp
->compressed
= false;
7359 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
7360 ctx
->block
->instructions
.emplace_back(std::move(exp
));
7363 static void create_vs_exports(isel_context
*ctx
)
7365 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
7367 if (outinfo
->export_prim_id
) {
7368 ctx
->vs_output
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
7369 ctx
->vs_output
.outputs
[VARYING_SLOT_PRIMITIVE_ID
][0] = ctx
->vs_prim_id
;
7372 if (ctx
->options
->key
.has_multiview_view_index
) {
7373 ctx
->vs_output
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
7374 ctx
->vs_output
.outputs
[VARYING_SLOT_LAYER
][0] = as_vgpr(ctx
, ctx
->view_index
);
7377 /* the order these position exports are created is important */
7379 export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
7380 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
7381 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
7383 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7384 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
7385 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7386 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
7388 if (ctx
->options
->key
.vs_common_out
.export_clip_dists
) {
7389 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
7390 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
7391 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
7392 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
7395 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
7396 if (i
< VARYING_SLOT_VAR0
&& i
!= VARYING_SLOT_LAYER
&&
7397 i
!= VARYING_SLOT_PRIMITIVE_ID
)
7400 export_vs_varying(ctx
, i
, false, NULL
);
7404 static void emit_stream_output(isel_context
*ctx
,
7405 Temp
const *so_buffers
,
7406 Temp
const *so_write_offset
,
7407 const struct radv_stream_output
*output
)
7409 unsigned num_comps
= util_bitcount(output
->component_mask
);
7410 unsigned loc
= output
->location
;
7411 unsigned buf
= output
->buffer
;
7412 unsigned offset
= output
->offset
;
7414 assert(num_comps
&& num_comps
<= 4);
7415 if (!num_comps
|| num_comps
> 4)
7418 unsigned start
= ffs(output
->component_mask
) - 1;
7421 bool all_undef
= true;
7422 assert(ctx
->stage
== vertex_vs
);
7423 for (unsigned i
= 0; i
< num_comps
; i
++) {
7424 out
[i
] = ctx
->vs_output
.outputs
[loc
][start
+ i
];
7425 all_undef
= all_undef
&& !out
[i
].id();
7430 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_comps
)};
7431 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_comps
, 1)};
7432 for (unsigned i
= 0; i
< num_comps
; ++i
)
7433 vec
->operands
[i
] = (ctx
->vs_output
.mask
[loc
] & 1 << i
) ? Operand(out
[i
]) : Operand(0u);
7434 vec
->definitions
[0] = Definition(write_data
);
7435 ctx
->block
->instructions
.emplace_back(std::move(vec
));
7438 switch (num_comps
) {
7440 opcode
= aco_opcode::buffer_store_dword
;
7443 opcode
= aco_opcode::buffer_store_dwordx2
;
7446 opcode
= aco_opcode::buffer_store_dwordx3
;
7449 opcode
= aco_opcode::buffer_store_dwordx4
;
7453 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
7454 store
->operands
[0] = Operand(so_write_offset
[buf
]);
7455 store
->operands
[1] = Operand(so_buffers
[buf
]);
7456 store
->operands
[2] = Operand((uint32_t) 0);
7457 store
->operands
[3] = Operand(write_data
);
7458 if (offset
> 4095) {
7459 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
7460 Builder
bld(ctx
->program
, ctx
->block
);
7461 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
7463 store
->offset
= offset
;
7465 store
->offen
= true;
7468 store
->can_reorder
= true;
7469 ctx
->block
->instructions
.emplace_back(std::move(store
));
7472 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
7474 Builder
bld(ctx
->program
, ctx
->block
);
7477 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, ctx
->streamout_buffers
);
7478 for (unsigned i
= 0; i
< 4; i
++) {
7479 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7483 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, Operand(i
* 16u));
7486 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7487 ctx
->streamout_config
, Operand(0x70010u
));
7489 Temp tid
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7490 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7492 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), so_vtx_count
, tid
);
7495 begin_divergent_if_then(ctx
, &ic
, can_emit
);
7497 bld
.reset(ctx
->block
);
7499 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), ctx
->streamout_write_idx
, tid
);
7501 Temp so_write_offset
[4];
7503 for (unsigned i
= 0; i
< 4; i
++) {
7504 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
7509 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
7510 ctx
->streamout_write_idx
, ctx
->streamout_offset
[i
]);
7511 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
7513 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
7515 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
7516 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u), ctx
->streamout_offset
[i
]);
7517 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
7521 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
7522 struct radv_stream_output
*output
=
7523 &ctx
->program
->info
->so
.outputs
[i
];
7524 if (stream
!= output
->stream
)
7527 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
7530 begin_divergent_if_else(ctx
, &ic
);
7531 end_divergent_if(ctx
, &ic
);
7534 } /* end namespace */
7536 void handle_bc_optimize(isel_context
*ctx
)
7538 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
7539 Builder
bld(ctx
->program
, ctx
->block
);
7540 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
7541 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
7542 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
7543 if (uses_center
&& uses_centroid
) {
7544 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(s2
)), ctx
->prim_mask
, Operand(0u));
7546 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
7547 for (unsigned i
= 0; i
< 2; i
++) {
7548 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7549 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
],
7550 ctx
->fs_inputs
[fs_input::persp_center_p1
+ i
],
7552 ctx
->fs_inputs
[fs_input::persp_centroid_p1
+ i
] = new_coord
;
7556 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
7557 for (unsigned i
= 0; i
< 2; i
++) {
7558 Temp new_coord
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
7559 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
],
7560 ctx
->fs_inputs
[fs_input::linear_center_p1
+ i
],
7562 ctx
->fs_inputs
[fs_input::linear_centroid_p1
+ i
] = new_coord
;
7568 void select_program(Program
*program
,
7569 unsigned shader_count
,
7570 struct nir_shader
*const *shaders
,
7571 ac_shader_config
* config
,
7572 struct radv_shader_info
*info
,
7573 struct radv_nir_compiler_options
*options
)
7575 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, info
, options
);
7577 for (unsigned i
= 0; i
< shader_count
; i
++) {
7578 nir_shader
*nir
= shaders
[i
];
7579 init_context(&ctx
, nir
);
7582 add_startpgm(&ctx
); /* needs to be after init_context() for FS */
7583 append_logical_start(ctx
.block
);
7587 if (shader_count
>= 2) {
7588 Builder
bld(ctx
.program
, ctx
.block
);
7589 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)));
7590 Temp thread_id
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1),
7591 bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, bld
.def(v1
), Operand((uint32_t) -1), Operand(0u)));
7592 Temp cond
= bld
.vopc(aco_opcode::v_cmp_gt_u32
, bld
.hint_vcc(bld
.def(s2
)), count
, thread_id
);
7594 begin_divergent_if_then(&ctx
, &ic
, cond
);
7598 Builder
bld(ctx
.program
, ctx
.block
);
7599 bld
.barrier(aco_opcode::p_memory_barrier_shared
); //TODO: different barriers are needed for different stages
7600 bld
.sopp(aco_opcode::s_barrier
);
7603 if (ctx
.stage
== fragment_fs
)
7604 handle_bc_optimize(&ctx
);
7606 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
7607 visit_cf_list(&ctx
, &func
->body
);
7609 if (ctx
.program
->info
->so
.num_outputs
/*&& !ctx->is_gs_copy_shader */)
7610 emit_streamout(&ctx
, 0);
7612 if (ctx
.stage
== vertex_vs
)
7613 create_vs_exports(&ctx
);
7615 if (shader_count
>= 2) {
7616 begin_divergent_if_else(&ctx
, &ic
);
7617 end_divergent_if(&ctx
, &ic
);
7620 ralloc_free(ctx
.divergent_vals
);
7623 append_logical_end(ctx
.block
);
7624 ctx
.block
->kind
|= block_kind_uniform
;
7625 Builder
bld(ctx
.program
, ctx
.block
);
7626 if (ctx
.program
->wb_smem_l1_on_end
)
7627 bld
.smem(aco_opcode::s_dcache_wb
, false);
7628 bld
.sopp(aco_opcode::s_endpgm
);
7631 for (Block
& BB
: program
->blocks
) {
7632 for (unsigned idx
: BB
.linear_preds
)
7633 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
7634 for (unsigned idx
: BB
.logical_preds
)
7635 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);