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 "ac_shader_util.h"
33 #include "aco_builder.h"
34 #include "aco_interface.h"
35 #include "aco_instruction_selection_setup.cpp"
36 #include "util/fast_idiv_by_const.h"
41 class loop_info_RAII
{
43 unsigned header_idx_old
;
45 bool divergent_cont_old
;
46 bool divergent_branch_old
;
47 bool divergent_if_old
;
50 loop_info_RAII(isel_context
* ctx
, unsigned loop_header_idx
, Block
* loop_exit
)
52 header_idx_old(ctx
->cf_info
.parent_loop
.header_idx
), exit_old(ctx
->cf_info
.parent_loop
.exit
),
53 divergent_cont_old(ctx
->cf_info
.parent_loop
.has_divergent_continue
),
54 divergent_branch_old(ctx
->cf_info
.parent_loop
.has_divergent_branch
),
55 divergent_if_old(ctx
->cf_info
.parent_if
.is_divergent
)
57 ctx
->cf_info
.parent_loop
.header_idx
= loop_header_idx
;
58 ctx
->cf_info
.parent_loop
.exit
= loop_exit
;
59 ctx
->cf_info
.parent_loop
.has_divergent_continue
= false;
60 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
61 ctx
->cf_info
.parent_if
.is_divergent
= false;
62 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
67 ctx
->cf_info
.parent_loop
.header_idx
= header_idx_old
;
68 ctx
->cf_info
.parent_loop
.exit
= exit_old
;
69 ctx
->cf_info
.parent_loop
.has_divergent_continue
= divergent_cont_old
;
70 ctx
->cf_info
.parent_loop
.has_divergent_branch
= divergent_branch_old
;
71 ctx
->cf_info
.parent_if
.is_divergent
= divergent_if_old
;
72 ctx
->cf_info
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
- 1;
73 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
)
74 ctx
->cf_info
.exec_potentially_empty_discard
= false;
82 bool exec_potentially_empty_discard_old
;
83 bool exec_potentially_empty_break_old
;
84 uint16_t exec_potentially_empty_break_depth_old
;
88 bool uniform_has_then_branch
;
89 bool then_branch_divergent
;
94 static bool visit_cf_list(struct isel_context
*ctx
,
95 struct exec_list
*list
);
97 static void add_logical_edge(unsigned pred_idx
, Block
*succ
)
99 succ
->logical_preds
.emplace_back(pred_idx
);
103 static void add_linear_edge(unsigned pred_idx
, Block
*succ
)
105 succ
->linear_preds
.emplace_back(pred_idx
);
108 static void add_edge(unsigned pred_idx
, Block
*succ
)
110 add_logical_edge(pred_idx
, succ
);
111 add_linear_edge(pred_idx
, succ
);
114 static void append_logical_start(Block
*b
)
116 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_start
);
119 static void append_logical_end(Block
*b
)
121 Builder(NULL
, b
).pseudo(aco_opcode::p_logical_end
);
124 Temp
get_ssa_temp(struct isel_context
*ctx
, nir_ssa_def
*def
)
126 assert(ctx
->allocated
[def
->index
].id());
127 return ctx
->allocated
[def
->index
];
130 Temp
emit_mbcnt(isel_context
*ctx
, Definition dst
,
131 Operand mask_lo
= Operand((uint32_t) -1), Operand mask_hi
= Operand((uint32_t) -1))
133 Builder
bld(ctx
->program
, ctx
->block
);
134 Definition lo_def
= ctx
->program
->wave_size
== 32 ? dst
: bld
.def(v1
);
135 Temp thread_id_lo
= bld
.vop3(aco_opcode::v_mbcnt_lo_u32_b32
, lo_def
, mask_lo
, Operand(0u));
137 if (ctx
->program
->wave_size
== 32) {
139 } else if (ctx
->program
->chip_class
<= GFX7
) {
140 Temp thread_id_hi
= bld
.vop2(aco_opcode::v_mbcnt_hi_u32_b32
, dst
, mask_hi
, thread_id_lo
);
143 Temp thread_id_hi
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32_e64
, dst
, mask_hi
, thread_id_lo
);
148 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
150 Builder
bld(ctx
->program
, ctx
->block
);
153 dst
= bld
.tmp(src
.regClass());
155 assert(src
.size() == dst
.size());
157 if (ctx
->stage
!= fragment_fs
) {
161 bld
.copy(Definition(dst
), src
);
165 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
166 ctx
->program
->needs_wqm
|= program_needs_wqm
;
170 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
172 if (index
.regClass() == s1
)
173 return bld
.readlane(bld
.def(s1
), data
, index
);
175 if (ctx
->options
->chip_class
<= GFX7
) {
176 /* GFX6-7: there is no bpermute instruction */
177 Operand
index_op(index
);
178 Operand
input_data(data
);
179 index_op
.setLateKill(true);
180 input_data
.setLateKill(true);
182 return bld
.pseudo(aco_opcode::p_bpermute
, bld
.def(v1
), bld
.def(bld
.lm
), bld
.def(bld
.lm
, vcc
), index_op
, input_data
);
183 } else if (ctx
->options
->chip_class
>= GFX10
&& ctx
->program
->wave_size
== 64) {
184 /* GFX10 wave64 mode: emulate full-wave bpermute */
185 if (!ctx
->has_gfx10_wave64_bpermute
) {
186 ctx
->has_gfx10_wave64_bpermute
= true;
187 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
188 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
191 Temp index_is_lo
= bld
.vopc(aco_opcode::v_cmp_ge_u32
, bld
.def(bld
.lm
), Operand(31u), index
);
192 Builder::Result index_is_lo_split
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), index_is_lo
);
193 Temp index_is_lo_n1
= bld
.sop1(aco_opcode::s_not_b32
, bld
.def(s1
), bld
.def(s1
, scc
), index_is_lo_split
.def(1).getTemp());
194 Operand same_half
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), index_is_lo_split
.def(0).getTemp(), index_is_lo_n1
);
195 Operand index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
196 Operand
input_data(data
);
198 index_x4
.setLateKill(true);
199 input_data
.setLateKill(true);
200 same_half
.setLateKill(true);
202 return bld
.pseudo(aco_opcode::p_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
), index_x4
, input_data
, same_half
);
204 /* GFX8-9 or GFX10 wave32: bpermute works normally */
205 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
206 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
210 static Temp
emit_masked_swizzle(isel_context
*ctx
, Builder
&bld
, Temp src
, unsigned mask
)
212 if (ctx
->options
->chip_class
>= GFX8
) {
213 unsigned and_mask
= mask
& 0x1f;
214 unsigned or_mask
= (mask
>> 5) & 0x1f;
215 unsigned xor_mask
= (mask
>> 10) & 0x1f;
217 uint16_t dpp_ctrl
= 0xffff;
219 // TODO: we could use DPP8 for some swizzles
220 if (and_mask
== 0x1f && or_mask
< 4 && xor_mask
< 4) {
221 unsigned res
[4] = {0, 1, 2, 3};
222 for (unsigned i
= 0; i
< 4; i
++)
223 res
[i
] = ((res
[i
] | or_mask
) ^ xor_mask
) & 0x3;
224 dpp_ctrl
= dpp_quad_perm(res
[0], res
[1], res
[2], res
[3]);
225 } else if (and_mask
== 0x1f && !or_mask
&& xor_mask
== 8) {
226 dpp_ctrl
= dpp_row_rr(8);
227 } else if (and_mask
== 0x1f && !or_mask
&& xor_mask
== 0xf) {
228 dpp_ctrl
= dpp_row_mirror
;
229 } else if (and_mask
== 0x1f && !or_mask
&& xor_mask
== 0x7) {
230 dpp_ctrl
= dpp_row_half_mirror
;
233 if (dpp_ctrl
!= 0xffff)
234 return bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
237 return bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false);
240 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
242 if (val
.type() == RegType::sgpr
) {
243 Builder
bld(ctx
->program
, ctx
->block
);
244 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
246 assert(val
.type() == RegType::vgpr
);
250 //assumes a != 0xffffffff
251 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
254 Builder
bld(ctx
->program
, ctx
->block
);
256 if (util_is_power_of_two_or_zero(b
)) {
257 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
261 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
263 assert(info
.multiplier
<= 0xffffffff);
265 bool pre_shift
= info
.pre_shift
!= 0;
266 bool increment
= info
.increment
!= 0;
267 bool multiply
= true;
268 bool post_shift
= info
.post_shift
!= 0;
270 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
271 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
275 Temp pre_shift_dst
= a
;
277 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
278 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
281 Temp increment_dst
= pre_shift_dst
;
283 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
284 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
287 Temp multiply_dst
= increment_dst
;
289 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
290 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
291 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
295 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
299 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
301 Builder
bld(ctx
->program
, ctx
->block
);
302 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
306 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
308 /* no need to extract the whole vector */
309 if (src
.regClass() == dst_rc
) {
314 assert(src
.bytes() > (idx
* dst_rc
.bytes()));
315 Builder
bld(ctx
->program
, ctx
->block
);
316 auto it
= ctx
->allocated_vec
.find(src
.id());
317 if (it
!= ctx
->allocated_vec
.end() && dst_rc
.bytes() == it
->second
[idx
].regClass().bytes()) {
318 if (it
->second
[idx
].regClass() == dst_rc
) {
319 return it
->second
[idx
];
321 assert(!dst_rc
.is_subdword());
322 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
323 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
327 if (dst_rc
.is_subdword())
328 src
= as_vgpr(ctx
, src
);
330 if (src
.bytes() == dst_rc
.bytes()) {
332 return bld
.copy(bld
.def(dst_rc
), src
);
334 Temp dst
= bld
.tmp(dst_rc
);
335 emit_extract_vector(ctx
, src
, idx
, dst
);
340 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
342 if (num_components
== 1)
344 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
347 if (num_components
> vec_src
.size()) {
348 if (vec_src
.type() == RegType::sgpr
) {
349 /* should still help get_alu_src() */
350 emit_split_vector(ctx
, vec_src
, vec_src
.size());
353 /* sub-dword split */
354 rc
= RegClass(RegType::vgpr
, vec_src
.bytes() / num_components
).as_subdword();
356 rc
= RegClass(vec_src
.type(), vec_src
.size() / num_components
);
358 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
359 split
->operands
[0] = Operand(vec_src
);
360 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
361 for (unsigned i
= 0; i
< num_components
; i
++) {
362 elems
[i
] = {ctx
->program
->allocateId(), rc
};
363 split
->definitions
[i
] = Definition(elems
[i
]);
365 ctx
->block
->instructions
.emplace_back(std::move(split
));
366 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
369 /* This vector expansion uses a mask to determine which elements in the new vector
370 * come from the original vector. The other elements are undefined. */
371 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
373 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
378 Builder
bld(ctx
->program
, ctx
->block
);
379 if (num_components
== 1) {
380 if (dst
.type() == RegType::sgpr
)
381 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
383 bld
.copy(Definition(dst
), vec_src
);
387 unsigned component_size
= dst
.size() / num_components
;
388 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
390 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
391 vec
->definitions
[0] = Definition(dst
);
393 for (unsigned i
= 0; i
< num_components
; i
++) {
394 if (mask
& (1 << i
)) {
395 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
396 if (dst
.type() == RegType::sgpr
)
397 src
= bld
.as_uniform(src
);
398 vec
->operands
[i
] = Operand(src
);
400 vec
->operands
[i
] = Operand(0u);
402 elems
[i
] = vec
->operands
[i
].getTemp();
404 ctx
->block
->instructions
.emplace_back(std::move(vec
));
405 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
408 /* adjust misaligned small bit size loads */
409 void byte_align_scalar(isel_context
*ctx
, Temp vec
, Operand offset
, Temp dst
)
411 Builder
bld(ctx
->program
, ctx
->block
);
413 Temp select
= Temp();
414 if (offset
.isConstant()) {
415 assert(offset
.constantValue() && offset
.constantValue() < 4);
416 shift
= Operand(offset
.constantValue() * 8);
418 /* bit_offset = 8 * (offset & 0x3) */
419 Temp tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(3u));
420 select
= bld
.tmp(s1
);
421 shift
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.scc(Definition(select
)), tmp
, Operand(3u));
424 if (vec
.size() == 1) {
425 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(dst
), bld
.def(s1
, scc
), vec
, shift
);
426 } else if (vec
.size() == 2) {
427 Temp tmp
= dst
.size() == 2 ? dst
: bld
.tmp(s2
);
428 bld
.sop2(aco_opcode::s_lshr_b64
, Definition(tmp
), bld
.def(s1
, scc
), vec
, shift
);
430 emit_split_vector(ctx
, dst
, 2);
432 emit_extract_vector(ctx
, tmp
, 0, dst
);
433 } else if (vec
.size() == 4) {
434 Temp lo
= bld
.tmp(s2
), hi
= bld
.tmp(s2
);
435 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), vec
);
436 hi
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(s1
), hi
, Operand(0u));
437 if (select
!= Temp())
438 hi
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), hi
, Operand(0u), bld
.scc(select
));
439 lo
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), lo
, shift
);
440 Temp mid
= bld
.tmp(s1
);
441 lo
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), Definition(mid
), lo
);
442 hi
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, shift
);
443 mid
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, mid
);
444 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, mid
);
445 emit_split_vector(ctx
, dst
, 2);
449 void byte_align_vector(isel_context
*ctx
, Temp vec
, Operand offset
, Temp dst
, unsigned component_size
)
451 Builder
bld(ctx
->program
, ctx
->block
);
452 if (offset
.isTemp()) {
453 Temp tmp
[4] = {vec
, vec
, vec
, vec
};
455 if (vec
.size() == 4) {
456 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = bld
.tmp(v1
), tmp
[3] = bld
.tmp(v1
);
457 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), Definition(tmp
[2]), Definition(tmp
[3]), vec
);
458 } else if (vec
.size() == 3) {
459 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = bld
.tmp(v1
);
460 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), Definition(tmp
[2]), vec
);
461 } else if (vec
.size() == 2) {
462 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = tmp
[1];
463 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), vec
);
465 for (unsigned i
= 0; i
< dst
.size(); i
++)
466 tmp
[i
] = bld
.vop3(aco_opcode::v_alignbyte_b32
, bld
.def(v1
), tmp
[i
+ 1], tmp
[i
], offset
);
470 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), tmp
[0], tmp
[1]);
472 offset
= Operand(0u);
475 unsigned num_components
= dst
.bytes() / component_size
;
476 if (vec
.regClass() == dst
.regClass()) {
477 assert(offset
.constantValue() == 0);
478 bld
.copy(Definition(dst
), vec
);
479 emit_split_vector(ctx
, dst
, num_components
);
483 emit_split_vector(ctx
, vec
, vec
.bytes() / component_size
);
484 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> elems
;
485 RegClass rc
= RegClass(RegType::vgpr
, component_size
).as_subdword();
487 assert(offset
.constantValue() % component_size
== 0);
488 unsigned skip
= offset
.constantValue() / component_size
;
489 for (unsigned i
= 0; i
< num_components
; i
++)
490 elems
[i
] = emit_extract_vector(ctx
, vec
, i
+ skip
, rc
);
492 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
493 if (dst
.type() == RegType::vgpr
) {
494 aco_ptr
<Pseudo_instruction
> create_vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
495 for (unsigned i
= 0; i
< num_components
; i
++)
496 create_vec
->operands
[i
] = Operand(elems
[i
]);
497 create_vec
->definitions
[0] = Definition(dst
);
498 bld
.insert(std::move(create_vec
));
500 /* if dst is sgpr - split the src, but move the original to sgpr. */
502 vec
= bld
.pseudo(aco_opcode::p_as_uniform
, bld
.def(RegClass(RegType::sgpr
, vec
.size())), vec
);
503 byte_align_scalar(ctx
, vec
, offset
, dst
);
505 assert(dst
.size() == vec
.size());
506 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
509 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
512 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
514 Builder
bld(ctx
->program
, ctx
->block
);
516 dst
= bld
.tmp(bld
.lm
);
518 assert(val
.regClass() == s1
);
519 assert(dst
.regClass() == bld
.lm
);
521 return bld
.sop2(Builder::s_cselect
, Definition(dst
), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
524 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
526 Builder
bld(ctx
->program
, ctx
->block
);
530 assert(val
.regClass() == bld
.lm
);
531 assert(dst
.regClass() == s1
);
533 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
534 Temp tmp
= bld
.tmp(s1
);
535 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, bld
.lm
));
536 return emit_wqm(ctx
, tmp
, dst
);
539 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
541 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
542 return get_ssa_temp(ctx
, src
.src
.ssa
);
544 if (src
.src
.ssa
->num_components
== size
) {
545 bool identity_swizzle
= true;
546 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
547 if (src
.swizzle
[i
] != i
)
548 identity_swizzle
= false;
550 if (identity_swizzle
)
551 return get_ssa_temp(ctx
, src
.src
.ssa
);
554 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
555 unsigned elem_size
= vec
.bytes() / src
.src
.ssa
->num_components
;
556 assert(elem_size
> 0);
557 assert(vec
.bytes() % elem_size
== 0);
559 if (elem_size
< 4 && vec
.type() == RegType::sgpr
) {
560 assert(src
.src
.ssa
->bit_size
== 8 || src
.src
.ssa
->bit_size
== 16);
562 unsigned swizzle
= src
.swizzle
[0];
563 if (vec
.size() > 1) {
564 assert(src
.src
.ssa
->bit_size
== 16);
565 vec
= emit_extract_vector(ctx
, vec
, swizzle
/ 2, s1
);
566 swizzle
= swizzle
& 1;
571 Temp dst
{ctx
->program
->allocateId(), s1
};
572 aco_ptr
<SOP2_instruction
> bfe
{create_instruction
<SOP2_instruction
>(aco_opcode::s_bfe_u32
, Format::SOP2
, 2, 2)};
573 bfe
->operands
[0] = Operand(vec
);
574 bfe
->operands
[1] = Operand(uint32_t((src
.src
.ssa
->bit_size
<< 16) | (src
.src
.ssa
->bit_size
* swizzle
)));
575 bfe
->definitions
[0] = Definition(dst
);
576 bfe
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
577 ctx
->block
->instructions
.emplace_back(std::move(bfe
));
581 RegClass elem_rc
= elem_size
< 4 ? RegClass(vec
.type(), elem_size
).as_subdword() : RegClass(vec
.type(), elem_size
/ 4);
583 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
586 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
587 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
588 for (unsigned i
= 0; i
< size
; ++i
) {
589 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
590 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
592 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
/ 4)};
593 vec_instr
->definitions
[0] = Definition(dst
);
594 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
595 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
600 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
604 Builder
bld(ctx
->program
, ctx
->block
);
605 if (ptr
.type() == RegType::vgpr
)
606 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
607 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
608 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
611 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
613 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
614 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
615 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
616 sop2
->definitions
[0] = Definition(dst
);
618 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
619 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
622 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
623 bool commutative
, bool swap_srcs
=false, bool flush_denorms
= false)
625 Builder
bld(ctx
->program
, ctx
->block
);
626 bld
.is_precise
= instr
->exact
;
628 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
629 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
630 if (src1
.type() == RegType::sgpr
) {
631 if (commutative
&& src0
.type() == RegType::vgpr
) {
636 src1
= as_vgpr(ctx
, src1
);
640 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
641 assert(dst
.size() == 1);
642 Temp tmp
= bld
.vop2(op
, bld
.def(v1
), src0
, src1
);
643 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
645 bld
.vop2(op
, Definition(dst
), src0
, src1
);
649 void emit_vop2_instruction_logic64(isel_context
*ctx
, nir_alu_instr
*instr
,
650 aco_opcode op
, Temp dst
)
652 Builder
bld(ctx
->program
, ctx
->block
);
653 bld
.is_precise
= instr
->exact
;
655 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
656 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
658 if (src1
.type() == RegType::sgpr
) {
659 assert(src0
.type() == RegType::vgpr
);
660 std::swap(src0
, src1
);
663 Temp src00
= bld
.tmp(src0
.type(), 1);
664 Temp src01
= bld
.tmp(src0
.type(), 1);
665 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
666 Temp src10
= bld
.tmp(v1
);
667 Temp src11
= bld
.tmp(v1
);
668 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
669 Temp lo
= bld
.vop2(op
, bld
.def(v1
), src00
, src10
);
670 Temp hi
= bld
.vop2(op
, bld
.def(v1
), src01
, src11
);
671 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
674 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
675 bool flush_denorms
= false)
677 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
678 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
679 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
681 /* ensure that the instruction has at most 1 sgpr operand
682 * The optimizer will inline constants for us */
683 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
684 src0
= as_vgpr(ctx
, src0
);
685 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
686 src1
= as_vgpr(ctx
, src1
);
687 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
688 src2
= as_vgpr(ctx
, src2
);
690 Builder
bld(ctx
->program
, ctx
->block
);
691 bld
.is_precise
= instr
->exact
;
692 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
693 assert(dst
.size() == 1);
694 Temp tmp
= bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
695 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
697 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
701 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
703 Builder
bld(ctx
->program
, ctx
->block
);
704 bld
.is_precise
= instr
->exact
;
705 if (dst
.type() == RegType::sgpr
)
706 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
707 bld
.vop1(op
, bld
.def(RegType::vgpr
, dst
.size()), get_alu_src(ctx
, instr
->src
[0])));
709 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
712 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
714 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
715 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
716 assert(src0
.size() == src1
.size());
718 aco_ptr
<Instruction
> vopc
;
719 if (src1
.type() == RegType::sgpr
) {
720 if (src0
.type() == RegType::vgpr
) {
721 /* to swap the operands, we might also have to change the opcode */
723 case aco_opcode::v_cmp_lt_f16
:
724 op
= aco_opcode::v_cmp_gt_f16
;
726 case aco_opcode::v_cmp_ge_f16
:
727 op
= aco_opcode::v_cmp_le_f16
;
729 case aco_opcode::v_cmp_lt_i16
:
730 op
= aco_opcode::v_cmp_gt_i16
;
732 case aco_opcode::v_cmp_ge_i16
:
733 op
= aco_opcode::v_cmp_le_i16
;
735 case aco_opcode::v_cmp_lt_u16
:
736 op
= aco_opcode::v_cmp_gt_u16
;
738 case aco_opcode::v_cmp_ge_u16
:
739 op
= aco_opcode::v_cmp_le_u16
;
741 case aco_opcode::v_cmp_lt_f32
:
742 op
= aco_opcode::v_cmp_gt_f32
;
744 case aco_opcode::v_cmp_ge_f32
:
745 op
= aco_opcode::v_cmp_le_f32
;
747 case aco_opcode::v_cmp_lt_i32
:
748 op
= aco_opcode::v_cmp_gt_i32
;
750 case aco_opcode::v_cmp_ge_i32
:
751 op
= aco_opcode::v_cmp_le_i32
;
753 case aco_opcode::v_cmp_lt_u32
:
754 op
= aco_opcode::v_cmp_gt_u32
;
756 case aco_opcode::v_cmp_ge_u32
:
757 op
= aco_opcode::v_cmp_le_u32
;
759 case aco_opcode::v_cmp_lt_f64
:
760 op
= aco_opcode::v_cmp_gt_f64
;
762 case aco_opcode::v_cmp_ge_f64
:
763 op
= aco_opcode::v_cmp_le_f64
;
765 case aco_opcode::v_cmp_lt_i64
:
766 op
= aco_opcode::v_cmp_gt_i64
;
768 case aco_opcode::v_cmp_ge_i64
:
769 op
= aco_opcode::v_cmp_le_i64
;
771 case aco_opcode::v_cmp_lt_u64
:
772 op
= aco_opcode::v_cmp_gt_u64
;
774 case aco_opcode::v_cmp_ge_u64
:
775 op
= aco_opcode::v_cmp_le_u64
;
777 default: /* eq and ne are commutative */
784 src1
= as_vgpr(ctx
, src1
);
788 Builder
bld(ctx
->program
, ctx
->block
);
789 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
792 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
794 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
795 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
796 Builder
bld(ctx
->program
, ctx
->block
);
798 assert(dst
.regClass() == bld
.lm
);
799 assert(src0
.type() == RegType::sgpr
);
800 assert(src1
.type() == RegType::sgpr
);
801 assert(src0
.regClass() == src1
.regClass());
803 /* Emit the SALU comparison instruction */
804 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
805 /* Turn the result into a per-lane bool */
806 bool_to_vector_condition(ctx
, cmp
, dst
);
809 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
810 aco_opcode v16_op
, aco_opcode v32_op
, aco_opcode v64_op
, aco_opcode s32_op
= aco_opcode::num_opcodes
, aco_opcode s64_op
= aco_opcode::num_opcodes
)
812 aco_opcode s_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? s64_op
: instr
->src
[0].src
.ssa
->bit_size
== 32 ? s32_op
: aco_opcode::num_opcodes
;
813 aco_opcode v_op
= instr
->src
[0].src
.ssa
->bit_size
== 64 ? v64_op
: instr
->src
[0].src
.ssa
->bit_size
== 32 ? v32_op
: v16_op
;
814 bool use_valu
= s_op
== aco_opcode::num_opcodes
||
815 nir_dest_is_divergent(instr
->dest
.dest
) ||
816 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
817 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
818 aco_opcode op
= use_valu
? v_op
: s_op
;
819 assert(op
!= aco_opcode::num_opcodes
);
820 assert(dst
.regClass() == ctx
->program
->lane_mask
);
823 emit_vopc_instruction(ctx
, instr
, op
, dst
);
825 emit_sopc_instruction(ctx
, instr
, op
, dst
);
828 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, Builder::WaveSpecificOpcode op
, Temp dst
)
830 Builder
bld(ctx
->program
, ctx
->block
);
831 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
832 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
834 assert(dst
.regClass() == bld
.lm
);
835 assert(src0
.regClass() == bld
.lm
);
836 assert(src1
.regClass() == bld
.lm
);
838 bld
.sop2(op
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
841 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
843 Builder
bld(ctx
->program
, ctx
->block
);
844 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
845 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
846 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
848 assert(cond
.regClass() == bld
.lm
);
850 if (dst
.type() == RegType::vgpr
) {
851 aco_ptr
<Instruction
> bcsel
;
852 if (dst
.size() == 1) {
853 then
= as_vgpr(ctx
, then
);
854 els
= as_vgpr(ctx
, els
);
856 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
857 } else if (dst
.size() == 2) {
858 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
859 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
860 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
861 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
863 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
864 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
866 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
868 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
869 nir_print_instr(&instr
->instr
, stderr
);
870 fprintf(stderr
, "\n");
875 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
876 assert(dst
.regClass() == bld
.lm
);
877 assert(then
.regClass() == bld
.lm
);
878 assert(els
.regClass() == bld
.lm
);
881 if (!nir_src_is_divergent(instr
->src
[0].src
)) { /* uniform condition and values in sgpr */
882 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
883 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
884 assert(dst
.size() == then
.size());
885 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
886 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
888 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
889 nir_print_instr(&instr
->instr
, stderr
);
890 fprintf(stderr
, "\n");
895 /* divergent boolean bcsel
896 * this implements bcsel on bools: dst = s0 ? s1 : s2
897 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
898 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
900 if (cond
.id() != then
.id())
901 then
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
, then
);
903 if (cond
.id() == els
.id())
904 bld
.sop1(Builder::s_mov
, Definition(dst
), then
);
906 bld
.sop2(Builder::s_or
, Definition(dst
), bld
.def(s1
, scc
), then
,
907 bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), els
, cond
));
910 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
911 aco_opcode op
, uint32_t undo
)
913 /* multiply by 16777216 to handle denormals */
914 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
915 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
916 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
917 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
918 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
920 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
922 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
925 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
927 if (ctx
->block
->fp_mode
.denorm32
== 0) {
928 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
932 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
935 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
937 if (ctx
->block
->fp_mode
.denorm32
== 0) {
938 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
942 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
945 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
947 if (ctx
->block
->fp_mode
.denorm32
== 0) {
948 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
952 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
955 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
957 if (ctx
->block
->fp_mode
.denorm32
== 0) {
958 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
962 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
965 Temp
emit_trunc_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
967 if (ctx
->options
->chip_class
>= GFX7
)
968 return bld
.vop1(aco_opcode::v_trunc_f64
, Definition(dst
), val
);
970 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
971 /* TODO: create more efficient code! */
972 if (val
.type() == RegType::sgpr
)
973 val
= as_vgpr(ctx
, val
);
975 /* Split the input value. */
976 Temp val_lo
= bld
.tmp(v1
), val_hi
= bld
.tmp(v1
);
977 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
979 /* Extract the exponent and compute the unbiased value. */
980 Temp exponent
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), val_hi
, Operand(20u), Operand(11u));
981 exponent
= bld
.vsub32(bld
.def(v1
), exponent
, Operand(1023u));
983 /* Extract the fractional part. */
984 Temp fract_mask
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x000fffffu
));
985 fract_mask
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), fract_mask
, exponent
);
987 Temp fract_mask_lo
= bld
.tmp(v1
), fract_mask_hi
= bld
.tmp(v1
);
988 bld
.pseudo(aco_opcode::p_split_vector
, Definition(fract_mask_lo
), Definition(fract_mask_hi
), fract_mask
);
990 Temp fract_lo
= bld
.tmp(v1
), fract_hi
= bld
.tmp(v1
);
991 Temp tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_lo
);
992 fract_lo
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_lo
, tmp
);
993 tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_hi
);
994 fract_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_hi
, tmp
);
996 /* Get the sign bit. */
997 Temp sign
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x80000000u
), val_hi
);
999 /* Decide the operation to apply depending on the unbiased exponent. */
1000 Temp exp_lt0
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), exponent
, Operand(0u));
1001 Temp dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_lo
, bld
.copy(bld
.def(v1
), Operand(0u)), exp_lt0
);
1002 Temp dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_hi
, sign
, exp_lt0
);
1003 Temp exp_gt51
= bld
.vopc_e64(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), exponent
, Operand(51u));
1004 dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_lo
, val_lo
, exp_gt51
);
1005 dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_hi
, val_hi
, exp_gt51
);
1007 return bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst_lo
, dst_hi
);
1010 Temp
emit_floor_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
1012 if (ctx
->options
->chip_class
>= GFX7
)
1013 return bld
.vop1(aco_opcode::v_floor_f64
, Definition(dst
), val
);
1015 /* GFX6 doesn't support V_FLOOR_F64, lower it (note that it's actually
1016 * lowered at NIR level for precision reasons). */
1017 Temp src0
= as_vgpr(ctx
, val
);
1019 Temp mask
= bld
.copy(bld
.def(s1
), Operand(3u)); /* isnan */
1020 Temp min_val
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(-1u), Operand(0x3fefffffu
));
1022 Temp isnan
= bld
.vopc_e64(aco_opcode::v_cmp_class_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, mask
);
1023 Temp fract
= bld
.vop1(aco_opcode::v_fract_f64
, bld
.def(v2
), src0
);
1024 Temp min
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
), fract
, min_val
);
1026 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
1027 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), src0
);
1028 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
1029 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), min
);
1031 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, isnan
);
1032 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, isnan
);
1034 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), dst0
, dst1
);
1036 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src0
, v
);
1037 static_cast<VOP3A_instruction
*>(add
)->neg
[1] = true;
1039 return add
->definitions
[0].getTemp();
1042 Temp
convert_int(isel_context
*ctx
, Builder
& bld
, Temp src
, unsigned src_bits
, unsigned dst_bits
, bool is_signed
, Temp dst
=Temp()) {
1044 if (dst_bits
% 32 == 0 || src
.type() == RegType::sgpr
)
1045 dst
= bld
.tmp(src
.type(), DIV_ROUND_UP(dst_bits
, 32u));
1047 dst
= bld
.tmp(RegClass(RegType::vgpr
, dst_bits
/ 8u).as_subdword());
1050 if (dst
.bytes() == src
.bytes() && dst_bits
< src_bits
)
1051 return bld
.copy(Definition(dst
), src
);
1052 else if (dst
.bytes() < src
.bytes())
1053 return bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(0u));
1057 tmp
= src_bits
== 32 ? src
: bld
.tmp(src
.type(), 1);
1060 } else if (src
.regClass() == s1
) {
1062 bld
.sop1(src_bits
== 8 ? aco_opcode::s_sext_i32_i8
: aco_opcode::s_sext_i32_i16
, Definition(tmp
), src
);
1064 bld
.sop2(aco_opcode::s_and_b32
, Definition(tmp
), bld
.def(s1
, scc
), Operand(src_bits
== 8 ? 0xFFu
: 0xFFFFu
), src
);
1065 } else if (ctx
->options
->chip_class
>= GFX8
) {
1066 assert(src_bits
!= 8 || src
.regClass() == v1b
);
1067 assert(src_bits
!= 16 || src
.regClass() == v2b
);
1068 aco_ptr
<SDWA_instruction
> sdwa
{create_instruction
<SDWA_instruction
>(aco_opcode::v_mov_b32
, asSDWA(Format::VOP1
), 1, 1)};
1069 sdwa
->operands
[0] = Operand(src
);
1070 sdwa
->definitions
[0] = Definition(tmp
);
1072 sdwa
->sel
[0] = src_bits
== 8 ? sdwa_sbyte
: sdwa_sword
;
1074 sdwa
->sel
[0] = src_bits
== 8 ? sdwa_ubyte
: sdwa_uword
;
1075 sdwa
->dst_sel
= tmp
.bytes() == 2 ? sdwa_uword
: sdwa_udword
;
1076 bld
.insert(std::move(sdwa
));
1078 assert(ctx
->options
->chip_class
== GFX6
|| ctx
->options
->chip_class
== GFX7
);
1079 aco_opcode opcode
= is_signed
? aco_opcode::v_bfe_i32
: aco_opcode::v_bfe_u32
;
1080 bld
.vop3(opcode
, Definition(tmp
), src
, Operand(0u), Operand(src_bits
== 8 ? 8u : 16u));
1083 if (dst_bits
== 64) {
1084 if (is_signed
&& dst
.regClass() == s2
) {
1085 Temp high
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(31u));
1086 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, high
);
1087 } else if (is_signed
&& dst
.regClass() == v2
) {
1088 Temp high
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), tmp
);
1089 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, high
);
1091 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, Operand(0u));
1098 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
1100 if (!instr
->dest
.dest
.is_ssa
) {
1101 fprintf(stderr
, "nir alu dst not in ssa: ");
1102 nir_print_instr(&instr
->instr
, stderr
);
1103 fprintf(stderr
, "\n");
1106 Builder
bld(ctx
->program
, ctx
->block
);
1107 bld
.is_precise
= instr
->exact
;
1108 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
1113 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
1114 unsigned num
= instr
->dest
.dest
.ssa
.num_components
;
1115 for (unsigned i
= 0; i
< num
; ++i
)
1116 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
1118 if (instr
->dest
.dest
.ssa
.bit_size
>= 32 || dst
.type() == RegType::vgpr
) {
1119 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
1120 RegClass elem_rc
= RegClass::get(RegType::vgpr
, instr
->dest
.dest
.ssa
.bit_size
/ 8u);
1121 for (unsigned i
= 0; i
< num
; ++i
) {
1122 if (elems
[i
].type() == RegType::sgpr
&& elem_rc
.is_subdword())
1123 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, elems
[i
], 0, elem_rc
));
1125 vec
->operands
[i
] = Operand
{elems
[i
]};
1127 vec
->definitions
[0] = Definition(dst
);
1128 ctx
->block
->instructions
.emplace_back(std::move(vec
));
1129 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
1131 // TODO: that is a bit suboptimal..
1132 Temp mask
= bld
.copy(bld
.def(s1
), Operand((1u << instr
->dest
.dest
.ssa
.bit_size
) - 1));
1133 for (unsigned i
= 0; i
< num
- 1; ++i
)
1134 if (((i
+1) * instr
->dest
.dest
.ssa
.bit_size
) % 32)
1135 elems
[i
] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), elems
[i
], mask
);
1136 for (unsigned i
= 0; i
< num
; ++i
) {
1137 unsigned bit
= i
* instr
->dest
.dest
.ssa
.bit_size
;
1138 if (bit
% 32 == 0) {
1139 elems
[bit
/ 32] = elems
[i
];
1141 elems
[i
] = bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
),
1142 elems
[i
], Operand((i
* instr
->dest
.dest
.ssa
.bit_size
) % 32));
1143 elems
[bit
/ 32] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), elems
[bit
/ 32], elems
[i
]);
1146 if (dst
.size() == 1)
1147 bld
.copy(Definition(dst
), elems
[0]);
1149 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), elems
[0], elems
[1]);
1154 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1155 aco_ptr
<Instruction
> mov
;
1156 if (dst
.type() == RegType::sgpr
) {
1157 if (src
.type() == RegType::vgpr
)
1158 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
1159 else if (src
.regClass() == s1
)
1160 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
1161 else if (src
.regClass() == s2
)
1162 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
1164 unreachable("wrong src register class for nir_op_imov");
1166 if (dst
.regClass() == v1
)
1167 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
1168 else if (dst
.regClass() == v1b
||
1169 dst
.regClass() == v2b
||
1170 dst
.regClass() == v2
)
1171 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
1173 unreachable("wrong src register class for nir_op_imov");
1178 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1179 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1180 assert(src
.regClass() == bld
.lm
);
1181 assert(dst
.regClass() == bld
.lm
);
1182 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1183 Temp tmp
= bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
);
1184 bld
.sop2(Builder::s_and
, Definition(dst
), bld
.def(s1
, scc
), tmp
, Operand(exec
, bld
.lm
));
1185 } else if (dst
.regClass() == v1
) {
1186 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
1187 } else if (dst
.regClass() == v2
) {
1188 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
1189 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
1190 lo
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), lo
);
1191 hi
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), hi
);
1192 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
1193 } else if (dst
.type() == RegType::sgpr
) {
1194 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
1195 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
1197 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1198 nir_print_instr(&instr
->instr
, stderr
);
1199 fprintf(stderr
, "\n");
1204 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1205 if (dst
.regClass() == v1
) {
1206 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
1207 } else if (dst
.regClass() == s1
) {
1208 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
1209 } else if (dst
.size() == 2) {
1210 Temp src0
= bld
.tmp(dst
.type(), 1);
1211 Temp src1
= bld
.tmp(dst
.type(), 1);
1212 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
1214 if (dst
.regClass() == s2
) {
1215 Temp carry
= bld
.tmp(s1
);
1216 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
1217 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
1218 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1220 Temp lower
= bld
.tmp(v1
);
1221 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
1222 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
1223 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1226 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1227 nir_print_instr(&instr
->instr
, stderr
);
1228 fprintf(stderr
, "\n");
1233 if (dst
.regClass() == s1
) {
1234 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
1235 } else if (dst
.regClass() == v1
) {
1236 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1237 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
1239 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1240 nir_print_instr(&instr
->instr
, stderr
);
1241 fprintf(stderr
, "\n");
1245 case nir_op_isign
: {
1246 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1247 if (dst
.regClass() == s1
) {
1248 Temp tmp
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand((uint32_t)-1));
1249 bld
.sop2(aco_opcode::s_min_i32
, Definition(dst
), bld
.def(s1
, scc
), tmp
, Operand(1u));
1250 } else if (dst
.regClass() == s2
) {
1251 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
1253 if (ctx
->program
->chip_class
>= GFX8
)
1254 neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
1256 neqz
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(0u)).def(1).getTemp();
1257 /* SCC gets zero-extended to 64 bit */
1258 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, bld
.scc(neqz
));
1259 } else if (dst
.regClass() == v1
) {
1260 bld
.vop3(aco_opcode::v_med3_i32
, Definition(dst
), Operand((uint32_t)-1), src
, Operand(1u));
1261 } else if (dst
.regClass() == v2
) {
1262 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
1263 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
1264 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1265 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
1266 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
1267 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1269 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1270 nir_print_instr(&instr
->instr
, stderr
);
1271 fprintf(stderr
, "\n");
1276 if (dst
.regClass() == v1
) {
1277 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
1278 } else if (dst
.regClass() == s1
) {
1279 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
1281 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1282 nir_print_instr(&instr
->instr
, stderr
);
1283 fprintf(stderr
, "\n");
1288 if (dst
.regClass() == v1
) {
1289 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
1290 } else if (dst
.regClass() == s1
) {
1291 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
1293 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1294 nir_print_instr(&instr
->instr
, stderr
);
1295 fprintf(stderr
, "\n");
1300 if (dst
.regClass() == v1
) {
1301 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
1302 } else if (dst
.regClass() == s1
) {
1303 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
1305 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1306 nir_print_instr(&instr
->instr
, stderr
);
1307 fprintf(stderr
, "\n");
1312 if (dst
.regClass() == v1
) {
1313 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
1314 } else if (dst
.regClass() == s1
) {
1315 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
1317 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1318 nir_print_instr(&instr
->instr
, stderr
);
1319 fprintf(stderr
, "\n");
1324 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1325 emit_boolean_logic(ctx
, instr
, Builder::s_or
, dst
);
1326 } else if (dst
.regClass() == v1
) {
1327 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
1328 } else if (dst
.regClass() == v2
) {
1329 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_or_b32
, dst
);
1330 } else if (dst
.regClass() == s1
) {
1331 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
1332 } else if (dst
.regClass() == s2
) {
1333 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
1335 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1336 nir_print_instr(&instr
->instr
, stderr
);
1337 fprintf(stderr
, "\n");
1342 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1343 emit_boolean_logic(ctx
, instr
, Builder::s_and
, dst
);
1344 } else if (dst
.regClass() == v1
) {
1345 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
1346 } else if (dst
.regClass() == v2
) {
1347 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_and_b32
, dst
);
1348 } else if (dst
.regClass() == s1
) {
1349 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
1350 } else if (dst
.regClass() == s2
) {
1351 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
1353 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1354 nir_print_instr(&instr
->instr
, stderr
);
1355 fprintf(stderr
, "\n");
1360 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1361 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
1362 } else if (dst
.regClass() == v1
) {
1363 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
1364 } else if (dst
.regClass() == v2
) {
1365 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_xor_b32
, dst
);
1366 } else if (dst
.regClass() == s1
) {
1367 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
1368 } else if (dst
.regClass() == s2
) {
1369 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
1371 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1372 nir_print_instr(&instr
->instr
, stderr
);
1373 fprintf(stderr
, "\n");
1378 if (dst
.regClass() == v1
) {
1379 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
1380 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1381 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
1382 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1383 } else if (dst
.regClass() == v2
) {
1384 bld
.vop3(aco_opcode::v_lshr_b64
, Definition(dst
),
1385 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1386 } else if (dst
.regClass() == s2
) {
1387 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
1388 } else if (dst
.regClass() == s1
) {
1389 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
1391 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1392 nir_print_instr(&instr
->instr
, stderr
);
1393 fprintf(stderr
, "\n");
1398 if (dst
.regClass() == v1
) {
1399 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
1400 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1401 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
1402 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1403 } else if (dst
.regClass() == v2
) {
1404 bld
.vop3(aco_opcode::v_lshl_b64
, Definition(dst
),
1405 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1406 } else if (dst
.regClass() == s1
) {
1407 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
1408 } else if (dst
.regClass() == s2
) {
1409 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
1411 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1412 nir_print_instr(&instr
->instr
, stderr
);
1413 fprintf(stderr
, "\n");
1418 if (dst
.regClass() == v1
) {
1419 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
1420 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1421 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
1422 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1423 } else if (dst
.regClass() == v2
) {
1424 bld
.vop3(aco_opcode::v_ashr_i64
, Definition(dst
),
1425 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1426 } else if (dst
.regClass() == s1
) {
1427 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
1428 } else if (dst
.regClass() == s2
) {
1429 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
1431 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1432 nir_print_instr(&instr
->instr
, stderr
);
1433 fprintf(stderr
, "\n");
1437 case nir_op_find_lsb
: {
1438 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1439 if (src
.regClass() == s1
) {
1440 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
1441 } else if (src
.regClass() == v1
) {
1442 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
1443 } else if (src
.regClass() == s2
) {
1444 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
1446 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1447 nir_print_instr(&instr
->instr
, stderr
);
1448 fprintf(stderr
, "\n");
1452 case nir_op_ufind_msb
:
1453 case nir_op_ifind_msb
: {
1454 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1455 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1456 aco_opcode op
= src
.regClass() == s2
?
1457 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1458 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1459 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1461 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1462 Operand(src
.size() * 32u - 1u), msb_rev
);
1463 Temp msb
= sub
.def(0).getTemp();
1464 Temp carry
= sub
.def(1).getTemp();
1466 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, bld
.scc(carry
));
1467 } else if (src
.regClass() == v1
) {
1468 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1469 Temp msb_rev
= bld
.tmp(v1
);
1470 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1471 Temp msb
= bld
.tmp(v1
);
1472 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1473 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1475 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1476 nir_print_instr(&instr
->instr
, stderr
);
1477 fprintf(stderr
, "\n");
1481 case nir_op_bitfield_reverse
: {
1482 if (dst
.regClass() == s1
) {
1483 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1484 } else if (dst
.regClass() == v1
) {
1485 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1487 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1488 nir_print_instr(&instr
->instr
, stderr
);
1489 fprintf(stderr
, "\n");
1494 if (dst
.regClass() == s1
) {
1495 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1499 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1500 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1501 if (dst
.regClass() == v1
) {
1502 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1506 assert(src0
.size() == 2 && src1
.size() == 2);
1507 Temp src00
= bld
.tmp(src0
.type(), 1);
1508 Temp src01
= bld
.tmp(dst
.type(), 1);
1509 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1510 Temp src10
= bld
.tmp(src1
.type(), 1);
1511 Temp src11
= bld
.tmp(dst
.type(), 1);
1512 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1514 if (dst
.regClass() == s2
) {
1515 Temp carry
= bld
.tmp(s1
);
1516 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1517 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1518 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1519 } else if (dst
.regClass() == v2
) {
1520 Temp dst0
= bld
.tmp(v1
);
1521 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1522 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1523 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1525 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1526 nir_print_instr(&instr
->instr
, stderr
);
1527 fprintf(stderr
, "\n");
1531 case nir_op_uadd_sat
: {
1532 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1533 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1534 if (dst
.regClass() == s1
) {
1535 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1536 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1538 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1539 } else if (dst
.regClass() == v1
) {
1540 if (ctx
->options
->chip_class
>= GFX9
) {
1541 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1542 add
->operands
[0] = Operand(src0
);
1543 add
->operands
[1] = Operand(src1
);
1544 add
->definitions
[0] = Definition(dst
);
1546 ctx
->block
->instructions
.emplace_back(std::move(add
));
1548 if (src1
.regClass() != v1
)
1549 std::swap(src0
, src1
);
1550 assert(src1
.regClass() == v1
);
1551 Temp tmp
= bld
.tmp(v1
);
1552 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1553 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1556 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1557 nir_print_instr(&instr
->instr
, stderr
);
1558 fprintf(stderr
, "\n");
1562 case nir_op_uadd_carry
: {
1563 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1564 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1565 if (dst
.regClass() == s1
) {
1566 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1569 if (dst
.regClass() == v1
) {
1570 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1571 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1575 Temp src00
= bld
.tmp(src0
.type(), 1);
1576 Temp src01
= bld
.tmp(dst
.type(), 1);
1577 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1578 Temp src10
= bld
.tmp(src1
.type(), 1);
1579 Temp src11
= bld
.tmp(dst
.type(), 1);
1580 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1581 if (dst
.regClass() == s2
) {
1582 Temp carry
= bld
.tmp(s1
);
1583 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1584 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1585 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1586 } else if (dst
.regClass() == v2
) {
1587 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1588 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1589 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1590 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1592 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1593 nir_print_instr(&instr
->instr
, stderr
);
1594 fprintf(stderr
, "\n");
1599 if (dst
.regClass() == s1
) {
1600 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1604 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1605 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1606 if (dst
.regClass() == v1
) {
1607 bld
.vsub32(Definition(dst
), src0
, src1
);
1611 Temp src00
= bld
.tmp(src0
.type(), 1);
1612 Temp src01
= bld
.tmp(dst
.type(), 1);
1613 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1614 Temp src10
= bld
.tmp(src1
.type(), 1);
1615 Temp src11
= bld
.tmp(dst
.type(), 1);
1616 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1617 if (dst
.regClass() == s2
) {
1618 Temp carry
= bld
.tmp(s1
);
1619 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1620 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1621 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1622 } else if (dst
.regClass() == v2
) {
1623 Temp lower
= bld
.tmp(v1
);
1624 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1625 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1626 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1628 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1629 nir_print_instr(&instr
->instr
, stderr
);
1630 fprintf(stderr
, "\n");
1634 case nir_op_usub_borrow
: {
1635 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1636 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1637 if (dst
.regClass() == s1
) {
1638 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1640 } else if (dst
.regClass() == v1
) {
1641 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1642 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1646 Temp src00
= bld
.tmp(src0
.type(), 1);
1647 Temp src01
= bld
.tmp(dst
.type(), 1);
1648 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1649 Temp src10
= bld
.tmp(src1
.type(), 1);
1650 Temp src11
= bld
.tmp(dst
.type(), 1);
1651 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1652 if (dst
.regClass() == s2
) {
1653 Temp borrow
= bld
.tmp(s1
);
1654 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1655 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1656 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1657 } else if (dst
.regClass() == v2
) {
1658 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1659 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1660 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1661 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1663 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1664 nir_print_instr(&instr
->instr
, stderr
);
1665 fprintf(stderr
, "\n");
1670 if (dst
.regClass() == v1
) {
1671 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1672 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1673 } else if (dst
.regClass() == s1
) {
1674 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1676 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1677 nir_print_instr(&instr
->instr
, stderr
);
1678 fprintf(stderr
, "\n");
1682 case nir_op_umul_high
: {
1683 if (dst
.regClass() == v1
) {
1684 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1685 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1686 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1687 } else if (dst
.regClass() == s1
) {
1688 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1689 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1690 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1692 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1693 nir_print_instr(&instr
->instr
, stderr
);
1694 fprintf(stderr
, "\n");
1698 case nir_op_imul_high
: {
1699 if (dst
.regClass() == v1
) {
1700 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1701 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1702 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1703 } else if (dst
.regClass() == s1
) {
1704 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1705 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1706 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1708 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1709 nir_print_instr(&instr
->instr
, stderr
);
1710 fprintf(stderr
, "\n");
1715 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1716 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1717 if (dst
.regClass() == v2b
) {
1718 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f16
, dst
, true);
1719 } else if (dst
.regClass() == v1
) {
1720 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1721 } else if (dst
.regClass() == v2
) {
1722 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), src0
, src1
);
1724 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1725 nir_print_instr(&instr
->instr
, stderr
);
1726 fprintf(stderr
, "\n");
1731 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1732 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1733 if (dst
.regClass() == v2b
) {
1734 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f16
, dst
, true);
1735 } else if (dst
.regClass() == v1
) {
1736 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1737 } else if (dst
.regClass() == v2
) {
1738 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src0
, src1
);
1740 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1741 nir_print_instr(&instr
->instr
, stderr
);
1742 fprintf(stderr
, "\n");
1747 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1748 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1749 if (dst
.regClass() == v2b
) {
1750 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1751 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f16
, dst
, false);
1753 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f16
, dst
, true);
1754 } else if (dst
.regClass() == v1
) {
1755 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1756 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1758 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1759 } else if (dst
.regClass() == v2
) {
1760 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1761 as_vgpr(ctx
, src0
), as_vgpr(ctx
, src1
));
1762 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1765 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1766 nir_print_instr(&instr
->instr
, stderr
);
1767 fprintf(stderr
, "\n");
1772 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1773 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1774 if (dst
.regClass() == v2b
) {
1775 // TODO: check fp_mode.must_flush_denorms16_64
1776 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f16
, dst
, true);
1777 } else if (dst
.regClass() == v1
) {
1778 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1779 } else if (dst
.regClass() == v2
) {
1780 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1781 Temp tmp
= bld
.vop3(aco_opcode::v_max_f64
, bld
.def(v2
), src0
, src1
);
1782 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1784 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
), src0
, src1
);
1787 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1788 nir_print_instr(&instr
->instr
, stderr
);
1789 fprintf(stderr
, "\n");
1794 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1795 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1796 if (dst
.regClass() == v2b
) {
1797 // TODO: check fp_mode.must_flush_denorms16_64
1798 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f16
, dst
, true);
1799 } else if (dst
.regClass() == v1
) {
1800 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1801 } else if (dst
.regClass() == v2
) {
1802 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1803 Temp tmp
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
), src0
, src1
);
1804 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1806 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
), src0
, src1
);
1809 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1810 nir_print_instr(&instr
->instr
, stderr
);
1811 fprintf(stderr
, "\n");
1815 case nir_op_fmax3
: {
1816 if (dst
.regClass() == v2b
) {
1817 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f16
, dst
, false);
1818 } else if (dst
.regClass() == v1
) {
1819 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1821 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1822 nir_print_instr(&instr
->instr
, stderr
);
1823 fprintf(stderr
, "\n");
1827 case nir_op_fmin3
: {
1828 if (dst
.regClass() == v2b
) {
1829 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f16
, dst
, false);
1830 } else if (dst
.regClass() == v1
) {
1831 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1833 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1834 nir_print_instr(&instr
->instr
, stderr
);
1835 fprintf(stderr
, "\n");
1839 case nir_op_fmed3
: {
1840 if (dst
.regClass() == v2b
) {
1841 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f16
, dst
, false);
1842 } else if (dst
.regClass() == v1
) {
1843 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1845 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1846 nir_print_instr(&instr
->instr
, stderr
);
1847 fprintf(stderr
, "\n");
1851 case nir_op_umax3
: {
1852 if (dst
.size() == 1) {
1853 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1855 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1856 nir_print_instr(&instr
->instr
, stderr
);
1857 fprintf(stderr
, "\n");
1861 case nir_op_umin3
: {
1862 if (dst
.size() == 1) {
1863 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1865 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1866 nir_print_instr(&instr
->instr
, stderr
);
1867 fprintf(stderr
, "\n");
1871 case nir_op_umed3
: {
1872 if (dst
.size() == 1) {
1873 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1875 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1876 nir_print_instr(&instr
->instr
, stderr
);
1877 fprintf(stderr
, "\n");
1881 case nir_op_imax3
: {
1882 if (dst
.size() == 1) {
1883 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1885 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1886 nir_print_instr(&instr
->instr
, stderr
);
1887 fprintf(stderr
, "\n");
1891 case nir_op_imin3
: {
1892 if (dst
.size() == 1) {
1893 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1895 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1896 nir_print_instr(&instr
->instr
, stderr
);
1897 fprintf(stderr
, "\n");
1901 case nir_op_imed3
: {
1902 if (dst
.size() == 1) {
1903 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1905 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1906 nir_print_instr(&instr
->instr
, stderr
);
1907 fprintf(stderr
, "\n");
1911 case nir_op_cube_face_coord
: {
1912 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1913 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1914 emit_extract_vector(ctx
, in
, 1, v1
),
1915 emit_extract_vector(ctx
, in
, 2, v1
) };
1916 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1917 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1918 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1919 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1920 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1921 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1922 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1925 case nir_op_cube_face_index
: {
1926 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1927 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1928 emit_extract_vector(ctx
, in
, 1, v1
),
1929 emit_extract_vector(ctx
, in
, 2, v1
) };
1930 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1933 case nir_op_bcsel
: {
1934 emit_bcsel(ctx
, instr
, dst
);
1938 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1939 if (dst
.regClass() == v2b
) {
1940 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f16
, dst
);
1941 } else if (dst
.regClass() == v1
) {
1942 emit_rsq(ctx
, bld
, Definition(dst
), src
);
1943 } else if (dst
.regClass() == v2
) {
1944 /* Lowered at NIR level for precision reasons. */
1945 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1947 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1948 nir_print_instr(&instr
->instr
, stderr
);
1949 fprintf(stderr
, "\n");
1954 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1955 if (dst
.regClass() == v2b
) {
1956 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1957 src
= bld
.vop2(aco_opcode::v_mul_f16
, bld
.def(v2b
), Operand((uint16_t)0x3C00), as_vgpr(ctx
, src
));
1958 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x8000u
), as_vgpr(ctx
, src
));
1959 } else if (dst
.regClass() == v1
) {
1960 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1961 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1962 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1963 } else if (dst
.regClass() == v2
) {
1964 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1965 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1966 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1967 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1968 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1969 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1971 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1972 nir_print_instr(&instr
->instr
, stderr
);
1973 fprintf(stderr
, "\n");
1978 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1979 if (dst
.regClass() == v2b
) {
1980 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1981 src
= bld
.vop2(aco_opcode::v_mul_f16
, bld
.def(v2b
), Operand((uint16_t)0x3C00), as_vgpr(ctx
, src
));
1982 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFu
), as_vgpr(ctx
, src
));
1983 } else if (dst
.regClass() == v1
) {
1984 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1985 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1986 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1987 } else if (dst
.regClass() == v2
) {
1988 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1989 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1990 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1991 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1992 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1993 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1995 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1996 nir_print_instr(&instr
->instr
, stderr
);
1997 fprintf(stderr
, "\n");
2002 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2003 if (dst
.regClass() == v2b
) {
2004 bld
.vop3(aco_opcode::v_med3_f16
, Definition(dst
), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src
);
2005 } else if (dst
.regClass() == v1
) {
2006 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
2007 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
2008 // TODO: confirm that this holds under any circumstances
2009 } else if (dst
.regClass() == v2
) {
2010 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
2011 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
2014 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2015 nir_print_instr(&instr
->instr
, stderr
);
2016 fprintf(stderr
, "\n");
2020 case nir_op_flog2
: {
2021 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2022 if (dst
.regClass() == v2b
) {
2023 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f16
, dst
);
2024 } else if (dst
.regClass() == v1
) {
2025 emit_log2(ctx
, bld
, Definition(dst
), src
);
2027 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2028 nir_print_instr(&instr
->instr
, stderr
);
2029 fprintf(stderr
, "\n");
2034 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2035 if (dst
.regClass() == v2b
) {
2036 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f16
, dst
);
2037 } else if (dst
.regClass() == v1
) {
2038 emit_rcp(ctx
, bld
, Definition(dst
), src
);
2039 } else if (dst
.regClass() == v2
) {
2040 /* Lowered at NIR level for precision reasons. */
2041 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
2043 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2044 nir_print_instr(&instr
->instr
, stderr
);
2045 fprintf(stderr
, "\n");
2049 case nir_op_fexp2
: {
2050 if (dst
.regClass() == v2b
) {
2051 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f16
, dst
);
2052 } else if (dst
.regClass() == v1
) {
2053 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
2055 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2056 nir_print_instr(&instr
->instr
, stderr
);
2057 fprintf(stderr
, "\n");
2061 case nir_op_fsqrt
: {
2062 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2063 if (dst
.regClass() == v2b
) {
2064 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f16
, dst
);
2065 } else if (dst
.regClass() == v1
) {
2066 emit_sqrt(ctx
, bld
, Definition(dst
), src
);
2067 } else if (dst
.regClass() == v2
) {
2068 /* Lowered at NIR level for precision reasons. */
2069 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
2071 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2072 nir_print_instr(&instr
->instr
, stderr
);
2073 fprintf(stderr
, "\n");
2077 case nir_op_ffract
: {
2078 if (dst
.regClass() == v2b
) {
2079 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f16
, dst
);
2080 } else if (dst
.regClass() == v1
) {
2081 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
2082 } else if (dst
.regClass() == v2
) {
2083 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
2085 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2086 nir_print_instr(&instr
->instr
, stderr
);
2087 fprintf(stderr
, "\n");
2091 case nir_op_ffloor
: {
2092 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2093 if (dst
.regClass() == v2b
) {
2094 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f16
, dst
);
2095 } else if (dst
.regClass() == v1
) {
2096 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
2097 } else if (dst
.regClass() == v2
) {
2098 emit_floor_f64(ctx
, bld
, Definition(dst
), src
);
2100 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2101 nir_print_instr(&instr
->instr
, stderr
);
2102 fprintf(stderr
, "\n");
2106 case nir_op_fceil
: {
2107 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2108 if (dst
.regClass() == v2b
) {
2109 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f16
, dst
);
2110 } else if (dst
.regClass() == v1
) {
2111 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
2112 } else if (dst
.regClass() == v2
) {
2113 if (ctx
->options
->chip_class
>= GFX7
) {
2114 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
2116 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2117 /* trunc = trunc(src0)
2118 * if (src0 > 0.0 && src0 != trunc)
2121 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src0
);
2122 Temp tmp0
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.def(bld
.lm
), src0
, Operand(0u));
2123 Temp tmp1
= bld
.vopc(aco_opcode::v_cmp_lg_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, trunc
);
2124 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.hint_vcc(bld
.def(s2
)), bld
.def(s1
, scc
), tmp0
, tmp1
);
2125 Temp add
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0u)), bld
.copy(bld
.def(v1
), Operand(0x3ff00000u
)), cond
);
2126 add
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), bld
.copy(bld
.def(v1
), Operand(0u)), add
);
2127 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), trunc
, add
);
2130 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2131 nir_print_instr(&instr
->instr
, stderr
);
2132 fprintf(stderr
, "\n");
2136 case nir_op_ftrunc
: {
2137 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2138 if (dst
.regClass() == v2b
) {
2139 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f16
, dst
);
2140 } else if (dst
.regClass() == v1
) {
2141 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
2142 } else if (dst
.regClass() == v2
) {
2143 emit_trunc_f64(ctx
, bld
, Definition(dst
), src
);
2145 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2146 nir_print_instr(&instr
->instr
, stderr
);
2147 fprintf(stderr
, "\n");
2151 case nir_op_fround_even
: {
2152 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2153 if (dst
.regClass() == v2b
) {
2154 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f16
, dst
);
2155 } else if (dst
.regClass() == v1
) {
2156 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
2157 } else if (dst
.regClass() == v2
) {
2158 if (ctx
->options
->chip_class
>= GFX7
) {
2159 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
2161 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2162 Temp src0_lo
= bld
.tmp(v1
), src0_hi
= bld
.tmp(v1
);
2163 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0_lo
), Definition(src0_hi
), src0
);
2165 Temp bitmask
= bld
.sop1(aco_opcode::s_brev_b32
, bld
.def(s1
), bld
.copy(bld
.def(s1
), Operand(-2u)));
2166 Temp bfi
= bld
.vop3(aco_opcode::v_bfi_b32
, bld
.def(v1
), bitmask
, bld
.copy(bld
.def(v1
), Operand(0x43300000u
)), as_vgpr(ctx
, src0_hi
));
2167 Temp tmp
= bld
.vop3(aco_opcode::v_add_f64
, bld
.def(v2
), src0
, bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), bfi
));
2168 Instruction
*sub
= bld
.vop3(aco_opcode::v_add_f64
, bld
.def(v2
), tmp
, bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), bfi
));
2169 static_cast<VOP3A_instruction
*>(sub
)->neg
[1] = true;
2170 tmp
= sub
->definitions
[0].getTemp();
2172 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x432fffffu
));
2173 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, v
);
2174 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2175 Temp cond
= vop3
->definitions
[0].getTemp();
2177 Temp tmp_lo
= bld
.tmp(v1
), tmp_hi
= bld
.tmp(v1
);
2178 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp_lo
), Definition(tmp_hi
), tmp
);
2179 Temp dst0
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_lo
, as_vgpr(ctx
, src0_lo
), cond
);
2180 Temp dst1
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_hi
, as_vgpr(ctx
, src0_hi
), cond
);
2182 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
2185 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2186 nir_print_instr(&instr
->instr
, stderr
);
2187 fprintf(stderr
, "\n");
2193 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
2194 aco_ptr
<Instruction
> norm
;
2195 if (dst
.regClass() == v2b
) {
2196 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3118u
));
2197 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f16
, bld
.def(v1
), half_pi
, src
);
2198 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f16
: aco_opcode::v_cos_f16
;
2199 bld
.vop1(opcode
, Definition(dst
), tmp
);
2200 } else if (dst
.regClass() == v1
) {
2201 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3e22f983u
));
2202 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
2204 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2205 if (ctx
->options
->chip_class
< GFX9
)
2206 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
2208 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
2209 bld
.vop1(opcode
, Definition(dst
), tmp
);
2211 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2212 nir_print_instr(&instr
->instr
, stderr
);
2213 fprintf(stderr
, "\n");
2217 case nir_op_ldexp
: {
2218 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2219 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2220 if (dst
.regClass() == v2b
) {
2221 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ldexp_f16
, dst
, false);
2222 } else if (dst
.regClass() == v1
) {
2223 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
), as_vgpr(ctx
, src0
), src1
);
2224 } else if (dst
.regClass() == v2
) {
2225 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
), as_vgpr(ctx
, src0
), src1
);
2227 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2228 nir_print_instr(&instr
->instr
, stderr
);
2229 fprintf(stderr
, "\n");
2233 case nir_op_frexp_sig
: {
2234 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2235 if (dst
.regClass() == v2b
) {
2236 bld
.vop1(aco_opcode::v_frexp_mant_f16
, Definition(dst
), src
);
2237 } else if (dst
.regClass() == v1
) {
2238 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
), src
);
2239 } else if (dst
.regClass() == v2
) {
2240 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
), src
);
2242 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2243 nir_print_instr(&instr
->instr
, stderr
);
2244 fprintf(stderr
, "\n");
2248 case nir_op_frexp_exp
: {
2249 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2250 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2251 Temp tmp
= bld
.vop1(aco_opcode::v_frexp_exp_i16_f16
, bld
.def(v1
), src
);
2252 tmp
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(v1b
), tmp
, Operand(0u));
2253 convert_int(ctx
, bld
, tmp
, 8, 32, true, dst
);
2254 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2255 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
), src
);
2256 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2257 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
), src
);
2259 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2260 nir_print_instr(&instr
->instr
, stderr
);
2261 fprintf(stderr
, "\n");
2265 case nir_op_fsign
: {
2266 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
2267 if (dst
.regClass() == v2b
) {
2268 Temp one
= bld
.copy(bld
.def(v1
), Operand(0x3c00u
));
2269 Temp minus_one
= bld
.copy(bld
.def(v1
), Operand(0xbc00u
));
2270 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2271 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), one
, src
, cond
);
2272 cond
= bld
.vopc(aco_opcode::v_cmp_le_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2273 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), minus_one
, src
, cond
);
2274 } else if (dst
.regClass() == v1
) {
2275 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2276 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
2277 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2278 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
2279 } else if (dst
.regClass() == v2
) {
2280 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2281 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
2282 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
2284 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2285 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
2286 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
2288 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2290 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2291 nir_print_instr(&instr
->instr
, stderr
);
2292 fprintf(stderr
, "\n");
2297 case nir_op_f2f16_rtne
: {
2298 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2299 if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2300 src
= bld
.vop1(aco_opcode::v_cvt_f32_f64
, bld
.def(v1
), src
);
2301 bld
.vop1(aco_opcode::v_cvt_f16_f32
, Definition(dst
), src
);
2304 case nir_op_f2f16_rtz
: {
2305 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2306 if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2307 src
= bld
.vop1(aco_opcode::v_cvt_f32_f64
, bld
.def(v1
), src
);
2308 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src
, Operand(0u));
2311 case nir_op_f2f32
: {
2312 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2313 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f16
, dst
);
2314 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2315 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
2317 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2318 nir_print_instr(&instr
->instr
, stderr
);
2319 fprintf(stderr
, "\n");
2323 case nir_op_f2f64
: {
2324 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2325 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2326 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2327 bld
.vop1(aco_opcode::v_cvt_f64_f32
, Definition(dst
), src
);
2330 case nir_op_i2f16
: {
2331 assert(dst
.regClass() == v2b
);
2332 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2333 if (instr
->src
[0].src
.ssa
->bit_size
== 8)
2334 src
= convert_int(ctx
, bld
, src
, 8, 16, true);
2335 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2336 src
= convert_int(ctx
, bld
, src
, 64, 32, false);
2337 bld
.vop1(aco_opcode::v_cvt_f16_i16
, Definition(dst
), src
);
2340 case nir_op_i2f32
: {
2341 assert(dst
.size() == 1);
2342 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2343 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2344 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2345 bld
.vop1(aco_opcode::v_cvt_f32_i32
, Definition(dst
), src
);
2348 case nir_op_i2f64
: {
2349 if (instr
->src
[0].src
.ssa
->bit_size
<= 32) {
2350 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2351 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2352 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2353 bld
.vop1(aco_opcode::v_cvt_f64_i32
, Definition(dst
), src
);
2354 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2355 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2356 RegClass rc
= RegClass(src
.type(), 1);
2357 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
2358 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
2359 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
2360 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
2361 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
2362 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
2365 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2366 nir_print_instr(&instr
->instr
, stderr
);
2367 fprintf(stderr
, "\n");
2371 case nir_op_u2f16
: {
2372 assert(dst
.regClass() == v2b
);
2373 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2374 if (instr
->src
[0].src
.ssa
->bit_size
== 8)
2375 src
= convert_int(ctx
, bld
, src
, 8, 16, false);
2376 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2377 src
= convert_int(ctx
, bld
, src
, 64, 32, false);
2378 bld
.vop1(aco_opcode::v_cvt_f16_u16
, Definition(dst
), src
);
2381 case nir_op_u2f32
: {
2382 assert(dst
.size() == 1);
2383 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2384 if (instr
->src
[0].src
.ssa
->bit_size
== 8) {
2385 bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, Definition(dst
), src
);
2387 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2388 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2389 bld
.vop1(aco_opcode::v_cvt_f32_u32
, Definition(dst
), src
);
2393 case nir_op_u2f64
: {
2394 if (instr
->src
[0].src
.ssa
->bit_size
<= 32) {
2395 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2396 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2397 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, false);
2398 bld
.vop1(aco_opcode::v_cvt_f64_u32
, Definition(dst
), src
);
2399 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2400 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2401 RegClass rc
= RegClass(src
.type(), 1);
2402 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
2403 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
2404 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
2405 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
2406 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
2407 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
2409 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2410 nir_print_instr(&instr
->instr
, stderr
);
2411 fprintf(stderr
, "\n");
2416 case nir_op_f2i16
: {
2417 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2418 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i16_f16
, dst
);
2419 else if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2420 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f32
, dst
);
2422 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f64
, dst
);
2426 case nir_op_f2u16
: {
2427 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2428 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u16_f16
, dst
);
2429 else if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2430 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f32
, dst
);
2432 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f64
, dst
);
2435 case nir_op_f2i32
: {
2436 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2437 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2438 Temp tmp
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2439 if (dst
.type() == RegType::vgpr
) {
2440 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), tmp
);
2442 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2443 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), tmp
));
2445 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2446 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f32
, dst
);
2447 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2448 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f64
, dst
);
2450 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2451 nir_print_instr(&instr
->instr
, stderr
);
2452 fprintf(stderr
, "\n");
2456 case nir_op_f2u32
: {
2457 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2458 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2459 Temp tmp
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2460 if (dst
.type() == RegType::vgpr
) {
2461 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), tmp
);
2463 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2464 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), tmp
));
2466 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2467 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f32
, dst
);
2468 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2469 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f64
, dst
);
2471 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2472 nir_print_instr(&instr
->instr
, stderr
);
2473 fprintf(stderr
, "\n");
2477 case nir_op_f2i64
: {
2478 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2479 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2480 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2482 if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::vgpr
) {
2483 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2484 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
2485 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2486 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
2487 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2488 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
2489 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2490 Temp new_exponent
= bld
.tmp(v1
);
2491 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
2492 if (ctx
->program
->chip_class
>= GFX8
)
2493 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2495 mantissa
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2496 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
2497 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2498 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2499 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
2500 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
2501 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
2502 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
2503 Temp new_lower
= bld
.tmp(v1
);
2504 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
2505 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
2506 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
2508 } else if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::sgpr
) {
2509 if (src
.type() == RegType::vgpr
)
2510 src
= bld
.as_uniform(src
);
2511 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2512 exponent
= bld
.sop2(aco_opcode::s_sub_i32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2513 exponent
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2514 exponent
= bld
.sop2(aco_opcode::s_min_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
2515 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2516 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
2517 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2518 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
2519 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2520 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
2521 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
2522 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
2523 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
2524 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
2525 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2526 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2527 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
2528 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
2529 Temp borrow
= bld
.tmp(s1
);
2530 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
2531 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
2532 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2534 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2535 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2536 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2537 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2538 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2539 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2540 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2541 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2542 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
2543 if (dst
.type() == RegType::sgpr
) {
2544 lower
= bld
.as_uniform(lower
);
2545 upper
= bld
.as_uniform(upper
);
2547 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2550 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2551 nir_print_instr(&instr
->instr
, stderr
);
2552 fprintf(stderr
, "\n");
2556 case nir_op_f2u64
: {
2557 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2558 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2559 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2561 if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::vgpr
) {
2562 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2563 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(64u), exponent
);
2564 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
2565 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2566 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2567 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
2568 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
2569 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2570 Temp new_exponent
= bld
.tmp(v1
);
2571 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
2572 if (ctx
->program
->chip_class
>= GFX8
)
2573 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2575 mantissa
= bld
.vop3(aco_opcode::v_lshl_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2576 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2577 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2578 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
2579 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
2580 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
2581 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
2582 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2584 } else if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::sgpr
) {
2585 if (src
.type() == RegType::vgpr
)
2586 src
= bld
.as_uniform(src
);
2587 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2588 exponent
= bld
.sop2(aco_opcode::s_sub_i32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2589 exponent
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2590 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2591 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2592 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
2593 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
2594 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2595 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
2596 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
2597 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
2598 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
2599 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2600 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2601 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
2602 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
2603 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
2604 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2606 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2607 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2608 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2609 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2610 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2611 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2612 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2613 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2614 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
2615 if (dst
.type() == RegType::sgpr
) {
2616 lower
= bld
.as_uniform(lower
);
2617 upper
= bld
.as_uniform(upper
);
2619 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2622 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2623 nir_print_instr(&instr
->instr
, stderr
);
2624 fprintf(stderr
, "\n");
2628 case nir_op_b2f16
: {
2629 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2630 assert(src
.regClass() == bld
.lm
);
2632 if (dst
.regClass() == s1
) {
2633 src
= bool_to_scalar_condition(ctx
, src
);
2634 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3c00u
), src
);
2635 } else if (dst
.regClass() == v2b
) {
2636 Temp one
= bld
.copy(bld
.def(v1
), Operand(0x3c00u
));
2637 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), one
, src
);
2639 unreachable("Wrong destination register class for nir_op_b2f16.");
2643 case nir_op_b2f32
: {
2644 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2645 assert(src
.regClass() == bld
.lm
);
2647 if (dst
.regClass() == s1
) {
2648 src
= bool_to_scalar_condition(ctx
, src
);
2649 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
2650 } else if (dst
.regClass() == v1
) {
2651 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
2653 unreachable("Wrong destination register class for nir_op_b2f32.");
2657 case nir_op_b2f64
: {
2658 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2659 assert(src
.regClass() == bld
.lm
);
2661 if (dst
.regClass() == s2
) {
2662 src
= bool_to_scalar_condition(ctx
, src
);
2663 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
2664 } else if (dst
.regClass() == v2
) {
2665 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
2666 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
2667 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2669 unreachable("Wrong destination register class for nir_op_b2f64.");
2676 case nir_op_i2i64
: {
2677 convert_int(ctx
, bld
, get_alu_src(ctx
, instr
->src
[0]),
2678 instr
->src
[0].src
.ssa
->bit_size
, instr
->dest
.dest
.ssa
.bit_size
, true, dst
);
2684 case nir_op_u2u64
: {
2685 convert_int(ctx
, bld
, get_alu_src(ctx
, instr
->src
[0]),
2686 instr
->src
[0].src
.ssa
->bit_size
, instr
->dest
.dest
.ssa
.bit_size
, false, dst
);
2690 case nir_op_b2i32
: {
2691 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2692 assert(src
.regClass() == bld
.lm
);
2694 if (dst
.regClass() == s1
) {
2695 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2696 bool_to_scalar_condition(ctx
, src
, dst
);
2697 } else if (dst
.regClass() == v1
) {
2698 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2700 unreachable("Invalid register class for b2i32");
2706 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2707 assert(dst
.regClass() == bld
.lm
);
2709 if (src
.type() == RegType::vgpr
) {
2710 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2711 assert(dst
.regClass() == bld
.lm
);
2712 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2713 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2715 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2717 if (src
.regClass() == s2
&& ctx
->program
->chip_class
<= GFX7
) {
2718 tmp
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u), src
).def(1).getTemp();
2720 tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2721 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2723 bool_to_vector_condition(ctx
, tmp
, dst
);
2727 case nir_op_pack_64_2x32_split
: {
2728 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2729 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2731 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2734 case nir_op_unpack_64_2x32_split_x
:
2735 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2737 case nir_op_unpack_64_2x32_split_y
:
2738 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2740 case nir_op_unpack_32_2x16_split_x
:
2741 if (dst
.type() == RegType::vgpr
) {
2742 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2744 bld
.copy(Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2747 case nir_op_unpack_32_2x16_split_y
:
2748 if (dst
.type() == RegType::vgpr
) {
2749 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2751 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]), Operand(uint32_t(16 << 16 | 16)));
2754 case nir_op_pack_32_2x16_split
: {
2755 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2756 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2757 if (dst
.regClass() == v1
) {
2758 src0
= emit_extract_vector(ctx
, src0
, 0, v2b
);
2759 src1
= emit_extract_vector(ctx
, src1
, 0, v2b
);
2760 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2762 src0
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), src0
, Operand(0xFFFFu
));
2763 src1
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), src1
, Operand(16u));
2764 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
2768 case nir_op_pack_half_2x16
: {
2769 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2771 if (dst
.regClass() == v1
) {
2772 Temp src0
= bld
.tmp(v1
);
2773 Temp src1
= bld
.tmp(v1
);
2774 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2775 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2776 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2778 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2779 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2780 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2782 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2783 nir_print_instr(&instr
->instr
, stderr
);
2784 fprintf(stderr
, "\n");
2788 case nir_op_unpack_half_2x16_split_x
: {
2789 if (dst
.regClass() == v1
) {
2790 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2792 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2793 nir_print_instr(&instr
->instr
, stderr
);
2794 fprintf(stderr
, "\n");
2798 case nir_op_unpack_half_2x16_split_y
: {
2799 if (dst
.regClass() == v1
) {
2800 /* TODO: use SDWA here */
2801 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2802 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2804 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2805 nir_print_instr(&instr
->instr
, stderr
);
2806 fprintf(stderr
, "\n");
2810 case nir_op_fquantize2f16
: {
2811 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2812 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2815 if (ctx
->program
->chip_class
>= GFX8
) {
2816 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2817 cmp_res
= bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), f16
, mask
);
2818 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2820 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2821 * so compare the result and flush to 0 if it's smaller.
2823 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2824 Temp smallest
= bld
.copy(bld
.def(s1
), Operand(0x38800000u
));
2825 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), f32
, smallest
);
2826 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2827 cmp_res
= vop3
->definitions
[0].getTemp();
2830 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
|| ctx
->program
->chip_class
< GFX8
) {
2831 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2832 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2834 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2839 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2840 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2842 if (dst
.regClass() == s1
) {
2843 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2844 } else if (dst
.regClass() == v1
) {
2845 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2847 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2848 nir_print_instr(&instr
->instr
, stderr
);
2849 fprintf(stderr
, "\n");
2853 case nir_op_bitfield_select
: {
2854 /* (mask & insert) | (~mask & base) */
2855 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2856 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2857 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2859 /* dst = (insert & bitmask) | (base & ~bitmask) */
2860 if (dst
.regClass() == s1
) {
2861 aco_ptr
<Instruction
> sop2
;
2862 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2863 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2865 if (const_insert
&& const_bitmask
) {
2866 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2868 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2869 lhs
= Operand(insert
);
2873 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2874 if (const_base
&& const_bitmask
) {
2875 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2877 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2878 rhs
= Operand(base
);
2881 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2883 } else if (dst
.regClass() == v1
) {
2884 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2885 base
= as_vgpr(ctx
, base
);
2886 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2887 insert
= as_vgpr(ctx
, insert
);
2889 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2892 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2893 nir_print_instr(&instr
->instr
, stderr
);
2894 fprintf(stderr
, "\n");
2900 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2901 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2902 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2904 if (dst
.type() == RegType::sgpr
) {
2906 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2907 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2908 if (const_offset
&& const_bits
) {
2909 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2910 extract
= Operand(const_extract
);
2914 width
= Operand(const_bits
->u32
<< 16);
2916 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2918 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2922 if (dst
.regClass() == s1
) {
2923 if (instr
->op
== nir_op_ubfe
)
2924 opcode
= aco_opcode::s_bfe_u32
;
2926 opcode
= aco_opcode::s_bfe_i32
;
2927 } else if (dst
.regClass() == s2
) {
2928 if (instr
->op
== nir_op_ubfe
)
2929 opcode
= aco_opcode::s_bfe_u64
;
2931 opcode
= aco_opcode::s_bfe_i64
;
2933 unreachable("Unsupported BFE bit size");
2936 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2940 if (dst
.regClass() == v1
) {
2941 if (instr
->op
== nir_op_ubfe
)
2942 opcode
= aco_opcode::v_bfe_u32
;
2944 opcode
= aco_opcode::v_bfe_i32
;
2946 unreachable("Unsupported BFE bit size");
2949 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2953 case nir_op_bit_count
: {
2954 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2955 if (src
.regClass() == s1
) {
2956 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2957 } else if (src
.regClass() == v1
) {
2958 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2959 } else if (src
.regClass() == v2
) {
2960 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2961 emit_extract_vector(ctx
, src
, 1, v1
),
2962 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2963 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2964 } else if (src
.regClass() == s2
) {
2965 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2967 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2968 nir_print_instr(&instr
->instr
, stderr
);
2969 fprintf(stderr
, "\n");
2974 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f16
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2978 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f16
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2982 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f16
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2986 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f16
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2990 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_i16
, aco_opcode::v_cmp_lt_i32
, aco_opcode::v_cmp_lt_i64
, aco_opcode::s_cmp_lt_i32
);
2994 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_i16
, aco_opcode::v_cmp_ge_i32
, aco_opcode::v_cmp_ge_i64
, aco_opcode::s_cmp_ge_i32
);
2998 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2999 emit_boolean_logic(ctx
, instr
, Builder::s_xnor
, dst
);
3001 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_i16
, aco_opcode::v_cmp_eq_i32
, aco_opcode::v_cmp_eq_i64
, aco_opcode::s_cmp_eq_i32
,
3002 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_eq_u64
: aco_opcode::num_opcodes
);
3006 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
3007 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
3009 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lg_i16
, aco_opcode::v_cmp_lg_i32
, aco_opcode::v_cmp_lg_i64
, aco_opcode::s_cmp_lg_i32
,
3010 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_lg_u64
: aco_opcode::num_opcodes
);
3014 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_u16
, aco_opcode::v_cmp_lt_u32
, aco_opcode::v_cmp_lt_u64
, aco_opcode::s_cmp_lt_u32
);
3018 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_u16
, aco_opcode::v_cmp_ge_u32
, aco_opcode::v_cmp_ge_u64
, aco_opcode::s_cmp_ge_u32
);
3023 case nir_op_fddx_fine
:
3024 case nir_op_fddy_fine
:
3025 case nir_op_fddx_coarse
:
3026 case nir_op_fddy_coarse
: {
3027 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
3028 uint16_t dpp_ctrl1
, dpp_ctrl2
;
3029 if (instr
->op
== nir_op_fddx_fine
) {
3030 dpp_ctrl1
= dpp_quad_perm(0, 0, 2, 2);
3031 dpp_ctrl2
= dpp_quad_perm(1, 1, 3, 3);
3032 } else if (instr
->op
== nir_op_fddy_fine
) {
3033 dpp_ctrl1
= dpp_quad_perm(0, 1, 0, 1);
3034 dpp_ctrl2
= dpp_quad_perm(2, 3, 2, 3);
3036 dpp_ctrl1
= dpp_quad_perm(0, 0, 0, 0);
3037 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
3038 dpp_ctrl2
= dpp_quad_perm(1, 1, 1, 1);
3040 dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
3044 if (ctx
->program
->chip_class
>= GFX8
) {
3045 Temp tl
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl1
);
3046 tmp
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), src
, tl
, dpp_ctrl2
);
3048 Temp tl
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl1
);
3049 Temp tr
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl2
);
3050 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), tr
, tl
);
3052 emit_wqm(ctx
, tmp
, dst
, true);
3056 fprintf(stderr
, "Unknown NIR ALU instr: ");
3057 nir_print_instr(&instr
->instr
, stderr
);
3058 fprintf(stderr
, "\n");
3062 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
3064 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
3066 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3067 // which get truncated the lsb if double and msb if int
3068 // for now, we only use s_mov_b64 with 64bit inline constants
3069 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
3070 assert(dst
.type() == RegType::sgpr
);
3072 Builder
bld(ctx
->program
, ctx
->block
);
3074 if (instr
->def
.bit_size
== 1) {
3075 assert(dst
.regClass() == bld
.lm
);
3076 int val
= instr
->value
[0].b
? -1 : 0;
3077 Operand op
= bld
.lm
.size() == 1 ? Operand((uint32_t) val
) : Operand((uint64_t) val
);
3078 bld
.sop1(Builder::s_mov
, Definition(dst
), op
);
3079 } else if (instr
->def
.bit_size
== 8) {
3080 /* ensure that the value is correctly represented in the low byte of the register */
3081 bld
.sopk(aco_opcode::s_movk_i32
, Definition(dst
), instr
->value
[0].u8
);
3082 } else if (instr
->def
.bit_size
== 16) {
3083 /* ensure that the value is correctly represented in the low half of the register */
3084 bld
.sopk(aco_opcode::s_movk_i32
, Definition(dst
), instr
->value
[0].u16
);
3085 } else if (dst
.size() == 1) {
3086 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
3088 assert(dst
.size() != 1);
3089 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3090 if (instr
->def
.bit_size
== 64)
3091 for (unsigned i
= 0; i
< dst
.size(); i
++)
3092 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
3094 for (unsigned i
= 0; i
< dst
.size(); i
++)
3095 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
3097 vec
->definitions
[0] = Definition(dst
);
3098 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3102 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
3104 uint32_t new_mask
= 0;
3105 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
3106 if (mask
& (1u << i
))
3107 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
3111 struct LoadEmitInfo
{
3114 unsigned num_components
;
3115 unsigned component_size
;
3116 Temp resource
= Temp(0, s1
);
3117 unsigned component_stride
= 0;
3118 unsigned const_offset
= 0;
3119 unsigned align_mul
= 0;
3120 unsigned align_offset
= 0;
3123 unsigned swizzle_component_size
= 0;
3124 barrier_interaction barrier
= barrier_none
;
3125 bool can_reorder
= true;
3126 Temp soffset
= Temp(0, s1
);
3129 using LoadCallback
= Temp(*)(
3130 Builder
& bld
, const LoadEmitInfo
* info
, Temp offset
, unsigned bytes_needed
,
3131 unsigned align
, unsigned const_offset
, Temp dst_hint
);
3133 template <LoadCallback callback
, bool byte_align_loads
, bool supports_8bit_16bit_loads
, unsigned max_const_offset_plus_one
>
3134 void emit_load(isel_context
*ctx
, Builder
& bld
, const LoadEmitInfo
*info
)
3136 unsigned load_size
= info
->num_components
* info
->component_size
;
3137 unsigned component_size
= info
->component_size
;
3139 unsigned num_vals
= 0;
3140 Temp vals
[info
->dst
.bytes()];
3142 unsigned const_offset
= info
->const_offset
;
3144 unsigned align_mul
= info
->align_mul
? info
->align_mul
: component_size
;
3145 unsigned align_offset
= (info
->align_offset
+ const_offset
) % align_mul
;
3147 unsigned bytes_read
= 0;
3148 while (bytes_read
< load_size
) {
3149 unsigned bytes_needed
= load_size
- bytes_read
;
3151 /* add buffer for unaligned loads */
3152 int byte_align
= align_mul
% 4 == 0 ? align_offset
% 4 : -1;
3155 if ((bytes_needed
> 2 ||
3156 (bytes_needed
== 2 && (align_mul
% 2 || align_offset
% 2)) ||
3157 !supports_8bit_16bit_loads
) && byte_align_loads
) {
3158 if (info
->component_stride
) {
3159 assert(supports_8bit_16bit_loads
&& "unimplemented");
3163 bytes_needed
+= byte_align
== -1 ? 4 - info
->align_mul
: byte_align
;
3164 bytes_needed
= align(bytes_needed
, 4);
3171 if (info
->swizzle_component_size
)
3172 bytes_needed
= MIN2(bytes_needed
, info
->swizzle_component_size
);
3173 if (info
->component_stride
)
3174 bytes_needed
= MIN2(bytes_needed
, info
->component_size
);
3176 bool need_to_align_offset
= byte_align
&& (align_mul
% 4 || align_offset
% 4);
3178 /* reduce constant offset */
3179 Operand offset
= info
->offset
;
3180 unsigned reduced_const_offset
= const_offset
;
3181 bool remove_const_offset_completely
= need_to_align_offset
;
3182 if (const_offset
&& (remove_const_offset_completely
|| const_offset
>= max_const_offset_plus_one
)) {
3183 unsigned to_add
= const_offset
;
3184 if (remove_const_offset_completely
) {
3185 reduced_const_offset
= 0;
3187 to_add
= const_offset
/ max_const_offset_plus_one
* max_const_offset_plus_one
;
3188 reduced_const_offset
%= max_const_offset_plus_one
;
3190 Temp offset_tmp
= offset
.isTemp() ? offset
.getTemp() : Temp();
3191 if (offset
.isConstant()) {
3192 offset
= Operand(offset
.constantValue() + to_add
);
3193 } else if (offset_tmp
.regClass() == s1
) {
3194 offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3195 offset_tmp
, Operand(to_add
));
3196 } else if (offset_tmp
.regClass() == v1
) {
3197 offset
= bld
.vadd32(bld
.def(v1
), offset_tmp
, Operand(to_add
));
3199 Temp lo
= bld
.tmp(offset_tmp
.type(), 1);
3200 Temp hi
= bld
.tmp(offset_tmp
.type(), 1);
3201 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), offset_tmp
);
3203 if (offset_tmp
.regClass() == s2
) {
3204 Temp carry
= bld
.tmp(s1
);
3205 lo
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), lo
, Operand(to_add
));
3206 hi
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, carry
);
3207 offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), lo
, hi
);
3209 Temp new_lo
= bld
.tmp(v1
);
3210 Temp carry
= bld
.vadd32(Definition(new_lo
), lo
, Operand(to_add
), true).def(1).getTemp();
3211 hi
= bld
.vadd32(bld
.def(v1
), hi
, Operand(0u), false, carry
);
3212 offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_lo
, hi
);
3217 /* align offset down if needed */
3218 Operand aligned_offset
= offset
;
3219 if (need_to_align_offset
) {
3220 Temp offset_tmp
= offset
.isTemp() ? offset
.getTemp() : Temp();
3221 if (offset
.isConstant()) {
3222 aligned_offset
= Operand(offset
.constantValue() & 0xfffffffcu
);
3223 } else if (offset_tmp
.regClass() == s1
) {
3224 aligned_offset
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfffffffcu
), offset_tmp
);
3225 } else if (offset_tmp
.regClass() == s2
) {
3226 aligned_offset
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand((uint64_t)0xfffffffffffffffcllu
), offset_tmp
);
3227 } else if (offset_tmp
.regClass() == v1
) {
3228 aligned_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xfffffffcu
), offset_tmp
);
3229 } else if (offset_tmp
.regClass() == v2
) {
3230 Temp hi
= bld
.tmp(v1
), lo
= bld
.tmp(v1
);
3231 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), offset_tmp
);
3232 lo
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xfffffffcu
), lo
);
3233 aligned_offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), lo
, hi
);
3236 Temp aligned_offset_tmp
= aligned_offset
.isTemp() ? aligned_offset
.getTemp() :
3237 bld
.copy(bld
.def(s1
), aligned_offset
);
3239 unsigned align
= align_offset
? 1 << (ffs(align_offset
) - 1) : align_mul
;
3240 Temp val
= callback(bld
, info
, aligned_offset_tmp
, bytes_needed
, align
,
3241 reduced_const_offset
, byte_align
? Temp() : info
->dst
);
3243 /* the callback wrote directly to dst */
3244 if (val
== info
->dst
) {
3245 assert(num_vals
== 0);
3246 emit_split_vector(ctx
, info
->dst
, info
->num_components
);
3250 /* shift result right if needed */
3251 if (info
->component_size
< 4 && byte_align_loads
) {
3252 Operand
align((uint32_t)byte_align
);
3253 if (byte_align
== -1) {
3254 if (offset
.isConstant())
3255 align
= Operand(offset
.constantValue() % 4u);
3256 else if (offset
.size() == 2)
3257 align
= Operand(emit_extract_vector(ctx
, offset
.getTemp(), 0, RegClass(offset
.getTemp().type(), 1)));
3262 assert(val
.bytes() >= load_size
&& "unimplemented");
3263 if (val
.type() == RegType::sgpr
)
3264 byte_align_scalar(ctx
, val
, align
, info
->dst
);
3266 byte_align_vector(ctx
, val
, align
, info
->dst
, component_size
);
3270 /* add result to list and advance */
3271 if (info
->component_stride
) {
3272 assert(val
.bytes() == info
->component_size
&& "unimplemented");
3273 const_offset
+= info
->component_stride
;
3274 align_offset
= (align_offset
+ info
->component_stride
) % align_mul
;
3276 const_offset
+= val
.bytes();
3277 align_offset
= (align_offset
+ val
.bytes()) % align_mul
;
3279 bytes_read
+= val
.bytes();
3280 vals
[num_vals
++] = val
;
3283 /* create array of components */
3284 unsigned components_split
= 0;
3285 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> allocated_vec
;
3286 bool has_vgprs
= false;
3287 for (unsigned i
= 0; i
< num_vals
;) {
3289 unsigned num_tmps
= 0;
3290 unsigned tmp_size
= 0;
3291 RegType reg_type
= RegType::sgpr
;
3292 while ((!tmp_size
|| (tmp_size
% component_size
)) && i
< num_vals
) {
3293 if (vals
[i
].type() == RegType::vgpr
)
3294 reg_type
= RegType::vgpr
;
3295 tmp_size
+= vals
[i
].bytes();
3296 tmp
[num_tmps
++] = vals
[i
++];
3299 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(
3300 aco_opcode::p_create_vector
, Format::PSEUDO
, num_tmps
, 1)};
3301 for (unsigned i
= 0; i
< num_vals
; i
++)
3302 vec
->operands
[i
] = Operand(tmp
[i
]);
3303 tmp
[0] = bld
.tmp(RegClass::get(reg_type
, tmp_size
));
3304 vec
->definitions
[0] = Definition(tmp
[0]);
3305 bld
.insert(std::move(vec
));
3308 if (tmp
[0].bytes() % component_size
) {
3310 assert(i
== num_vals
);
3311 RegClass new_rc
= RegClass::get(reg_type
, tmp
[0].bytes() / component_size
* component_size
);
3312 tmp
[0] = bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(new_rc
), tmp
[0], Operand(0u));
3315 RegClass elem_rc
= RegClass::get(reg_type
, component_size
);
3317 unsigned start
= components_split
;
3319 if (tmp_size
== elem_rc
.bytes()) {
3320 allocated_vec
[components_split
++] = tmp
[0];
3322 assert(tmp_size
% elem_rc
.bytes() == 0);
3323 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(
3324 aco_opcode::p_split_vector
, Format::PSEUDO
, 1, tmp_size
/ elem_rc
.bytes())};
3325 for (unsigned i
= 0; i
< split
->definitions
.size(); i
++) {
3326 Temp component
= bld
.tmp(elem_rc
);
3327 allocated_vec
[components_split
++] = component
;
3328 split
->definitions
[i
] = Definition(component
);
3330 split
->operands
[0] = Operand(tmp
[0]);
3331 bld
.insert(std::move(split
));
3334 /* try to p_as_uniform early so we can create more optimizable code and
3335 * also update allocated_vec */
3336 for (unsigned j
= start
; j
< components_split
; j
++) {
3337 if (allocated_vec
[j
].bytes() % 4 == 0 && info
->dst
.type() == RegType::sgpr
)
3338 allocated_vec
[j
] = bld
.as_uniform(allocated_vec
[j
]);
3339 has_vgprs
|= allocated_vec
[j
].type() == RegType::vgpr
;
3343 /* concatenate components and p_as_uniform() result if needed */
3344 if (info
->dst
.type() == RegType::vgpr
|| !has_vgprs
)
3345 ctx
->allocated_vec
.emplace(info
->dst
.id(), allocated_vec
);
3347 int padding_bytes
= MAX2((int)info
->dst
.bytes() - int(allocated_vec
[0].bytes() * info
->num_components
), 0);
3349 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(
3350 aco_opcode::p_create_vector
, Format::PSEUDO
, info
->num_components
+ !!padding_bytes
, 1)};
3351 for (unsigned i
= 0; i
< info
->num_components
; i
++)
3352 vec
->operands
[i
] = Operand(allocated_vec
[i
]);
3354 vec
->operands
[info
->num_components
] = Operand(RegClass::get(RegType::vgpr
, padding_bytes
));
3355 if (info
->dst
.type() == RegType::sgpr
&& has_vgprs
) {
3356 Temp tmp
= bld
.tmp(RegType::vgpr
, info
->dst
.size());
3357 vec
->definitions
[0] = Definition(tmp
);
3358 bld
.insert(std::move(vec
));
3359 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(info
->dst
), tmp
);
3361 vec
->definitions
[0] = Definition(info
->dst
);
3362 bld
.insert(std::move(vec
));
3366 Operand
load_lds_size_m0(Builder
& bld
)
3368 /* TODO: m0 does not need to be initialized on GFX9+ */
3369 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
3372 Temp
lds_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3373 Temp offset
, unsigned bytes_needed
,
3374 unsigned align
, unsigned const_offset
,
3377 offset
= offset
.regClass() == s1
? bld
.copy(bld
.def(v1
), offset
) : offset
;
3379 Operand m
= load_lds_size_m0(bld
);
3381 bool large_ds_read
= bld
.program
->chip_class
>= GFX7
;
3382 bool usable_read2
= bld
.program
->chip_class
>= GFX7
;
3387 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3388 if (bytes_needed
>= 16 && align
% 16 == 0 && large_ds_read
) {
3390 op
= aco_opcode::ds_read_b128
;
3391 } else if (bytes_needed
>= 16 && align
% 8 == 0 && const_offset
% 8 == 0 && usable_read2
) {
3394 op
= aco_opcode::ds_read2_b64
;
3395 } else if (bytes_needed
>= 12 && align
% 16 == 0 && large_ds_read
) {
3397 op
= aco_opcode::ds_read_b96
;
3398 } else if (bytes_needed
>= 8 && align
% 8 == 0) {
3400 op
= aco_opcode::ds_read_b64
;
3401 } else if (bytes_needed
>= 8 && align
% 4 == 0 && const_offset
% 4 == 0) {
3404 op
= aco_opcode::ds_read2_b32
;
3405 } else if (bytes_needed
>= 4 && align
% 4 == 0) {
3407 op
= aco_opcode::ds_read_b32
;
3408 } else if (bytes_needed
>= 2 && align
% 2 == 0) {
3410 op
= aco_opcode::ds_read_u16
;
3413 op
= aco_opcode::ds_read_u8
;
3416 unsigned max_offset_plus_one
= read2
? 254 * (size
/ 2u) + 1 : 65536;
3417 if (const_offset
>= max_offset_plus_one
) {
3418 offset
= bld
.vadd32(bld
.def(v1
), offset
, Operand(const_offset
/ max_offset_plus_one
));
3419 const_offset
%= max_offset_plus_one
;
3423 const_offset
/= (size
/ 2u);
3425 RegClass rc
= RegClass(RegType::vgpr
, DIV_ROUND_UP(size
, 4));
3426 Temp val
= rc
== info
->dst
.regClass() && dst_hint
.id() ? dst_hint
: bld
.tmp(rc
);
3428 bld
.ds(op
, Definition(val
), offset
, m
, const_offset
, const_offset
+ 1);
3430 bld
.ds(op
, Definition(val
), offset
, m
, const_offset
);
3433 val
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(RegClass::get(RegType::vgpr
, size
)), val
, Operand(0u));
3438 static auto emit_lds_load
= emit_load
<lds_load_callback
, false, true, UINT32_MAX
>;
3440 Temp
smem_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3441 Temp offset
, unsigned bytes_needed
,
3442 unsigned align
, unsigned const_offset
,
3447 if (bytes_needed
<= 4) {
3449 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dword
: aco_opcode::s_load_dword
;
3450 } else if (bytes_needed
<= 8) {
3452 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx2
: aco_opcode::s_load_dwordx2
;
3453 } else if (bytes_needed
<= 16) {
3455 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx4
: aco_opcode::s_load_dwordx4
;
3456 } else if (bytes_needed
<= 32) {
3458 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx8
: aco_opcode::s_load_dwordx8
;
3461 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx16
: aco_opcode::s_load_dwordx16
;
3463 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3464 if (info
->resource
.id()) {
3465 load
->operands
[0] = Operand(info
->resource
);
3466 load
->operands
[1] = Operand(offset
);
3468 load
->operands
[0] = Operand(offset
);
3469 load
->operands
[1] = Operand(0u);
3471 RegClass
rc(RegType::sgpr
, size
);
3472 Temp val
= dst_hint
.id() && dst_hint
.regClass() == rc
? dst_hint
: bld
.tmp(rc
);
3473 load
->definitions
[0] = Definition(val
);
3474 load
->glc
= info
->glc
;
3475 load
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3476 load
->barrier
= info
->barrier
;
3477 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3478 bld
.insert(std::move(load
));
3482 static auto emit_smem_load
= emit_load
<smem_load_callback
, true, false, 1024>;
3484 Temp
mubuf_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3485 Temp offset
, unsigned bytes_needed
,
3486 unsigned align_
, unsigned const_offset
,
3489 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3490 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3492 if (info
->soffset
.id()) {
3493 if (soffset
.isTemp())
3494 vaddr
= bld
.copy(bld
.def(v1
), soffset
);
3495 soffset
= Operand(info
->soffset
);
3498 unsigned bytes_size
= 0;
3500 if (bytes_needed
== 1) {
3502 op
= aco_opcode::buffer_load_ubyte
;
3503 } else if (bytes_needed
== 2) {
3505 op
= aco_opcode::buffer_load_ushort
;
3506 } else if (bytes_needed
<= 4) {
3508 op
= aco_opcode::buffer_load_dword
;
3509 } else if (bytes_needed
<= 8) {
3511 op
= aco_opcode::buffer_load_dwordx2
;
3512 } else if (bytes_needed
<= 12 && bld
.program
->chip_class
> GFX6
) {
3514 op
= aco_opcode::buffer_load_dwordx3
;
3517 op
= aco_opcode::buffer_load_dwordx4
;
3519 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3520 mubuf
->operands
[0] = Operand(info
->resource
);
3521 mubuf
->operands
[1] = vaddr
;
3522 mubuf
->operands
[2] = soffset
;
3523 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3524 mubuf
->glc
= info
->glc
;
3525 mubuf
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3526 mubuf
->barrier
= info
->barrier
;
3527 mubuf
->can_reorder
= info
->can_reorder
;
3528 mubuf
->offset
= const_offset
;
3529 RegClass rc
= RegClass::get(RegType::vgpr
, align(bytes_size
, 4));
3530 Temp val
= dst_hint
.id() && rc
== dst_hint
.regClass() ? dst_hint
: bld
.tmp(rc
);
3531 mubuf
->definitions
[0] = Definition(val
);
3532 bld
.insert(std::move(mubuf
));
3537 static auto emit_mubuf_load
= emit_load
<mubuf_load_callback
, true, true, 4096>;
3539 Temp
get_gfx6_global_rsrc(Builder
& bld
, Temp addr
)
3541 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3542 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3544 if (addr
.type() == RegType::vgpr
)
3545 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf
));
3546 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), addr
, Operand(-1u), Operand(rsrc_conf
));
3549 Temp
global_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3550 Temp offset
, unsigned bytes_needed
,
3551 unsigned align_
, unsigned const_offset
,
3554 unsigned bytes_size
= 0;
3555 bool mubuf
= bld
.program
->chip_class
== GFX6
;
3556 bool global
= bld
.program
->chip_class
>= GFX9
;
3558 if (bytes_needed
== 1) {
3560 op
= mubuf
? aco_opcode::buffer_load_ubyte
: global
? aco_opcode::global_load_ubyte
: aco_opcode::flat_load_ubyte
;
3561 } else if (bytes_needed
== 2) {
3563 op
= mubuf
? aco_opcode::buffer_load_ushort
: global
? aco_opcode::global_load_ushort
: aco_opcode::flat_load_ushort
;
3564 } else if (bytes_needed
<= 4) {
3566 op
= mubuf
? aco_opcode::buffer_load_dword
: global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
3567 } else if (bytes_needed
<= 8) {
3569 op
= mubuf
? aco_opcode::buffer_load_dwordx2
: global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
3570 } else if (bytes_needed
<= 12 && !mubuf
) {
3572 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
3575 op
= mubuf
? aco_opcode::buffer_load_dwordx4
: global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
3577 RegClass rc
= RegClass::get(RegType::vgpr
, align(bytes_size
, 4));
3578 Temp val
= dst_hint
.id() && rc
== dst_hint
.regClass() ? dst_hint
: bld
.tmp(rc
);
3580 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3581 mubuf
->operands
[0] = Operand(get_gfx6_global_rsrc(bld
, offset
));
3582 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3583 mubuf
->operands
[2] = Operand(0u);
3584 mubuf
->glc
= info
->glc
;
3587 mubuf
->addr64
= offset
.type() == RegType::vgpr
;
3588 mubuf
->disable_wqm
= false;
3589 mubuf
->barrier
= info
->barrier
;
3590 mubuf
->definitions
[0] = Definition(val
);
3591 bld
.insert(std::move(mubuf
));
3593 offset
= offset
.regClass() == s2
? bld
.copy(bld
.def(v2
), offset
) : offset
;
3595 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
3596 flat
->operands
[0] = Operand(offset
);
3597 flat
->operands
[1] = Operand(s1
);
3598 flat
->glc
= info
->glc
;
3599 flat
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3600 flat
->barrier
= info
->barrier
;
3602 flat
->definitions
[0] = Definition(val
);
3603 bld
.insert(std::move(flat
));
3609 static auto emit_global_load
= emit_load
<global_load_callback
, true, true, 1>;
3611 Temp
load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
3612 Temp address
, unsigned base_offset
, unsigned align
)
3614 assert(util_is_power_of_two_nonzero(align
));
3616 Builder
bld(ctx
->program
, ctx
->block
);
3618 unsigned num_components
= dst
.bytes() / elem_size_bytes
;
3619 LoadEmitInfo info
= {Operand(as_vgpr(ctx
, address
)), dst
, num_components
, elem_size_bytes
};
3620 info
.align_mul
= align
;
3621 info
.align_offset
= 0;
3622 info
.barrier
= barrier_shared
;
3623 info
.can_reorder
= false;
3624 info
.const_offset
= base_offset
;
3625 emit_lds_load(ctx
, bld
, &info
);
3630 void split_store_data(isel_context
*ctx
, RegType dst_type
, unsigned count
, Temp
*dst
, unsigned *offsets
, Temp src
)
3635 Builder
bld(ctx
->program
, ctx
->block
);
3637 ASSERTED
bool is_subdword
= false;
3638 for (unsigned i
= 0; i
< count
; i
++)
3639 is_subdword
|= offsets
[i
] % 4;
3640 is_subdword
|= (src
.bytes() - offsets
[count
- 1]) % 4;
3641 assert(!is_subdword
|| dst_type
== RegType::vgpr
);
3643 /* count == 1 fast path */
3645 if (dst_type
== RegType::sgpr
)
3646 dst
[0] = bld
.as_uniform(src
);
3648 dst
[0] = as_vgpr(ctx
, src
);
3652 for (unsigned i
= 0; i
< count
- 1; i
++)
3653 dst
[i
] = bld
.tmp(RegClass::get(dst_type
, offsets
[i
+ 1] - offsets
[i
]));
3654 dst
[count
- 1] = bld
.tmp(RegClass::get(dst_type
, src
.bytes() - offsets
[count
- 1]));
3656 if (is_subdword
&& src
.type() == RegType::sgpr
) {
3657 src
= as_vgpr(ctx
, src
);
3659 /* use allocated_vec if possible */
3660 auto it
= ctx
->allocated_vec
.find(src
.id());
3661 if (it
!= ctx
->allocated_vec
.end()) {
3662 unsigned total_size
= 0;
3663 for (unsigned i
= 0; it
->second
[i
].bytes() && (i
< NIR_MAX_VEC_COMPONENTS
); i
++)
3664 total_size
+= it
->second
[i
].bytes();
3665 if (total_size
!= src
.bytes())
3668 unsigned elem_size
= it
->second
[0].bytes();
3670 for (unsigned i
= 0; i
< count
; i
++) {
3671 if (offsets
[i
] % elem_size
|| dst
[i
].bytes() % elem_size
)
3675 for (unsigned i
= 0; i
< count
; i
++) {
3676 unsigned start_idx
= offsets
[i
] / elem_size
;
3677 unsigned op_count
= dst
[i
].bytes() / elem_size
;
3678 if (op_count
== 1) {
3679 if (dst_type
== RegType::sgpr
)
3680 dst
[i
] = bld
.as_uniform(it
->second
[start_idx
]);
3682 dst
[i
] = as_vgpr(ctx
, it
->second
[start_idx
]);
3686 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, op_count
, 1)};
3687 for (unsigned j
= 0; j
< op_count
; j
++) {
3688 Temp tmp
= it
->second
[start_idx
+ j
];
3689 if (dst_type
== RegType::sgpr
)
3690 tmp
= bld
.as_uniform(tmp
);
3691 vec
->operands
[j
] = Operand(tmp
);
3693 vec
->definitions
[0] = Definition(dst
[i
]);
3694 bld
.insert(std::move(vec
));
3700 if (dst_type
== RegType::sgpr
)
3701 src
= bld
.as_uniform(src
);
3705 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, count
)};
3706 split
->operands
[0] = Operand(src
);
3707 for (unsigned i
= 0; i
< count
; i
++)
3708 split
->definitions
[i
] = Definition(dst
[i
]);
3709 bld
.insert(std::move(split
));
3712 bool scan_write_mask(uint32_t mask
, uint32_t todo_mask
,
3713 int *start
, int *count
)
3715 unsigned start_elem
= ffs(todo_mask
) - 1;
3716 bool skip
= !(mask
& (1 << start_elem
));
3718 mask
= ~mask
& todo_mask
;
3722 u_bit_scan_consecutive_range(&mask
, start
, count
);
3727 void advance_write_mask(uint32_t *todo_mask
, int start
, int count
)
3729 *todo_mask
&= ~u_bit_consecutive(0, count
) << start
;
3732 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
3733 Temp address
, unsigned base_offset
, unsigned align
)
3735 assert(util_is_power_of_two_nonzero(align
));
3736 assert(util_is_power_of_two_nonzero(elem_size_bytes
) && elem_size_bytes
<= 8);
3738 Builder
bld(ctx
->program
, ctx
->block
);
3739 bool large_ds_write
= ctx
->options
->chip_class
>= GFX7
;
3740 bool usable_write2
= ctx
->options
->chip_class
>= GFX7
;
3742 unsigned write_count
= 0;
3743 Temp write_datas
[32];
3744 unsigned offsets
[32];
3745 aco_opcode opcodes
[32];
3747 wrmask
= widen_mask(wrmask
, elem_size_bytes
);
3749 uint32_t todo
= u_bit_consecutive(0, data
.bytes());
3752 if (!scan_write_mask(wrmask
, todo
, &offset
, &bytes
)) {
3753 offsets
[write_count
] = offset
;
3754 opcodes
[write_count
] = aco_opcode::num_opcodes
;
3756 advance_write_mask(&todo
, offset
, bytes
);
3760 bool aligned2
= offset
% 2 == 0 && align
% 2 == 0;
3761 bool aligned4
= offset
% 4 == 0 && align
% 4 == 0;
3762 bool aligned8
= offset
% 8 == 0 && align
% 8 == 0;
3763 bool aligned16
= offset
% 16 == 0 && align
% 16 == 0;
3765 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3766 aco_opcode op
= aco_opcode::num_opcodes
;
3767 if (bytes
>= 16 && aligned16
&& large_ds_write
) {
3768 op
= aco_opcode::ds_write_b128
;
3770 } else if (bytes
>= 12 && aligned16
&& large_ds_write
) {
3771 op
= aco_opcode::ds_write_b96
;
3773 } else if (bytes
>= 8 && aligned8
) {
3774 op
= aco_opcode::ds_write_b64
;
3776 } else if (bytes
>= 4 && aligned4
) {
3777 op
= aco_opcode::ds_write_b32
;
3779 } else if (bytes
>= 2 && aligned2
) {
3780 op
= aco_opcode::ds_write_b16
;
3782 } else if (bytes
>= 1) {
3783 op
= aco_opcode::ds_write_b8
;
3789 offsets
[write_count
] = offset
;
3790 opcodes
[write_count
] = op
;
3792 advance_write_mask(&todo
, offset
, bytes
);
3795 Operand m
= load_lds_size_m0(bld
);
3797 split_store_data(ctx
, RegType::vgpr
, write_count
, write_datas
, offsets
, data
);
3799 for (unsigned i
= 0; i
< write_count
; i
++) {
3800 aco_opcode op
= opcodes
[i
];
3801 if (op
== aco_opcode::num_opcodes
)
3804 Temp data
= write_datas
[i
];
3806 unsigned second
= write_count
;
3807 if (usable_write2
&& (op
== aco_opcode::ds_write_b32
|| op
== aco_opcode::ds_write_b64
)) {
3808 for (second
= i
+ 1; second
< write_count
; second
++) {
3809 if (opcodes
[second
] == op
&& (offsets
[second
] - offsets
[i
]) % data
.bytes() == 0) {
3810 op
= data
.bytes() == 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
3811 opcodes
[second
] = aco_opcode::num_opcodes
;
3817 bool write2
= op
== aco_opcode::ds_write2_b32
|| op
== aco_opcode::ds_write2_b64
;
3818 unsigned write2_off
= (offsets
[second
] - offsets
[i
]) / data
.bytes();
3820 unsigned inline_offset
= base_offset
+ offsets
[i
];
3821 unsigned max_offset
= write2
? (255 - write2_off
) * data
.bytes() : 65535;
3822 Temp address_offset
= address
;
3823 if (inline_offset
> max_offset
) {
3824 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
3825 inline_offset
= offsets
[i
];
3827 assert(inline_offset
<= max_offset
); /* offsets[i] shouldn't be large enough for this to happen */
3830 Temp second_data
= write_datas
[second
];
3831 inline_offset
/= data
.bytes();
3832 bld
.ds(op
, address_offset
, data
, second_data
, m
, inline_offset
, inline_offset
+ write2_off
);
3834 bld
.ds(op
, address_offset
, data
, m
, inline_offset
);
3839 unsigned calculate_lds_alignment(isel_context
*ctx
, unsigned const_offset
)
3841 unsigned align
= 16;
3843 align
= std::min(align
, 1u << (ffs(const_offset
) - 1));
3849 aco_opcode
get_buffer_store_op(bool smem
, unsigned bytes
)
3854 return aco_opcode::buffer_store_byte
;
3857 return aco_opcode::buffer_store_short
;
3859 return smem
? aco_opcode::s_buffer_store_dword
: aco_opcode::buffer_store_dword
;
3861 return smem
? aco_opcode::s_buffer_store_dwordx2
: aco_opcode::buffer_store_dwordx2
;
3864 return aco_opcode::buffer_store_dwordx3
;
3866 return smem
? aco_opcode::s_buffer_store_dwordx4
: aco_opcode::buffer_store_dwordx4
;
3868 unreachable("Unexpected store size");
3869 return aco_opcode::num_opcodes
;
3872 void split_buffer_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool smem
, RegType dst_type
,
3873 Temp data
, unsigned writemask
, int swizzle_element_size
,
3874 unsigned *write_count
, Temp
*write_datas
, unsigned *offsets
)
3876 unsigned write_count_with_skips
= 0;
3879 /* determine how to split the data */
3880 unsigned todo
= u_bit_consecutive(0, data
.bytes());
3883 skips
[write_count_with_skips
] = !scan_write_mask(writemask
, todo
, &offset
, &bytes
);
3884 offsets
[write_count_with_skips
] = offset
;
3885 if (skips
[write_count_with_skips
]) {
3886 advance_write_mask(&todo
, offset
, bytes
);
3887 write_count_with_skips
++;
3891 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3892 * larger than swizzle_element_size */
3893 bytes
= MIN2(bytes
, swizzle_element_size
);
3895 bytes
= bytes
> 4 ? bytes
& ~0x3 : MIN2(bytes
, 2);
3897 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3898 if ((ctx
->program
->chip_class
== GFX6
|| smem
) && bytes
== 12)
3901 /* dword or larger stores have to be dword-aligned */
3902 unsigned align_mul
= instr
? nir_intrinsic_align_mul(instr
) : 4;
3903 unsigned align_offset
= (instr
? nir_intrinsic_align_offset(instr
) : 0) + offset
;
3904 bool dword_aligned
= align_offset
% 4 == 0 && align_mul
% 4 == 0;
3906 bytes
= MIN2(bytes
, (align_offset
% 2 == 0 && align_mul
% 2 == 0) ? 2 : 1);
3908 advance_write_mask(&todo
, offset
, bytes
);
3909 write_count_with_skips
++;
3912 /* actually split data */
3913 split_store_data(ctx
, dst_type
, write_count_with_skips
, write_datas
, offsets
, data
);
3916 for (unsigned i
= 0; i
< write_count_with_skips
; i
++) {
3919 write_datas
[*write_count
] = write_datas
[i
];
3920 offsets
[*write_count
] = offsets
[i
];
3925 Temp
create_vec_from_array(isel_context
*ctx
, Temp arr
[], unsigned cnt
, RegType reg_type
, unsigned elem_size_bytes
,
3926 unsigned split_cnt
= 0u, Temp dst
= Temp())
3928 Builder
bld(ctx
->program
, ctx
->block
);
3929 unsigned dword_size
= elem_size_bytes
/ 4;
3932 dst
= bld
.tmp(RegClass(reg_type
, cnt
* dword_size
));
3934 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> allocated_vec
;
3935 aco_ptr
<Pseudo_instruction
> instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, cnt
, 1)};
3936 instr
->definitions
[0] = Definition(dst
);
3938 for (unsigned i
= 0; i
< cnt
; ++i
) {
3940 assert(arr
[i
].size() == dword_size
);
3941 allocated_vec
[i
] = arr
[i
];
3942 instr
->operands
[i
] = Operand(arr
[i
]);
3944 Temp zero
= bld
.copy(bld
.def(RegClass(reg_type
, dword_size
)), Operand(0u, dword_size
== 2));
3945 allocated_vec
[i
] = zero
;
3946 instr
->operands
[i
] = Operand(zero
);
3950 bld
.insert(std::move(instr
));
3953 emit_split_vector(ctx
, dst
, split_cnt
);
3955 ctx
->allocated_vec
.emplace(dst
.id(), allocated_vec
); /* emit_split_vector already does this */
3960 inline unsigned resolve_excess_vmem_const_offset(Builder
&bld
, Temp
&voffset
, unsigned const_offset
)
3962 if (const_offset
>= 4096) {
3963 unsigned excess_const_offset
= const_offset
/ 4096u * 4096u;
3964 const_offset
%= 4096u;
3967 voffset
= bld
.copy(bld
.def(v1
), Operand(excess_const_offset
));
3968 else if (unlikely(voffset
.regClass() == s1
))
3969 voffset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(excess_const_offset
), Operand(voffset
));
3970 else if (likely(voffset
.regClass() == v1
))
3971 voffset
= bld
.vadd32(bld
.def(v1
), Operand(voffset
), Operand(excess_const_offset
));
3973 unreachable("Unsupported register class of voffset");
3976 return const_offset
;
3979 void emit_single_mubuf_store(isel_context
*ctx
, Temp descriptor
, Temp voffset
, Temp soffset
, Temp vdata
,
3980 unsigned const_offset
= 0u, bool allow_reorder
= true, bool slc
= false)
3983 assert(vdata
.size() != 3 || ctx
->program
->chip_class
!= GFX6
);
3984 assert(vdata
.size() >= 1 && vdata
.size() <= 4);
3986 Builder
bld(ctx
->program
, ctx
->block
);
3987 aco_opcode op
= get_buffer_store_op(false, vdata
.bytes());
3988 const_offset
= resolve_excess_vmem_const_offset(bld
, voffset
, const_offset
);
3990 Operand voffset_op
= voffset
.id() ? Operand(as_vgpr(ctx
, voffset
)) : Operand(v1
);
3991 Operand soffset_op
= soffset
.id() ? Operand(soffset
) : Operand(0u);
3992 Builder::Result r
= bld
.mubuf(op
, Operand(descriptor
), voffset_op
, soffset_op
, Operand(vdata
), const_offset
,
3993 /* offen */ !voffset_op
.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3994 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc
);
3996 static_cast<MUBUF_instruction
*>(r
.instr
)->can_reorder
= allow_reorder
;
3999 void store_vmem_mubuf(isel_context
*ctx
, Temp src
, Temp descriptor
, Temp voffset
, Temp soffset
,
4000 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned write_mask
,
4001 bool allow_combining
= true, bool reorder
= true, bool slc
= false)
4003 Builder
bld(ctx
->program
, ctx
->block
);
4004 assert(elem_size_bytes
== 2 || elem_size_bytes
== 4 || elem_size_bytes
== 8);
4006 write_mask
= widen_mask(write_mask
, elem_size_bytes
);
4008 unsigned write_count
= 0;
4009 Temp write_datas
[32];
4010 unsigned offsets
[32];
4011 split_buffer_store(ctx
, NULL
, false, RegType::vgpr
, src
, write_mask
,
4012 allow_combining
? 16 : 4, &write_count
, write_datas
, offsets
);
4014 for (unsigned i
= 0; i
< write_count
; i
++) {
4015 unsigned const_offset
= offsets
[i
] + base_const_offset
;
4016 emit_single_mubuf_store(ctx
, descriptor
, voffset
, soffset
, write_datas
[i
], const_offset
, reorder
, slc
);
4020 void load_vmem_mubuf(isel_context
*ctx
, Temp dst
, Temp descriptor
, Temp voffset
, Temp soffset
,
4021 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned num_components
,
4022 unsigned stride
= 0u, bool allow_combining
= true, bool allow_reorder
= true)
4024 assert(elem_size_bytes
== 2 || elem_size_bytes
== 4 || elem_size_bytes
== 8);
4025 assert((num_components
* elem_size_bytes
) == dst
.bytes());
4026 assert(!!stride
!= allow_combining
);
4028 Builder
bld(ctx
->program
, ctx
->block
);
4030 LoadEmitInfo info
= {Operand(voffset
), dst
, num_components
, elem_size_bytes
, descriptor
};
4031 info
.component_stride
= allow_combining
? 0 : stride
;
4033 info
.swizzle_component_size
= allow_combining
? 0 : 4;
4034 info
.align_mul
= MIN2(elem_size_bytes
, 4);
4035 info
.align_offset
= 0;
4036 info
.soffset
= soffset
;
4037 info
.const_offset
= base_const_offset
;
4038 emit_mubuf_load(ctx
, bld
, &info
);
4041 std::pair
<Temp
, unsigned> offset_add_from_nir(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &base_offset
, nir_src
*off_src
, unsigned stride
= 1u)
4043 Builder
bld(ctx
->program
, ctx
->block
);
4044 Temp offset
= base_offset
.first
;
4045 unsigned const_offset
= base_offset
.second
;
4047 if (!nir_src_is_const(*off_src
)) {
4048 Temp indirect_offset_arg
= get_ssa_temp(ctx
, off_src
->ssa
);
4051 /* Calculate indirect offset with stride */
4052 if (likely(indirect_offset_arg
.regClass() == v1
))
4053 with_stride
= bld
.v_mul24_imm(bld
.def(v1
), indirect_offset_arg
, stride
);
4054 else if (indirect_offset_arg
.regClass() == s1
)
4055 with_stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), indirect_offset_arg
);
4057 unreachable("Unsupported register class of indirect offset");
4059 /* Add to the supplied base offset */
4060 if (offset
.id() == 0)
4061 offset
= with_stride
;
4062 else if (unlikely(offset
.regClass() == s1
&& with_stride
.regClass() == s1
))
4063 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), with_stride
, offset
);
4064 else if (offset
.size() == 1 && with_stride
.size() == 1)
4065 offset
= bld
.vadd32(bld
.def(v1
), with_stride
, offset
);
4067 unreachable("Unsupported register class of indirect offset");
4069 unsigned const_offset_arg
= nir_src_as_uint(*off_src
);
4070 const_offset
+= const_offset_arg
* stride
;
4073 return std::make_pair(offset
, const_offset
);
4076 std::pair
<Temp
, unsigned> offset_add(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &off1
, const std::pair
<Temp
, unsigned> &off2
)
4078 Builder
bld(ctx
->program
, ctx
->block
);
4081 if (off1
.first
.id() && off2
.first
.id()) {
4082 if (unlikely(off1
.first
.regClass() == s1
&& off2
.first
.regClass() == s1
))
4083 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), off1
.first
, off2
.first
);
4084 else if (off1
.first
.size() == 1 && off2
.first
.size() == 1)
4085 offset
= bld
.vadd32(bld
.def(v1
), off1
.first
, off2
.first
);
4087 unreachable("Unsupported register class of indirect offset");
4089 offset
= off1
.first
.id() ? off1
.first
: off2
.first
;
4092 return std::make_pair(offset
, off1
.second
+ off2
.second
);
4095 std::pair
<Temp
, unsigned> offset_mul(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &offs
, unsigned multiplier
)
4097 Builder
bld(ctx
->program
, ctx
->block
);
4098 unsigned const_offset
= offs
.second
* multiplier
;
4100 if (!offs
.first
.id())
4101 return std::make_pair(offs
.first
, const_offset
);
4103 Temp offset
= unlikely(offs
.first
.regClass() == s1
)
4104 ? bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(multiplier
), offs
.first
)
4105 : bld
.v_mul24_imm(bld
.def(v1
), offs
.first
, multiplier
);
4107 return std::make_pair(offset
, const_offset
);
4110 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
, unsigned component_stride
)
4112 Builder
bld(ctx
->program
, ctx
->block
);
4114 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4115 unsigned const_offset
= nir_intrinsic_base(instr
) * base_stride
;
4116 /* component is in bytes */
4117 const_offset
+= nir_intrinsic_component(instr
) * component_stride
;
4119 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4120 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4121 return offset_add_from_nir(ctx
, std::make_pair(Temp(), const_offset
), off_src
, 4u * base_stride
);
4124 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned stride
= 1u)
4126 return get_intrinsic_io_basic_offset(ctx
, instr
, stride
, stride
);
4129 Temp
get_tess_rel_patch_id(isel_context
*ctx
)
4131 Builder
bld(ctx
->program
, ctx
->block
);
4133 switch (ctx
->shader
->info
.stage
) {
4134 case MESA_SHADER_TESS_CTRL
:
4135 return bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffu
),
4136 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
));
4137 case MESA_SHADER_TESS_EVAL
:
4138 return get_arg(ctx
, ctx
->args
->tes_rel_patch_id
);
4140 unreachable("Unsupported stage in get_tess_rel_patch_id");
4144 std::pair
<Temp
, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4146 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4147 Builder
bld(ctx
->program
, ctx
->block
);
4149 uint32_t tcs_in_patch_stride
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 4;
4150 uint32_t tcs_in_vertex_stride
= ctx
->tcs_num_inputs
* 4;
4152 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
);
4154 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4155 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, tcs_in_vertex_stride
);
4157 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4158 Temp tcs_in_current_patch_offset
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, tcs_in_patch_stride
);
4159 offs
= offset_add(ctx
, offs
, std::make_pair(tcs_in_current_patch_offset
, 0));
4161 return offset_mul(ctx
, offs
, 4u);
4164 std::pair
<Temp
, unsigned> get_tcs_output_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, bool per_vertex
= false)
4166 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4167 Builder
bld(ctx
->program
, ctx
->block
);
4169 uint32_t input_patch_size
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 16;
4170 uint32_t output_vertex_size
= ctx
->tcs_num_outputs
* 16;
4171 uint32_t pervertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
4172 uint32_t output_patch_stride
= pervertex_output_patch_size
+ ctx
->tcs_num_patch_outputs
* 16;
4174 std::pair
<Temp
, unsigned> offs
= instr
4175 ? get_intrinsic_io_basic_offset(ctx
, instr
, 4u)
4176 : std::make_pair(Temp(), 0u);
4178 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4179 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, output_patch_stride
);
4184 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4185 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, output_vertex_size
);
4187 uint32_t output_patch0_offset
= (input_patch_size
* ctx
->tcs_num_patches
);
4188 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_offset
));
4190 uint32_t output_patch0_patch_data_offset
= (input_patch_size
* ctx
->tcs_num_patches
+ pervertex_output_patch_size
);
4191 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_patch_data_offset
));
4197 std::pair
<Temp
, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4199 Builder
bld(ctx
->program
, ctx
->block
);
4201 unsigned vertices_per_patch
= ctx
->shader
->info
.tess
.tcs_vertices_out
;
4202 unsigned attr_stride
= vertices_per_patch
* ctx
->tcs_num_patches
;
4204 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u);
4206 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4207 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, vertices_per_patch
* 16u);
4208 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, 0u));
4210 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4211 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, 16u);
4216 std::pair
<Temp
, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, unsigned const_base_offset
= 0u)
4218 Builder
bld(ctx
->program
, ctx
->block
);
4220 unsigned output_vertex_size
= ctx
->tcs_num_outputs
* 16;
4221 unsigned per_vertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
4222 unsigned per_patch_data_offset
= per_vertex_output_patch_size
* ctx
->tcs_num_patches
;
4223 unsigned attr_stride
= ctx
->tcs_num_patches
;
4225 std::pair
<Temp
, unsigned> offs
= instr
4226 ? get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u)
4227 : std::make_pair(Temp(), 0u);
4229 if (const_base_offset
)
4230 offs
.second
+= const_base_offset
* attr_stride
;
4232 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4233 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, 16u);
4234 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, per_patch_data_offset
));
4239 bool tcs_driver_location_matches_api_mask(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
, uint64_t mask
, bool *indirect
)
4241 assert(per_vertex
|| ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4246 unsigned drv_loc
= nir_intrinsic_base(instr
);
4247 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4249 if (!nir_src_is_const(*off_src
)) {
4255 uint64_t slot
= per_vertex
4256 ? ctx
->output_drv_loc_to_var_slot
[ctx
->shader
->info
.stage
][drv_loc
/ 4]
4257 : (ctx
->output_tcs_patch_drv_loc_to_var_slot
[drv_loc
/ 4] - VARYING_SLOT_PATCH0
);
4258 return (((uint64_t) 1) << slot
) & mask
;
4261 bool store_output_to_temps(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4263 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4264 unsigned component
= nir_intrinsic_component(instr
);
4265 unsigned idx
= nir_intrinsic_base(instr
) + component
;
4267 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
4268 if (off_instr
->type
!= nir_instr_type_load_const
)
4271 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4272 idx
+= nir_src_as_uint(instr
->src
[1]) * 4u;
4274 if (instr
->src
[0].ssa
->bit_size
== 64)
4275 write_mask
= widen_mask(write_mask
, 2);
4277 RegClass rc
= instr
->src
[0].ssa
->bit_size
== 16 ? v2b
: v1
;
4279 for (unsigned i
= 0; i
< 8; ++i
) {
4280 if (write_mask
& (1 << i
)) {
4281 ctx
->outputs
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
4282 ctx
->outputs
.temps
[idx
] = emit_extract_vector(ctx
, src
, i
, rc
);
4290 bool load_input_from_temps(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp dst
)
4292 /* Only TCS per-vertex inputs are supported by this function.
4293 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4295 if (ctx
->shader
->info
.stage
!= MESA_SHADER_TESS_CTRL
|| !ctx
->tcs_in_out_eq
)
4298 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4299 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4300 nir_instr
*vertex_index_instr
= vertex_index_src
->ssa
->parent_instr
;
4301 bool can_use_temps
= nir_src_is_const(*off_src
) &&
4302 vertex_index_instr
->type
== nir_instr_type_intrinsic
&&
4303 nir_instr_as_intrinsic(vertex_index_instr
)->intrinsic
== nir_intrinsic_load_invocation_id
;
4308 unsigned idx
= nir_intrinsic_base(instr
) + nir_intrinsic_component(instr
) + 4 * nir_src_as_uint(*off_src
);
4309 Temp
*src
= &ctx
->inputs
.temps
[idx
];
4310 create_vec_from_array(ctx
, src
, dst
.size(), dst
.regClass().type(), 4u, 0, dst
);
4315 void visit_store_ls_or_es_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4317 Builder
bld(ctx
->program
, ctx
->block
);
4319 if (ctx
->tcs_in_out_eq
&& store_output_to_temps(ctx
, instr
)) {
4320 /* When the TCS only reads this output directly and for the same vertices as its invocation id, it is unnecessary to store the VS output to LDS. */
4321 bool indirect_write
;
4322 bool temp_only_input
= tcs_driver_location_matches_api_mask(ctx
, instr
, true, ctx
->tcs_temp_only_inputs
, &indirect_write
);
4323 if (temp_only_input
&& !indirect_write
)
4327 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, 4u);
4328 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4329 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4330 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8u;
4332 if (ctx
->stage
== vertex_es
|| ctx
->stage
== tess_eval_es
) {
4333 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4334 Temp esgs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_VS
* 16u));
4335 Temp es2gs_offset
= get_arg(ctx
, ctx
->args
->es2gs_offset
);
4336 store_vmem_mubuf(ctx
, src
, esgs_ring
, offs
.first
, es2gs_offset
, offs
.second
, elem_size_bytes
, write_mask
, false, true, true);
4340 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
4341 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4342 unsigned itemsize
= ctx
->stage
== vertex_geometry_gs
4343 ? ctx
->program
->info
->vs
.es_info
.esgs_itemsize
4344 : ctx
->program
->info
->tes
.es_info
.esgs_itemsize
;
4345 Temp thread_id
= emit_mbcnt(ctx
, bld
.def(v1
));
4346 Temp wave_idx
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(4u << 16 | 24));
4347 Temp vertex_idx
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), thread_id
,
4348 bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_idx
), ctx
->program
->wave_size
));
4349 lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, itemsize
);
4350 } else if (ctx
->stage
== vertex_ls
|| ctx
->stage
== vertex_tess_control_hs
) {
4351 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4352 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4354 Temp vertex_idx
= get_arg(ctx
, ctx
->args
->rel_auto_id
);
4355 lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, ctx
->tcs_num_inputs
* 16u);
4357 unreachable("Invalid LS or ES stage");
4360 offs
= offset_add(ctx
, offs
, std::make_pair(lds_base
, 0u));
4361 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
4362 store_lds(ctx
, elem_size_bytes
, src
, write_mask
, offs
.first
, offs
.second
, lds_align
);
4366 bool tcs_output_is_tess_factor(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4371 unsigned off
= nir_intrinsic_base(instr
) * 4u;
4372 return off
== ctx
->tcs_tess_lvl_out_loc
||
4373 off
== ctx
->tcs_tess_lvl_in_loc
;
4377 bool tcs_output_is_read_by_tes(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4379 uint64_t mask
= per_vertex
4380 ? ctx
->program
->info
->tcs
.tes_inputs_read
4381 : ctx
->program
->info
->tcs
.tes_patch_inputs_read
;
4383 bool indirect_write
= false;
4384 bool output_read_by_tes
= tcs_driver_location_matches_api_mask(ctx
, instr
, per_vertex
, mask
, &indirect_write
);
4385 return indirect_write
|| output_read_by_tes
;
4388 bool tcs_output_is_read_by_tcs(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4390 uint64_t mask
= per_vertex
4391 ? ctx
->shader
->info
.outputs_read
4392 : ctx
->shader
->info
.patch_outputs_read
;
4394 bool indirect_write
= false;
4395 bool output_read
= tcs_driver_location_matches_api_mask(ctx
, instr
, per_vertex
, mask
, &indirect_write
);
4396 return indirect_write
|| output_read
;
4399 void visit_store_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4401 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
4402 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4404 Builder
bld(ctx
->program
, ctx
->block
);
4406 Temp store_val
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4407 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4408 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4410 bool is_tess_factor
= tcs_output_is_tess_factor(ctx
, instr
, per_vertex
);
4411 bool write_to_vmem
= !is_tess_factor
&& tcs_output_is_read_by_tes(ctx
, instr
, per_vertex
);
4412 bool write_to_lds
= is_tess_factor
|| tcs_output_is_read_by_tcs(ctx
, instr
, per_vertex
);
4414 if (write_to_vmem
) {
4415 std::pair
<Temp
, unsigned> vmem_offs
= per_vertex
4416 ? get_tcs_per_vertex_output_vmem_offset(ctx
, instr
)
4417 : get_tcs_per_patch_output_vmem_offset(ctx
, instr
);
4419 Temp hs_ring_tess_offchip
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
4420 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
4421 store_vmem_mubuf(ctx
, store_val
, hs_ring_tess_offchip
, vmem_offs
.first
, oc_lds
, vmem_offs
.second
, elem_size_bytes
, write_mask
, true, false);
4425 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
4426 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
4427 store_lds(ctx
, elem_size_bytes
, store_val
, write_mask
, lds_offs
.first
, lds_offs
.second
, lds_align
);
4431 void visit_load_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4433 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
4434 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4436 Builder
bld(ctx
->program
, ctx
->block
);
4438 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4439 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
4440 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
4441 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4443 load_lds(ctx
, elem_size_bytes
, dst
, lds_offs
.first
, lds_offs
.second
, lds_align
);
4446 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4448 if (ctx
->stage
== vertex_vs
||
4449 ctx
->stage
== tess_eval_vs
||
4450 ctx
->stage
== fragment_fs
||
4451 ctx
->stage
== ngg_vertex_gs
||
4452 ctx
->stage
== ngg_tess_eval_gs
||
4453 ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
4454 bool stored_to_temps
= store_output_to_temps(ctx
, instr
);
4455 if (!stored_to_temps
) {
4456 fprintf(stderr
, "Unimplemented output offset instruction:\n");
4457 nir_print_instr(instr
->src
[1].ssa
->parent_instr
, stderr
);
4458 fprintf(stderr
, "\n");
4461 } else if (ctx
->stage
== vertex_es
||
4462 ctx
->stage
== vertex_ls
||
4463 ctx
->stage
== tess_eval_es
||
4464 (ctx
->stage
== vertex_tess_control_hs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) ||
4465 (ctx
->stage
== vertex_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) ||
4466 (ctx
->stage
== tess_eval_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
)) {
4467 visit_store_ls_or_es_output(ctx
, instr
);
4468 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
4469 visit_store_tcs_output(ctx
, instr
, false);
4471 unreachable("Shader stage not implemented");
4475 void visit_load_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4477 visit_load_tcs_output(ctx
, instr
, false);
4480 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
4482 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
4483 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
4485 Builder
bld(ctx
->program
, ctx
->block
);
4487 if (dst
.regClass() == v2b
) {
4488 if (ctx
->program
->has_16bank_lds
) {
4489 assert(ctx
->options
->chip_class
<= GFX8
);
4490 Builder::Result interp_p1
=
4491 bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
),
4492 Operand(2u) /* P0 */, bld
.m0(prim_mask
), idx
, component
);
4493 interp_p1
= bld
.vintrp(aco_opcode::v_interp_p1lv_f16
, bld
.def(v2b
),
4494 coord1
, bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4495 bld
.vintrp(aco_opcode::v_interp_p2_legacy_f16
, Definition(dst
), coord2
,
4496 bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4498 aco_opcode interp_p2_op
= aco_opcode::v_interp_p2_f16
;
4500 if (ctx
->options
->chip_class
== GFX8
)
4501 interp_p2_op
= aco_opcode::v_interp_p2_legacy_f16
;
4503 Builder::Result interp_p1
=
4504 bld
.vintrp(aco_opcode::v_interp_p1ll_f16
, bld
.def(v1
),
4505 coord1
, bld
.m0(prim_mask
), idx
, component
);
4506 bld
.vintrp(interp_p2_op
, Definition(dst
), coord2
, bld
.m0(prim_mask
),
4507 interp_p1
, idx
, component
);
4510 Builder::Result interp_p1
=
4511 bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
,
4512 bld
.m0(prim_mask
), idx
, component
);
4514 if (ctx
->program
->has_16bank_lds
)
4515 interp_p1
.instr
->operands
[0].setLateKill(true);
4517 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
,
4518 bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4522 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
4524 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
4525 for (unsigned i
= 0; i
< num_components
; i
++)
4526 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
4527 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
4528 assert(num_components
== 4);
4529 Builder
bld(ctx
->program
, ctx
->block
);
4530 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
4533 for (Operand
& op
: vec
->operands
)
4534 op
= op
.isUndefined() ? Operand(0u) : op
;
4536 vec
->definitions
[0] = Definition(dst
);
4537 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4538 emit_split_vector(ctx
, dst
, num_components
);
4542 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4544 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4545 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4546 unsigned idx
= nir_intrinsic_base(instr
);
4547 unsigned component
= nir_intrinsic_component(instr
);
4548 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
4550 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
4552 assert(offset
->u32
== 0);
4554 /* the lower 15bit of the prim_mask contain the offset into LDS
4555 * while the upper bits contain the number of prims */
4556 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4557 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
4558 Builder
bld(ctx
->program
, ctx
->block
);
4559 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
4560 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
4561 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
4562 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
4563 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
4566 if (instr
->dest
.ssa
.num_components
== 1) {
4567 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
4569 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
4570 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
4572 Temp tmp
= {ctx
->program
->allocateId(), v1
};
4573 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
4574 vec
->operands
[i
] = Operand(tmp
);
4576 vec
->definitions
[0] = Definition(dst
);
4577 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4581 bool check_vertex_fetch_size(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
4582 unsigned offset
, unsigned stride
, unsigned channels
)
4584 unsigned vertex_byte_size
= vtx_info
->chan_byte_size
* channels
;
4585 if (vtx_info
->chan_byte_size
!= 4 && channels
== 3)
4587 return (ctx
->options
->chip_class
!= GFX6
&& ctx
->options
->chip_class
!= GFX10
) ||
4588 (offset
% vertex_byte_size
== 0 && stride
% vertex_byte_size
== 0);
4591 uint8_t get_fetch_data_format(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
4592 unsigned offset
, unsigned stride
, unsigned *channels
)
4594 if (!vtx_info
->chan_byte_size
) {
4595 *channels
= vtx_info
->num_channels
;
4596 return vtx_info
->chan_format
;
4599 unsigned num_channels
= *channels
;
4600 if (!check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, *channels
)) {
4601 unsigned new_channels
= num_channels
+ 1;
4602 /* first, assume more loads is worse and try using a larger data format */
4603 while (new_channels
<= 4 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
)) {
4605 /* don't make the attribute potentially out-of-bounds */
4606 if (offset
+ new_channels
* vtx_info
->chan_byte_size
> stride
)
4610 if (new_channels
== 5) {
4611 /* then try decreasing load size (at the cost of more loads) */
4612 new_channels
= *channels
;
4613 while (new_channels
> 1 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
))
4617 if (new_channels
< *channels
)
4618 *channels
= new_channels
;
4619 num_channels
= new_channels
;
4622 switch (vtx_info
->chan_format
) {
4623 case V_008F0C_BUF_DATA_FORMAT_8
:
4624 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8
, V_008F0C_BUF_DATA_FORMAT_8_8
,
4625 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_8_8_8_8
}[num_channels
- 1];
4626 case V_008F0C_BUF_DATA_FORMAT_16
:
4627 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16
, V_008F0C_BUF_DATA_FORMAT_16_16
,
4628 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_16_16_16_16
}[num_channels
- 1];
4629 case V_008F0C_BUF_DATA_FORMAT_32
:
4630 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32
, V_008F0C_BUF_DATA_FORMAT_32_32
,
4631 V_008F0C_BUF_DATA_FORMAT_32_32_32
, V_008F0C_BUF_DATA_FORMAT_32_32_32_32
}[num_channels
- 1];
4633 unreachable("shouldn't reach here");
4634 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
4637 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4638 * so we may need to fix it up. */
4639 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
4641 Builder
bld(ctx
->program
, ctx
->block
);
4643 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
4644 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
4646 /* For the integer-like cases, do a natural sign extension.
4648 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4649 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4652 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
4653 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
4655 /* Convert back to the right type. */
4656 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
4657 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
4658 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0xbf800000u
), alpha
);
4659 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
4660 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
4661 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
4667 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4669 Builder
bld(ctx
->program
, ctx
->block
);
4670 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4671 if (ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) {
4673 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
4674 if (off_instr
->type
!= nir_instr_type_load_const
) {
4675 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
4676 nir_print_instr(off_instr
, stderr
);
4677 fprintf(stderr
, "\n");
4679 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
4681 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
4683 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
4684 unsigned component
= nir_intrinsic_component(instr
);
4685 unsigned bitsize
= instr
->dest
.ssa
.bit_size
;
4686 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
4687 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
4688 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
4689 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
4691 unsigned dfmt
= attrib_format
& 0xf;
4692 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
4693 const struct ac_data_format_info
*vtx_info
= ac_get_data_format_info(dfmt
);
4695 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
4696 unsigned num_channels
= MIN2(util_last_bit(mask
), vtx_info
->num_channels
);
4697 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
4698 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
4700 num_channels
= MAX2(num_channels
, 3);
4702 Operand off
= bld
.copy(bld
.def(s1
), Operand(attrib_binding
* 16u));
4703 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, off
);
4706 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
4707 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
4708 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
4710 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
4712 Temp divided
= bld
.tmp(v1
);
4713 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
4714 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
4716 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
4719 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
4722 index
= bld
.vadd32(bld
.def(v1
),
4723 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
4724 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
4727 Temp channels
[num_channels
];
4728 unsigned channel_start
= 0;
4729 bool direct_fetch
= false;
4731 /* skip unused channels at the start */
4732 if (vtx_info
->chan_byte_size
&& !post_shuffle
) {
4733 channel_start
= ffs(mask
) - 1;
4734 for (unsigned i
= 0; i
< channel_start
; i
++)
4735 channels
[i
] = Temp(0, s1
);
4736 } else if (vtx_info
->chan_byte_size
&& post_shuffle
&& !(mask
& 0x8)) {
4737 num_channels
= 3 - (ffs(mask
) - 1);
4741 while (channel_start
< num_channels
) {
4742 unsigned fetch_component
= num_channels
- channel_start
;
4743 unsigned fetch_offset
= attrib_offset
+ channel_start
* vtx_info
->chan_byte_size
;
4744 bool expanded
= false;
4746 /* use MUBUF when possible to avoid possible alignment issues */
4747 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4748 bool use_mubuf
= (nfmt
== V_008F0C_BUF_NUM_FORMAT_FLOAT
||
4749 nfmt
== V_008F0C_BUF_NUM_FORMAT_UINT
||
4750 nfmt
== V_008F0C_BUF_NUM_FORMAT_SINT
) &&
4751 vtx_info
->chan_byte_size
== 4;
4752 unsigned fetch_dfmt
= V_008F0C_BUF_DATA_FORMAT_INVALID
;
4754 fetch_dfmt
= get_fetch_data_format(ctx
, vtx_info
, fetch_offset
, attrib_stride
, &fetch_component
);
4756 if (fetch_component
== 3 && ctx
->options
->chip_class
== GFX6
) {
4757 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4758 fetch_component
= 4;
4763 unsigned fetch_bytes
= fetch_component
* bitsize
/ 8;
4765 Temp fetch_index
= index
;
4766 if (attrib_stride
!= 0 && fetch_offset
> attrib_stride
) {
4767 fetch_index
= bld
.vadd32(bld
.def(v1
), Operand(fetch_offset
/ attrib_stride
), fetch_index
);
4768 fetch_offset
= fetch_offset
% attrib_stride
;
4771 Operand
soffset(0u);
4772 if (fetch_offset
>= 4096) {
4773 soffset
= bld
.copy(bld
.def(s1
), Operand(fetch_offset
/ 4096 * 4096));
4774 fetch_offset
%= 4096;
4778 switch (fetch_bytes
) {
4780 assert(!use_mubuf
&& bitsize
== 16);
4781 opcode
= aco_opcode::tbuffer_load_format_d16_x
;
4784 if (bitsize
== 16) {
4786 opcode
= aco_opcode::tbuffer_load_format_d16_xy
;
4788 opcode
= use_mubuf
? aco_opcode::buffer_load_dword
: aco_opcode::tbuffer_load_format_x
;
4792 assert(!use_mubuf
&& bitsize
== 16);
4793 opcode
= aco_opcode::tbuffer_load_format_d16_xyz
;
4796 if (bitsize
== 16) {
4798 opcode
= aco_opcode::tbuffer_load_format_d16_xyzw
;
4800 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx2
: aco_opcode::tbuffer_load_format_xy
;
4804 assert(ctx
->options
->chip_class
>= GFX7
||
4805 (!use_mubuf
&& ctx
->options
->chip_class
== GFX6
));
4806 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx3
: aco_opcode::tbuffer_load_format_xyz
;
4809 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx4
: aco_opcode::tbuffer_load_format_xyzw
;
4812 unreachable("Unimplemented load_input vector size");
4816 if (channel_start
== 0 && fetch_bytes
== dst
.bytes() && !post_shuffle
&&
4817 !expanded
&& (alpha_adjust
== RADV_ALPHA_ADJUST_NONE
||
4818 num_channels
<= 3)) {
4819 direct_fetch
= true;
4822 fetch_dst
= bld
.tmp(RegClass::get(RegType::vgpr
, fetch_bytes
));
4826 Instruction
*mubuf
= bld
.mubuf(opcode
,
4827 Definition(fetch_dst
), list
, fetch_index
, soffset
,
4828 fetch_offset
, false, true).instr
;
4829 static_cast<MUBUF_instruction
*>(mubuf
)->can_reorder
= true;
4831 Instruction
*mtbuf
= bld
.mtbuf(opcode
,
4832 Definition(fetch_dst
), list
, fetch_index
, soffset
,
4833 fetch_dfmt
, nfmt
, fetch_offset
, false, true).instr
;
4834 static_cast<MTBUF_instruction
*>(mtbuf
)->can_reorder
= true;
4837 emit_split_vector(ctx
, fetch_dst
, fetch_dst
.size());
4839 if (fetch_component
== 1) {
4840 channels
[channel_start
] = fetch_dst
;
4842 for (unsigned i
= 0; i
< MIN2(fetch_component
, num_channels
- channel_start
); i
++)
4843 channels
[channel_start
+ i
] = emit_extract_vector(ctx
, fetch_dst
, i
,
4844 bitsize
== 16 ? v2b
: v1
);
4847 channel_start
+= fetch_component
;
4850 if (!direct_fetch
) {
4851 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
4852 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
4854 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
4855 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
4856 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
4858 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
4859 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4860 unsigned num_temp
= 0;
4861 for (unsigned i
= 0; i
< dst
.size(); i
++) {
4862 unsigned idx
= i
+ component
;
4863 if (swizzle
[idx
] < num_channels
&& channels
[swizzle
[idx
]].id()) {
4864 Temp channel
= channels
[swizzle
[idx
]];
4865 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
)
4866 channel
= adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, channel
);
4867 vec
->operands
[i
] = Operand(channel
);
4871 } else if (is_float
&& idx
== 3) {
4872 vec
->operands
[i
] = Operand(0x3f800000u
);
4873 } else if (!is_float
&& idx
== 3) {
4874 vec
->operands
[i
] = Operand(1u);
4876 vec
->operands
[i
] = Operand(0u);
4879 vec
->definitions
[0] = Definition(dst
);
4880 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4881 emit_split_vector(ctx
, dst
, dst
.size());
4883 if (num_temp
== dst
.size())
4884 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4886 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
4887 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
4888 nir_instr
*off_instr
= instr
->src
[offset_idx
].ssa
->parent_instr
;
4889 if (off_instr
->type
!= nir_instr_type_load_const
||
4890 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
4891 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
4892 nir_print_instr(off_instr
, stderr
);
4893 fprintf(stderr
, "\n");
4896 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
4897 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
4899 assert(offset
->u32
== 0);
4901 /* the lower 15bit of the prim_mask contain the offset into LDS
4902 * while the upper bits contain the number of prims */
4903 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[offset_idx
].ssa
);
4904 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
4905 Builder
bld(ctx
->program
, ctx
->block
);
4906 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
4907 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
4908 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
4909 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
4910 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
4913 unsigned idx
= nir_intrinsic_base(instr
);
4914 unsigned component
= nir_intrinsic_component(instr
);
4915 unsigned vertex_id
= 2; /* P0 */
4917 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
4918 nir_const_value
* src0
= nir_src_as_const_value(instr
->src
[0]);
4919 switch (src0
->u32
) {
4921 vertex_id
= 2; /* P0 */
4924 vertex_id
= 0; /* P10 */
4927 vertex_id
= 1; /* P20 */
4930 unreachable("invalid vertex index");
4934 if (dst
.size() == 1) {
4935 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
);
4937 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
4938 for (unsigned i
= 0; i
< dst
.size(); i
++)
4939 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
+ i
);
4940 vec
->definitions
[0] = Definition(dst
);
4941 bld
.insert(std::move(vec
));
4944 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4945 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
4946 Temp soffset
= get_arg(ctx
, ctx
->args
->oc_lds
);
4947 std::pair
<Temp
, unsigned> offs
= get_tcs_per_patch_output_vmem_offset(ctx
, instr
);
4948 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8u;
4950 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, soffset
, offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
);
4952 unreachable("Shader stage not implemented");
4956 std::pair
<Temp
, unsigned> get_gs_per_vertex_input_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
= 1u)
4958 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4960 Builder
bld(ctx
->program
, ctx
->block
);
4961 nir_src
*vertex_src
= nir_get_io_vertex_index_src(instr
);
4964 if (!nir_src_is_const(*vertex_src
)) {
4965 /* better code could be created, but this case probably doesn't happen
4966 * much in practice */
4967 Temp indirect_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, vertex_src
->ssa
));
4968 for (unsigned i
= 0; i
< ctx
->shader
->info
.gs
.vertices_in
; i
++) {
4971 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
4972 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
/ 2u * 2u]);
4974 elem
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), elem
);
4976 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
]);
4979 if (vertex_offset
.id()) {
4980 Temp cond
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
4981 Operand(i
), indirect_vertex
);
4982 vertex_offset
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), vertex_offset
, elem
, cond
);
4984 vertex_offset
= elem
;
4988 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
)
4989 vertex_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
), vertex_offset
);
4991 unsigned vertex
= nir_src_as_uint(*vertex_src
);
4992 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
)
4993 vertex_offset
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
4994 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
/ 2u * 2u]),
4995 Operand((vertex
% 2u) * 16u), Operand(16u));
4997 vertex_offset
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
]);
5000 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, base_stride
);
5001 offs
= offset_add(ctx
, offs
, std::make_pair(vertex_offset
, 0u));
5002 return offset_mul(ctx
, offs
, 4u);
5005 void visit_load_gs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5007 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
5009 Builder
bld(ctx
->program
, ctx
->block
);
5010 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5011 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5013 if (ctx
->stage
== geometry_gs
) {
5014 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
, ctx
->program
->wave_size
);
5015 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_GS
* 16u));
5016 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, Temp(), offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
, 4u * ctx
->program
->wave_size
, false, true);
5017 } else if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
5018 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
);
5019 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
5020 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
5022 unreachable("Unsupported GS stage.");
5026 void visit_load_tcs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5028 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5030 Builder
bld(ctx
->program
, ctx
->block
);
5031 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5033 if (load_input_from_temps(ctx
, instr
, dst
))
5036 std::pair
<Temp
, unsigned> offs
= get_tcs_per_vertex_input_lds_offset(ctx
, instr
);
5037 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5038 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
5040 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
5043 void visit_load_tes_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5045 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
5047 Builder
bld(ctx
->program
, ctx
->block
);
5049 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
5050 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
5051 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5053 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5054 std::pair
<Temp
, unsigned> offs
= get_tcs_per_vertex_output_vmem_offset(ctx
, instr
);
5056 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, oc_lds
, offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
, 0u, true, true);
5059 void visit_load_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5061 switch (ctx
->shader
->info
.stage
) {
5062 case MESA_SHADER_GEOMETRY
:
5063 visit_load_gs_per_vertex_input(ctx
, instr
);
5065 case MESA_SHADER_TESS_CTRL
:
5066 visit_load_tcs_per_vertex_input(ctx
, instr
);
5068 case MESA_SHADER_TESS_EVAL
:
5069 visit_load_tes_per_vertex_input(ctx
, instr
);
5072 unreachable("Unimplemented shader stage");
5076 void visit_load_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5078 visit_load_tcs_output(ctx
, instr
, true);
5081 void visit_store_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5083 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
5084 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5086 visit_store_tcs_output(ctx
, instr
, true);
5089 void visit_load_tess_coord(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5091 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
5093 Builder
bld(ctx
->program
, ctx
->block
);
5094 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5096 Operand
tes_u(get_arg(ctx
, ctx
->args
->tes_u
));
5097 Operand
tes_v(get_arg(ctx
, ctx
->args
->tes_v
));
5100 if (ctx
->shader
->info
.tess
.primitive_mode
== GL_TRIANGLES
) {
5101 Temp tmp
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), tes_u
, tes_v
);
5102 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0x3f800000u
/* 1.0f */), tmp
);
5103 tes_w
= Operand(tmp
);
5106 Temp tess_coord
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tes_u
, tes_v
, tes_w
);
5107 emit_split_vector(ctx
, tess_coord
, 3);
5110 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
5112 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
5113 Builder
bld(ctx
->program
, ctx
->block
);
5114 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
5115 Operand off
= bld
.copy(bld
.def(s1
), Operand(desc_set
<< 2));
5116 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, off
);//, false, false, false);
5119 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
5123 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5125 Builder
bld(ctx
->program
, ctx
->block
);
5126 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5127 if (!nir_dest_is_divergent(instr
->dest
))
5128 index
= bld
.as_uniform(index
);
5129 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
5130 unsigned binding
= nir_intrinsic_binding(instr
);
5133 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
5134 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
5135 unsigned offset
= layout
->binding
[binding
].offset
;
5137 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
5138 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
5139 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
5140 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
5141 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
5144 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
5145 stride
= layout
->binding
[binding
].size
;
5148 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
5149 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
5151 if (nir_const_index
) {
5152 const_index
= const_index
* stride
;
5153 } else if (index
.type() == RegType::vgpr
) {
5154 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
5155 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
5157 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
5161 if (nir_const_index
) {
5162 const_index
= const_index
+ offset
;
5163 } else if (index
.type() == RegType::vgpr
) {
5164 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
5166 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
5170 if (nir_const_index
&& const_index
== 0) {
5172 } else if (index
.type() == RegType::vgpr
) {
5173 index
= bld
.vadd32(bld
.def(v1
),
5174 nir_const_index
? Operand(const_index
) : Operand(index
),
5177 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
5178 nir_const_index
? Operand(const_index
) : Operand(index
),
5182 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
5185 void load_buffer(isel_context
*ctx
, unsigned num_components
, unsigned component_size
,
5186 Temp dst
, Temp rsrc
, Temp offset
, unsigned align_mul
, unsigned align_offset
,
5187 bool glc
=false, bool readonly
=true, bool allow_smem
=true)
5189 Builder
bld(ctx
->program
, ctx
->block
);
5191 bool use_smem
= dst
.type() != RegType::vgpr
&& (!glc
|| ctx
->options
->chip_class
>= GFX8
) && allow_smem
;
5193 offset
= bld
.as_uniform(offset
);
5195 LoadEmitInfo info
= {Operand(offset
), dst
, num_components
, component_size
, rsrc
};
5197 info
.barrier
= readonly
? barrier_none
: barrier_buffer
;
5198 info
.can_reorder
= readonly
;
5199 info
.align_mul
= align_mul
;
5200 info
.align_offset
= align_offset
;
5202 emit_smem_load(ctx
, bld
, &info
);
5204 emit_mubuf_load(ctx
, bld
, &info
);
5207 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5209 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5210 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5212 Builder
bld(ctx
->program
, ctx
->block
);
5214 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
5215 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
5216 unsigned binding
= nir_intrinsic_binding(idx_instr
);
5217 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
5219 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
5220 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5221 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5222 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5223 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
5224 if (ctx
->options
->chip_class
>= GFX10
) {
5225 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5226 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5227 S_008F0C_RESOURCE_LEVEL(1);
5229 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5230 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5232 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
5233 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
5234 Operand(0xFFFFFFFFu
),
5235 Operand(desc_type
));
5236 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5237 rsrc
, upper_dwords
);
5239 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
5240 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
5242 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
5243 load_buffer(ctx
, instr
->num_components
, size
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
),
5244 nir_intrinsic_align_mul(instr
), nir_intrinsic_align_offset(instr
));
5247 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5249 Builder
bld(ctx
->program
, ctx
->block
);
5250 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5251 unsigned offset
= nir_intrinsic_base(instr
);
5252 unsigned count
= instr
->dest
.ssa
.num_components
;
5253 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
5255 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
5256 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
5257 start
-= ctx
->args
->ac
.base_inline_push_consts
;
5258 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
5259 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
5260 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5261 for (unsigned i
= 0; i
< count
; ++i
) {
5262 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
5263 vec
->operands
[i
] = Operand
{elems
[i
]};
5265 vec
->definitions
[0] = Definition(dst
);
5266 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5267 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5272 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5273 if (offset
!= 0) // TODO check if index != 0 as well
5274 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
5275 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
5278 bool aligned
= true;
5280 if (instr
->dest
.ssa
.bit_size
== 8) {
5281 aligned
= index_cv
&& (offset
+ index_cv
->u32
) % 4 == 0;
5282 bool fits_in_dword
= count
== 1 || (index_cv
&& ((offset
+ index_cv
->u32
) % 4 + count
) <= 4);
5284 vec
= fits_in_dword
? bld
.tmp(s1
) : bld
.tmp(s2
);
5285 } else if (instr
->dest
.ssa
.bit_size
== 16) {
5286 aligned
= index_cv
&& (offset
+ index_cv
->u32
) % 4 == 0;
5288 vec
= count
== 4 ? bld
.tmp(s4
) : count
> 1 ? bld
.tmp(s2
) : bld
.tmp(s1
);
5293 switch (vec
.size()) {
5295 op
= aco_opcode::s_load_dword
;
5298 op
= aco_opcode::s_load_dwordx2
;
5304 op
= aco_opcode::s_load_dwordx4
;
5310 op
= aco_opcode::s_load_dwordx8
;
5313 unreachable("unimplemented or forbidden load_push_constant.");
5316 bld
.smem(op
, Definition(vec
), ptr
, index
);
5319 Operand byte_offset
= index_cv
? Operand((offset
+ index_cv
->u32
) % 4) : Operand(index
);
5320 byte_align_scalar(ctx
, vec
, byte_offset
, dst
);
5325 emit_split_vector(ctx
, vec
, 4);
5326 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
5327 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5328 emit_extract_vector(ctx
, vec
, 0, rc
),
5329 emit_extract_vector(ctx
, vec
, 1, rc
),
5330 emit_extract_vector(ctx
, vec
, 2, rc
));
5333 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
5336 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5338 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5340 Builder
bld(ctx
->program
, ctx
->block
);
5342 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5343 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5344 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5345 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
5346 if (ctx
->options
->chip_class
>= GFX10
) {
5347 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5348 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5349 S_008F0C_RESOURCE_LEVEL(1);
5351 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5352 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5355 unsigned base
= nir_intrinsic_base(instr
);
5356 unsigned range
= nir_intrinsic_range(instr
);
5358 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5359 if (base
&& offset
.type() == RegType::sgpr
)
5360 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
5361 else if (base
&& offset
.type() == RegType::vgpr
)
5362 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
5364 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5365 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
5366 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
5367 Operand(desc_type
));
5368 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
5369 // TODO: get alignment information for subdword constants
5370 load_buffer(ctx
, instr
->num_components
, size
, dst
, rsrc
, offset
, size
, 0);
5373 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5375 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
5376 ctx
->cf_info
.exec_potentially_empty_discard
= true;
5378 ctx
->program
->needs_exact
= true;
5380 // TODO: optimize uniform conditions
5381 Builder
bld(ctx
->program
, ctx
->block
);
5382 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5383 assert(src
.regClass() == bld
.lm
);
5384 src
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5385 bld
.pseudo(aco_opcode::p_discard_if
, src
);
5386 ctx
->block
->kind
|= block_kind_uses_discard_if
;
5390 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
5392 Builder
bld(ctx
->program
, ctx
->block
);
5394 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
5395 ctx
->cf_info
.exec_potentially_empty_discard
= true;
5397 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
5398 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
5400 if (ctx
->block
->loop_nest_depth
&&
5401 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
5402 /* we handle discards the same way as jump instructions */
5403 append_logical_end(ctx
->block
);
5405 /* in loops, discard behaves like break */
5406 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
5407 ctx
->block
->kind
|= block_kind_discard
;
5410 /* uniform discard - loop ends here */
5411 assert(nir_instr_is_last(&instr
->instr
));
5412 ctx
->block
->kind
|= block_kind_uniform
;
5413 ctx
->cf_info
.has_branch
= true;
5414 bld
.branch(aco_opcode::p_branch
);
5415 add_linear_edge(ctx
->block
->index
, linear_target
);
5419 /* we add a break right behind the discard() instructions */
5420 ctx
->block
->kind
|= block_kind_break
;
5421 unsigned idx
= ctx
->block
->index
;
5423 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
5424 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = idx
;
5426 /* remove critical edges from linear CFG */
5427 bld
.branch(aco_opcode::p_branch
);
5428 Block
* break_block
= ctx
->program
->create_and_insert_block();
5429 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
5430 break_block
->kind
|= block_kind_uniform
;
5431 add_linear_edge(idx
, break_block
);
5432 add_linear_edge(break_block
->index
, linear_target
);
5433 bld
.reset(break_block
);
5434 bld
.branch(aco_opcode::p_branch
);
5436 Block
* continue_block
= ctx
->program
->create_and_insert_block();
5437 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
5438 add_linear_edge(idx
, continue_block
);
5439 append_logical_start(continue_block
);
5440 ctx
->block
= continue_block
;
5445 /* it can currently happen that NIR doesn't remove the unreachable code */
5446 if (!nir_instr_is_last(&instr
->instr
)) {
5447 ctx
->program
->needs_exact
= true;
5448 /* save exec somewhere temporarily so that it doesn't get
5449 * overwritten before the discard from outer exec masks */
5450 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, bld
.lm
));
5451 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
5452 ctx
->block
->kind
|= block_kind_uses_discard_if
;
5456 /* This condition is incorrect for uniformly branched discards in a loop
5457 * predicated by a divergent condition, but the above code catches that case
5458 * and the discard would end up turning into a discard_if.
5468 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
5469 /* program just ends here */
5470 ctx
->block
->kind
|= block_kind_uniform
;
5471 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
5472 0 /* enabled mask */, 9 /* dest */,
5473 false /* compressed */, true/* done */, true /* valid mask */);
5474 bld
.sopp(aco_opcode::s_endpgm
);
5475 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5477 ctx
->block
->kind
|= block_kind_discard
;
5478 /* branch and linear edge is added by visit_if() */
5482 enum aco_descriptor_type
{
5493 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
5494 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
5496 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
5497 return dim
== ac_image_cube
||
5498 dim
== ac_image_1darray
||
5499 dim
== ac_image_2darray
||
5500 dim
== ac_image_2darraymsaa
;
5503 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
5504 enum aco_descriptor_type desc_type
,
5505 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
5507 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5508 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5509 if (it != ctx->tex_desc.end())
5512 Temp index
= Temp();
5513 bool index_set
= false;
5514 unsigned constant_index
= 0;
5515 unsigned descriptor_set
;
5516 unsigned base_index
;
5517 Builder
bld(ctx
->program
, ctx
->block
);
5520 assert(tex_instr
&& !image
);
5522 base_index
= tex_instr
->sampler_index
;
5524 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
5525 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
5529 assert(deref_instr
->deref_type
== nir_deref_type_array
);
5530 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
5532 constant_index
+= array_size
* const_value
->u32
;
5534 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
5535 if (indirect
.type() == RegType::vgpr
)
5536 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
5538 if (array_size
!= 1)
5539 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
5545 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
5549 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
5551 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
5552 base_index
= deref_instr
->var
->data
.binding
;
5555 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
5556 list
= convert_pointer_to_64_bit(ctx
, list
);
5558 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
5559 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
5560 unsigned offset
= binding
->offset
;
5561 unsigned stride
= binding
->size
;
5565 assert(base_index
< layout
->binding_count
);
5567 switch (desc_type
) {
5568 case ACO_DESC_IMAGE
:
5570 opcode
= aco_opcode::s_load_dwordx8
;
5572 case ACO_DESC_FMASK
:
5574 opcode
= aco_opcode::s_load_dwordx8
;
5577 case ACO_DESC_SAMPLER
:
5579 opcode
= aco_opcode::s_load_dwordx4
;
5580 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
5581 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
5583 case ACO_DESC_BUFFER
:
5585 opcode
= aco_opcode::s_load_dwordx4
;
5587 case ACO_DESC_PLANE_0
:
5588 case ACO_DESC_PLANE_1
:
5590 opcode
= aco_opcode::s_load_dwordx8
;
5591 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
5593 case ACO_DESC_PLANE_2
:
5595 opcode
= aco_opcode::s_load_dwordx4
;
5599 unreachable("invalid desc_type\n");
5602 offset
+= constant_index
* stride
;
5604 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
5605 (!index_set
|| binding
->immutable_samplers_equal
)) {
5606 if (binding
->immutable_samplers_equal
)
5609 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
5610 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5611 Operand(samplers
[constant_index
* 4 + 0]),
5612 Operand(samplers
[constant_index
* 4 + 1]),
5613 Operand(samplers
[constant_index
* 4 + 2]),
5614 Operand(samplers
[constant_index
* 4 + 3]));
5619 off
= bld
.copy(bld
.def(s1
), Operand(offset
));
5621 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
5622 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
5625 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
5627 if (desc_type
== ACO_DESC_PLANE_2
) {
5629 for (unsigned i
= 0; i
< 8; i
++)
5630 components
[i
] = bld
.tmp(s1
);
5631 bld
.pseudo(aco_opcode::p_split_vector
,
5632 Definition(components
[0]),
5633 Definition(components
[1]),
5634 Definition(components
[2]),
5635 Definition(components
[3]),
5638 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
5639 bld
.pseudo(aco_opcode::p_split_vector
,
5640 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
5641 Definition(components
[4]),
5642 Definition(components
[5]),
5643 Definition(components
[6]),
5644 Definition(components
[7]),
5647 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
5648 components
[0], components
[1], components
[2], components
[3],
5649 components
[4], components
[5], components
[6], components
[7]);
5655 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
5658 case GLSL_SAMPLER_DIM_BUF
:
5660 case GLSL_SAMPLER_DIM_1D
:
5661 return array
? 2 : 1;
5662 case GLSL_SAMPLER_DIM_2D
:
5663 return array
? 3 : 2;
5664 case GLSL_SAMPLER_DIM_MS
:
5665 return array
? 4 : 3;
5666 case GLSL_SAMPLER_DIM_3D
:
5667 case GLSL_SAMPLER_DIM_CUBE
:
5669 case GLSL_SAMPLER_DIM_RECT
:
5670 case GLSL_SAMPLER_DIM_SUBPASS
:
5672 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
5681 /* Adjust the sample index according to FMASK.
5683 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5684 * which is the identity mapping. Each nibble says which physical sample
5685 * should be fetched to get that sample.
5687 * For example, 0x11111100 means there are only 2 samples stored and
5688 * the second sample covers 3/4 of the pixel. When reading samples 0
5689 * and 1, return physical sample 0 (determined by the first two 0s
5690 * in FMASK), otherwise return physical sample 1.
5692 * The sample index should be adjusted as follows:
5693 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5695 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, std::vector
<Temp
>& coords
, Operand sample_index
, Temp fmask_desc_ptr
)
5697 Builder
bld(ctx
->program
, ctx
->block
);
5698 Temp fmask
= bld
.tmp(v1
);
5699 unsigned dim
= ctx
->options
->chip_class
>= GFX10
5700 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
5703 Temp coord
= da
? bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), coords
[0], coords
[1], coords
[2]) :
5704 bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), coords
[0], coords
[1]);
5705 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 3, 1)};
5706 load
->operands
[0] = Operand(fmask_desc_ptr
);
5707 load
->operands
[1] = Operand(s4
); /* no sampler */
5708 load
->operands
[2] = Operand(coord
);
5709 load
->definitions
[0] = Definition(fmask
);
5716 load
->can_reorder
= true; /* fmask images shouldn't be modified */
5717 ctx
->block
->instructions
.emplace_back(std::move(load
));
5719 Operand sample_index4
;
5720 if (sample_index
.isConstant()) {
5721 if (sample_index
.constantValue() < 16) {
5722 sample_index4
= Operand(sample_index
.constantValue() << 2);
5724 sample_index4
= Operand(0u);
5726 } else if (sample_index
.regClass() == s1
) {
5727 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
5729 assert(sample_index
.regClass() == v1
);
5730 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
5734 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
5735 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
5736 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
5737 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
5739 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
5741 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5742 * resource descriptor is 0 (invalid),
5744 Temp compare
= bld
.tmp(bld
.lm
);
5745 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
5746 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
5748 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
5750 /* Replace the MSAA sample index. */
5751 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
5754 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
5757 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5758 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5759 bool is_array
= glsl_sampler_type_is_array(type
);
5760 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
5761 assert(!add_frag_pos
&& "Input attachments should be lowered.");
5762 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
5763 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
5764 int count
= image_type_to_components_count(dim
, is_array
);
5765 std::vector
<Temp
> coords(count
);
5766 Builder
bld(ctx
->program
, ctx
->block
);
5770 Temp src2
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
5771 /* get sample index */
5772 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
5773 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
5774 Operand sample_index
= sample_cv
? Operand(sample_cv
->u32
) : Operand(emit_extract_vector(ctx
, src2
, 0, v1
));
5775 std::vector
<Temp
> fmask_load_address
;
5776 for (unsigned i
= 0; i
< (is_array
? 3 : 2); i
++)
5777 fmask_load_address
.emplace_back(emit_extract_vector(ctx
, src0
, i
, v1
));
5779 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
5780 coords
[count
] = adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
);
5782 coords
[count
] = emit_extract_vector(ctx
, src2
, 0, v1
);
5787 coords
[0] = emit_extract_vector(ctx
, src0
, 0, v1
);
5788 coords
.resize(coords
.size() + 1);
5789 coords
[1] = bld
.copy(bld
.def(v1
), Operand(0u));
5791 coords
[2] = emit_extract_vector(ctx
, src0
, 1, v1
);
5793 for (int i
= 0; i
< count
; i
++)
5794 coords
[i
] = emit_extract_vector(ctx
, src0
, i
, v1
);
5797 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
5798 instr
->intrinsic
== nir_intrinsic_image_deref_store
) {
5799 int lod_index
= instr
->intrinsic
== nir_intrinsic_image_deref_load
? 3 : 4;
5800 bool level_zero
= nir_src_is_const(instr
->src
[lod_index
]) && nir_src_as_uint(instr
->src
[lod_index
]) == 0;
5803 coords
.emplace_back(get_ssa_temp(ctx
, instr
->src
[lod_index
].ssa
));
5806 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
5807 for (unsigned i
= 0; i
< coords
.size(); i
++)
5808 vec
->operands
[i
] = Operand(coords
[i
]);
5809 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
5810 vec
->definitions
[0] = Definition(res
);
5811 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5816 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5818 Builder
bld(ctx
->program
, ctx
->block
);
5819 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5820 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5821 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5822 bool is_array
= glsl_sampler_type_is_array(type
);
5823 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5825 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
5826 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
5827 unsigned num_channels
= util_last_bit(mask
);
5828 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
5829 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
5832 switch (num_channels
) {
5834 opcode
= aco_opcode::buffer_load_format_x
;
5837 opcode
= aco_opcode::buffer_load_format_xy
;
5840 opcode
= aco_opcode::buffer_load_format_xyz
;
5843 opcode
= aco_opcode::buffer_load_format_xyzw
;
5846 unreachable(">4 channel buffer image load");
5848 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
5849 load
->operands
[0] = Operand(rsrc
);
5850 load
->operands
[1] = Operand(vindex
);
5851 load
->operands
[2] = Operand((uint32_t) 0);
5853 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
5856 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
5857 load
->definitions
[0] = Definition(tmp
);
5859 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
5860 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
5861 load
->barrier
= barrier_image
;
5862 ctx
->block
->instructions
.emplace_back(std::move(load
));
5864 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
5868 Temp coords
= get_image_coords(ctx
, instr
, type
);
5869 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
5871 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
5872 unsigned num_components
= util_bitcount(dmask
);
5874 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
5877 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
5879 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
5880 aco_opcode opcode
= level_zero
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
5882 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1)};
5883 load
->operands
[0] = Operand(resource
);
5884 load
->operands
[1] = Operand(s4
); /* no sampler */
5885 load
->operands
[2] = Operand(coords
);
5886 load
->definitions
[0] = Definition(tmp
);
5887 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
5888 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
5889 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5890 load
->dmask
= dmask
;
5892 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
5893 load
->barrier
= barrier_image
;
5894 ctx
->block
->instructions
.emplace_back(std::move(load
));
5896 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
5900 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5902 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5903 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5904 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5905 bool is_array
= glsl_sampler_type_is_array(type
);
5906 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
5908 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
5910 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
5911 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
5912 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
5914 switch (data
.size()) {
5916 opcode
= aco_opcode::buffer_store_format_x
;
5919 opcode
= aco_opcode::buffer_store_format_xy
;
5922 opcode
= aco_opcode::buffer_store_format_xyz
;
5925 opcode
= aco_opcode::buffer_store_format_xyzw
;
5928 unreachable(">4 channel buffer image store");
5930 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
5931 store
->operands
[0] = Operand(rsrc
);
5932 store
->operands
[1] = Operand(vindex
);
5933 store
->operands
[2] = Operand((uint32_t) 0);
5934 store
->operands
[3] = Operand(data
);
5935 store
->idxen
= true;
5938 store
->disable_wqm
= true;
5939 store
->barrier
= barrier_image
;
5940 ctx
->program
->needs_exact
= true;
5941 ctx
->block
->instructions
.emplace_back(std::move(store
));
5945 assert(data
.type() == RegType::vgpr
);
5946 Temp coords
= get_image_coords(ctx
, instr
, type
);
5947 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
5949 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
5950 aco_opcode opcode
= level_zero
? aco_opcode::image_store
: aco_opcode::image_store_mip
;
5952 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 0)};
5953 store
->operands
[0] = Operand(resource
);
5954 store
->operands
[1] = Operand(data
);
5955 store
->operands
[2] = Operand(coords
);
5958 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5959 store
->dmask
= (1 << data
.size()) - 1;
5961 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
5962 store
->disable_wqm
= true;
5963 store
->barrier
= barrier_image
;
5964 ctx
->program
->needs_exact
= true;
5965 ctx
->block
->instructions
.emplace_back(std::move(store
));
5969 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5971 /* return the previous value if dest is ever used */
5972 bool return_previous
= false;
5973 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5974 return_previous
= true;
5977 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5978 return_previous
= true;
5982 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5983 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5984 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5985 bool is_array
= glsl_sampler_type_is_array(type
);
5986 Builder
bld(ctx
->program
, ctx
->block
);
5988 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
5989 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
5991 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
5992 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
5994 aco_opcode buf_op
, image_op
;
5995 switch (instr
->intrinsic
) {
5996 case nir_intrinsic_image_deref_atomic_add
:
5997 buf_op
= aco_opcode::buffer_atomic_add
;
5998 image_op
= aco_opcode::image_atomic_add
;
6000 case nir_intrinsic_image_deref_atomic_umin
:
6001 buf_op
= aco_opcode::buffer_atomic_umin
;
6002 image_op
= aco_opcode::image_atomic_umin
;
6004 case nir_intrinsic_image_deref_atomic_imin
:
6005 buf_op
= aco_opcode::buffer_atomic_smin
;
6006 image_op
= aco_opcode::image_atomic_smin
;
6008 case nir_intrinsic_image_deref_atomic_umax
:
6009 buf_op
= aco_opcode::buffer_atomic_umax
;
6010 image_op
= aco_opcode::image_atomic_umax
;
6012 case nir_intrinsic_image_deref_atomic_imax
:
6013 buf_op
= aco_opcode::buffer_atomic_smax
;
6014 image_op
= aco_opcode::image_atomic_smax
;
6016 case nir_intrinsic_image_deref_atomic_and
:
6017 buf_op
= aco_opcode::buffer_atomic_and
;
6018 image_op
= aco_opcode::image_atomic_and
;
6020 case nir_intrinsic_image_deref_atomic_or
:
6021 buf_op
= aco_opcode::buffer_atomic_or
;
6022 image_op
= aco_opcode::image_atomic_or
;
6024 case nir_intrinsic_image_deref_atomic_xor
:
6025 buf_op
= aco_opcode::buffer_atomic_xor
;
6026 image_op
= aco_opcode::image_atomic_xor
;
6028 case nir_intrinsic_image_deref_atomic_exchange
:
6029 buf_op
= aco_opcode::buffer_atomic_swap
;
6030 image_op
= aco_opcode::image_atomic_swap
;
6032 case nir_intrinsic_image_deref_atomic_comp_swap
:
6033 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
6034 image_op
= aco_opcode::image_atomic_cmpswap
;
6037 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
6040 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6042 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
6043 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
6044 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
6045 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6046 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6047 mubuf
->operands
[0] = Operand(resource
);
6048 mubuf
->operands
[1] = Operand(vindex
);
6049 mubuf
->operands
[2] = Operand((uint32_t)0);
6050 mubuf
->operands
[3] = Operand(data
);
6051 if (return_previous
)
6052 mubuf
->definitions
[0] = Definition(dst
);
6054 mubuf
->idxen
= true;
6055 mubuf
->glc
= return_previous
;
6056 mubuf
->dlc
= false; /* Not needed for atomics */
6057 mubuf
->disable_wqm
= true;
6058 mubuf
->barrier
= barrier_image
;
6059 ctx
->program
->needs_exact
= true;
6060 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6064 Temp coords
= get_image_coords(ctx
, instr
, type
);
6065 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
6066 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 3, return_previous
? 1 : 0)};
6067 mimg
->operands
[0] = Operand(resource
);
6068 mimg
->operands
[1] = Operand(data
);
6069 mimg
->operands
[2] = Operand(coords
);
6070 if (return_previous
)
6071 mimg
->definitions
[0] = Definition(dst
);
6072 mimg
->glc
= return_previous
;
6073 mimg
->dlc
= false; /* Not needed for atomics */
6074 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
6075 mimg
->dmask
= (1 << data
.size()) - 1;
6077 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
6078 mimg
->disable_wqm
= true;
6079 mimg
->barrier
= barrier_image
;
6080 ctx
->program
->needs_exact
= true;
6081 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
6085 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
6087 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
6088 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6089 Builder
bld(ctx
->program
, ctx
->block
);
6091 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
6093 Temp size_div3
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0xaaaaaaabu
)), size
);
6094 size_div3
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.as_uniform(size_div3
), Operand(1u));
6096 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
6097 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
6099 Temp is12
= bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.def(s1
, scc
), stride
, Operand(12u));
6100 size
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), size_div3
, size
, bld
.scc(is12
));
6102 Temp shr_dst
= dst
.type() == RegType::vgpr
? bld
.tmp(s1
) : dst
;
6103 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(shr_dst
), bld
.def(s1
, scc
),
6104 size
, bld
.sop1(aco_opcode::s_ff1_i32_b32
, bld
.def(s1
), stride
));
6105 if (dst
.type() == RegType::vgpr
)
6106 bld
.copy(Definition(dst
), shr_dst
);
6108 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6110 emit_extract_vector(ctx
, desc
, 2, dst
);
6114 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6116 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
6117 const struct glsl_type
*type
= glsl_without_array(var
->type
);
6118 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
6119 bool is_array
= glsl_sampler_type_is_array(type
);
6120 Builder
bld(ctx
->program
, ctx
->block
);
6122 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
6123 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
6124 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6128 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6131 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
6133 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6135 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1)};
6136 mimg
->operands
[0] = Operand(resource
);
6137 mimg
->operands
[1] = Operand(s4
); /* no sampler */
6138 mimg
->operands
[2] = Operand(lod
);
6139 uint8_t& dmask
= mimg
->dmask
;
6140 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
6141 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
6142 mimg
->da
= glsl_sampler_type_is_array(type
);
6143 mimg
->can_reorder
= true;
6144 Definition
& def
= mimg
->definitions
[0];
6145 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
6147 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
6148 glsl_sampler_type_is_array(type
)) {
6150 assert(instr
->dest
.ssa
.num_components
== 3);
6151 Temp tmp
= {ctx
->program
->allocateId(), v3
};
6152 def
= Definition(tmp
);
6153 emit_split_vector(ctx
, tmp
, 3);
6155 /* divide 3rd value by 6 by multiplying with magic number */
6156 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6157 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
6159 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6160 emit_extract_vector(ctx
, tmp
, 0, v1
),
6161 emit_extract_vector(ctx
, tmp
, 1, v1
),
6164 } else if (ctx
->options
->chip_class
== GFX9
&&
6165 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
6166 glsl_sampler_type_is_array(type
)) {
6167 assert(instr
->dest
.ssa
.num_components
== 2);
6168 def
= Definition(dst
);
6171 def
= Definition(dst
);
6174 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
6177 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6179 Builder
bld(ctx
->program
, ctx
->block
);
6180 unsigned num_components
= instr
->num_components
;
6182 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6183 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6184 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6186 unsigned access
= nir_intrinsic_access(instr
);
6187 bool glc
= access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
6188 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
6190 uint32_t flags
= get_all_buffer_resource_flags(ctx
, instr
->src
[0].ssa
, access
);
6191 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6192 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6194 bool allow_smem
= !(flags
& (0 && glc
? has_nonglc_vmem_store
: has_vmem_store
));
6195 allow_smem
|= ((access
& ACCESS_RESTRICT
) && (access
& ACCESS_NON_WRITEABLE
)) || (access
& ACCESS_CAN_REORDER
);
6197 load_buffer(ctx
, num_components
, size
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
),
6198 nir_intrinsic_align_mul(instr
), nir_intrinsic_align_offset(instr
), glc
, false, allow_smem
);
6201 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6203 Builder
bld(ctx
->program
, ctx
->block
);
6204 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6205 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6206 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6207 Temp offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
6209 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6210 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6212 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
6213 uint32_t flags
= get_all_buffer_resource_flags(ctx
, instr
->src
[1].ssa
, nir_intrinsic_access(instr
));
6214 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6215 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6217 bool allow_smem
= !(flags
& (0 && glc
? has_nonglc_vmem_loadstore
: has_vmem_loadstore
));
6219 bool smem
= !nir_src_is_divergent(instr
->src
[2]) &&
6220 ctx
->options
->chip_class
>= GFX8
&&
6221 (elem_size_bytes
>= 4 || can_subdword_ssbo_store_use_smem(instr
)) &&
6224 offset
= bld
.as_uniform(offset
);
6225 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
6227 unsigned write_count
= 0;
6228 Temp write_datas
[32];
6229 unsigned offsets
[32];
6230 split_buffer_store(ctx
, instr
, smem
, smem_nonfs
? RegType::sgpr
: (smem
? data
.type() : RegType::vgpr
),
6231 data
, writemask
, 16, &write_count
, write_datas
, offsets
);
6233 for (unsigned i
= 0; i
< write_count
; i
++) {
6234 aco_opcode op
= get_buffer_store_op(smem
, write_datas
[i
].bytes());
6235 if (smem
&& ctx
->stage
== fragment_fs
)
6236 op
= aco_opcode::p_fs_buffer_store_smem
;
6239 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 3, 0)};
6240 store
->operands
[0] = Operand(rsrc
);
6242 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
6243 offset
, Operand(offsets
[i
]));
6244 store
->operands
[1] = Operand(off
);
6246 store
->operands
[1] = Operand(offset
);
6248 if (op
!= aco_opcode::p_fs_buffer_store_smem
)
6249 store
->operands
[1].setFixed(m0
);
6250 store
->operands
[2] = Operand(write_datas
[i
]);
6253 store
->disable_wqm
= true;
6254 store
->barrier
= barrier_buffer
;
6255 ctx
->block
->instructions
.emplace_back(std::move(store
));
6256 ctx
->program
->wb_smem_l1_on_end
= true;
6257 if (op
== aco_opcode::p_fs_buffer_store_smem
) {
6258 ctx
->block
->kind
|= block_kind_needs_lowering
;
6259 ctx
->program
->needs_exact
= true;
6262 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
6263 store
->operands
[0] = Operand(rsrc
);
6264 store
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
6265 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
6266 store
->operands
[3] = Operand(write_datas
[i
]);
6267 store
->offset
= offsets
[i
];
6268 store
->offen
= (offset
.type() == RegType::vgpr
);
6271 store
->disable_wqm
= true;
6272 store
->barrier
= barrier_buffer
;
6273 ctx
->program
->needs_exact
= true;
6274 ctx
->block
->instructions
.emplace_back(std::move(store
));
6279 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6281 /* return the previous value if dest is ever used */
6282 bool return_previous
= false;
6283 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6284 return_previous
= true;
6287 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6288 return_previous
= true;
6292 Builder
bld(ctx
->program
, ctx
->block
);
6293 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6295 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
6296 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
6297 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
6299 Temp offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6300 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6301 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6303 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6305 aco_opcode op32
, op64
;
6306 switch (instr
->intrinsic
) {
6307 case nir_intrinsic_ssbo_atomic_add
:
6308 op32
= aco_opcode::buffer_atomic_add
;
6309 op64
= aco_opcode::buffer_atomic_add_x2
;
6311 case nir_intrinsic_ssbo_atomic_imin
:
6312 op32
= aco_opcode::buffer_atomic_smin
;
6313 op64
= aco_opcode::buffer_atomic_smin_x2
;
6315 case nir_intrinsic_ssbo_atomic_umin
:
6316 op32
= aco_opcode::buffer_atomic_umin
;
6317 op64
= aco_opcode::buffer_atomic_umin_x2
;
6319 case nir_intrinsic_ssbo_atomic_imax
:
6320 op32
= aco_opcode::buffer_atomic_smax
;
6321 op64
= aco_opcode::buffer_atomic_smax_x2
;
6323 case nir_intrinsic_ssbo_atomic_umax
:
6324 op32
= aco_opcode::buffer_atomic_umax
;
6325 op64
= aco_opcode::buffer_atomic_umax_x2
;
6327 case nir_intrinsic_ssbo_atomic_and
:
6328 op32
= aco_opcode::buffer_atomic_and
;
6329 op64
= aco_opcode::buffer_atomic_and_x2
;
6331 case nir_intrinsic_ssbo_atomic_or
:
6332 op32
= aco_opcode::buffer_atomic_or
;
6333 op64
= aco_opcode::buffer_atomic_or_x2
;
6335 case nir_intrinsic_ssbo_atomic_xor
:
6336 op32
= aco_opcode::buffer_atomic_xor
;
6337 op64
= aco_opcode::buffer_atomic_xor_x2
;
6339 case nir_intrinsic_ssbo_atomic_exchange
:
6340 op32
= aco_opcode::buffer_atomic_swap
;
6341 op64
= aco_opcode::buffer_atomic_swap_x2
;
6343 case nir_intrinsic_ssbo_atomic_comp_swap
:
6344 op32
= aco_opcode::buffer_atomic_cmpswap
;
6345 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
6348 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6350 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6351 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6352 mubuf
->operands
[0] = Operand(rsrc
);
6353 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
6354 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
6355 mubuf
->operands
[3] = Operand(data
);
6356 if (return_previous
)
6357 mubuf
->definitions
[0] = Definition(dst
);
6359 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
6360 mubuf
->glc
= return_previous
;
6361 mubuf
->dlc
= false; /* Not needed for atomics */
6362 mubuf
->disable_wqm
= true;
6363 mubuf
->barrier
= barrier_buffer
;
6364 ctx
->program
->needs_exact
= true;
6365 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6368 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6370 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6371 Builder
bld(ctx
->program
, ctx
->block
);
6372 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
6373 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
6376 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6378 Builder
bld(ctx
->program
, ctx
->block
);
6379 unsigned num_components
= instr
->num_components
;
6380 unsigned component_size
= instr
->dest
.ssa
.bit_size
/ 8;
6382 LoadEmitInfo info
= {Operand(get_ssa_temp(ctx
, instr
->src
[0].ssa
)),
6383 get_ssa_temp(ctx
, &instr
->dest
.ssa
),
6384 num_components
, component_size
};
6385 info
.glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
6386 info
.align_mul
= nir_intrinsic_align_mul(instr
);
6387 info
.align_offset
= nir_intrinsic_align_offset(instr
);
6388 info
.barrier
= barrier_buffer
;
6389 info
.can_reorder
= false;
6390 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6391 * it's safe to use SMEM */
6392 bool can_use_smem
= nir_intrinsic_access(instr
) & ACCESS_NON_WRITEABLE
;
6393 if (info
.dst
.type() == RegType::vgpr
|| (info
.glc
&& ctx
->options
->chip_class
< GFX8
) || !can_use_smem
) {
6394 emit_global_load(ctx
, bld
, &info
);
6396 info
.offset
= Operand(bld
.as_uniform(info
.offset
));
6397 emit_smem_load(ctx
, bld
, &info
);
6401 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6403 Builder
bld(ctx
->program
, ctx
->block
);
6404 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6405 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6407 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6408 Temp addr
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6409 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
6411 if (ctx
->options
->chip_class
>= GFX7
)
6412 addr
= as_vgpr(ctx
, addr
);
6414 unsigned write_count
= 0;
6415 Temp write_datas
[32];
6416 unsigned offsets
[32];
6417 split_buffer_store(ctx
, instr
, false, RegType::vgpr
, data
, writemask
,
6418 16, &write_count
, write_datas
, offsets
);
6420 for (unsigned i
= 0; i
< write_count
; i
++) {
6421 if (ctx
->options
->chip_class
>= GFX7
) {
6422 unsigned offset
= offsets
[i
];
6423 Temp store_addr
= addr
;
6424 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
6425 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
6426 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
6427 Temp carry
= bld
.tmp(bld
.lm
);
6428 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
6430 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
6431 Operand(offset
), addr0
);
6432 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(bld
.lm
),
6434 carry
).def(1).setHint(vcc
);
6436 store_addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
6441 bool global
= ctx
->options
->chip_class
>= GFX9
;
6443 switch (write_datas
[i
].bytes()) {
6445 op
= global
? aco_opcode::global_store_byte
: aco_opcode::flat_store_byte
;
6448 op
= global
? aco_opcode::global_store_short
: aco_opcode::flat_store_short
;
6451 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
6454 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
6457 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
6460 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
6463 unreachable("store_global not implemented for this size.");
6466 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
6467 flat
->operands
[0] = Operand(store_addr
);
6468 flat
->operands
[1] = Operand(s1
);
6469 flat
->operands
[2] = Operand(write_datas
[i
]);
6472 flat
->offset
= offset
;
6473 flat
->disable_wqm
= true;
6474 flat
->barrier
= barrier_buffer
;
6475 ctx
->program
->needs_exact
= true;
6476 ctx
->block
->instructions
.emplace_back(std::move(flat
));
6478 assert(ctx
->options
->chip_class
== GFX6
);
6480 aco_opcode op
= get_buffer_store_op(false, write_datas
[i
].bytes());
6482 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
6484 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
6485 mubuf
->operands
[0] = Operand(rsrc
);
6486 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
6487 mubuf
->operands
[2] = Operand(0u);
6488 mubuf
->operands
[3] = Operand(write_datas
[i
]);
6491 mubuf
->offset
= offsets
[i
];
6492 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
6493 mubuf
->disable_wqm
= true;
6494 mubuf
->barrier
= barrier_buffer
;
6495 ctx
->program
->needs_exact
= true;
6496 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6501 void visit_global_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6503 /* return the previous value if dest is ever used */
6504 bool return_previous
= false;
6505 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6506 return_previous
= true;
6509 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6510 return_previous
= true;
6514 Builder
bld(ctx
->program
, ctx
->block
);
6515 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6516 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6518 if (ctx
->options
->chip_class
>= GFX7
)
6519 addr
= as_vgpr(ctx
, addr
);
6521 if (instr
->intrinsic
== nir_intrinsic_global_atomic_comp_swap
)
6522 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
6523 get_ssa_temp(ctx
, instr
->src
[2].ssa
), data
);
6525 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6527 aco_opcode op32
, op64
;
6529 if (ctx
->options
->chip_class
>= GFX7
) {
6530 bool global
= ctx
->options
->chip_class
>= GFX9
;
6531 switch (instr
->intrinsic
) {
6532 case nir_intrinsic_global_atomic_add
:
6533 op32
= global
? aco_opcode::global_atomic_add
: aco_opcode::flat_atomic_add
;
6534 op64
= global
? aco_opcode::global_atomic_add_x2
: aco_opcode::flat_atomic_add_x2
;
6536 case nir_intrinsic_global_atomic_imin
:
6537 op32
= global
? aco_opcode::global_atomic_smin
: aco_opcode::flat_atomic_smin
;
6538 op64
= global
? aco_opcode::global_atomic_smin_x2
: aco_opcode::flat_atomic_smin_x2
;
6540 case nir_intrinsic_global_atomic_umin
:
6541 op32
= global
? aco_opcode::global_atomic_umin
: aco_opcode::flat_atomic_umin
;
6542 op64
= global
? aco_opcode::global_atomic_umin_x2
: aco_opcode::flat_atomic_umin_x2
;
6544 case nir_intrinsic_global_atomic_imax
:
6545 op32
= global
? aco_opcode::global_atomic_smax
: aco_opcode::flat_atomic_smax
;
6546 op64
= global
? aco_opcode::global_atomic_smax_x2
: aco_opcode::flat_atomic_smax_x2
;
6548 case nir_intrinsic_global_atomic_umax
:
6549 op32
= global
? aco_opcode::global_atomic_umax
: aco_opcode::flat_atomic_umax
;
6550 op64
= global
? aco_opcode::global_atomic_umax_x2
: aco_opcode::flat_atomic_umax_x2
;
6552 case nir_intrinsic_global_atomic_and
:
6553 op32
= global
? aco_opcode::global_atomic_and
: aco_opcode::flat_atomic_and
;
6554 op64
= global
? aco_opcode::global_atomic_and_x2
: aco_opcode::flat_atomic_and_x2
;
6556 case nir_intrinsic_global_atomic_or
:
6557 op32
= global
? aco_opcode::global_atomic_or
: aco_opcode::flat_atomic_or
;
6558 op64
= global
? aco_opcode::global_atomic_or_x2
: aco_opcode::flat_atomic_or_x2
;
6560 case nir_intrinsic_global_atomic_xor
:
6561 op32
= global
? aco_opcode::global_atomic_xor
: aco_opcode::flat_atomic_xor
;
6562 op64
= global
? aco_opcode::global_atomic_xor_x2
: aco_opcode::flat_atomic_xor_x2
;
6564 case nir_intrinsic_global_atomic_exchange
:
6565 op32
= global
? aco_opcode::global_atomic_swap
: aco_opcode::flat_atomic_swap
;
6566 op64
= global
? aco_opcode::global_atomic_swap_x2
: aco_opcode::flat_atomic_swap_x2
;
6568 case nir_intrinsic_global_atomic_comp_swap
:
6569 op32
= global
? aco_opcode::global_atomic_cmpswap
: aco_opcode::flat_atomic_cmpswap
;
6570 op64
= global
? aco_opcode::global_atomic_cmpswap_x2
: aco_opcode::flat_atomic_cmpswap_x2
;
6573 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6576 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6577 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, return_previous
? 1 : 0)};
6578 flat
->operands
[0] = Operand(addr
);
6579 flat
->operands
[1] = Operand(s1
);
6580 flat
->operands
[2] = Operand(data
);
6581 if (return_previous
)
6582 flat
->definitions
[0] = Definition(dst
);
6583 flat
->glc
= return_previous
;
6584 flat
->dlc
= false; /* Not needed for atomics */
6586 flat
->disable_wqm
= true;
6587 flat
->barrier
= barrier_buffer
;
6588 ctx
->program
->needs_exact
= true;
6589 ctx
->block
->instructions
.emplace_back(std::move(flat
));
6591 assert(ctx
->options
->chip_class
== GFX6
);
6593 switch (instr
->intrinsic
) {
6594 case nir_intrinsic_global_atomic_add
:
6595 op32
= aco_opcode::buffer_atomic_add
;
6596 op64
= aco_opcode::buffer_atomic_add_x2
;
6598 case nir_intrinsic_global_atomic_imin
:
6599 op32
= aco_opcode::buffer_atomic_smin
;
6600 op64
= aco_opcode::buffer_atomic_smin_x2
;
6602 case nir_intrinsic_global_atomic_umin
:
6603 op32
= aco_opcode::buffer_atomic_umin
;
6604 op64
= aco_opcode::buffer_atomic_umin_x2
;
6606 case nir_intrinsic_global_atomic_imax
:
6607 op32
= aco_opcode::buffer_atomic_smax
;
6608 op64
= aco_opcode::buffer_atomic_smax_x2
;
6610 case nir_intrinsic_global_atomic_umax
:
6611 op32
= aco_opcode::buffer_atomic_umax
;
6612 op64
= aco_opcode::buffer_atomic_umax_x2
;
6614 case nir_intrinsic_global_atomic_and
:
6615 op32
= aco_opcode::buffer_atomic_and
;
6616 op64
= aco_opcode::buffer_atomic_and_x2
;
6618 case nir_intrinsic_global_atomic_or
:
6619 op32
= aco_opcode::buffer_atomic_or
;
6620 op64
= aco_opcode::buffer_atomic_or_x2
;
6622 case nir_intrinsic_global_atomic_xor
:
6623 op32
= aco_opcode::buffer_atomic_xor
;
6624 op64
= aco_opcode::buffer_atomic_xor_x2
;
6626 case nir_intrinsic_global_atomic_exchange
:
6627 op32
= aco_opcode::buffer_atomic_swap
;
6628 op64
= aco_opcode::buffer_atomic_swap_x2
;
6630 case nir_intrinsic_global_atomic_comp_swap
:
6631 op32
= aco_opcode::buffer_atomic_cmpswap
;
6632 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
6635 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6638 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
6640 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6642 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6643 mubuf
->operands
[0] = Operand(rsrc
);
6644 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
6645 mubuf
->operands
[2] = Operand(0u);
6646 mubuf
->operands
[3] = Operand(data
);
6647 if (return_previous
)
6648 mubuf
->definitions
[0] = Definition(dst
);
6649 mubuf
->glc
= return_previous
;
6652 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
6653 mubuf
->disable_wqm
= true;
6654 mubuf
->barrier
= barrier_buffer
;
6655 ctx
->program
->needs_exact
= true;
6656 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6660 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6661 Builder
bld(ctx
->program
, ctx
->block
);
6662 switch(instr
->intrinsic
) {
6663 case nir_intrinsic_group_memory_barrier
:
6664 case nir_intrinsic_memory_barrier
:
6665 bld
.barrier(aco_opcode::p_memory_barrier_common
);
6667 case nir_intrinsic_memory_barrier_buffer
:
6668 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
6670 case nir_intrinsic_memory_barrier_image
:
6671 bld
.barrier(aco_opcode::p_memory_barrier_image
);
6673 case nir_intrinsic_memory_barrier_tcs_patch
:
6674 case nir_intrinsic_memory_barrier_shared
:
6675 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
6678 unreachable("Unimplemented memory barrier intrinsic");
6683 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6685 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6686 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6687 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6688 Builder
bld(ctx
->program
, ctx
->block
);
6690 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
6691 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
6692 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
6695 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6697 unsigned writemask
= nir_intrinsic_write_mask(instr
);
6698 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6699 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6700 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6702 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
6703 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
6706 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6708 unsigned offset
= nir_intrinsic_base(instr
);
6709 Builder
bld(ctx
->program
, ctx
->block
);
6710 Operand m
= load_lds_size_m0(bld
);
6711 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6712 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6714 unsigned num_operands
= 3;
6715 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
6716 switch(instr
->intrinsic
) {
6717 case nir_intrinsic_shared_atomic_add
:
6718 op32
= aco_opcode::ds_add_u32
;
6719 op64
= aco_opcode::ds_add_u64
;
6720 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
6721 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
6723 case nir_intrinsic_shared_atomic_imin
:
6724 op32
= aco_opcode::ds_min_i32
;
6725 op64
= aco_opcode::ds_min_i64
;
6726 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
6727 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
6729 case nir_intrinsic_shared_atomic_umin
:
6730 op32
= aco_opcode::ds_min_u32
;
6731 op64
= aco_opcode::ds_min_u64
;
6732 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
6733 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
6735 case nir_intrinsic_shared_atomic_imax
:
6736 op32
= aco_opcode::ds_max_i32
;
6737 op64
= aco_opcode::ds_max_i64
;
6738 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
6739 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
6741 case nir_intrinsic_shared_atomic_umax
:
6742 op32
= aco_opcode::ds_max_u32
;
6743 op64
= aco_opcode::ds_max_u64
;
6744 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
6745 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
6747 case nir_intrinsic_shared_atomic_and
:
6748 op32
= aco_opcode::ds_and_b32
;
6749 op64
= aco_opcode::ds_and_b64
;
6750 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
6751 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
6753 case nir_intrinsic_shared_atomic_or
:
6754 op32
= aco_opcode::ds_or_b32
;
6755 op64
= aco_opcode::ds_or_b64
;
6756 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
6757 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
6759 case nir_intrinsic_shared_atomic_xor
:
6760 op32
= aco_opcode::ds_xor_b32
;
6761 op64
= aco_opcode::ds_xor_b64
;
6762 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
6763 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
6765 case nir_intrinsic_shared_atomic_exchange
:
6766 op32
= aco_opcode::ds_write_b32
;
6767 op64
= aco_opcode::ds_write_b64
;
6768 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
6769 op64_rtn
= aco_opcode::ds_wrxchg_rtn_b64
;
6771 case nir_intrinsic_shared_atomic_comp_swap
:
6772 op32
= aco_opcode::ds_cmpst_b32
;
6773 op64
= aco_opcode::ds_cmpst_b64
;
6774 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
6775 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
6779 unreachable("Unhandled shared atomic intrinsic");
6782 /* return the previous value if dest is ever used */
6783 bool return_previous
= false;
6784 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6785 return_previous
= true;
6788 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6789 return_previous
= true;
6794 if (data
.size() == 1) {
6795 assert(instr
->dest
.ssa
.bit_size
== 32);
6796 op
= return_previous
? op32_rtn
: op32
;
6798 assert(instr
->dest
.ssa
.bit_size
== 64);
6799 op
= return_previous
? op64_rtn
: op64
;
6802 if (offset
> 65535) {
6803 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
6807 aco_ptr
<DS_instruction
> ds
;
6808 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
6809 ds
->operands
[0] = Operand(address
);
6810 ds
->operands
[1] = Operand(data
);
6811 if (num_operands
== 4)
6812 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6813 ds
->operands
[num_operands
- 1] = m
;
6814 ds
->offset0
= offset
;
6815 if (return_previous
)
6816 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
6817 ctx
->block
->instructions
.emplace_back(std::move(ds
));
6820 Temp
get_scratch_resource(isel_context
*ctx
)
6822 Builder
bld(ctx
->program
, ctx
->block
);
6823 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
6824 if (ctx
->stage
!= compute_cs
)
6825 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
6827 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
6828 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
6830 if (ctx
->program
->chip_class
>= GFX10
) {
6831 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
6832 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
6833 S_008F0C_RESOURCE_LEVEL(1);
6834 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6835 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
6836 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
6839 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6840 if (ctx
->program
->chip_class
<= GFX8
)
6841 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
6843 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
6846 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6847 Builder
bld(ctx
->program
, ctx
->block
);
6848 Temp rsrc
= get_scratch_resource(ctx
);
6849 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6850 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6852 LoadEmitInfo info
= {Operand(offset
), dst
, instr
->dest
.ssa
.num_components
,
6853 instr
->dest
.ssa
.bit_size
/ 8u, rsrc
};
6854 info
.align_mul
= nir_intrinsic_align_mul(instr
);
6855 info
.align_offset
= nir_intrinsic_align_offset(instr
);
6856 info
.swizzle_component_size
= 16;
6857 info
.can_reorder
= false;
6858 info
.soffset
= ctx
->program
->scratch_offset
;
6859 emit_mubuf_load(ctx
, bld
, &info
);
6862 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6863 Builder
bld(ctx
->program
, ctx
->block
);
6864 Temp rsrc
= get_scratch_resource(ctx
);
6865 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6866 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6868 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6869 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6871 unsigned write_count
= 0;
6872 Temp write_datas
[32];
6873 unsigned offsets
[32];
6874 split_buffer_store(ctx
, instr
, false, RegType::vgpr
, data
, writemask
,
6875 16, &write_count
, write_datas
, offsets
);
6877 for (unsigned i
= 0; i
< write_count
; i
++) {
6878 aco_opcode op
= get_buffer_store_op(false, write_datas
[i
].bytes());
6879 bld
.mubuf(op
, rsrc
, offset
, ctx
->program
->scratch_offset
, write_datas
[i
], offsets
[i
], true);
6883 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6884 uint8_t log2_ps_iter_samples
;
6885 if (ctx
->program
->info
->ps
.force_persample
) {
6886 log2_ps_iter_samples
=
6887 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
6889 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
6892 /* The bit pattern matches that used by fixed function fragment
6894 static const unsigned ps_iter_masks
[] = {
6895 0xffff, /* not used */
6901 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
6903 Builder
bld(ctx
->program
, ctx
->block
);
6905 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
6906 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
6907 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
6908 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
6909 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6910 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
6913 void visit_emit_vertex_with_counter(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6914 Builder
bld(ctx
->program
, ctx
->block
);
6916 unsigned stream
= nir_intrinsic_stream_id(instr
);
6917 Temp next_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6918 next_vertex
= bld
.v_mul_imm(bld
.def(v1
), next_vertex
, 4u);
6919 nir_const_value
*next_vertex_cv
= nir_src_as_const_value(instr
->src
[0]);
6922 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_GSVS_GS
* 16u));
6924 unsigned num_components
=
6925 ctx
->program
->info
->gs
.num_stream_output_components
[stream
];
6926 assert(num_components
);
6928 unsigned stride
= 4u * num_components
* ctx
->shader
->info
.gs
.vertices_out
;
6929 unsigned stream_offset
= 0;
6930 for (unsigned i
= 0; i
< stream
; i
++) {
6931 unsigned prev_stride
= 4u * ctx
->program
->info
->gs
.num_stream_output_components
[i
] * ctx
->shader
->info
.gs
.vertices_out
;
6932 stream_offset
+= prev_stride
* ctx
->program
->wave_size
;
6935 /* Limit on the stride field for <= GFX7. */
6936 assert(stride
< (1 << 14));
6938 Temp gsvs_dwords
[4];
6939 for (unsigned i
= 0; i
< 4; i
++)
6940 gsvs_dwords
[i
] = bld
.tmp(s1
);
6941 bld
.pseudo(aco_opcode::p_split_vector
,
6942 Definition(gsvs_dwords
[0]),
6943 Definition(gsvs_dwords
[1]),
6944 Definition(gsvs_dwords
[2]),
6945 Definition(gsvs_dwords
[3]),
6948 if (stream_offset
) {
6949 Temp stream_offset_tmp
= bld
.copy(bld
.def(s1
), Operand(stream_offset
));
6951 Temp carry
= bld
.tmp(s1
);
6952 gsvs_dwords
[0] = bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), gsvs_dwords
[0], stream_offset_tmp
);
6953 gsvs_dwords
[1] = bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), gsvs_dwords
[1], Operand(0u), bld
.scc(carry
));
6956 gsvs_dwords
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), gsvs_dwords
[1], Operand(S_008F04_STRIDE(stride
)));
6957 gsvs_dwords
[2] = bld
.copy(bld
.def(s1
), Operand((uint32_t)ctx
->program
->wave_size
));
6959 gsvs_ring
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
6960 gsvs_dwords
[0], gsvs_dwords
[1], gsvs_dwords
[2], gsvs_dwords
[3]);
6962 unsigned offset
= 0;
6963 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; i
++) {
6964 if (ctx
->program
->info
->gs
.output_streams
[i
] != stream
)
6967 for (unsigned j
= 0; j
< 4; j
++) {
6968 if (!(ctx
->program
->info
->gs
.output_usage_mask
[i
] & (1 << j
)))
6971 if (ctx
->outputs
.mask
[i
] & (1 << j
)) {
6972 Operand vaddr_offset
= next_vertex_cv
? Operand(v1
) : Operand(next_vertex
);
6973 unsigned const_offset
= (offset
+ (next_vertex_cv
? next_vertex_cv
->u32
: 0u)) * 4u;
6974 if (const_offset
>= 4096u) {
6975 if (vaddr_offset
.isUndefined())
6976 vaddr_offset
= bld
.copy(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u));
6978 vaddr_offset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), vaddr_offset
);
6979 const_offset
%= 4096u;
6982 aco_ptr
<MTBUF_instruction
> mtbuf
{create_instruction
<MTBUF_instruction
>(aco_opcode::tbuffer_store_format_x
, Format::MTBUF
, 4, 0)};
6983 mtbuf
->operands
[0] = Operand(gsvs_ring
);
6984 mtbuf
->operands
[1] = vaddr_offset
;
6985 mtbuf
->operands
[2] = Operand(get_arg(ctx
, ctx
->args
->gs2vs_offset
));
6986 mtbuf
->operands
[3] = Operand(ctx
->outputs
.temps
[i
* 4u + j
]);
6987 mtbuf
->offen
= !vaddr_offset
.isUndefined();
6988 mtbuf
->dfmt
= V_008F0C_BUF_DATA_FORMAT_32
;
6989 mtbuf
->nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
6990 mtbuf
->offset
= const_offset
;
6993 mtbuf
->barrier
= barrier_gs_data
;
6994 mtbuf
->can_reorder
= true;
6995 bld
.insert(std::move(mtbuf
));
6998 offset
+= ctx
->shader
->info
.gs
.vertices_out
;
7001 /* outputs for the next vertex are undefined and keeping them around can
7002 * create invalid IR with control flow */
7003 ctx
->outputs
.mask
[i
] = 0;
7006 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(false, true, stream
));
7009 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
7011 Builder
bld(ctx
->program
, ctx
->block
);
7013 if (cluster_size
== 1) {
7015 } if (op
== nir_op_iand
&& cluster_size
== 4) {
7016 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
7017 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
7018 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
7019 bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
));
7020 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
7021 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
7022 return bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
7023 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)));
7024 } else if (op
== nir_op_iand
&& cluster_size
== ctx
->program
->wave_size
) {
7025 //subgroupAnd(val) -> (exec & ~val) == 0
7026 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
7027 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
7028 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
);
7029 } else if (op
== nir_op_ior
&& cluster_size
== ctx
->program
->wave_size
) {
7030 //subgroupOr(val) -> (val & exec) != 0
7031 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)).def(1).getTemp();
7032 return bool_to_vector_condition(ctx
, tmp
);
7033 } else if (op
== nir_op_ixor
&& cluster_size
== ctx
->program
->wave_size
) {
7034 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
7035 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7036 tmp
= bld
.sop1(Builder::s_bcnt1_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
);
7037 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
7038 return bool_to_vector_condition(ctx
, tmp
);
7040 //subgroupClustered{And,Or,Xor}(val, n) ->
7041 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7042 //cluster_offset = ~(n - 1) & lane_id
7043 //cluster_mask = ((1 << n) - 1)
7044 //subgroupClusteredAnd():
7045 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7046 //subgroupClusteredOr():
7047 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7048 //subgroupClusteredXor():
7049 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7050 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
7051 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
7054 if (op
== nir_op_iand
)
7055 tmp
= bld
.sop2(Builder::s_orn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7057 tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7059 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
7061 if (ctx
->program
->chip_class
<= GFX7
)
7062 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), tmp
, cluster_offset
);
7063 else if (ctx
->program
->wave_size
== 64)
7064 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
7066 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), cluster_offset
, tmp
);
7067 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
7068 if (cluster_mask
!= 0xffffffff)
7069 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
7071 Definition cmp_def
= Definition();
7072 if (op
== nir_op_iand
) {
7073 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(cluster_mask
), tmp
).def(0);
7074 } else if (op
== nir_op_ior
) {
7075 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
7076 } else if (op
== nir_op_ixor
) {
7077 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
7078 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
7079 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
7081 cmp_def
.setHint(vcc
);
7082 return cmp_def
.getTemp();
7086 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
7088 Builder
bld(ctx
->program
, ctx
->block
);
7090 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7091 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7092 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7094 if (op
== nir_op_iand
)
7095 tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
7097 tmp
= bld
.sop2(Builder::s_and
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7099 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
7100 Temp lo
= lohi
.def(0).getTemp();
7101 Temp hi
= lohi
.def(1).getTemp();
7102 Temp mbcnt
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(lo
), Operand(hi
));
7104 Definition cmp_def
= Definition();
7105 if (op
== nir_op_iand
)
7106 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
7107 else if (op
== nir_op_ior
)
7108 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
7109 else if (op
== nir_op_ixor
)
7110 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u),
7111 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
7112 cmp_def
.setHint(vcc
);
7113 return cmp_def
.getTemp();
7116 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
7118 Builder
bld(ctx
->program
, ctx
->block
);
7120 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7121 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7122 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7123 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
7124 if (op
== nir_op_iand
)
7125 return bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7126 else if (op
== nir_op_ior
)
7127 return bld
.sop2(Builder::s_or
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7128 else if (op
== nir_op_ixor
)
7129 return bld
.sop2(Builder::s_xor
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7135 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
7137 Builder
bld(ctx
->program
, ctx
->block
);
7138 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
7139 if (src
.regClass().type() == RegType::vgpr
) {
7140 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
7141 } else if (src
.regClass() == s1
) {
7142 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
7143 } else if (src
.regClass() == s2
) {
7144 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
7146 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7147 nir_print_instr(&instr
->instr
, stderr
);
7148 fprintf(stderr
, "\n");
7152 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
7154 Builder
bld(ctx
->program
, ctx
->block
);
7155 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
7156 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
7157 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
7159 Temp ddx_1
, ddx_2
, ddy_1
, ddy_2
;
7160 uint32_t dpp_ctrl0
= dpp_quad_perm(0, 0, 0, 0);
7161 uint32_t dpp_ctrl1
= dpp_quad_perm(1, 1, 1, 1);
7162 uint32_t dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
7165 if (ctx
->program
->chip_class
>= GFX8
) {
7166 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_ctrl0
);
7167 ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl1
);
7168 ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl2
);
7169 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_ctrl0
);
7170 ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl1
);
7171 ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl2
);
7173 Temp tl_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl0
);
7174 ddx_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl1
);
7175 ddx_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_1
, tl_1
);
7176 ddx_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl2
);
7177 ddx_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_2
, tl_1
);
7178 Temp tl_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl0
);
7179 ddy_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl1
);
7180 ddy_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_1
, tl_2
);
7181 ddy_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl2
);
7182 ddy_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_2
, tl_2
);
7185 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7186 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
7187 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
7188 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
7189 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
7190 Temp wqm1
= bld
.tmp(v1
);
7191 emit_wqm(ctx
, tmp1
, wqm1
, true);
7192 Temp wqm2
= bld
.tmp(v1
);
7193 emit_wqm(ctx
, tmp2
, wqm2
, true);
7194 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
7198 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
7200 Builder
bld(ctx
->program
, ctx
->block
);
7201 switch(instr
->intrinsic
) {
7202 case nir_intrinsic_load_barycentric_sample
:
7203 case nir_intrinsic_load_barycentric_pixel
:
7204 case nir_intrinsic_load_barycentric_centroid
: {
7205 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
7206 Temp bary
= Temp(0, s2
);
7208 case INTERP_MODE_SMOOTH
:
7209 case INTERP_MODE_NONE
:
7210 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
7211 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
7212 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
7213 bary
= ctx
->persp_centroid
;
7214 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
7215 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
7217 case INTERP_MODE_NOPERSPECTIVE
:
7218 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
7219 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
7220 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
7221 bary
= ctx
->linear_centroid
;
7222 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
7223 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
7228 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7229 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
7230 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
7231 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7232 Operand(p1
), Operand(p2
));
7233 emit_split_vector(ctx
, dst
, 2);
7236 case nir_intrinsic_load_barycentric_model
: {
7237 Temp model
= get_arg(ctx
, ctx
->args
->ac
.pull_model
);
7239 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7240 Temp p1
= emit_extract_vector(ctx
, model
, 0, v1
);
7241 Temp p2
= emit_extract_vector(ctx
, model
, 1, v1
);
7242 Temp p3
= emit_extract_vector(ctx
, model
, 2, v1
);
7243 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7244 Operand(p1
), Operand(p2
), Operand(p3
));
7245 emit_split_vector(ctx
, dst
, 3);
7248 case nir_intrinsic_load_barycentric_at_sample
: {
7249 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
7250 switch (ctx
->options
->key
.fs
.num_samples
) {
7251 case 2: sample_pos_offset
+= 1 << 3; break;
7252 case 4: sample_pos_offset
+= 3 << 3; break;
7253 case 8: sample_pos_offset
+= 7 << 3; break;
7257 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7258 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
7259 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
7260 if (addr
.type() == RegType::sgpr
) {
7263 sample_pos_offset
+= const_addr
->u32
<< 3;
7264 offset
= Operand(sample_pos_offset
);
7265 } else if (ctx
->options
->chip_class
>= GFX9
) {
7266 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
7268 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
7269 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
7272 Operand off
= bld
.copy(bld
.def(s1
), Operand(offset
));
7273 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, off
);
7275 } else if (ctx
->options
->chip_class
>= GFX9
) {
7276 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7277 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
7278 } else if (ctx
->options
->chip_class
>= GFX7
) {
7279 /* addr += private_segment_buffer + sample_pos_offset */
7280 Temp tmp0
= bld
.tmp(s1
);
7281 Temp tmp1
= bld
.tmp(s1
);
7282 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
7283 Definition scc_tmp
= bld
.def(s1
, scc
);
7284 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
7285 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
7286 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7287 Temp pck0
= bld
.tmp(v1
);
7288 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
7289 tmp1
= as_vgpr(ctx
, tmp1
);
7290 Temp pck1
= bld
.vop2_e64(aco_opcode::v_addc_co_u32
, bld
.def(v1
), bld
.hint_vcc(bld
.def(bld
.lm
)), tmp1
, Operand(0u), carry
);
7291 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
7293 /* sample_pos = flat_load_dwordx2 addr */
7294 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
7296 assert(ctx
->options
->chip_class
== GFX6
);
7298 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
7299 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
7300 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), private_segment_buffer
, Operand(0u), Operand(rsrc_conf
));
7302 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7303 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), addr
, Operand(0u));
7305 sample_pos
= bld
.tmp(v2
);
7307 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dwordx2
, Format::MUBUF
, 3, 1)};
7308 load
->definitions
[0] = Definition(sample_pos
);
7309 load
->operands
[0] = Operand(rsrc
);
7310 load
->operands
[1] = Operand(addr
);
7311 load
->operands
[2] = Operand(0u);
7312 load
->offset
= sample_pos_offset
;
7314 load
->addr64
= true;
7317 load
->disable_wqm
= false;
7318 load
->barrier
= barrier_none
;
7319 load
->can_reorder
= true;
7320 ctx
->block
->instructions
.emplace_back(std::move(load
));
7323 /* sample_pos -= 0.5 */
7324 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
7325 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
7326 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
7327 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
7328 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
7330 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
7333 case nir_intrinsic_load_barycentric_at_offset
: {
7334 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7335 RegClass rc
= RegClass(offset
.type(), 1);
7336 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
7337 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
7338 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
7341 case nir_intrinsic_load_front_face
: {
7342 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7343 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
7346 case nir_intrinsic_load_view_index
: {
7347 if (ctx
->stage
& (sw_vs
| sw_gs
| sw_tcs
| sw_tes
)) {
7348 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7349 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
7355 case nir_intrinsic_load_layer_id
: {
7356 unsigned idx
= nir_intrinsic_base(instr
);
7357 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7358 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
7361 case nir_intrinsic_load_frag_coord
: {
7362 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
7365 case nir_intrinsic_load_sample_pos
: {
7366 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
7367 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
7368 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7369 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
7370 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
7373 case nir_intrinsic_load_tess_coord
:
7374 visit_load_tess_coord(ctx
, instr
);
7376 case nir_intrinsic_load_interpolated_input
:
7377 visit_load_interpolated_input(ctx
, instr
);
7379 case nir_intrinsic_store_output
:
7380 visit_store_output(ctx
, instr
);
7382 case nir_intrinsic_load_input
:
7383 case nir_intrinsic_load_input_vertex
:
7384 visit_load_input(ctx
, instr
);
7386 case nir_intrinsic_load_output
:
7387 visit_load_output(ctx
, instr
);
7389 case nir_intrinsic_load_per_vertex_input
:
7390 visit_load_per_vertex_input(ctx
, instr
);
7392 case nir_intrinsic_load_per_vertex_output
:
7393 visit_load_per_vertex_output(ctx
, instr
);
7395 case nir_intrinsic_store_per_vertex_output
:
7396 visit_store_per_vertex_output(ctx
, instr
);
7398 case nir_intrinsic_load_ubo
:
7399 visit_load_ubo(ctx
, instr
);
7401 case nir_intrinsic_load_push_constant
:
7402 visit_load_push_constant(ctx
, instr
);
7404 case nir_intrinsic_load_constant
:
7405 visit_load_constant(ctx
, instr
);
7407 case nir_intrinsic_vulkan_resource_index
:
7408 visit_load_resource(ctx
, instr
);
7410 case nir_intrinsic_discard
:
7411 visit_discard(ctx
, instr
);
7413 case nir_intrinsic_discard_if
:
7414 visit_discard_if(ctx
, instr
);
7416 case nir_intrinsic_load_shared
:
7417 visit_load_shared(ctx
, instr
);
7419 case nir_intrinsic_store_shared
:
7420 visit_store_shared(ctx
, instr
);
7422 case nir_intrinsic_shared_atomic_add
:
7423 case nir_intrinsic_shared_atomic_imin
:
7424 case nir_intrinsic_shared_atomic_umin
:
7425 case nir_intrinsic_shared_atomic_imax
:
7426 case nir_intrinsic_shared_atomic_umax
:
7427 case nir_intrinsic_shared_atomic_and
:
7428 case nir_intrinsic_shared_atomic_or
:
7429 case nir_intrinsic_shared_atomic_xor
:
7430 case nir_intrinsic_shared_atomic_exchange
:
7431 case nir_intrinsic_shared_atomic_comp_swap
:
7432 visit_shared_atomic(ctx
, instr
);
7434 case nir_intrinsic_image_deref_load
:
7435 visit_image_load(ctx
, instr
);
7437 case nir_intrinsic_image_deref_store
:
7438 visit_image_store(ctx
, instr
);
7440 case nir_intrinsic_image_deref_atomic_add
:
7441 case nir_intrinsic_image_deref_atomic_umin
:
7442 case nir_intrinsic_image_deref_atomic_imin
:
7443 case nir_intrinsic_image_deref_atomic_umax
:
7444 case nir_intrinsic_image_deref_atomic_imax
:
7445 case nir_intrinsic_image_deref_atomic_and
:
7446 case nir_intrinsic_image_deref_atomic_or
:
7447 case nir_intrinsic_image_deref_atomic_xor
:
7448 case nir_intrinsic_image_deref_atomic_exchange
:
7449 case nir_intrinsic_image_deref_atomic_comp_swap
:
7450 visit_image_atomic(ctx
, instr
);
7452 case nir_intrinsic_image_deref_size
:
7453 visit_image_size(ctx
, instr
);
7455 case nir_intrinsic_load_ssbo
:
7456 visit_load_ssbo(ctx
, instr
);
7458 case nir_intrinsic_store_ssbo
:
7459 visit_store_ssbo(ctx
, instr
);
7461 case nir_intrinsic_load_global
:
7462 visit_load_global(ctx
, instr
);
7464 case nir_intrinsic_store_global
:
7465 visit_store_global(ctx
, instr
);
7467 case nir_intrinsic_global_atomic_add
:
7468 case nir_intrinsic_global_atomic_imin
:
7469 case nir_intrinsic_global_atomic_umin
:
7470 case nir_intrinsic_global_atomic_imax
:
7471 case nir_intrinsic_global_atomic_umax
:
7472 case nir_intrinsic_global_atomic_and
:
7473 case nir_intrinsic_global_atomic_or
:
7474 case nir_intrinsic_global_atomic_xor
:
7475 case nir_intrinsic_global_atomic_exchange
:
7476 case nir_intrinsic_global_atomic_comp_swap
:
7477 visit_global_atomic(ctx
, instr
);
7479 case nir_intrinsic_ssbo_atomic_add
:
7480 case nir_intrinsic_ssbo_atomic_imin
:
7481 case nir_intrinsic_ssbo_atomic_umin
:
7482 case nir_intrinsic_ssbo_atomic_imax
:
7483 case nir_intrinsic_ssbo_atomic_umax
:
7484 case nir_intrinsic_ssbo_atomic_and
:
7485 case nir_intrinsic_ssbo_atomic_or
:
7486 case nir_intrinsic_ssbo_atomic_xor
:
7487 case nir_intrinsic_ssbo_atomic_exchange
:
7488 case nir_intrinsic_ssbo_atomic_comp_swap
:
7489 visit_atomic_ssbo(ctx
, instr
);
7491 case nir_intrinsic_load_scratch
:
7492 visit_load_scratch(ctx
, instr
);
7494 case nir_intrinsic_store_scratch
:
7495 visit_store_scratch(ctx
, instr
);
7497 case nir_intrinsic_get_buffer_size
:
7498 visit_get_buffer_size(ctx
, instr
);
7500 case nir_intrinsic_control_barrier
: {
7501 if (ctx
->program
->chip_class
== GFX6
&& ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
7502 /* GFX6 only (thanks to a hw bug workaround):
7503 * The real barrier instruction isn’t needed, because an entire patch
7504 * always fits into a single wave.
7509 if (ctx
->program
->workgroup_size
> ctx
->program
->wave_size
)
7510 bld
.sopp(aco_opcode::s_barrier
);
7514 case nir_intrinsic_memory_barrier_tcs_patch
:
7515 case nir_intrinsic_group_memory_barrier
:
7516 case nir_intrinsic_memory_barrier
:
7517 case nir_intrinsic_memory_barrier_buffer
:
7518 case nir_intrinsic_memory_barrier_image
:
7519 case nir_intrinsic_memory_barrier_shared
:
7520 emit_memory_barrier(ctx
, instr
);
7522 case nir_intrinsic_load_num_work_groups
: {
7523 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7524 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
7525 emit_split_vector(ctx
, dst
, 3);
7528 case nir_intrinsic_load_local_invocation_id
: {
7529 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7530 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
7531 emit_split_vector(ctx
, dst
, 3);
7534 case nir_intrinsic_load_work_group_id
: {
7535 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7536 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
7537 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7538 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(0u),
7539 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(0u),
7540 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(0u));
7541 emit_split_vector(ctx
, dst
, 3);
7544 case nir_intrinsic_load_local_invocation_index
: {
7545 Temp id
= emit_mbcnt(ctx
, bld
.def(v1
));
7547 /* The tg_size bits [6:11] contain the subgroup id,
7548 * we need this multiplied by the wave size, and then OR the thread id to it.
7550 if (ctx
->program
->wave_size
== 64) {
7551 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7552 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
7553 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
7554 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
7556 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7557 Temp tg_num
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7558 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
7559 bld
.vop3(aco_opcode::v_lshl_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, Operand(0x5u
), id
);
7563 case nir_intrinsic_load_subgroup_id
: {
7564 if (ctx
->stage
== compute_cs
) {
7565 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
),
7566 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
7568 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
7572 case nir_intrinsic_load_subgroup_invocation
: {
7573 emit_mbcnt(ctx
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
7576 case nir_intrinsic_load_num_subgroups
: {
7577 if (ctx
->stage
== compute_cs
)
7578 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
7579 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
7581 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
7584 case nir_intrinsic_ballot
: {
7585 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7586 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7587 Definition tmp
= bld
.def(dst
.regClass());
7588 Definition lanemask_tmp
= dst
.size() == bld
.lm
.size() ? tmp
: bld
.def(src
.regClass());
7589 if (instr
->src
[0].ssa
->bit_size
== 1) {
7590 assert(src
.regClass() == bld
.lm
);
7591 bld
.sop2(Builder::s_and
, lanemask_tmp
, bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
7592 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
7593 bld
.vopc(aco_opcode::v_cmp_lg_u32
, lanemask_tmp
, Operand(0u), src
);
7594 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
7595 bld
.vopc(aco_opcode::v_cmp_lg_u64
, lanemask_tmp
, Operand(0u), src
);
7597 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7598 nir_print_instr(&instr
->instr
, stderr
);
7599 fprintf(stderr
, "\n");
7601 if (dst
.size() != bld
.lm
.size()) {
7602 /* Wave32 with ballot size set to 64 */
7603 bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
), lanemask_tmp
.getTemp(), Operand(0u));
7605 emit_wqm(ctx
, tmp
.getTemp(), dst
);
7608 case nir_intrinsic_shuffle
:
7609 case nir_intrinsic_read_invocation
: {
7610 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7611 if (!nir_src_is_divergent(instr
->src
[0])) {
7612 emit_uniform_subgroup(ctx
, instr
, src
);
7614 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
7615 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !nir_src_is_divergent(instr
->src
[1]))
7616 tid
= bld
.as_uniform(tid
);
7617 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7618 if (src
.regClass() == v1b
|| src
.regClass() == v2b
) {
7619 Temp tmp
= bld
.tmp(v1
);
7620 tmp
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), tmp
);
7621 if (dst
.type() == RegType::vgpr
)
7622 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(src
.regClass() == v1b
? v3b
: v2b
), tmp
);
7624 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
7625 } else if (src
.regClass() == v1
) {
7626 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
7627 } else if (src
.regClass() == v2
) {
7628 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7629 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7630 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
7631 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
7632 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7633 emit_split_vector(ctx
, dst
, 2);
7634 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
7635 assert(src
.regClass() == bld
.lm
);
7636 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
, tid
);
7637 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7638 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
7639 assert(src
.regClass() == bld
.lm
);
7641 if (ctx
->program
->chip_class
<= GFX7
)
7642 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), src
, tid
);
7643 else if (ctx
->program
->wave_size
== 64)
7644 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
7646 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), tid
, src
);
7647 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
7648 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
7649 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
), dst
);
7651 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7652 nir_print_instr(&instr
->instr
, stderr
);
7653 fprintf(stderr
, "\n");
7658 case nir_intrinsic_load_sample_id
: {
7659 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7660 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
7663 case nir_intrinsic_load_sample_mask_in
: {
7664 visit_load_sample_mask_in(ctx
, instr
);
7667 case nir_intrinsic_read_first_invocation
: {
7668 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7669 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7670 if (src
.regClass() == v1b
|| src
.regClass() == v2b
|| src
.regClass() == v1
) {
7672 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
7674 } else if (src
.regClass() == v2
) {
7675 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7676 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7677 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
7678 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
7679 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7680 emit_split_vector(ctx
, dst
, 2);
7681 } else if (instr
->dest
.ssa
.bit_size
== 1) {
7682 assert(src
.regClass() == bld
.lm
);
7683 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
,
7684 bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)));
7685 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7686 } else if (src
.regClass() == s1
) {
7687 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
7688 } else if (src
.regClass() == s2
) {
7689 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
7691 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7692 nir_print_instr(&instr
->instr
, stderr
);
7693 fprintf(stderr
, "\n");
7697 case nir_intrinsic_vote_all
: {
7698 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7699 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7700 assert(src
.regClass() == bld
.lm
);
7701 assert(dst
.regClass() == bld
.lm
);
7703 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
7704 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
7705 bld
.sop1(Builder::s_not
, Definition(dst
), bld
.def(s1
, scc
), cond
);
7708 case nir_intrinsic_vote_any
: {
7709 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7710 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7711 assert(src
.regClass() == bld
.lm
);
7712 assert(dst
.regClass() == bld
.lm
);
7714 Temp tmp
= bool_to_scalar_condition(ctx
, src
);
7715 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7718 case nir_intrinsic_reduce
:
7719 case nir_intrinsic_inclusive_scan
:
7720 case nir_intrinsic_exclusive_scan
: {
7721 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7722 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7723 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
7724 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
7725 nir_intrinsic_cluster_size(instr
) : 0;
7726 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: ctx
->program
->wave_size
, ctx
->program
->wave_size
));
7728 if (!nir_src_is_divergent(instr
->src
[0]) && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
7729 emit_uniform_subgroup(ctx
, instr
, src
);
7730 } else if (instr
->dest
.ssa
.bit_size
== 1) {
7731 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
7733 else if (op
== nir_op_iadd
)
7735 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
7737 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
7739 switch (instr
->intrinsic
) {
7740 case nir_intrinsic_reduce
:
7741 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
7743 case nir_intrinsic_exclusive_scan
:
7744 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
7746 case nir_intrinsic_inclusive_scan
:
7747 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
7752 } else if (cluster_size
== 1) {
7753 bld
.copy(Definition(dst
), src
);
7755 unsigned bit_size
= instr
->src
[0].ssa
->bit_size
;
7757 src
= emit_extract_vector(ctx
, src
, 0, RegClass::get(RegType::vgpr
, bit_size
/ 8));
7761 #define CASEI(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : (bit_size == 8) ? name##8 : name##64; break;
7762 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7777 unreachable("unknown reduction op");
7783 switch (instr
->intrinsic
) {
7784 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
7785 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
7786 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
7788 unreachable("unknown reduce intrinsic");
7791 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
7792 reduce
->operands
[0] = Operand(src
);
7793 // filled in by aco_reduce_assign.cpp, used internally as part of the
7795 assert(dst
.size() == 1 || dst
.size() == 2);
7796 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
7797 reduce
->operands
[2] = Operand(v1
.as_linear());
7799 Temp tmp_dst
= bld
.tmp(dst
.regClass());
7800 reduce
->definitions
[0] = Definition(tmp_dst
);
7801 reduce
->definitions
[1] = bld
.def(ctx
->program
->lane_mask
); // used internally
7802 reduce
->definitions
[2] = Definition();
7803 reduce
->definitions
[3] = Definition(scc
, s1
);
7804 reduce
->definitions
[4] = Definition();
7805 reduce
->reduce_op
= reduce_op
;
7806 reduce
->cluster_size
= cluster_size
;
7807 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
7809 emit_wqm(ctx
, tmp_dst
, dst
);
7813 case nir_intrinsic_quad_broadcast
: {
7814 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7815 if (!nir_dest_is_divergent(instr
->dest
)) {
7816 emit_uniform_subgroup(ctx
, instr
, src
);
7818 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7819 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
7820 uint32_t dpp_ctrl
= dpp_quad_perm(lane
, lane
, lane
, lane
);
7822 if (instr
->dest
.ssa
.bit_size
== 1) {
7823 assert(src
.regClass() == bld
.lm
);
7824 assert(dst
.regClass() == bld
.lm
);
7825 uint32_t half_mask
= 0x11111111u
<< lane
;
7826 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
7827 Temp tmp
= bld
.tmp(bld
.lm
);
7828 bld
.sop1(Builder::s_wqm
, Definition(tmp
),
7829 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), mask_tmp
,
7830 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
))));
7831 emit_wqm(ctx
, tmp
, dst
);
7832 } else if (instr
->dest
.ssa
.bit_size
== 8) {
7833 Temp tmp
= bld
.tmp(v1
);
7834 if (ctx
->program
->chip_class
>= GFX8
)
7835 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7837 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), tmp
);
7838 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v3b
), tmp
);
7839 } else if (instr
->dest
.ssa
.bit_size
== 16) {
7840 Temp tmp
= bld
.tmp(v1
);
7841 if (ctx
->program
->chip_class
>= GFX8
)
7842 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7844 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), tmp
);
7845 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v2b
), tmp
);
7846 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7847 if (ctx
->program
->chip_class
>= GFX8
)
7848 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), dst
);
7850 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), dst
);
7851 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7852 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7853 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7854 if (ctx
->program
->chip_class
>= GFX8
) {
7855 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7856 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7858 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, (1 << 15) | dpp_ctrl
));
7859 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, (1 << 15) | dpp_ctrl
));
7861 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7862 emit_split_vector(ctx
, dst
, 2);
7864 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7865 nir_print_instr(&instr
->instr
, stderr
);
7866 fprintf(stderr
, "\n");
7871 case nir_intrinsic_quad_swap_horizontal
:
7872 case nir_intrinsic_quad_swap_vertical
:
7873 case nir_intrinsic_quad_swap_diagonal
:
7874 case nir_intrinsic_quad_swizzle_amd
: {
7875 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7876 if (!nir_dest_is_divergent(instr
->dest
)) {
7877 emit_uniform_subgroup(ctx
, instr
, src
);
7880 uint16_t dpp_ctrl
= 0;
7881 switch (instr
->intrinsic
) {
7882 case nir_intrinsic_quad_swap_horizontal
:
7883 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
7885 case nir_intrinsic_quad_swap_vertical
:
7886 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
7888 case nir_intrinsic_quad_swap_diagonal
:
7889 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
7891 case nir_intrinsic_quad_swizzle_amd
:
7892 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
7897 if (ctx
->program
->chip_class
< GFX8
)
7898 dpp_ctrl
|= (1 << 15);
7900 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7901 if (instr
->dest
.ssa
.bit_size
== 1) {
7902 assert(src
.regClass() == bld
.lm
);
7903 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
7904 if (ctx
->program
->chip_class
>= GFX8
)
7905 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7907 src
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7908 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
7909 emit_wqm(ctx
, tmp
, dst
);
7910 } else if (instr
->dest
.ssa
.bit_size
== 8) {
7911 Temp tmp
= bld
.tmp(v1
);
7912 if (ctx
->program
->chip_class
>= GFX8
)
7913 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7915 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7916 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v3b
), tmp
);
7917 } else if (instr
->dest
.ssa
.bit_size
== 16) {
7918 Temp tmp
= bld
.tmp(v1
);
7919 if (ctx
->program
->chip_class
>= GFX8
)
7920 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7922 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7923 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v2b
), tmp
);
7924 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7926 if (ctx
->program
->chip_class
>= GFX8
)
7927 tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7929 tmp
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7930 emit_wqm(ctx
, tmp
, dst
);
7931 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7932 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7933 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7934 if (ctx
->program
->chip_class
>= GFX8
) {
7935 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7936 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7938 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7939 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7941 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7942 emit_split_vector(ctx
, dst
, 2);
7944 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7945 nir_print_instr(&instr
->instr
, stderr
);
7946 fprintf(stderr
, "\n");
7950 case nir_intrinsic_masked_swizzle_amd
: {
7951 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7952 if (!nir_dest_is_divergent(instr
->dest
)) {
7953 emit_uniform_subgroup(ctx
, instr
, src
);
7956 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7957 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
7958 if (instr
->dest
.ssa
.bit_size
== 1) {
7959 assert(src
.regClass() == bld
.lm
);
7960 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
7961 src
= emit_masked_swizzle(ctx
, bld
, src
, mask
);
7962 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
7963 emit_wqm(ctx
, tmp
, dst
);
7964 } else if (dst
.regClass() == v1b
) {
7965 Temp tmp
= emit_wqm(ctx
, emit_masked_swizzle(ctx
, bld
, src
, mask
));
7966 emit_extract_vector(ctx
, tmp
, 0, dst
);
7967 } else if (dst
.regClass() == v2b
) {
7968 Temp tmp
= emit_wqm(ctx
, emit_masked_swizzle(ctx
, bld
, src
, mask
));
7969 emit_extract_vector(ctx
, tmp
, 0, dst
);
7970 } else if (dst
.regClass() == v1
) {
7971 emit_wqm(ctx
, emit_masked_swizzle(ctx
, bld
, src
, mask
), dst
);
7972 } else if (dst
.regClass() == v2
) {
7973 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7974 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7975 lo
= emit_wqm(ctx
, emit_masked_swizzle(ctx
, bld
, lo
, mask
));
7976 hi
= emit_wqm(ctx
, emit_masked_swizzle(ctx
, bld
, hi
, mask
));
7977 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7978 emit_split_vector(ctx
, dst
, 2);
7980 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7981 nir_print_instr(&instr
->instr
, stderr
);
7982 fprintf(stderr
, "\n");
7986 case nir_intrinsic_write_invocation_amd
: {
7987 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
7988 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
7989 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
7990 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7991 if (dst
.regClass() == v1
) {
7992 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7993 emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val
, lane
, src
), dst
);
7994 } else if (dst
.regClass() == v2
) {
7995 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
7996 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
7997 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
7998 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
7999 Temp lo
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_lo
, lane
, src_hi
));
8000 Temp hi
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_hi
, lane
, src_hi
));
8001 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
8002 emit_split_vector(ctx
, dst
, 2);
8004 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
8005 nir_print_instr(&instr
->instr
, stderr
);
8006 fprintf(stderr
, "\n");
8010 case nir_intrinsic_mbcnt_amd
: {
8011 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
8012 RegClass rc
= RegClass(src
.type(), 1);
8013 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
8014 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
8015 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8016 Temp wqm_tmp
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(mask_lo
), Operand(mask_hi
));
8017 emit_wqm(ctx
, wqm_tmp
, dst
);
8020 case nir_intrinsic_load_helper_invocation
: {
8021 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8022 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
8023 ctx
->block
->kind
|= block_kind_needs_lowering
;
8024 ctx
->program
->needs_exact
= true;
8027 case nir_intrinsic_is_helper_invocation
: {
8028 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8029 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
8030 ctx
->block
->kind
|= block_kind_needs_lowering
;
8031 ctx
->program
->needs_exact
= true;
8034 case nir_intrinsic_demote
:
8035 bld
.pseudo(aco_opcode::p_demote_to_helper
, Operand(-1u));
8037 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
8038 ctx
->cf_info
.exec_potentially_empty_discard
= true;
8039 ctx
->block
->kind
|= block_kind_uses_demote
;
8040 ctx
->program
->needs_exact
= true;
8042 case nir_intrinsic_demote_if
: {
8043 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
8044 assert(src
.regClass() == bld
.lm
);
8045 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
8046 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
8048 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
8049 ctx
->cf_info
.exec_potentially_empty_discard
= true;
8050 ctx
->block
->kind
|= block_kind_uses_demote
;
8051 ctx
->program
->needs_exact
= true;
8054 case nir_intrinsic_first_invocation
: {
8055 emit_wqm(ctx
, bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)),
8056 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
8059 case nir_intrinsic_shader_clock
: {
8061 nir_intrinsic_memory_scope(instr
) == NIR_SCOPE_DEVICE
?
8062 aco_opcode::s_memrealtime
: aco_opcode::s_memtime
;
8063 bld
.smem(opcode
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
8064 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
8067 case nir_intrinsic_load_vertex_id_zero_base
: {
8068 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8069 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
8072 case nir_intrinsic_load_first_vertex
: {
8073 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8074 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
8077 case nir_intrinsic_load_base_instance
: {
8078 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8079 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
8082 case nir_intrinsic_load_instance_id
: {
8083 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8084 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
8087 case nir_intrinsic_load_draw_id
: {
8088 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8089 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
8092 case nir_intrinsic_load_invocation_id
: {
8093 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8095 if (ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
8096 if (ctx
->options
->chip_class
>= GFX10
)
8097 bld
.vop2_e64(aco_opcode::v_and_b32
, Definition(dst
), Operand(127u), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
8099 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
8100 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
8101 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(dst
),
8102 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
), Operand(8u), Operand(5u));
8104 unreachable("Unsupported stage for load_invocation_id");
8109 case nir_intrinsic_load_primitive_id
: {
8110 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8112 switch (ctx
->shader
->info
.stage
) {
8113 case MESA_SHADER_GEOMETRY
:
8114 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
));
8116 case MESA_SHADER_TESS_CTRL
:
8117 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tcs_patch_id
));
8119 case MESA_SHADER_TESS_EVAL
:
8120 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tes_patch_id
));
8123 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8128 case nir_intrinsic_load_patch_vertices_in
: {
8129 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
8130 ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
8132 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8133 bld
.copy(Definition(dst
), Operand(ctx
->args
->options
->key
.tcs
.input_vertices
));
8136 case nir_intrinsic_emit_vertex_with_counter
: {
8137 visit_emit_vertex_with_counter(ctx
, instr
);
8140 case nir_intrinsic_end_primitive_with_counter
: {
8141 unsigned stream
= nir_intrinsic_stream_id(instr
);
8142 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(true, false, stream
));
8145 case nir_intrinsic_set_vertex_count
: {
8146 /* unused, the HW keeps track of this for us */
8150 fprintf(stderr
, "Unimplemented intrinsic instr: ");
8151 nir_print_instr(&instr
->instr
, stderr
);
8152 fprintf(stderr
, "\n");
8160 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
8161 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
8162 enum glsl_base_type
*stype
)
8164 nir_deref_instr
*texture_deref_instr
= NULL
;
8165 nir_deref_instr
*sampler_deref_instr
= NULL
;
8168 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
8169 switch (instr
->src
[i
].src_type
) {
8170 case nir_tex_src_texture_deref
:
8171 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
8173 case nir_tex_src_sampler_deref
:
8174 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
8176 case nir_tex_src_plane
:
8177 plane
= nir_src_as_int(instr
->src
[i
].src
);
8184 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
8186 if (!sampler_deref_instr
)
8187 sampler_deref_instr
= texture_deref_instr
;
8190 assert(instr
->op
!= nir_texop_txf_ms
&&
8191 instr
->op
!= nir_texop_samples_identical
);
8192 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
8193 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
8194 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
8195 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
8196 } else if (instr
->op
== nir_texop_fragment_mask_fetch
) {
8197 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
8199 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
8202 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
8204 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
8205 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8206 Builder
bld(ctx
->program
, ctx
->block
);
8208 /* to avoid unnecessary moves, we split and recombine sampler and image */
8209 Temp img
[8] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
),
8210 bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
8211 Temp samp
[4] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
8212 bld
.pseudo(aco_opcode::p_split_vector
, Definition(img
[0]), Definition(img
[1]),
8213 Definition(img
[2]), Definition(img
[3]), Definition(img
[4]),
8214 Definition(img
[5]), Definition(img
[6]), Definition(img
[7]), *res_ptr
);
8215 bld
.pseudo(aco_opcode::p_split_vector
, Definition(samp
[0]), Definition(samp
[1]),
8216 Definition(samp
[2]), Definition(samp
[3]), *samp_ptr
);
8218 samp
[0] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), samp
[0], img
[7]);
8219 *res_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
8220 img
[0], img
[1], img
[2], img
[3],
8221 img
[4], img
[5], img
[6], img
[7]);
8222 *samp_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
8223 samp
[0], samp
[1], samp
[2], samp
[3]);
8226 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
8227 instr
->op
== nir_texop_samples_identical
))
8228 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
8231 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
8232 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
8234 Builder
bld(ctx
->program
, ctx
->block
);
8236 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
8237 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
8238 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
8240 Operand
neg_one(0xbf800000u
);
8241 Operand
one(0x3f800000u
);
8242 Operand
two(0x40000000u
);
8243 Operand
four(0x40800000u
);
8245 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), ma
);
8246 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
8247 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
8249 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), four
, id
);
8250 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(bld
.lm
), two
, id
);
8251 is_ma_y
= bld
.sop2(Builder::s_andn2
, bld
.hint_vcc(bld
.def(bld
.lm
)), is_ma_y
, is_ma_z
);
8252 Temp is_not_ma_x
= bld
.sop2(aco_opcode::s_or_b64
, bld
.hint_vcc(bld
.def(bld
.lm
)), bld
.def(s1
, scc
), is_ma_z
, is_ma_y
);
8255 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
8256 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8257 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
8259 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
8262 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
8263 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
8264 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
8267 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8268 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
8270 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
8271 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
8274 void prepare_cube_coords(isel_context
*ctx
, std::vector
<Temp
>& coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
8276 Builder
bld(ctx
->program
, ctx
->block
);
8277 Temp ma
, tc
, sc
, id
;
8280 coords
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[3]);
8282 // see comment in ac_prepare_cube_coords()
8283 if (ctx
->options
->chip_class
<= GFX8
)
8284 coords
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coords
[3]);
8287 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8289 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
8290 vop3a
->operands
[0] = Operand(ma
);
8291 vop3a
->abs
[0] = true;
8292 Temp invma
= bld
.tmp(v1
);
8293 vop3a
->definitions
[0] = Definition(invma
);
8294 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
8296 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8298 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
8300 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8302 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
8304 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8307 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
8308 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
8310 for (unsigned i
= 0; i
< 2; i
++) {
8311 // see comment in ac_prepare_cube_coords()
8313 Temp deriv_sc
, deriv_tc
;
8314 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
8315 &deriv_ma
, &deriv_sc
, &deriv_tc
);
8317 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
8319 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
8320 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
8321 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
8322 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
8323 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
8324 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
8325 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
8328 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
8329 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
8333 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coords
[3], id
, Operand(0x41000000u
/*8.0*/));
8340 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
8342 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
8344 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
8345 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
8348 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
8349 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
8350 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
8354 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
8356 Builder
bld(ctx
->program
, ctx
->block
);
8357 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
8358 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false,
8359 has_clamped_lod
= false;
8360 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), compare
= Temp(), sample_index
= Temp(),
8361 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(),
8362 clamped_lod
= Temp();
8363 std::vector
<Temp
> coords
;
8364 std::vector
<Temp
> derivs
;
8365 nir_const_value
*sample_index_cv
= NULL
;
8366 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
8367 enum glsl_base_type stype
;
8368 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
8370 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
8371 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
8372 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
8373 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
8375 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
8376 switch (instr
->src
[i
].src_type
) {
8377 case nir_tex_src_coord
: {
8378 Temp coord
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8379 for (unsigned i
= 0; i
< coord
.size(); i
++)
8380 coords
.emplace_back(emit_extract_vector(ctx
, coord
, i
, v1
));
8383 case nir_tex_src_bias
:
8384 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8387 case nir_tex_src_lod
: {
8388 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
8390 if (val
&& val
->f32
<= 0.0) {
8393 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8398 case nir_tex_src_min_lod
:
8399 clamped_lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8400 has_clamped_lod
= true;
8402 case nir_tex_src_comparator
:
8403 if (instr
->is_shadow
) {
8404 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8408 case nir_tex_src_offset
:
8409 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8410 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
8413 case nir_tex_src_ddx
:
8414 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8417 case nir_tex_src_ddy
:
8418 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8421 case nir_tex_src_ms_index
:
8422 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8423 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
8424 has_sample_index
= true;
8426 case nir_tex_src_texture_offset
:
8427 case nir_tex_src_sampler_offset
:
8433 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
8434 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
8436 if (instr
->op
== nir_texop_texture_samples
) {
8437 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
8439 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
8440 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
8441 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 */));
8443 Operand default_sample
= Operand(1u);
8444 if (ctx
->options
->robust_buffer_access
) {
8445 /* Extract the second dword of the descriptor, if it's
8446 * all zero, then it's a null descriptor.
8448 Temp dword1
= emit_extract_vector(ctx
, resource
, 1, s1
);
8449 Temp is_non_null_descriptor
= bld
.sopc(aco_opcode::s_cmp_gt_u32
, bld
.def(s1
, scc
), dword1
, Operand(0u));
8450 default_sample
= Operand(is_non_null_descriptor
);
8453 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
8454 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
8455 samples
, default_sample
, bld
.scc(is_msaa
));
8459 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
8460 aco_ptr
<Instruction
> tmp_instr
;
8461 Temp acc
, pack
= Temp();
8463 uint32_t pack_const
= 0;
8464 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8465 if (!const_offset
[i
])
8467 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
8470 if (offset
.type() == RegType::sgpr
) {
8471 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8472 if (const_offset
[i
])
8475 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
8476 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
8479 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
8482 if (pack
== Temp()) {
8485 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
8489 if (pack_const
&& pack
!= Temp())
8490 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
8492 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8493 if (const_offset
[i
])
8496 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
8497 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
8500 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
8503 if (pack
== Temp()) {
8506 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
8510 if (pack_const
&& pack
!= Temp())
8511 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
8513 if (pack_const
&& pack
== Temp())
8514 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
8515 else if (pack
== Temp())
8521 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
8522 prepare_cube_coords(ctx
, coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
8524 /* pack derivatives */
8525 if (has_ddx
|| has_ddy
) {
8526 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
8527 assert(has_ddx
&& has_ddy
&& ddx
.size() == 1 && ddy
.size() == 1);
8528 Temp zero
= bld
.copy(bld
.def(v1
), Operand(0u));
8529 derivs
= {ddx
, zero
, ddy
, zero
};
8531 for (unsigned i
= 0; has_ddx
&& i
< ddx
.size(); i
++)
8532 derivs
.emplace_back(emit_extract_vector(ctx
, ddx
, i
, v1
));
8533 for (unsigned i
= 0; has_ddy
&& i
< ddy
.size(); i
++)
8534 derivs
.emplace_back(emit_extract_vector(ctx
, ddy
, i
, v1
));
8539 if (instr
->coord_components
> 1 &&
8540 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8542 instr
->op
!= nir_texop_txf
)
8543 coords
[1] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[1]);
8545 if (instr
->coord_components
> 2 &&
8546 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
8547 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
8548 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
8549 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
8551 instr
->op
!= nir_texop_txf
&&
8552 instr
->op
!= nir_texop_txf_ms
&&
8553 instr
->op
!= nir_texop_fragment_fetch
&&
8554 instr
->op
!= nir_texop_fragment_mask_fetch
)
8555 coords
[2] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[2]);
8557 if (ctx
->options
->chip_class
== GFX9
&&
8558 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8559 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
8560 assert(coords
.size() > 0 && coords
.size() < 3);
8562 coords
.insert(std::next(coords
.begin()), bld
.copy(bld
.def(v1
), instr
->op
== nir_texop_txf
?
8563 Operand((uint32_t) 0) :
8564 Operand((uint32_t) 0x3f000000)));
8567 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
8569 if (instr
->op
== nir_texop_samples_identical
)
8570 resource
= fmask_ptr
;
8572 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
8573 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
8574 instr
->op
!= nir_texop_txs
&&
8575 instr
->op
!= nir_texop_fragment_fetch
&&
8576 instr
->op
!= nir_texop_fragment_mask_fetch
) {
8577 assert(has_sample_index
);
8578 Operand
op(sample_index
);
8579 if (sample_index_cv
)
8580 op
= Operand(sample_index_cv
->u32
);
8581 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
8584 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
8585 for (unsigned i
= 0; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
8586 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
8587 coords
[i
] = bld
.vadd32(bld
.def(v1
), coords
[i
], off
);
8592 /* Build tex instruction */
8593 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
8594 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
8595 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
8597 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8600 /* gather4 selects the component by dmask and always returns vec4 */
8601 if (instr
->op
== nir_texop_tg4
) {
8602 assert(instr
->dest
.ssa
.num_components
== 4);
8603 if (instr
->is_shadow
)
8606 dmask
= 1 << instr
->component
;
8607 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
8608 tmp_dst
= bld
.tmp(v4
);
8609 } else if (instr
->op
== nir_texop_samples_identical
) {
8610 tmp_dst
= bld
.tmp(v1
);
8611 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
8612 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
8615 aco_ptr
<MIMG_instruction
> tex
;
8616 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
8618 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
8620 bool div_by_6
= instr
->op
== nir_texop_txs
&&
8621 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
8624 if (tmp_dst
.id() == dst
.id() && div_by_6
)
8625 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
8627 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
8628 tex
->operands
[0] = Operand(resource
);
8629 tex
->operands
[1] = Operand(s4
); /* no sampler */
8630 tex
->operands
[2] = Operand(as_vgpr(ctx
,lod
));
8631 if (ctx
->options
->chip_class
== GFX9
&&
8632 instr
->op
== nir_texop_txs
&&
8633 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8635 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
8636 } else if (instr
->op
== nir_texop_query_levels
) {
8637 tex
->dmask
= 1 << 3;
8642 tex
->definitions
[0] = Definition(tmp_dst
);
8644 tex
->can_reorder
= true;
8645 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8648 /* divide 3rd value by 6 by multiplying with magic number */
8649 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
8650 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
8651 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
8652 assert(instr
->dest
.ssa
.num_components
== 3);
8653 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
8654 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
8655 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
8656 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
8661 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
8665 Temp tg4_compare_cube_wa64
= Temp();
8667 if (tg4_integer_workarounds
) {
8668 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
8669 tex
->operands
[0] = Operand(resource
);
8670 tex
->operands
[1] = Operand(s4
); /* no sampler */
8671 tex
->operands
[2] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
8675 Temp size
= bld
.tmp(v2
);
8676 tex
->definitions
[0] = Definition(size
);
8677 tex
->can_reorder
= true;
8678 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8679 emit_split_vector(ctx
, size
, size
.size());
8682 for (unsigned i
= 0; i
< 2; i
++) {
8683 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
8684 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
8685 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
8686 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
8689 Temp new_coords
[2] = {
8690 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[0], half_texel
[0]),
8691 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[1], half_texel
[1])
8694 if (tg4_integer_cube_workaround
) {
8695 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8696 Temp desc
[resource
.size()];
8697 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
8698 Format::PSEUDO
, 1, resource
.size())};
8699 split
->operands
[0] = Operand(resource
);
8700 for (unsigned i
= 0; i
< resource
.size(); i
++) {
8701 desc
[i
] = bld
.tmp(s1
);
8702 split
->definitions
[i
] = Definition(desc
[i
]);
8704 ctx
->block
->instructions
.emplace_back(std::move(split
));
8706 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
8707 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
8708 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
8711 if (stype
== GLSL_TYPE_UINT
) {
8712 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
8713 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
8714 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
8715 bld
.scc(compare_cube_wa
));
8717 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
8718 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
8719 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
8720 bld
.scc(compare_cube_wa
));
8722 tg4_compare_cube_wa64
= bld
.tmp(bld
.lm
);
8723 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
8725 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
8727 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
8728 Operand((uint32_t)C_008F14_NUM_FORMAT
));
8729 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
8731 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
8732 Format::PSEUDO
, resource
.size(), 1)};
8733 for (unsigned i
= 0; i
< resource
.size(); i
++)
8734 vec
->operands
[i
] = Operand(desc
[i
]);
8735 resource
= bld
.tmp(resource
.regClass());
8736 vec
->definitions
[0] = Definition(resource
);
8737 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8739 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8740 new_coords
[0], coords
[0], tg4_compare_cube_wa64
);
8741 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8742 new_coords
[1], coords
[1], tg4_compare_cube_wa64
);
8744 coords
[0] = new_coords
[0];
8745 coords
[1] = new_coords
[1];
8748 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
8749 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8751 assert(coords
.size() == 1);
8752 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
8756 op
= aco_opcode::buffer_load_format_x
; break;
8758 op
= aco_opcode::buffer_load_format_xy
; break;
8760 op
= aco_opcode::buffer_load_format_xyz
; break;
8762 op
= aco_opcode::buffer_load_format_xyzw
; break;
8764 unreachable("Tex instruction loads more than 4 components.");
8767 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8768 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
8771 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
8773 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
8774 mubuf
->operands
[0] = Operand(resource
);
8775 mubuf
->operands
[1] = Operand(coords
[0]);
8776 mubuf
->operands
[2] = Operand((uint32_t) 0);
8777 mubuf
->definitions
[0] = Definition(tmp_dst
);
8778 mubuf
->idxen
= true;
8779 mubuf
->can_reorder
= true;
8780 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
8782 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
8786 /* gather MIMG address components */
8787 std::vector
<Temp
> args
;
8789 args
.emplace_back(offset
);
8791 args
.emplace_back(bias
);
8793 args
.emplace_back(compare
);
8795 args
.insert(args
.end(), derivs
.begin(), derivs
.end());
8797 args
.insert(args
.end(), coords
.begin(), coords
.end());
8798 if (has_sample_index
)
8799 args
.emplace_back(sample_index
);
8801 args
.emplace_back(lod
);
8802 if (has_clamped_lod
)
8803 args
.emplace_back(clamped_lod
);
8805 Temp arg
= bld
.tmp(RegClass(RegType::vgpr
, args
.size()));
8806 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
8807 vec
->definitions
[0] = Definition(arg
);
8808 for (unsigned i
= 0; i
< args
.size(); i
++)
8809 vec
->operands
[i
] = Operand(args
[i
]);
8810 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8813 if (instr
->op
== nir_texop_txf
||
8814 instr
->op
== nir_texop_txf_ms
||
8815 instr
->op
== nir_texop_samples_identical
||
8816 instr
->op
== nir_texop_fragment_fetch
||
8817 instr
->op
== nir_texop_fragment_mask_fetch
) {
8818 aco_opcode op
= level_zero
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
8819 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 3, 1));
8820 tex
->operands
[0] = Operand(resource
);
8821 tex
->operands
[1] = Operand(s4
); /* no sampler */
8822 tex
->operands
[2] = Operand(arg
);
8827 tex
->definitions
[0] = Definition(tmp_dst
);
8828 tex
->can_reorder
= true;
8829 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8831 if (instr
->op
== nir_texop_samples_identical
) {
8832 assert(dmask
== 1 && dst
.regClass() == v1
);
8833 assert(dst
.id() != tmp_dst
.id());
8835 Temp tmp
= bld
.tmp(bld
.lm
);
8836 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
8837 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
8840 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
8845 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8846 aco_opcode opcode
= aco_opcode::image_sample
;
8847 if (has_offset
) { /* image_sample_*_o */
8848 if (has_clamped_lod
) {
8850 opcode
= aco_opcode::image_sample_c_cl_o
;
8852 opcode
= aco_opcode::image_sample_c_d_cl_o
;
8854 opcode
= aco_opcode::image_sample_c_b_cl_o
;
8856 opcode
= aco_opcode::image_sample_cl_o
;
8858 opcode
= aco_opcode::image_sample_d_cl_o
;
8860 opcode
= aco_opcode::image_sample_b_cl_o
;
8862 } else if (has_compare
) {
8863 opcode
= aco_opcode::image_sample_c_o
;
8865 opcode
= aco_opcode::image_sample_c_d_o
;
8867 opcode
= aco_opcode::image_sample_c_b_o
;
8869 opcode
= aco_opcode::image_sample_c_lz_o
;
8871 opcode
= aco_opcode::image_sample_c_l_o
;
8873 opcode
= aco_opcode::image_sample_o
;
8875 opcode
= aco_opcode::image_sample_d_o
;
8877 opcode
= aco_opcode::image_sample_b_o
;
8879 opcode
= aco_opcode::image_sample_lz_o
;
8881 opcode
= aco_opcode::image_sample_l_o
;
8883 } else if (has_clamped_lod
) { /* image_sample_*_cl */
8885 opcode
= aco_opcode::image_sample_c_cl
;
8887 opcode
= aco_opcode::image_sample_c_d_cl
;
8889 opcode
= aco_opcode::image_sample_c_b_cl
;
8891 opcode
= aco_opcode::image_sample_cl
;
8893 opcode
= aco_opcode::image_sample_d_cl
;
8895 opcode
= aco_opcode::image_sample_b_cl
;
8897 } else { /* no offset */
8899 opcode
= aco_opcode::image_sample_c
;
8901 opcode
= aco_opcode::image_sample_c_d
;
8903 opcode
= aco_opcode::image_sample_c_b
;
8905 opcode
= aco_opcode::image_sample_c_lz
;
8907 opcode
= aco_opcode::image_sample_c_l
;
8909 opcode
= aco_opcode::image_sample
;
8911 opcode
= aco_opcode::image_sample_d
;
8913 opcode
= aco_opcode::image_sample_b
;
8915 opcode
= aco_opcode::image_sample_lz
;
8917 opcode
= aco_opcode::image_sample_l
;
8921 if (instr
->op
== nir_texop_tg4
) {
8922 if (has_offset
) { /* image_gather4_*_o */
8924 opcode
= aco_opcode::image_gather4_c_lz_o
;
8926 opcode
= aco_opcode::image_gather4_c_l_o
;
8928 opcode
= aco_opcode::image_gather4_c_b_o
;
8930 opcode
= aco_opcode::image_gather4_lz_o
;
8932 opcode
= aco_opcode::image_gather4_l_o
;
8934 opcode
= aco_opcode::image_gather4_b_o
;
8938 opcode
= aco_opcode::image_gather4_c_lz
;
8940 opcode
= aco_opcode::image_gather4_c_l
;
8942 opcode
= aco_opcode::image_gather4_c_b
;
8944 opcode
= aco_opcode::image_gather4_lz
;
8946 opcode
= aco_opcode::image_gather4_l
;
8948 opcode
= aco_opcode::image_gather4_b
;
8951 } else if (instr
->op
== nir_texop_lod
) {
8952 opcode
= aco_opcode::image_get_lod
;
8955 /* we don't need the bias, sample index, compare value or offset to be
8956 * computed in WQM but if the p_create_vector copies the coordinates, then it
8957 * needs to be in WQM */
8958 if (ctx
->stage
== fragment_fs
&&
8959 !has_derivs
&& !has_lod
&& !level_zero
&&
8960 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
8961 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
8962 arg
= emit_wqm(ctx
, arg
, bld
.tmp(arg
.regClass()), true);
8964 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
8965 tex
->operands
[0] = Operand(resource
);
8966 tex
->operands
[1] = Operand(sampler
);
8967 tex
->operands
[2] = Operand(arg
);
8971 tex
->definitions
[0] = Definition(tmp_dst
);
8972 tex
->can_reorder
= true;
8973 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8975 if (tg4_integer_cube_workaround
) {
8976 assert(tmp_dst
.id() != dst
.id());
8977 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
8979 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
8981 for (unsigned i
= 0; i
< dst
.size(); i
++) {
8982 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
8984 if (stype
== GLSL_TYPE_UINT
)
8985 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
8987 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
8988 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
8990 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
8991 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
8992 val
[0], val
[1], val
[2], val
[3]);
8994 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
8995 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
9000 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
, RegClass rc
, bool logical
)
9002 Temp tmp
= get_ssa_temp(ctx
, ssa
);
9003 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
9005 } else if (logical
&& ssa
->bit_size
== 1 && ssa
->parent_instr
->type
== nir_instr_type_load_const
) {
9006 if (ctx
->program
->wave_size
== 64)
9007 return Operand(nir_instr_as_load_const(ssa
->parent_instr
)->value
[0].b
? UINT64_MAX
: 0u);
9009 return Operand(nir_instr_as_load_const(ssa
->parent_instr
)->value
[0].b
? UINT32_MAX
: 0u);
9011 return Operand(tmp
);
9015 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
9017 aco_ptr
<Pseudo_instruction
> phi
;
9018 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
9019 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == ctx
->program
->lane_mask
);
9021 bool logical
= !dst
.is_linear() || nir_dest_is_divergent(instr
->dest
);
9022 logical
|= ctx
->block
->kind
& block_kind_merge
;
9023 aco_opcode opcode
= logical
? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
9025 /* we want a sorted list of sources, since the predecessor list is also sorted */
9026 std::map
<unsigned, nir_ssa_def
*> phi_src
;
9027 nir_foreach_phi_src(src
, instr
)
9028 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
9030 std::vector
<unsigned>& preds
= logical
? ctx
->block
->logical_preds
: ctx
->block
->linear_preds
;
9031 unsigned num_operands
= 0;
9032 Operand operands
[std::max(exec_list_length(&instr
->srcs
), (unsigned)preds
.size()) + 1];
9033 unsigned num_defined
= 0;
9034 unsigned cur_pred_idx
= 0;
9035 for (std::pair
<unsigned, nir_ssa_def
*> src
: phi_src
) {
9036 if (cur_pred_idx
< preds
.size()) {
9037 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
9038 unsigned block
= ctx
->cf_info
.nir_to_aco
[src
.first
];
9039 unsigned skipped
= 0;
9040 while (cur_pred_idx
+ skipped
< preds
.size() && preds
[cur_pred_idx
+ skipped
] != block
)
9042 if (cur_pred_idx
+ skipped
< preds
.size()) {
9043 for (unsigned i
= 0; i
< skipped
; i
++)
9044 operands
[num_operands
++] = Operand(dst
.regClass());
9045 cur_pred_idx
+= skipped
;
9050 /* Handle missing predecessors at the end. This shouldn't happen with loop
9051 * headers and we can't ignore these sources for loop header phis. */
9052 if (!(ctx
->block
->kind
& block_kind_loop_header
) && cur_pred_idx
>= preds
.size())
9055 Operand op
= get_phi_operand(ctx
, src
.second
, dst
.regClass(), logical
);
9056 operands
[num_operands
++] = op
;
9057 num_defined
+= !op
.isUndefined();
9059 /* handle block_kind_continue_or_break at loop exit blocks */
9060 while (cur_pred_idx
++ < preds
.size())
9061 operands
[num_operands
++] = Operand(dst
.regClass());
9063 /* If the loop ends with a break, still add a linear continue edge in case
9064 * that break is divergent or continue_or_break is used. We'll either remove
9065 * this operand later in visit_loop() if it's not necessary or replace the
9066 * undef with something correct. */
9067 if (!logical
&& ctx
->block
->kind
& block_kind_loop_header
) {
9068 nir_loop
*loop
= nir_cf_node_as_loop(instr
->instr
.block
->cf_node
.parent
);
9069 nir_block
*last
= nir_loop_last_block(loop
);
9070 if (last
->successors
[0] != instr
->instr
.block
)
9071 operands
[num_operands
++] = Operand(RegClass());
9074 if (num_defined
== 0) {
9075 Builder
bld(ctx
->program
, ctx
->block
);
9076 if (dst
.regClass() == s1
) {
9077 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
9078 } else if (dst
.regClass() == v1
) {
9079 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
9081 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
9082 for (unsigned i
= 0; i
< dst
.size(); i
++)
9083 vec
->operands
[i
] = Operand(0u);
9084 vec
->definitions
[0] = Definition(dst
);
9085 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9090 /* we can use a linear phi in some cases if one src is undef */
9091 if (dst
.is_linear() && ctx
->block
->kind
& block_kind_merge
&& num_defined
== 1) {
9092 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, num_operands
, 1));
9094 Block
*linear_else
= &ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
9095 Block
*invert
= &ctx
->program
->blocks
[linear_else
->linear_preds
[0]];
9096 assert(invert
->kind
& block_kind_invert
);
9098 unsigned then_block
= invert
->linear_preds
[0];
9100 Block
* insert_block
= NULL
;
9101 for (unsigned i
= 0; i
< num_operands
; i
++) {
9102 Operand op
= operands
[i
];
9103 if (op
.isUndefined())
9105 insert_block
= ctx
->block
->logical_preds
[i
] == then_block
? invert
: ctx
->block
;
9106 phi
->operands
[0] = op
;
9109 assert(insert_block
); /* should be handled by the "num_defined == 0" case above */
9110 phi
->operands
[1] = Operand(dst
.regClass());
9111 phi
->definitions
[0] = Definition(dst
);
9112 insert_block
->instructions
.emplace(insert_block
->instructions
.begin(), std::move(phi
));
9116 /* try to scalarize vector phis */
9117 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
9118 // TODO: scalarize linear phis on divergent ifs
9119 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
9120 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> new_vec
;
9121 for (unsigned i
= 0; can_scalarize
&& (i
< num_operands
); i
++) {
9122 Operand src
= operands
[i
];
9123 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end())
9124 can_scalarize
= false;
9126 if (can_scalarize
) {
9127 unsigned num_components
= instr
->dest
.ssa
.num_components
;
9128 assert(dst
.size() % num_components
== 0);
9129 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
9131 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
9132 for (unsigned k
= 0; k
< num_components
; k
++) {
9133 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
9134 for (unsigned i
= 0; i
< num_operands
; i
++) {
9135 Operand src
= operands
[i
];
9136 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
9138 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
9139 phi
->definitions
[0] = Definition(phi_dst
);
9140 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
9141 new_vec
[k
] = phi_dst
;
9142 vec
->operands
[k
] = Operand(phi_dst
);
9144 vec
->definitions
[0] = Definition(dst
);
9145 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9146 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
9151 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
9152 for (unsigned i
= 0; i
< num_operands
; i
++)
9153 phi
->operands
[i
] = operands
[i
];
9154 phi
->definitions
[0] = Definition(dst
);
9155 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
9159 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
9161 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
9163 assert(dst
.type() == RegType::sgpr
);
9165 if (dst
.size() == 1) {
9166 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
9168 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
9169 for (unsigned i
= 0; i
< dst
.size(); i
++)
9170 vec
->operands
[i
] = Operand(0u);
9171 vec
->definitions
[0] = Definition(dst
);
9172 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9176 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
9178 Builder
bld(ctx
->program
, ctx
->block
);
9179 Block
*logical_target
;
9180 append_logical_end(ctx
->block
);
9181 unsigned idx
= ctx
->block
->index
;
9183 switch (instr
->type
) {
9184 case nir_jump_break
:
9185 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
9186 add_logical_edge(idx
, logical_target
);
9187 ctx
->block
->kind
|= block_kind_break
;
9189 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
9190 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
9191 /* uniform break - directly jump out of the loop */
9192 ctx
->block
->kind
|= block_kind_uniform
;
9193 ctx
->cf_info
.has_branch
= true;
9194 bld
.branch(aco_opcode::p_branch
);
9195 add_linear_edge(idx
, logical_target
);
9198 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
9199 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
9201 case nir_jump_continue
:
9202 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
9203 add_logical_edge(idx
, logical_target
);
9204 ctx
->block
->kind
|= block_kind_continue
;
9206 if (ctx
->cf_info
.parent_if
.is_divergent
) {
9207 /* for potential uniform breaks after this continue,
9208 we must ensure that they are handled correctly */
9209 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
9210 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
9211 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
9213 /* uniform continue - directly jump to the loop header */
9214 ctx
->block
->kind
|= block_kind_uniform
;
9215 ctx
->cf_info
.has_branch
= true;
9216 bld
.branch(aco_opcode::p_branch
);
9217 add_linear_edge(idx
, logical_target
);
9222 fprintf(stderr
, "Unknown NIR jump instr: ");
9223 nir_print_instr(&instr
->instr
, stderr
);
9224 fprintf(stderr
, "\n");
9228 if (ctx
->cf_info
.parent_if
.is_divergent
&& !ctx
->cf_info
.exec_potentially_empty_break
) {
9229 ctx
->cf_info
.exec_potentially_empty_break
= true;
9230 ctx
->cf_info
.exec_potentially_empty_break_depth
= ctx
->cf_info
.loop_nest_depth
;
9233 /* remove critical edges from linear CFG */
9234 bld
.branch(aco_opcode::p_branch
);
9235 Block
* break_block
= ctx
->program
->create_and_insert_block();
9236 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9237 break_block
->kind
|= block_kind_uniform
;
9238 add_linear_edge(idx
, break_block
);
9239 /* the loop_header pointer might be invalidated by this point */
9240 if (instr
->type
== nir_jump_continue
)
9241 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
9242 add_linear_edge(break_block
->index
, logical_target
);
9243 bld
.reset(break_block
);
9244 bld
.branch(aco_opcode::p_branch
);
9246 Block
* continue_block
= ctx
->program
->create_and_insert_block();
9247 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9248 add_linear_edge(idx
, continue_block
);
9249 append_logical_start(continue_block
);
9250 ctx
->block
= continue_block
;
9254 void visit_block(isel_context
*ctx
, nir_block
*block
)
9256 nir_foreach_instr(instr
, block
) {
9257 switch (instr
->type
) {
9258 case nir_instr_type_alu
:
9259 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
9261 case nir_instr_type_load_const
:
9262 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
9264 case nir_instr_type_intrinsic
:
9265 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
9267 case nir_instr_type_tex
:
9268 visit_tex(ctx
, nir_instr_as_tex(instr
));
9270 case nir_instr_type_phi
:
9271 visit_phi(ctx
, nir_instr_as_phi(instr
));
9273 case nir_instr_type_ssa_undef
:
9274 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
9276 case nir_instr_type_deref
:
9278 case nir_instr_type_jump
:
9279 visit_jump(ctx
, nir_instr_as_jump(instr
));
9282 fprintf(stderr
, "Unknown NIR instr type: ");
9283 nir_print_instr(instr
, stderr
);
9284 fprintf(stderr
, "\n");
9289 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9290 ctx
->cf_info
.nir_to_aco
[block
->index
] = ctx
->block
->index
;
9295 static Operand
create_continue_phis(isel_context
*ctx
, unsigned first
, unsigned last
,
9296 aco_ptr
<Instruction
>& header_phi
, Operand
*vals
)
9298 vals
[0] = Operand(header_phi
->definitions
[0].getTemp());
9299 RegClass rc
= vals
[0].regClass();
9301 unsigned loop_nest_depth
= ctx
->program
->blocks
[first
].loop_nest_depth
;
9303 unsigned next_pred
= 1;
9305 for (unsigned idx
= first
+ 1; idx
<= last
; idx
++) {
9306 Block
& block
= ctx
->program
->blocks
[idx
];
9307 if (block
.loop_nest_depth
!= loop_nest_depth
) {
9308 vals
[idx
- first
] = vals
[idx
- 1 - first
];
9312 if (block
.kind
& block_kind_continue
) {
9313 vals
[idx
- first
] = header_phi
->operands
[next_pred
];
9318 bool all_same
= true;
9319 for (unsigned i
= 1; all_same
&& (i
< block
.linear_preds
.size()); i
++)
9320 all_same
= vals
[block
.linear_preds
[i
] - first
] == vals
[block
.linear_preds
[0] - first
];
9324 val
= vals
[block
.linear_preds
[0] - first
];
9326 aco_ptr
<Instruction
> phi(create_instruction
<Pseudo_instruction
>(
9327 aco_opcode::p_linear_phi
, Format::PSEUDO
, block
.linear_preds
.size(), 1));
9328 for (unsigned i
= 0; i
< block
.linear_preds
.size(); i
++)
9329 phi
->operands
[i
] = vals
[block
.linear_preds
[i
] - first
];
9330 val
= Operand(Temp(ctx
->program
->allocateId(), rc
));
9331 phi
->definitions
[0] = Definition(val
.getTemp());
9332 block
.instructions
.emplace(block
.instructions
.begin(), std::move(phi
));
9334 vals
[idx
- first
] = val
;
9337 return vals
[last
- first
];
9340 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
9342 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9343 append_logical_end(ctx
->block
);
9344 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
9345 Builder
bld(ctx
->program
, ctx
->block
);
9346 bld
.branch(aco_opcode::p_branch
);
9347 unsigned loop_preheader_idx
= ctx
->block
->index
;
9349 Block loop_exit
= Block();
9350 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9351 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
9353 Block
* loop_header
= ctx
->program
->create_and_insert_block();
9354 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
9355 loop_header
->kind
|= block_kind_loop_header
;
9356 add_edge(loop_preheader_idx
, loop_header
);
9357 ctx
->block
= loop_header
;
9359 /* emit loop body */
9360 unsigned loop_header_idx
= loop_header
->index
;
9361 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
9362 append_logical_start(ctx
->block
);
9363 bool unreachable
= visit_cf_list(ctx
, &loop
->body
);
9365 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9366 if (!ctx
->cf_info
.has_branch
) {
9367 append_logical_end(ctx
->block
);
9368 if (ctx
->cf_info
.exec_potentially_empty_discard
|| ctx
->cf_info
.exec_potentially_empty_break
) {
9369 /* Discards can result in code running with an empty exec mask.
9370 * This would result in divergent breaks not ever being taken. As a
9371 * workaround, break the loop when the loop mask is empty instead of
9372 * always continuing. */
9373 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
9374 unsigned block_idx
= ctx
->block
->index
;
9376 /* create helper blocks to avoid critical edges */
9377 Block
*break_block
= ctx
->program
->create_and_insert_block();
9378 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9379 break_block
->kind
= block_kind_uniform
;
9380 bld
.reset(break_block
);
9381 bld
.branch(aco_opcode::p_branch
);
9382 add_linear_edge(block_idx
, break_block
);
9383 add_linear_edge(break_block
->index
, &loop_exit
);
9385 Block
*continue_block
= ctx
->program
->create_and_insert_block();
9386 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9387 continue_block
->kind
= block_kind_uniform
;
9388 bld
.reset(continue_block
);
9389 bld
.branch(aco_opcode::p_branch
);
9390 add_linear_edge(block_idx
, continue_block
);
9391 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9393 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9394 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
9395 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
9397 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
9398 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9399 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9401 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9404 bld
.reset(ctx
->block
);
9405 bld
.branch(aco_opcode::p_branch
);
9408 /* Fixup phis in loop header from unreachable blocks.
9409 * has_branch/has_divergent_branch also indicates if the loop ends with a
9410 * break/continue instruction, but we don't emit those if unreachable=true */
9412 assert(ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
);
9413 bool linear
= ctx
->cf_info
.has_branch
;
9414 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9415 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
9416 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
9417 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
9418 /* the last operand should be the one that needs to be removed */
9419 instr
->operands
.pop_back();
9420 } else if (!is_phi(instr
)) {
9426 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9427 * and the previous one shouldn't both happen at once because a break in the
9428 * merge block would get CSE'd */
9429 if (nir_loop_last_block(loop
)->successors
[0] != nir_loop_first_block(loop
)) {
9430 unsigned num_vals
= ctx
->cf_info
.has_branch
? 1 : (ctx
->block
->index
- loop_header_idx
+ 1);
9431 Operand vals
[num_vals
];
9432 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
9433 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
9434 if (ctx
->cf_info
.has_branch
)
9435 instr
->operands
.pop_back();
9437 instr
->operands
.back() = create_continue_phis(ctx
, loop_header_idx
, ctx
->block
->index
, instr
, vals
);
9438 } else if (!is_phi(instr
)) {
9444 ctx
->cf_info
.has_branch
= false;
9446 // TODO: if the loop has not a single exit, we must add one °°
9447 /* emit loop successor block */
9448 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
9449 append_logical_start(ctx
->block
);
9452 // TODO: check if it is beneficial to not branch on continues
9453 /* trim linear phis in loop header */
9454 for (auto&& instr
: loop_entry
->instructions
) {
9455 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
9456 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
9457 new_phi
->definitions
[0] = instr
->definitions
[0];
9458 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
9459 new_phi
->operands
[i
] = instr
->operands
[i
];
9460 /* check that the remaining operands are all the same */
9461 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
9462 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
9463 instr
.swap(new_phi
);
9464 } else if (instr
->opcode
== aco_opcode::p_phi
) {
9473 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
9477 append_logical_end(ctx
->block
);
9478 ctx
->block
->kind
|= block_kind_branch
;
9480 /* branch to linear then block */
9481 assert(cond
.regClass() == ctx
->program
->lane_mask
);
9482 aco_ptr
<Pseudo_branch_instruction
> branch
;
9483 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
9484 branch
->operands
[0] = Operand(cond
);
9485 ctx
->block
->instructions
.push_back(std::move(branch
));
9487 ic
->BB_if_idx
= ctx
->block
->index
;
9488 ic
->BB_invert
= Block();
9489 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9490 /* Invert blocks are intentionally not marked as top level because they
9491 * are not part of the logical cfg. */
9492 ic
->BB_invert
.kind
|= block_kind_invert
;
9493 ic
->BB_endif
= Block();
9494 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9495 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
9497 ic
->exec_potentially_empty_discard_old
= ctx
->cf_info
.exec_potentially_empty_discard
;
9498 ic
->exec_potentially_empty_break_old
= ctx
->cf_info
.exec_potentially_empty_break
;
9499 ic
->exec_potentially_empty_break_depth_old
= ctx
->cf_info
.exec_potentially_empty_break_depth
;
9500 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
9501 ctx
->cf_info
.parent_if
.is_divergent
= true;
9503 /* divergent branches use cbranch_execz */
9504 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9505 ctx
->cf_info
.exec_potentially_empty_break
= false;
9506 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9508 /** emit logical then block */
9509 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
9510 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9511 add_edge(ic
->BB_if_idx
, BB_then_logical
);
9512 ctx
->block
= BB_then_logical
;
9513 append_logical_start(BB_then_logical
);
9516 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
9518 Block
*BB_then_logical
= ctx
->block
;
9519 append_logical_end(BB_then_logical
);
9520 /* branch from logical then block to invert block */
9521 aco_ptr
<Pseudo_branch_instruction
> branch
;
9522 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9523 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
9524 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
9525 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9526 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
9527 BB_then_logical
->kind
|= block_kind_uniform
;
9528 assert(!ctx
->cf_info
.has_branch
);
9529 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9530 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9532 /** emit linear then block */
9533 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
9534 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9535 BB_then_linear
->kind
|= block_kind_uniform
;
9536 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
9537 /* branch from linear then block to invert block */
9538 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9539 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
9540 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
9542 /** emit invert merge block */
9543 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
9544 ic
->invert_idx
= ctx
->block
->index
;
9546 /* branch to linear else block (skip else) */
9547 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
9548 branch
->operands
[0] = Operand(ic
->cond
);
9549 ctx
->block
->instructions
.push_back(std::move(branch
));
9551 ic
->exec_potentially_empty_discard_old
|= ctx
->cf_info
.exec_potentially_empty_discard
;
9552 ic
->exec_potentially_empty_break_old
|= ctx
->cf_info
.exec_potentially_empty_break
;
9553 ic
->exec_potentially_empty_break_depth_old
=
9554 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
9555 /* divergent branches use cbranch_execz */
9556 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9557 ctx
->cf_info
.exec_potentially_empty_break
= false;
9558 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9560 /** emit logical else block */
9561 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
9562 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9563 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
9564 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
9565 ctx
->block
= BB_else_logical
;
9566 append_logical_start(BB_else_logical
);
9569 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
9571 Block
*BB_else_logical
= ctx
->block
;
9572 append_logical_end(BB_else_logical
);
9574 /* branch from logical else block to endif block */
9575 aco_ptr
<Pseudo_branch_instruction
> branch
;
9576 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9577 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
9578 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
9579 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9580 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
9581 BB_else_logical
->kind
|= block_kind_uniform
;
9583 assert(!ctx
->cf_info
.has_branch
);
9584 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
9587 /** emit linear else block */
9588 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
9589 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9590 BB_else_linear
->kind
|= block_kind_uniform
;
9591 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
9593 /* branch from linear else block to endif block */
9594 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9595 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
9596 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
9599 /** emit endif merge block */
9600 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
9601 append_logical_start(ctx
->block
);
9604 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
9605 ctx
->cf_info
.exec_potentially_empty_discard
|= ic
->exec_potentially_empty_discard_old
;
9606 ctx
->cf_info
.exec_potentially_empty_break
|= ic
->exec_potentially_empty_break_old
;
9607 ctx
->cf_info
.exec_potentially_empty_break_depth
=
9608 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
9609 if (ctx
->cf_info
.loop_nest_depth
== ctx
->cf_info
.exec_potentially_empty_break_depth
&&
9610 !ctx
->cf_info
.parent_if
.is_divergent
) {
9611 ctx
->cf_info
.exec_potentially_empty_break
= false;
9612 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9614 /* uniform control flow never has an empty exec-mask */
9615 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
) {
9616 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9617 ctx
->cf_info
.exec_potentially_empty_break
= false;
9618 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9622 static void begin_uniform_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
9624 assert(cond
.regClass() == s1
);
9626 append_logical_end(ctx
->block
);
9627 ctx
->block
->kind
|= block_kind_uniform
;
9629 aco_ptr
<Pseudo_branch_instruction
> branch
;
9630 aco_opcode branch_opcode
= aco_opcode::p_cbranch_z
;
9631 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(branch_opcode
, Format::PSEUDO_BRANCH
, 1, 0));
9632 branch
->operands
[0] = Operand(cond
);
9633 branch
->operands
[0].setFixed(scc
);
9634 ctx
->block
->instructions
.emplace_back(std::move(branch
));
9636 ic
->BB_if_idx
= ctx
->block
->index
;
9637 ic
->BB_endif
= Block();
9638 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9639 ic
->BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
9641 ctx
->cf_info
.has_branch
= false;
9642 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9644 /** emit then block */
9645 Block
* BB_then
= ctx
->program
->create_and_insert_block();
9646 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9647 add_edge(ic
->BB_if_idx
, BB_then
);
9648 append_logical_start(BB_then
);
9649 ctx
->block
= BB_then
;
9652 static void begin_uniform_if_else(isel_context
*ctx
, if_context
*ic
)
9654 Block
*BB_then
= ctx
->block
;
9656 ic
->uniform_has_then_branch
= ctx
->cf_info
.has_branch
;
9657 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9659 if (!ic
->uniform_has_then_branch
) {
9660 append_logical_end(BB_then
);
9661 /* branch from then block to endif block */
9662 aco_ptr
<Pseudo_branch_instruction
> branch
;
9663 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9664 BB_then
->instructions
.emplace_back(std::move(branch
));
9665 add_linear_edge(BB_then
->index
, &ic
->BB_endif
);
9666 if (!ic
->then_branch_divergent
)
9667 add_logical_edge(BB_then
->index
, &ic
->BB_endif
);
9668 BB_then
->kind
|= block_kind_uniform
;
9671 ctx
->cf_info
.has_branch
= false;
9672 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9674 /** emit else block */
9675 Block
* BB_else
= ctx
->program
->create_and_insert_block();
9676 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9677 add_edge(ic
->BB_if_idx
, BB_else
);
9678 append_logical_start(BB_else
);
9679 ctx
->block
= BB_else
;
9682 static void end_uniform_if(isel_context
*ctx
, if_context
*ic
)
9684 Block
*BB_else
= ctx
->block
;
9686 if (!ctx
->cf_info
.has_branch
) {
9687 append_logical_end(BB_else
);
9688 /* branch from then block to endif block */
9689 aco_ptr
<Pseudo_branch_instruction
> branch
;
9690 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9691 BB_else
->instructions
.emplace_back(std::move(branch
));
9692 add_linear_edge(BB_else
->index
, &ic
->BB_endif
);
9693 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9694 add_logical_edge(BB_else
->index
, &ic
->BB_endif
);
9695 BB_else
->kind
|= block_kind_uniform
;
9698 ctx
->cf_info
.has_branch
&= ic
->uniform_has_then_branch
;
9699 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
9701 /** emit endif merge block */
9702 if (!ctx
->cf_info
.has_branch
) {
9703 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
9704 append_logical_start(ctx
->block
);
9708 static bool visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
9710 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
9711 Builder
bld(ctx
->program
, ctx
->block
);
9712 aco_ptr
<Pseudo_branch_instruction
> branch
;
9715 if (!nir_src_is_divergent(if_stmt
->condition
)) { /* uniform condition */
9717 * Uniform conditionals are represented in the following way*) :
9719 * The linear and logical CFG:
9722 * BB_THEN (logical) BB_ELSE (logical)
9726 * *) Exceptions may be due to break and continue statements within loops
9727 * If a break/continue happens within uniform control flow, it branches
9728 * to the loop exit/entry block. Otherwise, it branches to the next
9732 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9733 assert(cond
.regClass() == ctx
->program
->lane_mask
);
9734 cond
= bool_to_scalar_condition(ctx
, cond
);
9736 begin_uniform_if_then(ctx
, &ic
, cond
);
9737 visit_cf_list(ctx
, &if_stmt
->then_list
);
9739 begin_uniform_if_else(ctx
, &ic
);
9740 visit_cf_list(ctx
, &if_stmt
->else_list
);
9742 end_uniform_if(ctx
, &ic
);
9743 } else { /* non-uniform condition */
9745 * To maintain a logical and linear CFG without critical edges,
9746 * non-uniform conditionals are represented in the following way*) :
9751 * BB_THEN (logical) BB_THEN (linear)
9753 * BB_INVERT (linear)
9755 * BB_ELSE (logical) BB_ELSE (linear)
9762 * BB_THEN (logical) BB_ELSE (logical)
9766 * *) Exceptions may be due to break and continue statements within loops
9769 begin_divergent_if_then(ctx
, &ic
, cond
);
9770 visit_cf_list(ctx
, &if_stmt
->then_list
);
9772 begin_divergent_if_else(ctx
, &ic
);
9773 visit_cf_list(ctx
, &if_stmt
->else_list
);
9775 end_divergent_if(ctx
, &ic
);
9778 return !ctx
->cf_info
.has_branch
&& !ctx
->block
->logical_preds
.empty();
9781 static bool visit_cf_list(isel_context
*ctx
,
9782 struct exec_list
*list
)
9784 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
9785 switch (node
->type
) {
9786 case nir_cf_node_block
:
9787 visit_block(ctx
, nir_cf_node_as_block(node
));
9789 case nir_cf_node_if
:
9790 if (!visit_if(ctx
, nir_cf_node_as_if(node
)))
9793 case nir_cf_node_loop
:
9794 visit_loop(ctx
, nir_cf_node_as_loop(node
));
9797 unreachable("unimplemented cf list type");
9803 static void create_null_export(isel_context
*ctx
)
9805 /* Some shader stages always need to have exports.
9806 * So when there is none, we need to add a null export.
9809 unsigned dest
= (ctx
->program
->stage
& hw_fs
) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS
;
9810 bool vm
= (ctx
->program
->stage
& hw_fs
) || ctx
->program
->chip_class
>= GFX10
;
9811 Builder
bld(ctx
->program
, ctx
->block
);
9812 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
9813 /* enabled_mask */ 0, dest
, /* compr */ false, /* done */ true, vm
);
9816 static bool export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
9818 assert(ctx
->stage
== vertex_vs
||
9819 ctx
->stage
== tess_eval_vs
||
9820 ctx
->stage
== gs_copy_vs
||
9821 ctx
->stage
== ngg_vertex_gs
||
9822 ctx
->stage
== ngg_tess_eval_gs
);
9824 int offset
= (ctx
->stage
& sw_tes
)
9825 ? ctx
->program
->info
->tes
.outinfo
.vs_output_param_offset
[slot
]
9826 : ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
9827 uint64_t mask
= ctx
->outputs
.mask
[slot
];
9828 if (!is_pos
&& !mask
)
9830 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
9832 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
9833 exp
->enabled_mask
= mask
;
9834 for (unsigned i
= 0; i
< 4; ++i
) {
9835 if (mask
& (1 << i
))
9836 exp
->operands
[i
] = Operand(ctx
->outputs
.temps
[slot
* 4u + i
]);
9838 exp
->operands
[i
] = Operand(v1
);
9840 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9841 * Setting valid_mask=1 prevents it and has no other effect.
9843 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& is_pos
&& *next_pos
== 0;
9845 exp
->compressed
= false;
9847 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
9849 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
9850 ctx
->block
->instructions
.emplace_back(std::move(exp
));
9855 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
9857 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
9858 exp
->enabled_mask
= 0;
9859 for (unsigned i
= 0; i
< 4; ++i
)
9860 exp
->operands
[i
] = Operand(v1
);
9861 if (ctx
->outputs
.mask
[VARYING_SLOT_PSIZ
]) {
9862 exp
->operands
[0] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_PSIZ
* 4u]);
9863 exp
->enabled_mask
|= 0x1;
9865 if (ctx
->outputs
.mask
[VARYING_SLOT_LAYER
]) {
9866 exp
->operands
[2] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_LAYER
* 4u]);
9867 exp
->enabled_mask
|= 0x4;
9869 if (ctx
->outputs
.mask
[VARYING_SLOT_VIEWPORT
]) {
9870 if (ctx
->options
->chip_class
< GFX9
) {
9871 exp
->operands
[3] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_VIEWPORT
* 4u]);
9872 exp
->enabled_mask
|= 0x8;
9874 Builder
bld(ctx
->program
, ctx
->block
);
9876 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
9877 Operand(ctx
->outputs
.temps
[VARYING_SLOT_VIEWPORT
* 4u]));
9878 if (exp
->operands
[2].isTemp())
9879 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
9881 exp
->operands
[2] = Operand(out
);
9882 exp
->enabled_mask
|= 0x4;
9885 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& *next_pos
== 0;
9887 exp
->compressed
= false;
9888 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
9889 ctx
->block
->instructions
.emplace_back(std::move(exp
));
9892 static void create_export_phis(isel_context
*ctx
)
9894 /* Used when exports are needed, but the output temps are defined in a preceding block.
9895 * This function will set up phis in order to access the outputs in the next block.
9898 assert(ctx
->block
->instructions
.back()->opcode
== aco_opcode::p_logical_start
);
9899 aco_ptr
<Instruction
> logical_start
= aco_ptr
<Instruction
>(ctx
->block
->instructions
.back().release());
9900 ctx
->block
->instructions
.pop_back();
9902 Builder
bld(ctx
->program
, ctx
->block
);
9904 for (unsigned slot
= 0; slot
<= VARYING_SLOT_VAR31
; ++slot
) {
9905 uint64_t mask
= ctx
->outputs
.mask
[slot
];
9906 for (unsigned i
= 0; i
< 4; ++i
) {
9907 if (!(mask
& (1 << i
)))
9910 Temp old
= ctx
->outputs
.temps
[slot
* 4 + i
];
9911 Temp phi
= bld
.pseudo(aco_opcode::p_phi
, bld
.def(v1
), old
, Operand(v1
));
9912 ctx
->outputs
.temps
[slot
* 4 + i
] = phi
;
9916 bld
.insert(std::move(logical_start
));
9919 static void create_vs_exports(isel_context
*ctx
)
9921 assert(ctx
->stage
== vertex_vs
||
9922 ctx
->stage
== tess_eval_vs
||
9923 ctx
->stage
== gs_copy_vs
||
9924 ctx
->stage
== ngg_vertex_gs
||
9925 ctx
->stage
== ngg_tess_eval_gs
);
9927 radv_vs_output_info
*outinfo
= (ctx
->stage
& sw_tes
)
9928 ? &ctx
->program
->info
->tes
.outinfo
9929 : &ctx
->program
->info
->vs
.outinfo
;
9931 if (outinfo
->export_prim_id
&& !(ctx
->stage
& hw_ngg_gs
)) {
9932 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
9933 ctx
->outputs
.temps
[VARYING_SLOT_PRIMITIVE_ID
* 4u] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
9936 if (ctx
->options
->key
.has_multiview_view_index
) {
9937 ctx
->outputs
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
9938 ctx
->outputs
.temps
[VARYING_SLOT_LAYER
* 4u] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
9941 /* the order these position exports are created is important */
9943 bool exported_pos
= export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
9944 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
9945 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
9946 exported_pos
= true;
9948 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
9949 exported_pos
|= export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
9950 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
9951 exported_pos
|= export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
9953 if (ctx
->export_clip_dists
) {
9954 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
9955 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
9956 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
9957 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
9960 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
9961 if (i
< VARYING_SLOT_VAR0
&&
9962 i
!= VARYING_SLOT_LAYER
&&
9963 i
!= VARYING_SLOT_PRIMITIVE_ID
&&
9964 i
!= VARYING_SLOT_VIEWPORT
)
9967 export_vs_varying(ctx
, i
, false, NULL
);
9971 create_null_export(ctx
);
9974 static bool export_fs_mrt_z(isel_context
*ctx
)
9976 Builder
bld(ctx
->program
, ctx
->block
);
9977 unsigned enabled_channels
= 0;
9981 for (unsigned i
= 0; i
< 4; ++i
) {
9982 values
[i
] = Operand(v1
);
9985 /* Both stencil and sample mask only need 16-bits. */
9986 if (!ctx
->program
->info
->ps
.writes_z
&&
9987 (ctx
->program
->info
->ps
.writes_stencil
||
9988 ctx
->program
->info
->ps
.writes_sample_mask
)) {
9989 compr
= true; /* COMPR flag */
9991 if (ctx
->program
->info
->ps
.writes_stencil
) {
9992 /* Stencil should be in X[23:16]. */
9993 values
[0] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_STENCIL
* 4u]);
9994 values
[0] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u), values
[0]);
9995 enabled_channels
|= 0x3;
9998 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
9999 /* SampleMask should be in Y[15:0]. */
10000 values
[1] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_SAMPLE_MASK
* 4u]);
10001 enabled_channels
|= 0xc;
10004 if (ctx
->program
->info
->ps
.writes_z
) {
10005 values
[0] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_DEPTH
* 4u]);
10006 enabled_channels
|= 0x1;
10009 if (ctx
->program
->info
->ps
.writes_stencil
) {
10010 values
[1] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_STENCIL
* 4u]);
10011 enabled_channels
|= 0x2;
10014 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
10015 values
[2] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_SAMPLE_MASK
* 4u]);
10016 enabled_channels
|= 0x4;
10020 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
10021 * writemask component.
10023 if (ctx
->options
->chip_class
== GFX6
&&
10024 ctx
->options
->family
!= CHIP_OLAND
&&
10025 ctx
->options
->family
!= CHIP_HAINAN
) {
10026 enabled_channels
|= 0x1;
10029 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
10030 enabled_channels
, V_008DFC_SQ_EXP_MRTZ
, compr
);
10035 static bool export_fs_mrt_color(isel_context
*ctx
, int slot
)
10037 Builder
bld(ctx
->program
, ctx
->block
);
10038 unsigned write_mask
= ctx
->outputs
.mask
[slot
];
10041 for (unsigned i
= 0; i
< 4; ++i
) {
10042 if (write_mask
& (1 << i
)) {
10043 values
[i
] = Operand(ctx
->outputs
.temps
[slot
* 4u + i
]);
10045 values
[i
] = Operand(v1
);
10049 unsigned target
, col_format
;
10050 unsigned enabled_channels
= 0;
10051 aco_opcode compr_op
= (aco_opcode
)0;
10053 slot
-= FRAG_RESULT_DATA0
;
10054 target
= V_008DFC_SQ_EXP_MRT
+ slot
;
10055 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * slot
)) & 0xf;
10057 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> slot
) & 1;
10058 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> slot
) & 1;
10059 bool is_16bit
= values
[0].regClass() == v2b
;
10061 switch (col_format
)
10063 case V_028714_SPI_SHADER_ZERO
:
10064 enabled_channels
= 0; /* writemask */
10065 target
= V_008DFC_SQ_EXP_NULL
;
10068 case V_028714_SPI_SHADER_32_R
:
10069 enabled_channels
= 1;
10072 case V_028714_SPI_SHADER_32_GR
:
10073 enabled_channels
= 0x3;
10076 case V_028714_SPI_SHADER_32_AR
:
10077 if (ctx
->options
->chip_class
>= GFX10
) {
10078 /* Special case: on GFX10, the outputs are different for 32_AR */
10079 enabled_channels
= 0x3;
10080 values
[1] = values
[3];
10081 values
[3] = Operand(v1
);
10083 enabled_channels
= 0x9;
10087 case V_028714_SPI_SHADER_FP16_ABGR
:
10088 enabled_channels
= 0x5;
10089 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
10091 if (ctx
->options
->chip_class
>= GFX9
) {
10092 /* Pack the FP16 values together instead of converting them to
10093 * FP32 and back to FP16.
10094 * TODO: use p_create_vector and let the compiler optimizes.
10096 compr_op
= aco_opcode::v_pack_b32_f16
;
10098 for (unsigned i
= 0; i
< 4; i
++) {
10099 if ((write_mask
>> i
) & 1)
10100 values
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), values
[i
]);
10106 case V_028714_SPI_SHADER_UNORM16_ABGR
:
10107 enabled_channels
= 0x5;
10108 if (is_16bit
&& ctx
->options
->chip_class
>= GFX9
) {
10109 compr_op
= aco_opcode::v_cvt_pknorm_u16_f16
;
10111 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
10115 case V_028714_SPI_SHADER_SNORM16_ABGR
:
10116 enabled_channels
= 0x5;
10117 if (is_16bit
&& ctx
->options
->chip_class
>= GFX9
) {
10118 compr_op
= aco_opcode::v_cvt_pknorm_i16_f16
;
10120 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
10124 case V_028714_SPI_SHADER_UINT16_ABGR
: {
10125 enabled_channels
= 0x5;
10126 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
10127 if (is_int8
|| is_int10
) {
10129 uint32_t max_rgb
= is_int8
? 255 : is_int10
? 1023 : 0;
10130 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
10132 for (unsigned i
= 0; i
< 4; i
++) {
10133 if ((write_mask
>> i
) & 1) {
10134 values
[i
] = bld
.vop2(aco_opcode::v_min_u32
, bld
.def(v1
),
10135 i
== 3 && is_int10
? Operand(3u) : Operand(max_rgb_val
),
10139 } else if (is_16bit
) {
10140 for (unsigned i
= 0; i
< 4; i
++) {
10141 if ((write_mask
>> i
) & 1) {
10142 Temp tmp
= convert_int(ctx
, bld
, values
[i
].getTemp(), 16, 32, false);
10143 values
[i
] = Operand(tmp
);
10150 case V_028714_SPI_SHADER_SINT16_ABGR
:
10151 enabled_channels
= 0x5;
10152 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
10153 if (is_int8
|| is_int10
) {
10155 uint32_t max_rgb
= is_int8
? 127 : is_int10
? 511 : 0;
10156 uint32_t min_rgb
= is_int8
? -128 :is_int10
? -512 : 0;
10157 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
10158 Temp min_rgb_val
= bld
.copy(bld
.def(s1
), Operand(min_rgb
));
10160 for (unsigned i
= 0; i
< 4; i
++) {
10161 if ((write_mask
>> i
) & 1) {
10162 values
[i
] = bld
.vop2(aco_opcode::v_min_i32
, bld
.def(v1
),
10163 i
== 3 && is_int10
? Operand(1u) : Operand(max_rgb_val
),
10165 values
[i
] = bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
),
10166 i
== 3 && is_int10
? Operand(-2u) : Operand(min_rgb_val
),
10170 } else if (is_16bit
) {
10171 for (unsigned i
= 0; i
< 4; i
++) {
10172 if ((write_mask
>> i
) & 1) {
10173 Temp tmp
= convert_int(ctx
, bld
, values
[i
].getTemp(), 16, 32, true);
10174 values
[i
] = Operand(tmp
);
10180 case V_028714_SPI_SHADER_32_ABGR
:
10181 enabled_channels
= 0xF;
10188 if (target
== V_008DFC_SQ_EXP_NULL
)
10191 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10192 if (ctx
->options
->enable_mrt_output_nan_fixup
&&
10194 (col_format
== V_028714_SPI_SHADER_32_R
||
10195 col_format
== V_028714_SPI_SHADER_32_GR
||
10196 col_format
== V_028714_SPI_SHADER_32_AR
||
10197 col_format
== V_028714_SPI_SHADER_32_ABGR
||
10198 col_format
== V_028714_SPI_SHADER_FP16_ABGR
)) {
10199 for (int i
= 0; i
< 4; i
++) {
10200 if (!(write_mask
& (1 << i
)))
10203 Temp isnan
= bld
.vopc(aco_opcode::v_cmp_class_f32
,
10204 bld
.hint_vcc(bld
.def(bld
.lm
)), values
[i
],
10205 bld
.copy(bld
.def(v1
), Operand(3u)));
10206 values
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), values
[i
],
10207 bld
.copy(bld
.def(v1
), Operand(0u)), isnan
);
10211 if ((bool) compr_op
) {
10212 for (int i
= 0; i
< 2; i
++) {
10213 /* check if at least one of the values to be compressed is enabled */
10214 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
10216 enabled_channels
|= enabled
<< (i
*2);
10217 values
[i
] = bld
.vop3(compr_op
, bld
.def(v1
),
10218 values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2],
10219 values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1]);
10221 values
[i
] = Operand(v1
);
10224 values
[2] = Operand(v1
);
10225 values
[3] = Operand(v1
);
10227 for (int i
= 0; i
< 4; i
++)
10228 values
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
10231 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
10232 enabled_channels
, target
, (bool) compr_op
);
10236 static void create_fs_exports(isel_context
*ctx
)
10238 bool exported
= false;
10240 /* Export depth, stencil and sample mask. */
10241 if (ctx
->outputs
.mask
[FRAG_RESULT_DEPTH
] ||
10242 ctx
->outputs
.mask
[FRAG_RESULT_STENCIL
] ||
10243 ctx
->outputs
.mask
[FRAG_RESULT_SAMPLE_MASK
])
10244 exported
|= export_fs_mrt_z(ctx
);
10246 /* Export all color render targets. */
10247 for (unsigned i
= FRAG_RESULT_DATA0
; i
< FRAG_RESULT_DATA7
+ 1; ++i
)
10248 if (ctx
->outputs
.mask
[i
])
10249 exported
|= export_fs_mrt_color(ctx
, i
);
10252 create_null_export(ctx
);
10255 static void write_tcs_tess_factors(isel_context
*ctx
)
10257 unsigned outer_comps
;
10258 unsigned inner_comps
;
10260 switch (ctx
->args
->options
->key
.tcs
.primitive_mode
) {
10277 Builder
bld(ctx
->program
, ctx
->block
);
10279 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10280 if (unlikely(ctx
->program
->chip_class
!= GFX6
&& ctx
->program
->workgroup_size
> ctx
->program
->wave_size
))
10281 bld
.sopp(aco_opcode::s_barrier
);
10283 Temp tcs_rel_ids
= get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
);
10284 Temp invocation_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), tcs_rel_ids
, Operand(8u), Operand(5u));
10286 Temp invocation_id_is_zero
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), invocation_id
);
10287 if_context ic_invocation_id_is_zero
;
10288 begin_divergent_if_then(ctx
, &ic_invocation_id_is_zero
, invocation_id_is_zero
);
10289 bld
.reset(ctx
->block
);
10291 Temp hs_ring_tess_factor
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_FACTOR
* 16u));
10293 std::pair
<Temp
, unsigned> lds_base
= get_tcs_output_lds_offset(ctx
);
10294 unsigned stride
= inner_comps
+ outer_comps
;
10295 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_base
.second
);
10299 assert(stride
<= (sizeof(out
) / sizeof(Temp
)));
10301 if (ctx
->args
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
10303 tf_outer_vec
= load_lds(ctx
, 4, bld
.tmp(v2
), lds_base
.first
, lds_base
.second
+ ctx
->tcs_tess_lvl_out_loc
, lds_align
);
10304 out
[1] = emit_extract_vector(ctx
, tf_outer_vec
, 0, v1
);
10305 out
[0] = emit_extract_vector(ctx
, tf_outer_vec
, 1, v1
);
10307 tf_outer_vec
= load_lds(ctx
, 4, bld
.tmp(RegClass(RegType::vgpr
, outer_comps
)), lds_base
.first
, lds_base
.second
+ ctx
->tcs_tess_lvl_out_loc
, lds_align
);
10308 tf_inner_vec
= load_lds(ctx
, 4, bld
.tmp(RegClass(RegType::vgpr
, inner_comps
)), lds_base
.first
, lds_base
.second
+ ctx
->tcs_tess_lvl_in_loc
, lds_align
);
10310 for (unsigned i
= 0; i
< outer_comps
; ++i
)
10311 out
[i
] = emit_extract_vector(ctx
, tf_outer_vec
, i
, v1
);
10312 for (unsigned i
= 0; i
< inner_comps
; ++i
)
10313 out
[outer_comps
+ i
] = emit_extract_vector(ctx
, tf_inner_vec
, i
, v1
);
10316 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
10317 Temp tf_base
= get_arg(ctx
, ctx
->args
->tess_factor_offset
);
10318 Temp byte_offset
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, stride
* 4u);
10319 unsigned tf_const_offset
= 0;
10321 if (ctx
->program
->chip_class
<= GFX8
) {
10322 Temp rel_patch_id_is_zero
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), rel_patch_id
);
10323 if_context ic_rel_patch_id_is_zero
;
10324 begin_divergent_if_then(ctx
, &ic_rel_patch_id_is_zero
, rel_patch_id_is_zero
);
10325 bld
.reset(ctx
->block
);
10327 /* Store the dynamic HS control word. */
10328 Temp control_word
= bld
.copy(bld
.def(v1
), Operand(0x80000000u
));
10329 bld
.mubuf(aco_opcode::buffer_store_dword
,
10330 /* SRSRC */ hs_ring_tess_factor
, /* VADDR */ Operand(v1
), /* SOFFSET */ tf_base
, /* VDATA */ control_word
,
10331 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10332 /* disable_wqm */ false, /* glc */ true);
10333 tf_const_offset
+= 4;
10335 begin_divergent_if_else(ctx
, &ic_rel_patch_id_is_zero
);
10336 end_divergent_if(ctx
, &ic_rel_patch_id_is_zero
);
10337 bld
.reset(ctx
->block
);
10340 assert(stride
== 2 || stride
== 4 || stride
== 6);
10341 Temp tf_vec
= create_vec_from_array(ctx
, out
, stride
, RegType::vgpr
, 4u);
10342 store_vmem_mubuf(ctx
, tf_vec
, hs_ring_tess_factor
, byte_offset
, tf_base
, tf_const_offset
, 4, (1 << stride
) - 1, true, false);
10344 /* Store to offchip for TES to read - only if TES reads them */
10345 if (ctx
->args
->options
->key
.tcs
.tes_reads_tess_factors
) {
10346 Temp hs_ring_tess_offchip
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
10347 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
10349 std::pair
<Temp
, unsigned> vmem_offs_outer
= get_tcs_per_patch_output_vmem_offset(ctx
, nullptr, ctx
->tcs_tess_lvl_out_loc
);
10350 store_vmem_mubuf(ctx
, tf_outer_vec
, hs_ring_tess_offchip
, vmem_offs_outer
.first
, oc_lds
, vmem_offs_outer
.second
, 4, (1 << outer_comps
) - 1, true, false);
10352 if (likely(inner_comps
)) {
10353 std::pair
<Temp
, unsigned> vmem_offs_inner
= get_tcs_per_patch_output_vmem_offset(ctx
, nullptr, ctx
->tcs_tess_lvl_in_loc
);
10354 store_vmem_mubuf(ctx
, tf_inner_vec
, hs_ring_tess_offchip
, vmem_offs_inner
.first
, oc_lds
, vmem_offs_inner
.second
, 4, (1 << inner_comps
) - 1, true, false);
10358 begin_divergent_if_else(ctx
, &ic_invocation_id_is_zero
);
10359 end_divergent_if(ctx
, &ic_invocation_id_is_zero
);
10362 static void emit_stream_output(isel_context
*ctx
,
10363 Temp
const *so_buffers
,
10364 Temp
const *so_write_offset
,
10365 const struct radv_stream_output
*output
)
10367 unsigned num_comps
= util_bitcount(output
->component_mask
);
10368 unsigned writemask
= (1 << num_comps
) - 1;
10369 unsigned loc
= output
->location
;
10370 unsigned buf
= output
->buffer
;
10372 assert(num_comps
&& num_comps
<= 4);
10373 if (!num_comps
|| num_comps
> 4)
10376 unsigned start
= ffs(output
->component_mask
) - 1;
10379 bool all_undef
= true;
10380 assert(ctx
->stage
& hw_vs
);
10381 for (unsigned i
= 0; i
< num_comps
; i
++) {
10382 out
[i
] = ctx
->outputs
.temps
[loc
* 4 + start
+ i
];
10383 all_undef
= all_undef
&& !out
[i
].id();
10388 while (writemask
) {
10390 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
10391 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
10392 /* GFX6 doesn't support storing vec3, split it. */
10393 writemask
|= 1u << (start
+ 2);
10397 unsigned offset
= output
->offset
+ start
* 4;
10399 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, count
)};
10400 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
10401 for (int i
= 0; i
< count
; ++i
)
10402 vec
->operands
[i
] = (ctx
->outputs
.mask
[loc
] & 1 << (start
+ i
)) ? Operand(out
[start
+ i
]) : Operand(0u);
10403 vec
->definitions
[0] = Definition(write_data
);
10404 ctx
->block
->instructions
.emplace_back(std::move(vec
));
10409 opcode
= aco_opcode::buffer_store_dword
;
10412 opcode
= aco_opcode::buffer_store_dwordx2
;
10415 opcode
= aco_opcode::buffer_store_dwordx3
;
10418 opcode
= aco_opcode::buffer_store_dwordx4
;
10421 unreachable("Unsupported dword count.");
10424 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
10425 store
->operands
[0] = Operand(so_buffers
[buf
]);
10426 store
->operands
[1] = Operand(so_write_offset
[buf
]);
10427 store
->operands
[2] = Operand((uint32_t) 0);
10428 store
->operands
[3] = Operand(write_data
);
10429 if (offset
> 4095) {
10430 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10431 Builder
bld(ctx
->program
, ctx
->block
);
10432 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
10434 store
->offset
= offset
;
10436 store
->offen
= true;
10438 store
->dlc
= false;
10440 store
->can_reorder
= true;
10441 ctx
->block
->instructions
.emplace_back(std::move(store
));
10445 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
10447 Builder
bld(ctx
->program
, ctx
->block
);
10449 Temp so_buffers
[4];
10450 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
10451 for (unsigned i
= 0; i
< 4; i
++) {
10452 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
10456 Operand off
= bld
.copy(bld
.def(s1
), Operand(i
* 16u));
10457 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, off
);
10460 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10461 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
10463 Temp tid
= emit_mbcnt(ctx
, bld
.def(v1
));
10465 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(bld
.lm
), so_vtx_count
, tid
);
10468 begin_divergent_if_then(ctx
, &ic
, can_emit
);
10470 bld
.reset(ctx
->block
);
10472 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
10474 Temp so_write_offset
[4];
10476 for (unsigned i
= 0; i
< 4; i
++) {
10477 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
10482 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
10483 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
10484 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
10485 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
10487 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
10489 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
10490 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
10491 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
10492 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
10496 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
10497 struct radv_stream_output
*output
=
10498 &ctx
->program
->info
->so
.outputs
[i
];
10499 if (stream
!= output
->stream
)
10502 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
10505 begin_divergent_if_else(ctx
, &ic
);
10506 end_divergent_if(ctx
, &ic
);
10509 } /* end namespace */
10511 void fix_ls_vgpr_init_bug(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
10513 assert(ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
);
10514 Builder
bld(ctx
->program
, ctx
->block
);
10515 constexpr unsigned hs_idx
= 1u;
10516 Builder::Result hs_thread_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10517 get_arg(ctx
, ctx
->args
->merged_wave_info
),
10518 Operand((8u << 16) | (hs_idx
* 8u)));
10519 Temp ls_has_nonzero_hs_threads
= bool_to_vector_condition(ctx
, hs_thread_count
.def(1).getTemp());
10521 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10523 Temp instance_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10524 get_arg(ctx
, ctx
->args
->rel_auto_id
),
10525 get_arg(ctx
, ctx
->args
->ac
.instance_id
),
10526 ls_has_nonzero_hs_threads
);
10527 Temp rel_auto_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10528 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
),
10529 get_arg(ctx
, ctx
->args
->rel_auto_id
),
10530 ls_has_nonzero_hs_threads
);
10531 Temp vertex_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10532 get_arg(ctx
, ctx
->args
->ac
.tcs_patch_id
),
10533 get_arg(ctx
, ctx
->args
->ac
.vertex_id
),
10534 ls_has_nonzero_hs_threads
);
10536 ctx
->arg_temps
[ctx
->args
->ac
.instance_id
.arg_index
] = instance_id
;
10537 ctx
->arg_temps
[ctx
->args
->rel_auto_id
.arg_index
] = rel_auto_id
;
10538 ctx
->arg_temps
[ctx
->args
->ac
.vertex_id
.arg_index
] = vertex_id
;
10541 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
10543 /* Split all arguments except for the first (ring_offsets) and the last
10544 * (exec) so that the dead channels don't stay live throughout the program.
10546 for (int i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
10547 if (startpgm
->definitions
[i
].regClass().size() > 1) {
10548 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
10549 startpgm
->definitions
[i
].regClass().size());
10554 void handle_bc_optimize(isel_context
*ctx
)
10556 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10557 Builder
bld(ctx
->program
, ctx
->block
);
10558 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
10559 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
10560 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
10561 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
10562 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
10563 if (uses_center
&& uses_centroid
) {
10564 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
10565 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
10567 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
10569 for (unsigned i
= 0; i
< 2; i
++) {
10570 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
10571 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
10572 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10573 persp_centroid
, persp_center
, sel
);
10575 ctx
->persp_centroid
= bld
.tmp(v2
);
10576 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
10577 Operand(new_coord
[0]), Operand(new_coord
[1]));
10578 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
10581 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
10583 for (unsigned i
= 0; i
< 2; i
++) {
10584 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
10585 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
10586 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10587 linear_centroid
, linear_center
, sel
);
10589 ctx
->linear_centroid
= bld
.tmp(v2
);
10590 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
10591 Operand(new_coord
[0]), Operand(new_coord
[1]));
10592 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
10597 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
10599 Program
*program
= ctx
->program
;
10601 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
10603 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
10604 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
10605 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
10606 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
10607 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
10609 program
->next_fp_mode
.must_flush_denorms32
=
10610 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
10611 program
->next_fp_mode
.must_flush_denorms16_64
=
10612 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
10613 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
10615 program
->next_fp_mode
.care_about_round32
=
10616 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
10618 program
->next_fp_mode
.care_about_round16_64
=
10619 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
10620 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
10622 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10623 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10624 if (program
->next_fp_mode
.must_flush_denorms16_64
)
10625 program
->next_fp_mode
.denorm16_64
= 0;
10627 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
10629 /* preserving fp32 denorms is expensive, so only do it if asked */
10630 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
10631 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
10633 program
->next_fp_mode
.denorm32
= 0;
10635 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
10636 program
->next_fp_mode
.round32
= fp_round_tz
;
10638 program
->next_fp_mode
.round32
= fp_round_ne
;
10640 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
10641 program
->next_fp_mode
.round16_64
= fp_round_tz
;
10643 program
->next_fp_mode
.round16_64
= fp_round_ne
;
10645 ctx
->block
->fp_mode
= program
->next_fp_mode
;
10648 void cleanup_cfg(Program
*program
)
10650 /* create linear_succs/logical_succs */
10651 for (Block
& BB
: program
->blocks
) {
10652 for (unsigned idx
: BB
.linear_preds
)
10653 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
10654 for (unsigned idx
: BB
.logical_preds
)
10655 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);
10659 Temp
merged_wave_info_to_mask(isel_context
*ctx
, unsigned i
)
10661 Builder
bld(ctx
->program
, ctx
->block
);
10663 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10664 Temp count
= i
== 0
10665 ? get_arg(ctx
, ctx
->args
->merged_wave_info
)
10666 : bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
),
10667 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(i
* 8u));
10669 Temp mask
= bld
.sop2(aco_opcode::s_bfm_b64
, bld
.def(s2
), count
, Operand(0u));
10672 if (ctx
->program
->wave_size
== 64) {
10673 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10674 Temp active_64
= bld
.sopc(aco_opcode::s_bitcmp1_b32
, bld
.def(s1
, scc
), count
, Operand(6u /* log2(64) */));
10675 cond
= bld
.sop2(Builder::s_cselect
, bld
.def(bld
.lm
), Operand(-1u), mask
, bld
.scc(active_64
));
10677 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10678 cond
= emit_extract_vector(ctx
, mask
, 0, bld
.lm
);
10684 bool ngg_early_prim_export(isel_context
*ctx
)
10686 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10690 void ngg_emit_sendmsg_gs_alloc_req(isel_context
*ctx
)
10692 Builder
bld(ctx
->program
, ctx
->block
);
10694 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10695 bld
.sopp(aco_opcode::s_setprio
, -1u, 0x3u
);
10697 /* Get the id of the current wave within the threadgroup (workgroup) */
10698 Builder::Result wave_id_in_tg
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10699 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(24u | (4u << 16)));
10701 /* Execute the following code only on the first wave (wave id 0),
10702 * use the SCC def to tell if the wave id is zero or not.
10704 Temp cond
= wave_id_in_tg
.def(1).getTemp();
10706 begin_uniform_if_then(ctx
, &ic
, cond
);
10707 begin_uniform_if_else(ctx
, &ic
);
10708 bld
.reset(ctx
->block
);
10710 /* Number of vertices output by VS/TES */
10711 Temp vtx_cnt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10712 get_arg(ctx
, ctx
->args
->gs_tg_info
), Operand(12u | (9u << 16u)));
10713 /* Number of primitives output by VS/TES */
10714 Temp prm_cnt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10715 get_arg(ctx
, ctx
->args
->gs_tg_info
), Operand(22u | (9u << 16u)));
10717 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10718 Temp tmp
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prm_cnt
, Operand(12u));
10719 tmp
= bld
.sop2(aco_opcode::s_or_b32
, bld
.m0(bld
.def(s1
)), bld
.def(s1
, scc
), tmp
, vtx_cnt
);
10721 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10722 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(tmp
), -1, sendmsg_gs_alloc_req
);
10724 end_uniform_if(ctx
, &ic
);
10726 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10727 bld
.reset(ctx
->block
);
10728 bld
.sopp(aco_opcode::s_setprio
, -1u, 0x0u
);
10731 Temp
ngg_get_prim_exp_arg(isel_context
*ctx
, unsigned num_vertices
, const Temp vtxindex
[])
10733 Builder
bld(ctx
->program
, ctx
->block
);
10735 if (ctx
->args
->options
->key
.vs_common_out
.as_ngg_passthrough
) {
10736 return get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]);
10739 Temp gs_invocation_id
= get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
);
10742 for (unsigned i
= 0; i
< num_vertices
; ++i
) {
10743 assert(vtxindex
[i
].id());
10746 tmp
= bld
.vop3(aco_opcode::v_lshl_add_u32
, bld
.def(v1
), vtxindex
[i
], Operand(10u * i
), tmp
);
10750 /* The initial edge flag is always false in tess eval shaders. */
10751 if (ctx
->stage
== ngg_vertex_gs
) {
10752 Temp edgeflag
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), gs_invocation_id
, Operand(8 + i
), Operand(1u));
10753 tmp
= bld
.vop3(aco_opcode::v_lshl_add_u32
, bld
.def(v1
), edgeflag
, Operand(10u * i
+ 9u), tmp
);
10757 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10762 void ngg_emit_prim_export(isel_context
*ctx
, unsigned num_vertices_per_primitive
, const Temp vtxindex
[])
10764 Builder
bld(ctx
->program
, ctx
->block
);
10765 Temp prim_exp_arg
= ngg_get_prim_exp_arg(ctx
, num_vertices_per_primitive
, vtxindex
);
10767 bld
.exp(aco_opcode::exp
, prim_exp_arg
, Operand(v1
), Operand(v1
), Operand(v1
),
10768 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM
/* dest */,
10769 false /* compressed */, true/* done */, false /* valid mask */);
10772 void ngg_emit_nogs_gsthreads(isel_context
*ctx
)
10774 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10775 * These must always come before VS exports.
10777 * It is recommended to do these as early as possible. They can be at the beginning when
10778 * there is no SW GS and the shader doesn't write edge flags.
10782 Temp is_gs_thread
= merged_wave_info_to_mask(ctx
, 1);
10783 begin_divergent_if_then(ctx
, &ic
, is_gs_thread
);
10785 Builder
bld(ctx
->program
, ctx
->block
);
10786 constexpr unsigned max_vertices_per_primitive
= 3;
10787 unsigned num_vertices_per_primitive
= max_vertices_per_primitive
;
10789 if (ctx
->stage
== ngg_vertex_gs
) {
10790 /* TODO: optimize for points & lines */
10791 } else if (ctx
->stage
== ngg_tess_eval_gs
) {
10792 if (ctx
->shader
->info
.tess
.point_mode
)
10793 num_vertices_per_primitive
= 1;
10794 else if (ctx
->shader
->info
.tess
.primitive_mode
== GL_ISOLINES
)
10795 num_vertices_per_primitive
= 2;
10797 unreachable("Unsupported NGG shader stage");
10800 Temp vtxindex
[max_vertices_per_primitive
];
10801 vtxindex
[0] = bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
),
10802 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]));
10803 vtxindex
[1] = num_vertices_per_primitive
< 2 ? Temp(0, v1
) :
10804 bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
10805 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]), Operand(16u), Operand(16u));
10806 vtxindex
[2] = num_vertices_per_primitive
< 3 ? Temp(0, v1
) :
10807 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
),
10808 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[2]));
10810 /* Export primitive data to the index buffer. */
10811 ngg_emit_prim_export(ctx
, num_vertices_per_primitive
, vtxindex
);
10813 /* Export primitive ID. */
10814 if (ctx
->stage
== ngg_vertex_gs
&& ctx
->args
->options
->key
.vs_common_out
.export_prim_id
) {
10815 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10816 Temp prim_id
= get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
);
10817 Temp provoking_vtx_index
= vtxindex
[0];
10818 Temp addr
= bld
.v_mul_imm(bld
.def(v1
), provoking_vtx_index
, 4u);
10820 store_lds(ctx
, 4, prim_id
, 0x1u
, addr
, 0u, 4u);
10823 begin_divergent_if_else(ctx
, &ic
);
10824 end_divergent_if(ctx
, &ic
);
10827 void ngg_emit_nogs_output(isel_context
*ctx
)
10829 /* Emits NGG GS output, for stages that don't have SW GS. */
10832 Builder
bld(ctx
->program
, ctx
->block
);
10833 bool late_prim_export
= !ngg_early_prim_export(ctx
);
10835 /* NGG streamout is currently disabled by default. */
10836 assert(!ctx
->args
->shader_info
->so
.num_outputs
);
10838 if (late_prim_export
) {
10839 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10840 create_export_phis(ctx
);
10841 /* Do what we need to do in the GS threads. */
10842 ngg_emit_nogs_gsthreads(ctx
);
10844 /* What comes next should be executed on ES threads. */
10845 Temp is_es_thread
= merged_wave_info_to_mask(ctx
, 0);
10846 begin_divergent_if_then(ctx
, &ic
, is_es_thread
);
10847 bld
.reset(ctx
->block
);
10850 /* Export VS outputs */
10851 ctx
->block
->kind
|= block_kind_export_end
;
10852 create_vs_exports(ctx
);
10854 /* Export primitive ID */
10855 if (ctx
->args
->options
->key
.vs_common_out
.export_prim_id
) {
10858 if (ctx
->stage
== ngg_vertex_gs
) {
10859 /* Wait for GS threads to store primitive ID in LDS. */
10860 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10861 bld
.sopp(aco_opcode::s_barrier
);
10863 /* Calculate LDS address where the GS threads stored the primitive ID. */
10864 Temp wave_id_in_tg
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10865 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(24u | (4u << 16)));
10866 Temp thread_id_in_wave
= emit_mbcnt(ctx
, bld
.def(v1
));
10867 Temp wave_id_mul
= bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_id_in_tg
), ctx
->program
->wave_size
);
10868 Temp thread_id_in_tg
= bld
.vadd32(bld
.def(v1
), Operand(wave_id_mul
), Operand(thread_id_in_wave
));
10869 Temp addr
= bld
.v_mul24_imm(bld
.def(v1
), thread_id_in_tg
, 4u);
10871 /* Load primitive ID from LDS. */
10872 prim_id
= load_lds(ctx
, 4, bld
.tmp(v1
), addr
, 0u, 4u);
10873 } else if (ctx
->stage
== ngg_tess_eval_gs
) {
10874 /* TES: Just use the patch ID as the primitive ID. */
10875 prim_id
= get_arg(ctx
, ctx
->args
->ac
.tes_patch_id
);
10877 unreachable("unsupported NGG shader stage.");
10880 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
10881 ctx
->outputs
.temps
[VARYING_SLOT_PRIMITIVE_ID
* 4u] = prim_id
;
10883 export_vs_varying(ctx
, VARYING_SLOT_PRIMITIVE_ID
, false, nullptr);
10886 if (late_prim_export
) {
10887 begin_divergent_if_else(ctx
, &ic
);
10888 end_divergent_if(ctx
, &ic
);
10889 bld
.reset(ctx
->block
);
10893 void select_program(Program
*program
,
10894 unsigned shader_count
,
10895 struct nir_shader
*const *shaders
,
10896 ac_shader_config
* config
,
10897 struct radv_shader_args
*args
)
10899 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
, false);
10900 if_context ic_merged_wave_info
;
10901 bool ngg_no_gs
= ctx
.stage
== ngg_vertex_gs
|| ctx
.stage
== ngg_tess_eval_gs
;
10903 for (unsigned i
= 0; i
< shader_count
; i
++) {
10904 nir_shader
*nir
= shaders
[i
];
10905 init_context(&ctx
, nir
);
10907 setup_fp_mode(&ctx
, nir
);
10910 /* needs to be after init_context() for FS */
10911 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
10912 append_logical_start(ctx
.block
);
10914 if (unlikely(args
->options
->has_ls_vgpr_init_bug
&& ctx
.stage
== vertex_tess_control_hs
))
10915 fix_ls_vgpr_init_bug(&ctx
, startpgm
);
10917 split_arguments(&ctx
, startpgm
);
10921 ngg_emit_sendmsg_gs_alloc_req(&ctx
);
10923 if (ngg_early_prim_export(&ctx
))
10924 ngg_emit_nogs_gsthreads(&ctx
);
10927 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10928 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
10929 bool empty_shader
= nir_cf_list_is_empty_block(&func
->body
) &&
10930 ((nir
->info
.stage
== MESA_SHADER_VERTEX
&&
10931 (ctx
.stage
== vertex_tess_control_hs
|| ctx
.stage
== vertex_geometry_gs
)) ||
10932 (nir
->info
.stage
== MESA_SHADER_TESS_EVAL
&&
10933 ctx
.stage
== tess_eval_geometry_gs
));
10935 bool check_merged_wave_info
= ctx
.tcs_in_out_eq
? i
== 0 : ((shader_count
>= 2 && !empty_shader
) || ngg_no_gs
);
10936 bool endif_merged_wave_info
= ctx
.tcs_in_out_eq
? i
== 1 : check_merged_wave_info
;
10937 if (check_merged_wave_info
) {
10938 Temp cond
= merged_wave_info_to_mask(&ctx
, i
);
10939 begin_divergent_if_then(&ctx
, &ic_merged_wave_info
, cond
);
10943 Builder
bld(ctx
.program
, ctx
.block
);
10945 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10946 bld
.sopp(aco_opcode::s_barrier
);
10948 if (ctx
.stage
== vertex_geometry_gs
|| ctx
.stage
== tess_eval_geometry_gs
) {
10949 ctx
.gs_wave_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), get_arg(&ctx
, args
->merged_wave_info
), Operand((8u << 16) | 16u));
10951 } else if (ctx
.stage
== geometry_gs
)
10952 ctx
.gs_wave_id
= get_arg(&ctx
, args
->gs_wave_id
);
10954 if (ctx
.stage
== fragment_fs
)
10955 handle_bc_optimize(&ctx
);
10957 visit_cf_list(&ctx
, &func
->body
);
10959 if (ctx
.program
->info
->so
.num_outputs
&& (ctx
.stage
& hw_vs
))
10960 emit_streamout(&ctx
, 0);
10962 if (ctx
.stage
& hw_vs
) {
10963 create_vs_exports(&ctx
);
10964 ctx
.block
->kind
|= block_kind_export_end
;
10965 } else if (ngg_no_gs
&& ngg_early_prim_export(&ctx
)) {
10966 ngg_emit_nogs_output(&ctx
);
10967 } else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
10968 Builder
bld(ctx
.program
, ctx
.block
);
10969 bld
.barrier(aco_opcode::p_memory_barrier_gs_data
);
10970 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
.gs_wave_id
), -1, sendmsg_gs_done(false, false, 0));
10971 } else if (nir
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
10972 write_tcs_tess_factors(&ctx
);
10975 if (ctx
.stage
== fragment_fs
) {
10976 create_fs_exports(&ctx
);
10977 ctx
.block
->kind
|= block_kind_export_end
;
10980 if (endif_merged_wave_info
) {
10981 begin_divergent_if_else(&ctx
, &ic_merged_wave_info
);
10982 end_divergent_if(&ctx
, &ic_merged_wave_info
);
10985 if (ngg_no_gs
&& !ngg_early_prim_export(&ctx
))
10986 ngg_emit_nogs_output(&ctx
);
10988 if (i
== 0 && ctx
.stage
== vertex_tess_control_hs
&& ctx
.tcs_in_out_eq
) {
10989 /* Outputs of the previous stage are inputs to the next stage */
10990 ctx
.inputs
= ctx
.outputs
;
10991 ctx
.outputs
= shader_io_state();
10995 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
10997 append_logical_end(ctx
.block
);
10998 ctx
.block
->kind
|= block_kind_uniform
;
10999 Builder
bld(ctx
.program
, ctx
.block
);
11000 if (ctx
.program
->wb_smem_l1_on_end
)
11001 bld
.smem(aco_opcode::s_dcache_wb
, false);
11002 bld
.sopp(aco_opcode::s_endpgm
);
11004 cleanup_cfg(program
);
11007 void select_gs_copy_shader(Program
*program
, struct nir_shader
*gs_shader
,
11008 ac_shader_config
* config
,
11009 struct radv_shader_args
*args
)
11011 isel_context ctx
= setup_isel_context(program
, 1, &gs_shader
, config
, args
, true);
11013 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
= false;
11014 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
= false;
11015 program
->next_fp_mode
.must_flush_denorms32
= false;
11016 program
->next_fp_mode
.must_flush_denorms16_64
= false;
11017 program
->next_fp_mode
.care_about_round32
= false;
11018 program
->next_fp_mode
.care_about_round16_64
= false;
11019 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
11020 program
->next_fp_mode
.denorm32
= 0;
11021 program
->next_fp_mode
.round32
= fp_round_ne
;
11022 program
->next_fp_mode
.round16_64
= fp_round_ne
;
11023 ctx
.block
->fp_mode
= program
->next_fp_mode
;
11025 add_startpgm(&ctx
);
11026 append_logical_start(ctx
.block
);
11028 Builder
bld(ctx
.program
, ctx
.block
);
11030 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), program
->private_segment_buffer
, Operand(RING_GSVS_VS
* 16u));
11032 Operand
stream_id(0u);
11033 if (args
->shader_info
->so
.num_outputs
)
11034 stream_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
11035 get_arg(&ctx
, ctx
.args
->streamout_config
), Operand(0x20018u
));
11037 Temp vtx_offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), get_arg(&ctx
, ctx
.args
->ac
.vertex_id
));
11039 std::stack
<Block
> endif_blocks
;
11041 for (unsigned stream
= 0; stream
< 4; stream
++) {
11042 if (stream_id
.isConstant() && stream
!= stream_id
.constantValue())
11045 unsigned num_components
= args
->shader_info
->gs
.num_stream_output_components
[stream
];
11046 if (stream
> 0 && (!num_components
|| !args
->shader_info
->so
.num_outputs
))
11049 memset(ctx
.outputs
.mask
, 0, sizeof(ctx
.outputs
.mask
));
11051 unsigned BB_if_idx
= ctx
.block
->index
;
11052 Block BB_endif
= Block();
11053 if (!stream_id
.isConstant()) {
11055 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), stream_id
, Operand(stream
));
11056 append_logical_end(ctx
.block
);
11057 ctx
.block
->kind
|= block_kind_uniform
;
11058 bld
.branch(aco_opcode::p_cbranch_z
, cond
);
11060 BB_endif
.kind
|= ctx
.block
->kind
& block_kind_top_level
;
11062 ctx
.block
= ctx
.program
->create_and_insert_block();
11063 add_edge(BB_if_idx
, ctx
.block
);
11064 bld
.reset(ctx
.block
);
11065 append_logical_start(ctx
.block
);
11068 unsigned offset
= 0;
11069 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
11070 if (args
->shader_info
->gs
.output_streams
[i
] != stream
)
11073 unsigned output_usage_mask
= args
->shader_info
->gs
.output_usage_mask
[i
];
11074 unsigned length
= util_last_bit(output_usage_mask
);
11075 for (unsigned j
= 0; j
< length
; ++j
) {
11076 if (!(output_usage_mask
& (1 << j
)))
11079 unsigned const_offset
= offset
* args
->shader_info
->gs
.vertices_out
* 16 * 4;
11080 Temp voffset
= vtx_offset
;
11081 if (const_offset
>= 4096u) {
11082 voffset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), voffset
);
11083 const_offset
%= 4096u;
11086 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dword
, Format::MUBUF
, 3, 1)};
11087 mubuf
->definitions
[0] = bld
.def(v1
);
11088 mubuf
->operands
[0] = Operand(gsvs_ring
);
11089 mubuf
->operands
[1] = Operand(voffset
);
11090 mubuf
->operands
[2] = Operand(0u);
11091 mubuf
->offen
= true;
11092 mubuf
->offset
= const_offset
;
11095 mubuf
->dlc
= args
->options
->chip_class
>= GFX10
;
11096 mubuf
->barrier
= barrier_none
;
11097 mubuf
->can_reorder
= true;
11099 ctx
.outputs
.mask
[i
] |= 1 << j
;
11100 ctx
.outputs
.temps
[i
* 4u + j
] = mubuf
->definitions
[0].getTemp();
11102 bld
.insert(std::move(mubuf
));
11108 if (args
->shader_info
->so
.num_outputs
) {
11109 emit_streamout(&ctx
, stream
);
11110 bld
.reset(ctx
.block
);
11114 create_vs_exports(&ctx
);
11115 ctx
.block
->kind
|= block_kind_export_end
;
11118 if (!stream_id
.isConstant()) {
11119 append_logical_end(ctx
.block
);
11121 /* branch from then block to endif block */
11122 bld
.branch(aco_opcode::p_branch
);
11123 add_edge(ctx
.block
->index
, &BB_endif
);
11124 ctx
.block
->kind
|= block_kind_uniform
;
11126 /* emit else block */
11127 ctx
.block
= ctx
.program
->create_and_insert_block();
11128 add_edge(BB_if_idx
, ctx
.block
);
11129 bld
.reset(ctx
.block
);
11130 append_logical_start(ctx
.block
);
11132 endif_blocks
.push(std::move(BB_endif
));
11136 while (!endif_blocks
.empty()) {
11137 Block BB_endif
= std::move(endif_blocks
.top());
11138 endif_blocks
.pop();
11140 Block
*BB_else
= ctx
.block
;
11142 append_logical_end(BB_else
);
11143 /* branch from else block to endif block */
11144 bld
.branch(aco_opcode::p_branch
);
11145 add_edge(BB_else
->index
, &BB_endif
);
11146 BB_else
->kind
|= block_kind_uniform
;
11148 /** emit endif merge block */
11149 ctx
.block
= program
->insert_block(std::move(BB_endif
));
11150 bld
.reset(ctx
.block
);
11151 append_logical_start(ctx
.block
);
11154 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
11156 append_logical_end(ctx
.block
);
11157 ctx
.block
->kind
|= block_kind_uniform
;
11158 bld
.sopp(aco_opcode::s_endpgm
);
11160 cleanup_cfg(program
);