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) {
140 Temp thread_id_hi
= bld
.vop3(aco_opcode::v_mbcnt_hi_u32_b32
, dst
, mask_hi
, thread_id_lo
);
145 Temp
emit_wqm(isel_context
*ctx
, Temp src
, Temp dst
=Temp(0, s1
), bool program_needs_wqm
= false)
147 Builder
bld(ctx
->program
, ctx
->block
);
150 dst
= bld
.tmp(src
.regClass());
152 assert(src
.size() == dst
.size());
154 if (ctx
->stage
!= fragment_fs
) {
158 bld
.copy(Definition(dst
), src
);
162 bld
.pseudo(aco_opcode::p_wqm
, Definition(dst
), src
);
163 ctx
->program
->needs_wqm
|= program_needs_wqm
;
167 static Temp
emit_bpermute(isel_context
*ctx
, Builder
&bld
, Temp index
, Temp data
)
169 if (index
.regClass() == s1
)
170 return bld
.readlane(bld
.def(s1
), data
, index
);
172 if (ctx
->options
->chip_class
<= GFX7
) {
173 /* GFX6-7: there is no bpermute instruction */
174 Operand
index_op(index
);
175 Operand
input_data(data
);
176 index_op
.setLateKill(true);
177 input_data
.setLateKill(true);
179 return bld
.pseudo(aco_opcode::p_bpermute
, bld
.def(v1
), bld
.def(bld
.lm
), bld
.def(bld
.lm
, vcc
), index_op
, input_data
);
180 } else if (ctx
->options
->chip_class
>= GFX10
&& ctx
->program
->wave_size
== 64) {
181 /* GFX10 wave64 mode: emulate full-wave bpermute */
182 if (!ctx
->has_gfx10_wave64_bpermute
) {
183 ctx
->has_gfx10_wave64_bpermute
= true;
184 ctx
->program
->config
->num_shared_vgprs
= 8; /* Shared VGPRs are allocated in groups of 8 */
185 ctx
->program
->vgpr_limit
-= 4; /* We allocate 8 shared VGPRs, so we'll have 4 fewer normal VGPRs */
188 Temp index_is_lo
= bld
.vopc(aco_opcode::v_cmp_ge_u32
, bld
.def(bld
.lm
), Operand(31u), index
);
189 Builder::Result index_is_lo_split
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), index_is_lo
);
190 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());
191 Operand same_half
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), index_is_lo_split
.def(0).getTemp(), index_is_lo_n1
);
192 Operand index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
193 Operand
input_data(data
);
195 index_x4
.setLateKill(true);
196 input_data
.setLateKill(true);
197 same_half
.setLateKill(true);
199 return bld
.pseudo(aco_opcode::p_bpermute
, bld
.def(v1
), bld
.def(s2
), bld
.def(s1
, scc
), index_x4
, input_data
, same_half
);
201 /* GFX8-9 or GFX10 wave32: bpermute works normally */
202 Temp index_x4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), index
);
203 return bld
.ds(aco_opcode::ds_bpermute_b32
, bld
.def(v1
), index_x4
, data
);
207 Temp
as_vgpr(isel_context
*ctx
, Temp val
)
209 if (val
.type() == RegType::sgpr
) {
210 Builder
bld(ctx
->program
, ctx
->block
);
211 return bld
.copy(bld
.def(RegType::vgpr
, val
.size()), val
);
213 assert(val
.type() == RegType::vgpr
);
217 //assumes a != 0xffffffff
218 void emit_v_div_u32(isel_context
*ctx
, Temp dst
, Temp a
, uint32_t b
)
221 Builder
bld(ctx
->program
, ctx
->block
);
223 if (util_is_power_of_two_or_zero(b
)) {
224 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)util_logbase2(b
)), a
);
228 util_fast_udiv_info info
= util_compute_fast_udiv_info(b
, 32, 32);
230 assert(info
.multiplier
<= 0xffffffff);
232 bool pre_shift
= info
.pre_shift
!= 0;
233 bool increment
= info
.increment
!= 0;
234 bool multiply
= true;
235 bool post_shift
= info
.post_shift
!= 0;
237 if (!pre_shift
&& !increment
&& !multiply
&& !post_shift
) {
238 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), a
);
242 Temp pre_shift_dst
= a
;
244 pre_shift_dst
= (increment
|| multiply
|| post_shift
) ? bld
.tmp(v1
) : dst
;
245 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(pre_shift_dst
), Operand((uint32_t)info
.pre_shift
), a
);
248 Temp increment_dst
= pre_shift_dst
;
250 increment_dst
= (post_shift
|| multiply
) ? bld
.tmp(v1
) : dst
;
251 bld
.vadd32(Definition(increment_dst
), Operand((uint32_t) info
.increment
), pre_shift_dst
);
254 Temp multiply_dst
= increment_dst
;
256 multiply_dst
= post_shift
? bld
.tmp(v1
) : dst
;
257 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(multiply_dst
), increment_dst
,
258 bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand((uint32_t)info
.multiplier
)));
262 bld
.vop2(aco_opcode::v_lshrrev_b32
, Definition(dst
), Operand((uint32_t)info
.post_shift
), multiply_dst
);
266 void emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, Temp dst
)
268 Builder
bld(ctx
->program
, ctx
->block
);
269 bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(idx
));
273 Temp
emit_extract_vector(isel_context
* ctx
, Temp src
, uint32_t idx
, RegClass dst_rc
)
275 /* no need to extract the whole vector */
276 if (src
.regClass() == dst_rc
) {
281 assert(src
.bytes() > (idx
* dst_rc
.bytes()));
282 Builder
bld(ctx
->program
, ctx
->block
);
283 auto it
= ctx
->allocated_vec
.find(src
.id());
284 if (it
!= ctx
->allocated_vec
.end() && dst_rc
.bytes() == it
->second
[idx
].regClass().bytes()) {
285 if (it
->second
[idx
].regClass() == dst_rc
) {
286 return it
->second
[idx
];
288 assert(!dst_rc
.is_subdword());
289 assert(dst_rc
.type() == RegType::vgpr
&& it
->second
[idx
].type() == RegType::sgpr
);
290 return bld
.copy(bld
.def(dst_rc
), it
->second
[idx
]);
294 if (dst_rc
.is_subdword())
295 src
= as_vgpr(ctx
, src
);
297 if (src
.bytes() == dst_rc
.bytes()) {
299 return bld
.copy(bld
.def(dst_rc
), src
);
301 Temp dst
= bld
.tmp(dst_rc
);
302 emit_extract_vector(ctx
, src
, idx
, dst
);
307 void emit_split_vector(isel_context
* ctx
, Temp vec_src
, unsigned num_components
)
309 if (num_components
== 1)
311 if (ctx
->allocated_vec
.find(vec_src
.id()) != ctx
->allocated_vec
.end())
314 if (num_components
> vec_src
.size()) {
315 if (vec_src
.type() == RegType::sgpr
) {
316 /* should still help get_alu_src() */
317 emit_split_vector(ctx
, vec_src
, vec_src
.size());
320 /* sub-dword split */
321 rc
= RegClass(RegType::vgpr
, vec_src
.bytes() / num_components
).as_subdword();
323 rc
= RegClass(vec_src
.type(), vec_src
.size() / num_components
);
325 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, num_components
)};
326 split
->operands
[0] = Operand(vec_src
);
327 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
328 for (unsigned i
= 0; i
< num_components
; i
++) {
329 elems
[i
] = {ctx
->program
->allocateId(), rc
};
330 split
->definitions
[i
] = Definition(elems
[i
]);
332 ctx
->block
->instructions
.emplace_back(std::move(split
));
333 ctx
->allocated_vec
.emplace(vec_src
.id(), elems
);
336 /* This vector expansion uses a mask to determine which elements in the new vector
337 * come from the original vector. The other elements are undefined. */
338 void expand_vector(isel_context
* ctx
, Temp vec_src
, Temp dst
, unsigned num_components
, unsigned mask
)
340 emit_split_vector(ctx
, vec_src
, util_bitcount(mask
));
345 Builder
bld(ctx
->program
, ctx
->block
);
346 if (num_components
== 1) {
347 if (dst
.type() == RegType::sgpr
)
348 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec_src
);
350 bld
.copy(Definition(dst
), vec_src
);
354 unsigned component_size
= dst
.size() / num_components
;
355 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
357 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
358 vec
->definitions
[0] = Definition(dst
);
360 for (unsigned i
= 0; i
< num_components
; i
++) {
361 if (mask
& (1 << i
)) {
362 Temp src
= emit_extract_vector(ctx
, vec_src
, k
++, RegClass(vec_src
.type(), component_size
));
363 if (dst
.type() == RegType::sgpr
)
364 src
= bld
.as_uniform(src
);
365 vec
->operands
[i
] = Operand(src
);
367 vec
->operands
[i
] = Operand(0u);
369 elems
[i
] = vec
->operands
[i
].getTemp();
371 ctx
->block
->instructions
.emplace_back(std::move(vec
));
372 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
375 /* adjust misaligned small bit size loads */
376 void byte_align_scalar(isel_context
*ctx
, Temp vec
, Operand offset
, Temp dst
)
378 Builder
bld(ctx
->program
, ctx
->block
);
380 Temp select
= Temp();
381 if (offset
.isConstant()) {
382 assert(offset
.constantValue() && offset
.constantValue() < 4);
383 shift
= Operand(offset
.constantValue() * 8);
385 /* bit_offset = 8 * (offset & 0x3) */
386 Temp tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(3u));
387 select
= bld
.tmp(s1
);
388 shift
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.scc(Definition(select
)), tmp
, Operand(3u));
391 if (vec
.size() == 1) {
392 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(dst
), bld
.def(s1
, scc
), vec
, shift
);
393 } else if (vec
.size() == 2) {
394 Temp tmp
= dst
.size() == 2 ? dst
: bld
.tmp(s2
);
395 bld
.sop2(aco_opcode::s_lshr_b64
, Definition(tmp
), bld
.def(s1
, scc
), vec
, shift
);
397 emit_split_vector(ctx
, dst
, 2);
399 emit_extract_vector(ctx
, tmp
, 0, dst
);
400 } else if (vec
.size() == 4) {
401 Temp lo
= bld
.tmp(s2
), hi
= bld
.tmp(s2
);
402 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), vec
);
403 hi
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(s1
), hi
, Operand(0u));
404 if (select
!= Temp())
405 hi
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), hi
, Operand(0u), bld
.scc(select
));
406 lo
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), lo
, shift
);
407 Temp mid
= bld
.tmp(s1
);
408 lo
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), Definition(mid
), lo
);
409 hi
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, shift
);
410 mid
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, mid
);
411 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, mid
);
412 emit_split_vector(ctx
, dst
, 2);
416 void byte_align_vector(isel_context
*ctx
, Temp vec
, Operand offset
, Temp dst
, unsigned component_size
)
418 Builder
bld(ctx
->program
, ctx
->block
);
419 if (offset
.isTemp()) {
420 Temp tmp
[4] = {vec
, vec
, vec
, vec
};
422 if (vec
.size() == 4) {
423 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = bld
.tmp(v1
), tmp
[3] = bld
.tmp(v1
);
424 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), Definition(tmp
[2]), Definition(tmp
[3]), vec
);
425 } else if (vec
.size() == 3) {
426 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = bld
.tmp(v1
);
427 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), Definition(tmp
[2]), vec
);
428 } else if (vec
.size() == 2) {
429 tmp
[0] = bld
.tmp(v1
), tmp
[1] = bld
.tmp(v1
), tmp
[2] = tmp
[1];
430 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp
[0]), Definition(tmp
[1]), vec
);
432 for (unsigned i
= 0; i
< dst
.size(); i
++)
433 tmp
[i
] = bld
.vop3(aco_opcode::v_alignbyte_b32
, bld
.def(v1
), tmp
[i
+ 1], tmp
[i
], offset
);
437 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), tmp
[0], tmp
[1]);
439 offset
= Operand(0u);
442 unsigned num_components
= dst
.bytes() / component_size
;
443 if (vec
.regClass() == dst
.regClass()) {
444 assert(offset
.constantValue() == 0);
445 bld
.copy(Definition(dst
), vec
);
446 emit_split_vector(ctx
, dst
, num_components
);
450 emit_split_vector(ctx
, vec
, vec
.bytes() / component_size
);
451 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> elems
;
452 RegClass rc
= RegClass(RegType::vgpr
, component_size
).as_subdword();
454 assert(offset
.constantValue() % component_size
== 0);
455 unsigned skip
= offset
.constantValue() / component_size
;
456 for (unsigned i
= 0; i
< num_components
; i
++)
457 elems
[i
] = emit_extract_vector(ctx
, vec
, i
+ skip
, rc
);
459 /* if dst is vgpr - split the src and create a shrunk version according to the mask. */
460 if (dst
.type() == RegType::vgpr
) {
461 aco_ptr
<Pseudo_instruction
> create_vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
462 for (unsigned i
= 0; i
< num_components
; i
++)
463 create_vec
->operands
[i
] = Operand(elems
[i
]);
464 create_vec
->definitions
[0] = Definition(dst
);
465 bld
.insert(std::move(create_vec
));
467 /* if dst is sgpr - split the src, but move the original to sgpr. */
469 vec
= bld
.pseudo(aco_opcode::p_as_uniform
, bld
.def(RegClass(RegType::sgpr
, vec
.size())), vec
);
470 byte_align_scalar(ctx
, vec
, offset
, dst
);
472 assert(dst
.size() == vec
.size());
473 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), vec
);
476 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
479 Temp
bool_to_vector_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s2
))
481 Builder
bld(ctx
->program
, ctx
->block
);
483 dst
= bld
.tmp(bld
.lm
);
485 assert(val
.regClass() == s1
);
486 assert(dst
.regClass() == bld
.lm
);
488 return bld
.sop2(Builder::s_cselect
, Definition(dst
), Operand((uint32_t) -1), Operand(0u), bld
.scc(val
));
491 Temp
bool_to_scalar_condition(isel_context
*ctx
, Temp val
, Temp dst
= Temp(0, s1
))
493 Builder
bld(ctx
->program
, ctx
->block
);
497 assert(val
.regClass() == bld
.lm
);
498 assert(dst
.regClass() == s1
);
500 /* if we're currently in WQM mode, ensure that the source is also computed in WQM */
501 Temp tmp
= bld
.tmp(s1
);
502 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.scc(Definition(tmp
)), val
, Operand(exec
, bld
.lm
));
503 return emit_wqm(ctx
, tmp
, dst
);
506 Temp
get_alu_src(struct isel_context
*ctx
, nir_alu_src src
, unsigned size
=1)
508 if (src
.src
.ssa
->num_components
== 1 && src
.swizzle
[0] == 0 && size
== 1)
509 return get_ssa_temp(ctx
, src
.src
.ssa
);
511 if (src
.src
.ssa
->num_components
== size
) {
512 bool identity_swizzle
= true;
513 for (unsigned i
= 0; identity_swizzle
&& i
< size
; i
++) {
514 if (src
.swizzle
[i
] != i
)
515 identity_swizzle
= false;
517 if (identity_swizzle
)
518 return get_ssa_temp(ctx
, src
.src
.ssa
);
521 Temp vec
= get_ssa_temp(ctx
, src
.src
.ssa
);
522 unsigned elem_size
= vec
.bytes() / src
.src
.ssa
->num_components
;
523 assert(elem_size
> 0);
524 assert(vec
.bytes() % elem_size
== 0);
526 if (elem_size
< 4 && vec
.type() == RegType::sgpr
) {
527 assert(src
.src
.ssa
->bit_size
== 8 || src
.src
.ssa
->bit_size
== 16);
529 unsigned swizzle
= src
.swizzle
[0];
530 if (vec
.size() > 1) {
531 assert(src
.src
.ssa
->bit_size
== 16);
532 vec
= emit_extract_vector(ctx
, vec
, swizzle
/ 2, s1
);
533 swizzle
= swizzle
& 1;
538 Temp dst
{ctx
->program
->allocateId(), s1
};
539 aco_ptr
<SOP2_instruction
> bfe
{create_instruction
<SOP2_instruction
>(aco_opcode::s_bfe_u32
, Format::SOP2
, 2, 2)};
540 bfe
->operands
[0] = Operand(vec
);
541 bfe
->operands
[1] = Operand(uint32_t((src
.src
.ssa
->bit_size
<< 16) | (src
.src
.ssa
->bit_size
* swizzle
)));
542 bfe
->definitions
[0] = Definition(dst
);
543 bfe
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
544 ctx
->block
->instructions
.emplace_back(std::move(bfe
));
548 RegClass elem_rc
= elem_size
< 4 ? RegClass(vec
.type(), elem_size
).as_subdword() : RegClass(vec
.type(), elem_size
/ 4);
550 return emit_extract_vector(ctx
, vec
, src
.swizzle
[0], elem_rc
);
553 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
554 aco_ptr
<Pseudo_instruction
> vec_instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, size
, 1)};
555 for (unsigned i
= 0; i
< size
; ++i
) {
556 elems
[i
] = emit_extract_vector(ctx
, vec
, src
.swizzle
[i
], elem_rc
);
557 vec_instr
->operands
[i
] = Operand
{elems
[i
]};
559 Temp dst
{ctx
->program
->allocateId(), RegClass(vec
.type(), elem_size
* size
/ 4)};
560 vec_instr
->definitions
[0] = Definition(dst
);
561 ctx
->block
->instructions
.emplace_back(std::move(vec_instr
));
562 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
567 Temp
convert_pointer_to_64_bit(isel_context
*ctx
, Temp ptr
)
571 Builder
bld(ctx
->program
, ctx
->block
);
572 if (ptr
.type() == RegType::vgpr
)
573 ptr
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), ptr
);
574 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
),
575 ptr
, Operand((unsigned)ctx
->options
->address32_hi
));
578 void emit_sop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
, bool writes_scc
)
580 aco_ptr
<SOP2_instruction
> sop2
{create_instruction
<SOP2_instruction
>(op
, Format::SOP2
, 2, writes_scc
? 2 : 1)};
581 sop2
->operands
[0] = Operand(get_alu_src(ctx
, instr
->src
[0]));
582 sop2
->operands
[1] = Operand(get_alu_src(ctx
, instr
->src
[1]));
583 sop2
->definitions
[0] = Definition(dst
);
585 sop2
->definitions
[1] = Definition(ctx
->program
->allocateId(), scc
, s1
);
586 ctx
->block
->instructions
.emplace_back(std::move(sop2
));
589 void emit_vop2_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
590 bool commutative
, bool swap_srcs
=false, bool flush_denorms
= false)
592 Builder
bld(ctx
->program
, ctx
->block
);
593 bld
.is_precise
= instr
->exact
;
595 Temp src0
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 1 : 0]);
596 Temp src1
= get_alu_src(ctx
, instr
->src
[swap_srcs
? 0 : 1]);
597 if (src1
.type() == RegType::sgpr
) {
598 if (commutative
&& src0
.type() == RegType::vgpr
) {
603 src1
= as_vgpr(ctx
, src1
);
607 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
608 assert(dst
.size() == 1);
609 Temp tmp
= bld
.vop2(op
, bld
.def(v1
), src0
, src1
);
610 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
612 bld
.vop2(op
, Definition(dst
), src0
, src1
);
616 void emit_vop2_instruction_logic64(isel_context
*ctx
, nir_alu_instr
*instr
,
617 aco_opcode op
, Temp dst
)
619 Builder
bld(ctx
->program
, ctx
->block
);
620 bld
.is_precise
= instr
->exact
;
622 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
623 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
625 if (src1
.type() == RegType::sgpr
) {
626 assert(src0
.type() == RegType::vgpr
);
627 std::swap(src0
, src1
);
630 Temp src00
= bld
.tmp(src0
.type(), 1);
631 Temp src01
= bld
.tmp(src0
.type(), 1);
632 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
633 Temp src10
= bld
.tmp(v1
);
634 Temp src11
= bld
.tmp(v1
);
635 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
636 Temp lo
= bld
.vop2(op
, bld
.def(v1
), src00
, src10
);
637 Temp hi
= bld
.vop2(op
, bld
.def(v1
), src01
, src11
);
638 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
641 void emit_vop3a_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
,
642 bool flush_denorms
= false)
644 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
645 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
646 Temp src2
= get_alu_src(ctx
, instr
->src
[2]);
648 /* ensure that the instruction has at most 1 sgpr operand
649 * The optimizer will inline constants for us */
650 if (src0
.type() == RegType::sgpr
&& src1
.type() == RegType::sgpr
)
651 src0
= as_vgpr(ctx
, src0
);
652 if (src1
.type() == RegType::sgpr
&& src2
.type() == RegType::sgpr
)
653 src1
= as_vgpr(ctx
, src1
);
654 if (src2
.type() == RegType::sgpr
&& src0
.type() == RegType::sgpr
)
655 src2
= as_vgpr(ctx
, src2
);
657 Builder
bld(ctx
->program
, ctx
->block
);
658 bld
.is_precise
= instr
->exact
;
659 if (flush_denorms
&& ctx
->program
->chip_class
< GFX9
) {
660 assert(dst
.size() == 1);
661 Temp tmp
= bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
662 bld
.vop2(aco_opcode::v_mul_f32
, Definition(dst
), Operand(0x3f800000u
), tmp
);
664 bld
.vop3(op
, Definition(dst
), src0
, src1
, src2
);
668 void emit_vop1_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
670 Builder
bld(ctx
->program
, ctx
->block
);
671 bld
.is_precise
= instr
->exact
;
672 if (dst
.type() == RegType::sgpr
)
673 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
674 bld
.vop1(op
, bld
.def(RegType::vgpr
, dst
.size()), get_alu_src(ctx
, instr
->src
[0])));
676 bld
.vop1(op
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
679 void emit_vopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
681 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
682 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
683 assert(src0
.size() == src1
.size());
685 aco_ptr
<Instruction
> vopc
;
686 if (src1
.type() == RegType::sgpr
) {
687 if (src0
.type() == RegType::vgpr
) {
688 /* to swap the operands, we might also have to change the opcode */
690 case aco_opcode::v_cmp_lt_f16
:
691 op
= aco_opcode::v_cmp_gt_f16
;
693 case aco_opcode::v_cmp_ge_f16
:
694 op
= aco_opcode::v_cmp_le_f16
;
696 case aco_opcode::v_cmp_lt_i16
:
697 op
= aco_opcode::v_cmp_gt_i16
;
699 case aco_opcode::v_cmp_ge_i16
:
700 op
= aco_opcode::v_cmp_le_i16
;
702 case aco_opcode::v_cmp_lt_u16
:
703 op
= aco_opcode::v_cmp_gt_u16
;
705 case aco_opcode::v_cmp_ge_u16
:
706 op
= aco_opcode::v_cmp_le_u16
;
708 case aco_opcode::v_cmp_lt_f32
:
709 op
= aco_opcode::v_cmp_gt_f32
;
711 case aco_opcode::v_cmp_ge_f32
:
712 op
= aco_opcode::v_cmp_le_f32
;
714 case aco_opcode::v_cmp_lt_i32
:
715 op
= aco_opcode::v_cmp_gt_i32
;
717 case aco_opcode::v_cmp_ge_i32
:
718 op
= aco_opcode::v_cmp_le_i32
;
720 case aco_opcode::v_cmp_lt_u32
:
721 op
= aco_opcode::v_cmp_gt_u32
;
723 case aco_opcode::v_cmp_ge_u32
:
724 op
= aco_opcode::v_cmp_le_u32
;
726 case aco_opcode::v_cmp_lt_f64
:
727 op
= aco_opcode::v_cmp_gt_f64
;
729 case aco_opcode::v_cmp_ge_f64
:
730 op
= aco_opcode::v_cmp_le_f64
;
732 case aco_opcode::v_cmp_lt_i64
:
733 op
= aco_opcode::v_cmp_gt_i64
;
735 case aco_opcode::v_cmp_ge_i64
:
736 op
= aco_opcode::v_cmp_le_i64
;
738 case aco_opcode::v_cmp_lt_u64
:
739 op
= aco_opcode::v_cmp_gt_u64
;
741 case aco_opcode::v_cmp_ge_u64
:
742 op
= aco_opcode::v_cmp_le_u64
;
744 default: /* eq and ne are commutative */
751 src1
= as_vgpr(ctx
, src1
);
755 Builder
bld(ctx
->program
, ctx
->block
);
756 bld
.vopc(op
, bld
.hint_vcc(Definition(dst
)), src0
, src1
);
759 void emit_sopc_instruction(isel_context
*ctx
, nir_alu_instr
*instr
, aco_opcode op
, Temp dst
)
761 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
762 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
763 Builder
bld(ctx
->program
, ctx
->block
);
765 assert(dst
.regClass() == bld
.lm
);
766 assert(src0
.type() == RegType::sgpr
);
767 assert(src1
.type() == RegType::sgpr
);
768 assert(src0
.regClass() == src1
.regClass());
770 /* Emit the SALU comparison instruction */
771 Temp cmp
= bld
.sopc(op
, bld
.scc(bld
.def(s1
)), src0
, src1
);
772 /* Turn the result into a per-lane bool */
773 bool_to_vector_condition(ctx
, cmp
, dst
);
776 void emit_comparison(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
,
777 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
)
779 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
;
780 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
;
781 bool use_valu
= s_op
== aco_opcode::num_opcodes
||
782 nir_dest_is_divergent(instr
->dest
.dest
) ||
783 ctx
->allocated
[instr
->src
[0].src
.ssa
->index
].type() == RegType::vgpr
||
784 ctx
->allocated
[instr
->src
[1].src
.ssa
->index
].type() == RegType::vgpr
;
785 aco_opcode op
= use_valu
? v_op
: s_op
;
786 assert(op
!= aco_opcode::num_opcodes
);
787 assert(dst
.regClass() == ctx
->program
->lane_mask
);
790 emit_vopc_instruction(ctx
, instr
, op
, dst
);
792 emit_sopc_instruction(ctx
, instr
, op
, dst
);
795 void emit_boolean_logic(isel_context
*ctx
, nir_alu_instr
*instr
, Builder::WaveSpecificOpcode op
, Temp dst
)
797 Builder
bld(ctx
->program
, ctx
->block
);
798 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
799 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
801 assert(dst
.regClass() == bld
.lm
);
802 assert(src0
.regClass() == bld
.lm
);
803 assert(src1
.regClass() == bld
.lm
);
805 bld
.sop2(op
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
808 void emit_bcsel(isel_context
*ctx
, nir_alu_instr
*instr
, Temp dst
)
810 Builder
bld(ctx
->program
, ctx
->block
);
811 Temp cond
= get_alu_src(ctx
, instr
->src
[0]);
812 Temp then
= get_alu_src(ctx
, instr
->src
[1]);
813 Temp els
= get_alu_src(ctx
, instr
->src
[2]);
815 assert(cond
.regClass() == bld
.lm
);
817 if (dst
.type() == RegType::vgpr
) {
818 aco_ptr
<Instruction
> bcsel
;
819 if (dst
.size() == 1) {
820 then
= as_vgpr(ctx
, then
);
821 els
= as_vgpr(ctx
, els
);
823 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), els
, then
, cond
);
824 } else if (dst
.size() == 2) {
825 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
826 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), then
);
827 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
828 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), els
);
830 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, cond
);
831 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, cond
);
833 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
835 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
836 nir_print_instr(&instr
->instr
, stderr
);
837 fprintf(stderr
, "\n");
842 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
843 assert(dst
.regClass() == bld
.lm
);
844 assert(then
.regClass() == bld
.lm
);
845 assert(els
.regClass() == bld
.lm
);
848 if (!nir_src_is_divergent(instr
->src
[0].src
)) { /* uniform condition and values in sgpr */
849 if (dst
.regClass() == s1
|| dst
.regClass() == s2
) {
850 assert((then
.regClass() == s1
|| then
.regClass() == s2
) && els
.regClass() == then
.regClass());
851 assert(dst
.size() == then
.size());
852 aco_opcode op
= dst
.regClass() == s1
? aco_opcode::s_cselect_b32
: aco_opcode::s_cselect_b64
;
853 bld
.sop2(op
, Definition(dst
), then
, els
, bld
.scc(bool_to_scalar_condition(ctx
, cond
)));
855 fprintf(stderr
, "Unimplemented uniform bcsel bit size: ");
856 nir_print_instr(&instr
->instr
, stderr
);
857 fprintf(stderr
, "\n");
862 /* divergent boolean bcsel
863 * this implements bcsel on bools: dst = s0 ? s1 : s2
864 * are going to be: dst = (s0 & s1) | (~s0 & s2) */
865 assert(instr
->dest
.dest
.ssa
.bit_size
== 1);
867 if (cond
.id() != then
.id())
868 then
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
, then
);
870 if (cond
.id() == els
.id())
871 bld
.sop1(Builder::s_mov
, Definition(dst
), then
);
873 bld
.sop2(Builder::s_or
, Definition(dst
), bld
.def(s1
, scc
), then
,
874 bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), els
, cond
));
877 void emit_scaled_op(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
,
878 aco_opcode op
, uint32_t undo
)
880 /* multiply by 16777216 to handle denormals */
881 Temp is_denormal
= bld
.vopc(aco_opcode::v_cmp_class_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
882 as_vgpr(ctx
, val
), bld
.copy(bld
.def(v1
), Operand((1u << 7) | (1u << 4))));
883 Temp scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x4b800000u
), val
);
884 scaled
= bld
.vop1(op
, bld
.def(v1
), scaled
);
885 scaled
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(undo
), scaled
);
887 Temp not_scaled
= bld
.vop1(op
, bld
.def(v1
), val
);
889 bld
.vop2(aco_opcode::v_cndmask_b32
, dst
, not_scaled
, scaled
, is_denormal
);
892 void emit_rcp(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
894 if (ctx
->block
->fp_mode
.denorm32
== 0) {
895 bld
.vop1(aco_opcode::v_rcp_f32
, dst
, val
);
899 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rcp_f32
, 0x4b800000u
);
902 void emit_rsq(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
904 if (ctx
->block
->fp_mode
.denorm32
== 0) {
905 bld
.vop1(aco_opcode::v_rsq_f32
, dst
, val
);
909 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_rsq_f32
, 0x45800000u
);
912 void emit_sqrt(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
914 if (ctx
->block
->fp_mode
.denorm32
== 0) {
915 bld
.vop1(aco_opcode::v_sqrt_f32
, dst
, val
);
919 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_sqrt_f32
, 0x39800000u
);
922 void emit_log2(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
924 if (ctx
->block
->fp_mode
.denorm32
== 0) {
925 bld
.vop1(aco_opcode::v_log_f32
, dst
, val
);
929 emit_scaled_op(ctx
, bld
, dst
, val
, aco_opcode::v_log_f32
, 0xc1c00000u
);
932 Temp
emit_trunc_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
934 if (ctx
->options
->chip_class
>= GFX7
)
935 return bld
.vop1(aco_opcode::v_trunc_f64
, Definition(dst
), val
);
937 /* GFX6 doesn't support V_TRUNC_F64, lower it. */
938 /* TODO: create more efficient code! */
939 if (val
.type() == RegType::sgpr
)
940 val
= as_vgpr(ctx
, val
);
942 /* Split the input value. */
943 Temp val_lo
= bld
.tmp(v1
), val_hi
= bld
.tmp(v1
);
944 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
946 /* Extract the exponent and compute the unbiased value. */
947 Temp exponent
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), val_hi
, Operand(20u), Operand(11u));
948 exponent
= bld
.vsub32(bld
.def(v1
), exponent
, Operand(1023u));
950 /* Extract the fractional part. */
951 Temp fract_mask
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x000fffffu
));
952 fract_mask
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), fract_mask
, exponent
);
954 Temp fract_mask_lo
= bld
.tmp(v1
), fract_mask_hi
= bld
.tmp(v1
);
955 bld
.pseudo(aco_opcode::p_split_vector
, Definition(fract_mask_lo
), Definition(fract_mask_hi
), fract_mask
);
957 Temp fract_lo
= bld
.tmp(v1
), fract_hi
= bld
.tmp(v1
);
958 Temp tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_lo
);
959 fract_lo
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_lo
, tmp
);
960 tmp
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), fract_mask_hi
);
961 fract_hi
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), val_hi
, tmp
);
963 /* Get the sign bit. */
964 Temp sign
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x80000000u
), val_hi
);
966 /* Decide the operation to apply depending on the unbiased exponent. */
967 Temp exp_lt0
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), exponent
, Operand(0u));
968 Temp dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_lo
, bld
.copy(bld
.def(v1
), Operand(0u)), exp_lt0
);
969 Temp dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), fract_hi
, sign
, exp_lt0
);
970 Temp exp_gt51
= bld
.vopc_e64(aco_opcode::v_cmp_gt_i32
, bld
.def(s2
), exponent
, Operand(51u));
971 dst_lo
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_lo
, val_lo
, exp_gt51
);
972 dst_hi
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), dst_hi
, val_hi
, exp_gt51
);
974 return bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst_lo
, dst_hi
);
977 Temp
emit_floor_f64(isel_context
*ctx
, Builder
& bld
, Definition dst
, Temp val
)
979 if (ctx
->options
->chip_class
>= GFX7
)
980 return bld
.vop1(aco_opcode::v_floor_f64
, Definition(dst
), val
);
982 /* GFX6 doesn't support V_FLOOR_F64, lower it. */
983 Temp src0
= as_vgpr(ctx
, val
);
985 Temp mask
= bld
.copy(bld
.def(s1
), Operand(3u)); /* isnan */
986 Temp min_val
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(-1u), Operand(0x3fefffffu
));
988 Temp isnan
= bld
.vopc_e64(aco_opcode::v_cmp_class_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, mask
);
989 Temp fract
= bld
.vop1(aco_opcode::v_fract_f64
, bld
.def(v2
), src0
);
990 Temp min
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
), fract
, min_val
);
992 Temp then_lo
= bld
.tmp(v1
), then_hi
= bld
.tmp(v1
);
993 bld
.pseudo(aco_opcode::p_split_vector
, Definition(then_lo
), Definition(then_hi
), src0
);
994 Temp else_lo
= bld
.tmp(v1
), else_hi
= bld
.tmp(v1
);
995 bld
.pseudo(aco_opcode::p_split_vector
, Definition(else_lo
), Definition(else_hi
), min
);
997 Temp dst0
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_lo
, then_lo
, isnan
);
998 Temp dst1
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), else_hi
, then_hi
, isnan
);
1000 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), dst0
, dst1
);
1002 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src0
, v
);
1003 static_cast<VOP3A_instruction
*>(add
)->neg
[1] = true;
1005 return add
->definitions
[0].getTemp();
1008 Temp
convert_int(isel_context
*ctx
, Builder
& bld
, Temp src
, unsigned src_bits
, unsigned dst_bits
, bool is_signed
, Temp dst
=Temp()) {
1010 if (dst_bits
% 32 == 0 || src
.type() == RegType::sgpr
)
1011 dst
= bld
.tmp(src
.type(), DIV_ROUND_UP(dst_bits
, 32u));
1013 dst
= bld
.tmp(RegClass(RegType::vgpr
, dst_bits
/ 8u).as_subdword());
1016 if (dst
.bytes() == src
.bytes() && dst_bits
< src_bits
)
1017 return bld
.copy(Definition(dst
), src
);
1018 else if (dst
.bytes() < src
.bytes())
1019 return bld
.pseudo(aco_opcode::p_extract_vector
, Definition(dst
), src
, Operand(0u));
1023 tmp
= src_bits
== 32 ? src
: bld
.tmp(src
.type(), 1);
1026 } else if (src
.regClass() == s1
) {
1028 bld
.sop1(src_bits
== 8 ? aco_opcode::s_sext_i32_i8
: aco_opcode::s_sext_i32_i16
, Definition(tmp
), src
);
1030 bld
.sop2(aco_opcode::s_and_b32
, Definition(tmp
), bld
.def(s1
, scc
), Operand(src_bits
== 8 ? 0xFFu
: 0xFFFFu
), src
);
1031 } else if (ctx
->options
->chip_class
>= GFX8
) {
1032 assert(src_bits
!= 8 || src
.regClass() == v1b
);
1033 assert(src_bits
!= 16 || src
.regClass() == v2b
);
1034 aco_ptr
<SDWA_instruction
> sdwa
{create_instruction
<SDWA_instruction
>(aco_opcode::v_mov_b32
, asSDWA(Format::VOP1
), 1, 1)};
1035 sdwa
->operands
[0] = Operand(src
);
1036 sdwa
->definitions
[0] = Definition(tmp
);
1038 sdwa
->sel
[0] = src_bits
== 8 ? sdwa_sbyte
: sdwa_sword
;
1040 sdwa
->sel
[0] = src_bits
== 8 ? sdwa_ubyte
: sdwa_uword
;
1041 sdwa
->dst_sel
= tmp
.bytes() == 2 ? sdwa_uword
: sdwa_udword
;
1042 bld
.insert(std::move(sdwa
));
1044 assert(ctx
->options
->chip_class
== GFX6
|| ctx
->options
->chip_class
== GFX7
);
1045 aco_opcode opcode
= is_signed
? aco_opcode::v_bfe_i32
: aco_opcode::v_bfe_u32
;
1046 bld
.vop3(opcode
, Definition(tmp
), src
, Operand(0u), Operand(src_bits
== 8 ? 8u : 16u));
1049 if (dst_bits
== 64) {
1050 if (is_signed
&& dst
.regClass() == s2
) {
1051 Temp high
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(31u));
1052 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, high
);
1053 } else if (is_signed
&& dst
.regClass() == v2
) {
1054 Temp high
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), tmp
);
1055 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, high
);
1057 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tmp
, Operand(0u));
1064 void visit_alu_instr(isel_context
*ctx
, nir_alu_instr
*instr
)
1066 if (!instr
->dest
.dest
.is_ssa
) {
1067 fprintf(stderr
, "nir alu dst not in ssa: ");
1068 nir_print_instr(&instr
->instr
, stderr
);
1069 fprintf(stderr
, "\n");
1072 Builder
bld(ctx
->program
, ctx
->block
);
1073 bld
.is_precise
= instr
->exact
;
1074 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.dest
.ssa
);
1079 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
1080 unsigned num
= instr
->dest
.dest
.ssa
.num_components
;
1081 for (unsigned i
= 0; i
< num
; ++i
)
1082 elems
[i
] = get_alu_src(ctx
, instr
->src
[i
]);
1084 if (instr
->dest
.dest
.ssa
.bit_size
>= 32 || dst
.type() == RegType::vgpr
) {
1085 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.dest
.ssa
.num_components
, 1)};
1086 RegClass elem_rc
= RegClass::get(RegType::vgpr
, instr
->dest
.dest
.ssa
.bit_size
/ 8u);
1087 for (unsigned i
= 0; i
< num
; ++i
) {
1088 if (elems
[i
].type() == RegType::sgpr
&& elem_rc
.is_subdword())
1089 vec
->operands
[i
] = Operand(emit_extract_vector(ctx
, elems
[i
], 0, elem_rc
));
1091 vec
->operands
[i
] = Operand
{elems
[i
]};
1093 vec
->definitions
[0] = Definition(dst
);
1094 ctx
->block
->instructions
.emplace_back(std::move(vec
));
1095 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
1097 // TODO: that is a bit suboptimal..
1098 Temp mask
= bld
.copy(bld
.def(s1
), Operand((1u << instr
->dest
.dest
.ssa
.bit_size
) - 1));
1099 for (unsigned i
= 0; i
< num
- 1; ++i
)
1100 if (((i
+1) * instr
->dest
.dest
.ssa
.bit_size
) % 32)
1101 elems
[i
] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), elems
[i
], mask
);
1102 for (unsigned i
= 0; i
< num
; ++i
) {
1103 unsigned bit
= i
* instr
->dest
.dest
.ssa
.bit_size
;
1104 if (bit
% 32 == 0) {
1105 elems
[bit
/ 32] = elems
[i
];
1107 elems
[i
] = bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
),
1108 elems
[i
], Operand((i
* instr
->dest
.dest
.ssa
.bit_size
) % 32));
1109 elems
[bit
/ 32] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), elems
[bit
/ 32], elems
[i
]);
1112 if (dst
.size() == 1)
1113 bld
.copy(Definition(dst
), elems
[0]);
1115 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), elems
[0], elems
[1]);
1120 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1121 aco_ptr
<Instruction
> mov
;
1122 if (dst
.type() == RegType::sgpr
) {
1123 if (src
.type() == RegType::vgpr
)
1124 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), src
);
1125 else if (src
.regClass() == s1
)
1126 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
1127 else if (src
.regClass() == s2
)
1128 bld
.sop1(aco_opcode::s_mov_b64
, Definition(dst
), src
);
1130 unreachable("wrong src register class for nir_op_imov");
1132 if (dst
.regClass() == v1
)
1133 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), src
);
1134 else if (dst
.regClass() == v1b
||
1135 dst
.regClass() == v2b
||
1136 dst
.regClass() == v2
)
1137 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
1139 unreachable("wrong src register class for nir_op_imov");
1144 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1145 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1146 assert(src
.regClass() == bld
.lm
);
1147 assert(dst
.regClass() == bld
.lm
);
1148 /* Don't use s_andn2 here, this allows the optimizer to make a better decision */
1149 Temp tmp
= bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
);
1150 bld
.sop2(Builder::s_and
, Definition(dst
), bld
.def(s1
, scc
), tmp
, Operand(exec
, bld
.lm
));
1151 } else if (dst
.regClass() == v1
) {
1152 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_not_b32
, dst
);
1153 } else if (dst
.regClass() == v2
) {
1154 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
1155 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
1156 lo
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), lo
);
1157 hi
= bld
.vop1(aco_opcode::v_not_b32
, bld
.def(v1
), hi
);
1158 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
1159 } else if (dst
.type() == RegType::sgpr
) {
1160 aco_opcode opcode
= dst
.size() == 1 ? aco_opcode::s_not_b32
: aco_opcode::s_not_b64
;
1161 bld
.sop1(opcode
, Definition(dst
), bld
.def(s1
, scc
), src
);
1163 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1164 nir_print_instr(&instr
->instr
, stderr
);
1165 fprintf(stderr
, "\n");
1170 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1171 if (dst
.regClass() == v1
) {
1172 bld
.vsub32(Definition(dst
), Operand(0u), Operand(src
));
1173 } else if (dst
.regClass() == s1
) {
1174 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand((uint32_t) -1), src
);
1175 } else if (dst
.size() == 2) {
1176 Temp src0
= bld
.tmp(dst
.type(), 1);
1177 Temp src1
= bld
.tmp(dst
.type(), 1);
1178 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
1180 if (dst
.regClass() == s2
) {
1181 Temp carry
= bld
.tmp(s1
);
1182 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), Operand(0u), src0
);
1183 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), src1
, carry
);
1184 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1186 Temp lower
= bld
.tmp(v1
);
1187 Temp borrow
= bld
.vsub32(Definition(lower
), Operand(0u), src0
, true).def(1).getTemp();
1188 Temp upper
= bld
.vsub32(bld
.def(v1
), Operand(0u), src1
, false, borrow
);
1189 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1192 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1193 nir_print_instr(&instr
->instr
, stderr
);
1194 fprintf(stderr
, "\n");
1199 if (dst
.regClass() == s1
) {
1200 bld
.sop1(aco_opcode::s_abs_i32
, Definition(dst
), bld
.def(s1
, scc
), get_alu_src(ctx
, instr
->src
[0]));
1201 } else if (dst
.regClass() == v1
) {
1202 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1203 bld
.vop2(aco_opcode::v_max_i32
, Definition(dst
), src
, bld
.vsub32(bld
.def(v1
), Operand(0u), src
));
1205 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1206 nir_print_instr(&instr
->instr
, stderr
);
1207 fprintf(stderr
, "\n");
1211 case nir_op_isign
: {
1212 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1213 if (dst
.regClass() == s1
) {
1214 Temp tmp
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand((uint32_t)-1));
1215 bld
.sop2(aco_opcode::s_min_i32
, Definition(dst
), bld
.def(s1
, scc
), tmp
, Operand(1u));
1216 } else if (dst
.regClass() == s2
) {
1217 Temp neg
= bld
.sop2(aco_opcode::s_ashr_i64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(63u));
1219 if (ctx
->program
->chip_class
>= GFX8
)
1220 neqz
= bld
.sopc(aco_opcode::s_cmp_lg_u64
, bld
.def(s1
, scc
), src
, Operand(0u));
1222 neqz
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(0u)).def(1).getTemp();
1223 /* SCC gets zero-extended to 64 bit */
1224 bld
.sop2(aco_opcode::s_or_b64
, Definition(dst
), bld
.def(s1
, scc
), neg
, bld
.scc(neqz
));
1225 } else if (dst
.regClass() == v1
) {
1226 bld
.vop3(aco_opcode::v_med3_i32
, Definition(dst
), Operand((uint32_t)-1), src
, Operand(1u));
1227 } else if (dst
.regClass() == v2
) {
1228 Temp upper
= emit_extract_vector(ctx
, src
, 1, v1
);
1229 Temp neg
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), upper
);
1230 Temp gtz
= bld
.vopc(aco_opcode::v_cmp_ge_i64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
1231 Temp lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(1u), neg
, gtz
);
1232 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), neg
, gtz
);
1233 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1235 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1236 nir_print_instr(&instr
->instr
, stderr
);
1237 fprintf(stderr
, "\n");
1242 if (dst
.regClass() == v1
) {
1243 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_i32
, dst
, true);
1244 } else if (dst
.regClass() == s1
) {
1245 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_i32
, dst
, true);
1247 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1248 nir_print_instr(&instr
->instr
, stderr
);
1249 fprintf(stderr
, "\n");
1254 if (dst
.regClass() == v1
) {
1255 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_u32
, dst
, true);
1256 } else if (dst
.regClass() == s1
) {
1257 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_max_u32
, dst
, true);
1259 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1260 nir_print_instr(&instr
->instr
, stderr
);
1261 fprintf(stderr
, "\n");
1266 if (dst
.regClass() == v1
) {
1267 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_i32
, dst
, true);
1268 } else if (dst
.regClass() == s1
) {
1269 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_i32
, dst
, true);
1271 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1272 nir_print_instr(&instr
->instr
, stderr
);
1273 fprintf(stderr
, "\n");
1278 if (dst
.regClass() == v1
) {
1279 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_u32
, dst
, true);
1280 } else if (dst
.regClass() == s1
) {
1281 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_min_u32
, dst
, true);
1283 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1284 nir_print_instr(&instr
->instr
, stderr
);
1285 fprintf(stderr
, "\n");
1290 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1291 emit_boolean_logic(ctx
, instr
, Builder::s_or
, dst
);
1292 } else if (dst
.regClass() == v1
) {
1293 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_or_b32
, dst
, true);
1294 } else if (dst
.regClass() == v2
) {
1295 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_or_b32
, dst
);
1296 } else if (dst
.regClass() == s1
) {
1297 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b32
, dst
, true);
1298 } else if (dst
.regClass() == s2
) {
1299 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_or_b64
, dst
, true);
1301 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1302 nir_print_instr(&instr
->instr
, stderr
);
1303 fprintf(stderr
, "\n");
1308 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1309 emit_boolean_logic(ctx
, instr
, Builder::s_and
, dst
);
1310 } else if (dst
.regClass() == v1
) {
1311 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_and_b32
, dst
, true);
1312 } else if (dst
.regClass() == v2
) {
1313 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_and_b32
, dst
);
1314 } else if (dst
.regClass() == s1
) {
1315 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b32
, dst
, true);
1316 } else if (dst
.regClass() == s2
) {
1317 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_and_b64
, dst
, true);
1319 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1320 nir_print_instr(&instr
->instr
, stderr
);
1321 fprintf(stderr
, "\n");
1326 if (instr
->dest
.dest
.ssa
.bit_size
== 1) {
1327 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
1328 } else if (dst
.regClass() == v1
) {
1329 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_xor_b32
, dst
, true);
1330 } else if (dst
.regClass() == v2
) {
1331 emit_vop2_instruction_logic64(ctx
, instr
, aco_opcode::v_xor_b32
, dst
);
1332 } else if (dst
.regClass() == s1
) {
1333 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b32
, dst
, true);
1334 } else if (dst
.regClass() == s2
) {
1335 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_xor_b64
, dst
, true);
1337 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1338 nir_print_instr(&instr
->instr
, stderr
);
1339 fprintf(stderr
, "\n");
1344 if (dst
.regClass() == v1
) {
1345 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshrrev_b32
, dst
, false, true);
1346 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1347 bld
.vop3(aco_opcode::v_lshrrev_b64
, Definition(dst
),
1348 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1349 } else if (dst
.regClass() == v2
) {
1350 bld
.vop3(aco_opcode::v_lshr_b64
, Definition(dst
),
1351 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1352 } else if (dst
.regClass() == s2
) {
1353 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b64
, dst
, true);
1354 } else if (dst
.regClass() == s1
) {
1355 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshr_b32
, dst
, true);
1357 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1358 nir_print_instr(&instr
->instr
, stderr
);
1359 fprintf(stderr
, "\n");
1364 if (dst
.regClass() == v1
) {
1365 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_lshlrev_b32
, dst
, false, true);
1366 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1367 bld
.vop3(aco_opcode::v_lshlrev_b64
, Definition(dst
),
1368 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1369 } else if (dst
.regClass() == v2
) {
1370 bld
.vop3(aco_opcode::v_lshl_b64
, Definition(dst
),
1371 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1372 } else if (dst
.regClass() == s1
) {
1373 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b32
, dst
, true);
1374 } else if (dst
.regClass() == s2
) {
1375 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_lshl_b64
, dst
, true);
1377 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1378 nir_print_instr(&instr
->instr
, stderr
);
1379 fprintf(stderr
, "\n");
1384 if (dst
.regClass() == v1
) {
1385 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ashrrev_i32
, dst
, false, true);
1386 } else if (dst
.regClass() == v2
&& ctx
->program
->chip_class
>= GFX8
) {
1387 bld
.vop3(aco_opcode::v_ashrrev_i64
, Definition(dst
),
1388 get_alu_src(ctx
, instr
->src
[1]), get_alu_src(ctx
, instr
->src
[0]));
1389 } else if (dst
.regClass() == v2
) {
1390 bld
.vop3(aco_opcode::v_ashr_i64
, Definition(dst
),
1391 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1392 } else if (dst
.regClass() == s1
) {
1393 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i32
, dst
, true);
1394 } else if (dst
.regClass() == s2
) {
1395 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_ashr_i64
, dst
, true);
1397 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1398 nir_print_instr(&instr
->instr
, stderr
);
1399 fprintf(stderr
, "\n");
1403 case nir_op_find_lsb
: {
1404 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1405 if (src
.regClass() == s1
) {
1406 bld
.sop1(aco_opcode::s_ff1_i32_b32
, Definition(dst
), src
);
1407 } else if (src
.regClass() == v1
) {
1408 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ffbl_b32
, dst
);
1409 } else if (src
.regClass() == s2
) {
1410 bld
.sop1(aco_opcode::s_ff1_i32_b64
, Definition(dst
), src
);
1412 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1413 nir_print_instr(&instr
->instr
, stderr
);
1414 fprintf(stderr
, "\n");
1418 case nir_op_ufind_msb
:
1419 case nir_op_ifind_msb
: {
1420 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1421 if (src
.regClass() == s1
|| src
.regClass() == s2
) {
1422 aco_opcode op
= src
.regClass() == s2
?
1423 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b64
: aco_opcode::s_flbit_i32_i64
) :
1424 (instr
->op
== nir_op_ufind_msb
? aco_opcode::s_flbit_i32_b32
: aco_opcode::s_flbit_i32
);
1425 Temp msb_rev
= bld
.sop1(op
, bld
.def(s1
), src
);
1427 Builder::Result sub
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
1428 Operand(src
.size() * 32u - 1u), msb_rev
);
1429 Temp msb
= sub
.def(0).getTemp();
1430 Temp carry
= sub
.def(1).getTemp();
1432 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t)-1), msb
, bld
.scc(carry
));
1433 } else if (src
.regClass() == v1
) {
1434 aco_opcode op
= instr
->op
== nir_op_ufind_msb
? aco_opcode::v_ffbh_u32
: aco_opcode::v_ffbh_i32
;
1435 Temp msb_rev
= bld
.tmp(v1
);
1436 emit_vop1_instruction(ctx
, instr
, op
, msb_rev
);
1437 Temp msb
= bld
.tmp(v1
);
1438 Temp carry
= bld
.vsub32(Definition(msb
), Operand(31u), Operand(msb_rev
), true).def(1).getTemp();
1439 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), msb
, Operand((uint32_t)-1), carry
);
1441 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1442 nir_print_instr(&instr
->instr
, stderr
);
1443 fprintf(stderr
, "\n");
1447 case nir_op_bitfield_reverse
: {
1448 if (dst
.regClass() == s1
) {
1449 bld
.sop1(aco_opcode::s_brev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1450 } else if (dst
.regClass() == v1
) {
1451 bld
.vop1(aco_opcode::v_bfrev_b32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
1453 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1454 nir_print_instr(&instr
->instr
, stderr
);
1455 fprintf(stderr
, "\n");
1460 if (dst
.regClass() == s1
) {
1461 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_add_u32
, dst
, true);
1465 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1466 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1467 if (dst
.regClass() == v1
) {
1468 bld
.vadd32(Definition(dst
), Operand(src0
), Operand(src1
));
1472 assert(src0
.size() == 2 && src1
.size() == 2);
1473 Temp src00
= bld
.tmp(src0
.type(), 1);
1474 Temp src01
= bld
.tmp(dst
.type(), 1);
1475 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1476 Temp src10
= bld
.tmp(src1
.type(), 1);
1477 Temp src11
= bld
.tmp(dst
.type(), 1);
1478 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1480 if (dst
.regClass() == s2
) {
1481 Temp carry
= bld
.tmp(s1
);
1482 Temp dst0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1483 Temp dst1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, bld
.scc(carry
));
1484 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1485 } else if (dst
.regClass() == v2
) {
1486 Temp dst0
= bld
.tmp(v1
);
1487 Temp carry
= bld
.vadd32(Definition(dst0
), src00
, src10
, true).def(1).getTemp();
1488 Temp dst1
= bld
.vadd32(bld
.def(v1
), src01
, src11
, false, carry
);
1489 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1491 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1492 nir_print_instr(&instr
->instr
, stderr
);
1493 fprintf(stderr
, "\n");
1497 case nir_op_uadd_sat
: {
1498 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1499 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1500 if (dst
.regClass() == s1
) {
1501 Temp tmp
= bld
.tmp(s1
), carry
= bld
.tmp(s1
);
1502 bld
.sop2(aco_opcode::s_add_u32
, Definition(tmp
), bld
.scc(Definition(carry
)),
1504 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(dst
), Operand((uint32_t) -1), tmp
, bld
.scc(carry
));
1505 } else if (dst
.regClass() == v1
) {
1506 if (ctx
->options
->chip_class
>= GFX9
) {
1507 aco_ptr
<VOP3A_instruction
> add
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_add_u32
, asVOP3(Format::VOP2
), 2, 1)};
1508 add
->operands
[0] = Operand(src0
);
1509 add
->operands
[1] = Operand(src1
);
1510 add
->definitions
[0] = Definition(dst
);
1512 ctx
->block
->instructions
.emplace_back(std::move(add
));
1514 if (src1
.regClass() != v1
)
1515 std::swap(src0
, src1
);
1516 assert(src1
.regClass() == v1
);
1517 Temp tmp
= bld
.tmp(v1
);
1518 Temp carry
= bld
.vadd32(Definition(tmp
), src0
, src1
, true).def(1).getTemp();
1519 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), tmp
, Operand((uint32_t) -1), carry
);
1522 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1523 nir_print_instr(&instr
->instr
, stderr
);
1524 fprintf(stderr
, "\n");
1528 case nir_op_uadd_carry
: {
1529 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1530 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1531 if (dst
.regClass() == s1
) {
1532 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1535 if (dst
.regClass() == v1
) {
1536 Temp carry
= bld
.vadd32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1537 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), carry
);
1541 Temp src00
= bld
.tmp(src0
.type(), 1);
1542 Temp src01
= bld
.tmp(dst
.type(), 1);
1543 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1544 Temp src10
= bld
.tmp(src1
.type(), 1);
1545 Temp src11
= bld
.tmp(dst
.type(), 1);
1546 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1547 if (dst
.regClass() == s2
) {
1548 Temp carry
= bld
.tmp(s1
);
1549 bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1550 carry
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(carry
)).def(1).getTemp();
1551 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1552 } else if (dst
.regClass() == v2
) {
1553 Temp carry
= bld
.vadd32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1554 carry
= bld
.vadd32(bld
.def(v1
), src01
, src11
, true, carry
).def(1).getTemp();
1555 carry
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), carry
);
1556 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), carry
, Operand(0u));
1558 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1559 nir_print_instr(&instr
->instr
, stderr
);
1560 fprintf(stderr
, "\n");
1565 if (dst
.regClass() == s1
) {
1566 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_sub_i32
, dst
, true);
1570 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1571 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1572 if (dst
.regClass() == v1
) {
1573 bld
.vsub32(Definition(dst
), src0
, src1
);
1577 Temp src00
= bld
.tmp(src0
.type(), 1);
1578 Temp src01
= bld
.tmp(dst
.type(), 1);
1579 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1580 Temp src10
= bld
.tmp(src1
.type(), 1);
1581 Temp src11
= bld
.tmp(dst
.type(), 1);
1582 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1583 if (dst
.regClass() == s2
) {
1584 Temp carry
= bld
.tmp(s1
);
1585 Temp dst0
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), src00
, src10
);
1586 Temp dst1
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src01
, src11
, carry
);
1587 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
1588 } else if (dst
.regClass() == v2
) {
1589 Temp lower
= bld
.tmp(v1
);
1590 Temp borrow
= bld
.vsub32(Definition(lower
), src00
, src10
, true).def(1).getTemp();
1591 Temp upper
= bld
.vsub32(bld
.def(v1
), src01
, src11
, false, borrow
);
1592 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1594 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1595 nir_print_instr(&instr
->instr
, stderr
);
1596 fprintf(stderr
, "\n");
1600 case nir_op_usub_borrow
: {
1601 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1602 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1603 if (dst
.regClass() == s1
) {
1604 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(dst
)), src0
, src1
);
1606 } else if (dst
.regClass() == v1
) {
1607 Temp borrow
= bld
.vsub32(bld
.def(v1
), src0
, src1
, true).def(1).getTemp();
1608 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), borrow
);
1612 Temp src00
= bld
.tmp(src0
.type(), 1);
1613 Temp src01
= bld
.tmp(dst
.type(), 1);
1614 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src00
), Definition(src01
), src0
);
1615 Temp src10
= bld
.tmp(src1
.type(), 1);
1616 Temp src11
= bld
.tmp(dst
.type(), 1);
1617 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src10
), Definition(src11
), src1
);
1618 if (dst
.regClass() == s2
) {
1619 Temp borrow
= bld
.tmp(s1
);
1620 bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), src00
, src10
);
1621 borrow
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.scc(bld
.def(s1
)), src01
, src11
, bld
.scc(borrow
)).def(1).getTemp();
1622 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1623 } else if (dst
.regClass() == v2
) {
1624 Temp borrow
= bld
.vsub32(bld
.def(v1
), src00
, src10
, true).def(1).getTemp();
1625 borrow
= bld
.vsub32(bld
.def(v1
), src01
, src11
, true, Operand(borrow
)).def(1).getTemp();
1626 borrow
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand(1u), borrow
);
1627 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), borrow
, Operand(0u));
1629 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1630 nir_print_instr(&instr
->instr
, stderr
);
1631 fprintf(stderr
, "\n");
1636 if (dst
.regClass() == v1
) {
1637 bld
.vop3(aco_opcode::v_mul_lo_u32
, Definition(dst
),
1638 get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1639 } else if (dst
.regClass() == s1
) {
1640 emit_sop2_instruction(ctx
, instr
, aco_opcode::s_mul_i32
, dst
, false);
1642 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1643 nir_print_instr(&instr
->instr
, stderr
);
1644 fprintf(stderr
, "\n");
1648 case nir_op_umul_high
: {
1649 if (dst
.regClass() == v1
) {
1650 bld
.vop3(aco_opcode::v_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1651 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1652 bld
.sop2(aco_opcode::s_mul_hi_u32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1653 } else if (dst
.regClass() == s1
) {
1654 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1655 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1656 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1658 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1659 nir_print_instr(&instr
->instr
, stderr
);
1660 fprintf(stderr
, "\n");
1664 case nir_op_imul_high
: {
1665 if (dst
.regClass() == v1
) {
1666 bld
.vop3(aco_opcode::v_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1667 } else if (dst
.regClass() == s1
&& ctx
->options
->chip_class
>= GFX9
) {
1668 bld
.sop2(aco_opcode::s_mul_hi_i32
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]), get_alu_src(ctx
, instr
->src
[1]));
1669 } else if (dst
.regClass() == s1
) {
1670 Temp tmp
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), get_alu_src(ctx
, instr
->src
[0]),
1671 as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1])));
1672 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
1674 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1675 nir_print_instr(&instr
->instr
, stderr
);
1676 fprintf(stderr
, "\n");
1681 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1682 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1683 if (dst
.regClass() == v2b
) {
1684 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f16
, dst
, true);
1685 } else if (dst
.regClass() == v1
) {
1686 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_mul_f32
, dst
, true);
1687 } else if (dst
.regClass() == v2
) {
1688 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), src0
, src1
);
1690 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1691 nir_print_instr(&instr
->instr
, stderr
);
1692 fprintf(stderr
, "\n");
1697 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1698 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1699 if (dst
.regClass() == v2b
) {
1700 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f16
, dst
, true);
1701 } else if (dst
.regClass() == v1
) {
1702 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_add_f32
, dst
, true);
1703 } else if (dst
.regClass() == v2
) {
1704 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src0
, src1
);
1706 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1707 nir_print_instr(&instr
->instr
, stderr
);
1708 fprintf(stderr
, "\n");
1713 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1714 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
1715 if (dst
.regClass() == v2b
) {
1716 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1717 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f16
, dst
, false);
1719 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f16
, dst
, true);
1720 } else if (dst
.regClass() == v1
) {
1721 if (src1
.type() == RegType::vgpr
|| src0
.type() != RegType::vgpr
)
1722 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_sub_f32
, dst
, false);
1724 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_subrev_f32
, dst
, true);
1725 } else if (dst
.regClass() == v2
) {
1726 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
),
1727 as_vgpr(ctx
, src0
), as_vgpr(ctx
, src1
));
1728 VOP3A_instruction
* sub
= static_cast<VOP3A_instruction
*>(add
);
1731 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1732 nir_print_instr(&instr
->instr
, stderr
);
1733 fprintf(stderr
, "\n");
1738 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1739 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1740 if (dst
.regClass() == v2b
) {
1741 // TODO: check fp_mode.must_flush_denorms16_64
1742 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f16
, dst
, true);
1743 } else if (dst
.regClass() == v1
) {
1744 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_max_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1745 } else if (dst
.regClass() == v2
) {
1746 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1747 Temp tmp
= bld
.vop3(aco_opcode::v_max_f64
, bld
.def(v2
), src0
, src1
);
1748 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1750 bld
.vop3(aco_opcode::v_max_f64
, Definition(dst
), src0
, src1
);
1753 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1754 nir_print_instr(&instr
->instr
, stderr
);
1755 fprintf(stderr
, "\n");
1760 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
1761 Temp src1
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[1]));
1762 if (dst
.regClass() == v2b
) {
1763 // TODO: check fp_mode.must_flush_denorms16_64
1764 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f16
, dst
, true);
1765 } else if (dst
.regClass() == v1
) {
1766 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_min_f32
, dst
, true, false, ctx
->block
->fp_mode
.must_flush_denorms32
);
1767 } else if (dst
.regClass() == v2
) {
1768 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
&& ctx
->program
->chip_class
< GFX9
) {
1769 Temp tmp
= bld
.vop3(aco_opcode::v_min_f64
, bld
.def(v2
), src0
, src1
);
1770 bld
.vop3(aco_opcode::v_mul_f64
, Definition(dst
), Operand(0x3FF0000000000000lu
), tmp
);
1772 bld
.vop3(aco_opcode::v_min_f64
, Definition(dst
), src0
, src1
);
1775 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1776 nir_print_instr(&instr
->instr
, stderr
);
1777 fprintf(stderr
, "\n");
1781 case nir_op_fmax3
: {
1782 if (dst
.regClass() == v2b
) {
1783 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f16
, dst
, false);
1784 } else if (dst
.regClass() == v1
) {
1785 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1787 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1788 nir_print_instr(&instr
->instr
, stderr
);
1789 fprintf(stderr
, "\n");
1793 case nir_op_fmin3
: {
1794 if (dst
.regClass() == v2b
) {
1795 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f16
, dst
, false);
1796 } else if (dst
.regClass() == v1
) {
1797 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1799 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1800 nir_print_instr(&instr
->instr
, stderr
);
1801 fprintf(stderr
, "\n");
1805 case nir_op_fmed3
: {
1806 if (dst
.regClass() == v2b
) {
1807 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f16
, dst
, false);
1808 } else if (dst
.regClass() == v1
) {
1809 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_f32
, dst
, ctx
->block
->fp_mode
.must_flush_denorms32
);
1811 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1812 nir_print_instr(&instr
->instr
, stderr
);
1813 fprintf(stderr
, "\n");
1817 case nir_op_umax3
: {
1818 if (dst
.size() == 1) {
1819 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_u32
, dst
);
1821 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1822 nir_print_instr(&instr
->instr
, stderr
);
1823 fprintf(stderr
, "\n");
1827 case nir_op_umin3
: {
1828 if (dst
.size() == 1) {
1829 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_u32
, dst
);
1831 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1832 nir_print_instr(&instr
->instr
, stderr
);
1833 fprintf(stderr
, "\n");
1837 case nir_op_umed3
: {
1838 if (dst
.size() == 1) {
1839 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_u32
, dst
);
1841 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1842 nir_print_instr(&instr
->instr
, stderr
);
1843 fprintf(stderr
, "\n");
1847 case nir_op_imax3
: {
1848 if (dst
.size() == 1) {
1849 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_max3_i32
, dst
);
1851 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1852 nir_print_instr(&instr
->instr
, stderr
);
1853 fprintf(stderr
, "\n");
1857 case nir_op_imin3
: {
1858 if (dst
.size() == 1) {
1859 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_min3_i32
, dst
);
1861 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1862 nir_print_instr(&instr
->instr
, stderr
);
1863 fprintf(stderr
, "\n");
1867 case nir_op_imed3
: {
1868 if (dst
.size() == 1) {
1869 emit_vop3a_instruction(ctx
, instr
, aco_opcode::v_med3_i32
, dst
);
1871 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1872 nir_print_instr(&instr
->instr
, stderr
);
1873 fprintf(stderr
, "\n");
1877 case nir_op_cube_face_coord
: {
1878 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1879 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1880 emit_extract_vector(ctx
, in
, 1, v1
),
1881 emit_extract_vector(ctx
, in
, 2, v1
) };
1882 Temp ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1883 ma
= bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), ma
);
1884 Temp sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1885 Temp tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), src
[0], src
[1], src
[2]);
1886 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, ma
, Operand(0x3f000000u
/*0.5*/));
1887 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, ma
, Operand(0x3f000000u
/*0.5*/));
1888 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), sc
, tc
);
1891 case nir_op_cube_face_index
: {
1892 Temp in
= get_alu_src(ctx
, instr
->src
[0], 3);
1893 Temp src
[3] = { emit_extract_vector(ctx
, in
, 0, v1
),
1894 emit_extract_vector(ctx
, in
, 1, v1
),
1895 emit_extract_vector(ctx
, in
, 2, v1
) };
1896 bld
.vop3(aco_opcode::v_cubeid_f32
, Definition(dst
), src
[0], src
[1], src
[2]);
1899 case nir_op_bcsel
: {
1900 emit_bcsel(ctx
, instr
, dst
);
1904 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1905 if (dst
.regClass() == v2b
) {
1906 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f16
, dst
);
1907 } else if (dst
.regClass() == v1
) {
1908 emit_rsq(ctx
, bld
, Definition(dst
), src
);
1909 } else if (dst
.regClass() == v2
) {
1910 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rsq_f64
, dst
);
1912 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1913 nir_print_instr(&instr
->instr
, stderr
);
1914 fprintf(stderr
, "\n");
1919 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1920 if (dst
.regClass() == v2b
) {
1921 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x8000u
), as_vgpr(ctx
, src
));
1922 } else if (dst
.regClass() == v1
) {
1923 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1924 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1925 bld
.vop2(aco_opcode::v_xor_b32
, Definition(dst
), Operand(0x80000000u
), as_vgpr(ctx
, src
));
1926 } else if (dst
.regClass() == v2
) {
1927 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1928 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1929 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1930 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1931 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), Operand(0x80000000u
), upper
);
1932 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1934 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1935 nir_print_instr(&instr
->instr
, stderr
);
1936 fprintf(stderr
, "\n");
1941 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1942 if (dst
.regClass() == v2b
) {
1943 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFu
), as_vgpr(ctx
, src
));
1944 } else if (dst
.regClass() == v1
) {
1945 if (ctx
->block
->fp_mode
.must_flush_denorms32
)
1946 src
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0x3f800000u
), as_vgpr(ctx
, src
));
1947 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), Operand(0x7FFFFFFFu
), as_vgpr(ctx
, src
));
1948 } else if (dst
.regClass() == v2
) {
1949 if (ctx
->block
->fp_mode
.must_flush_denorms16_64
)
1950 src
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), Operand(0x3FF0000000000000lu
), as_vgpr(ctx
, src
));
1951 Temp upper
= bld
.tmp(v1
), lower
= bld
.tmp(v1
);
1952 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
1953 upper
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7FFFFFFFu
), upper
);
1954 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
1956 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1957 nir_print_instr(&instr
->instr
, stderr
);
1958 fprintf(stderr
, "\n");
1963 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1964 if (dst
.regClass() == v2b
) {
1965 bld
.vop3(aco_opcode::v_med3_f16
, Definition(dst
), Operand((uint16_t)0u), Operand((uint16_t)0x3c00), src
);
1966 } else if (dst
.regClass() == v1
) {
1967 bld
.vop3(aco_opcode::v_med3_f32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
1968 /* apparently, it is not necessary to flush denorms if this instruction is used with these operands */
1969 // TODO: confirm that this holds under any circumstances
1970 } else if (dst
.regClass() == v2
) {
1971 Instruction
* add
= bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), src
, Operand(0u));
1972 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(add
);
1975 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1976 nir_print_instr(&instr
->instr
, stderr
);
1977 fprintf(stderr
, "\n");
1981 case nir_op_flog2
: {
1982 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1983 if (dst
.regClass() == v2b
) {
1984 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_log_f16
, dst
);
1985 } else if (dst
.regClass() == v1
) {
1986 emit_log2(ctx
, bld
, Definition(dst
), src
);
1988 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
1989 nir_print_instr(&instr
->instr
, stderr
);
1990 fprintf(stderr
, "\n");
1995 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
1996 if (dst
.regClass() == v2b
) {
1997 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f16
, dst
);
1998 } else if (dst
.regClass() == v1
) {
1999 emit_rcp(ctx
, bld
, Definition(dst
), src
);
2000 } else if (dst
.regClass() == v2
) {
2001 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rcp_f64
, dst
);
2003 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2004 nir_print_instr(&instr
->instr
, stderr
);
2005 fprintf(stderr
, "\n");
2009 case nir_op_fexp2
: {
2010 if (dst
.regClass() == v2b
) {
2011 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f16
, dst
);
2012 } else if (dst
.regClass() == v1
) {
2013 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_exp_f32
, dst
);
2015 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2016 nir_print_instr(&instr
->instr
, stderr
);
2017 fprintf(stderr
, "\n");
2021 case nir_op_fsqrt
: {
2022 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2023 if (dst
.regClass() == v2b
) {
2024 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f16
, dst
);
2025 } else if (dst
.regClass() == v1
) {
2026 emit_sqrt(ctx
, bld
, Definition(dst
), src
);
2027 } else if (dst
.regClass() == v2
) {
2028 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_sqrt_f64
, dst
);
2030 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2031 nir_print_instr(&instr
->instr
, stderr
);
2032 fprintf(stderr
, "\n");
2036 case nir_op_ffract
: {
2037 if (dst
.regClass() == v2b
) {
2038 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f16
, dst
);
2039 } else if (dst
.regClass() == v1
) {
2040 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f32
, dst
);
2041 } else if (dst
.regClass() == v2
) {
2042 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_fract_f64
, dst
);
2044 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2045 nir_print_instr(&instr
->instr
, stderr
);
2046 fprintf(stderr
, "\n");
2050 case nir_op_ffloor
: {
2051 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2052 if (dst
.regClass() == v2b
) {
2053 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f16
, dst
);
2054 } else if (dst
.regClass() == v1
) {
2055 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_floor_f32
, dst
);
2056 } else if (dst
.regClass() == v2
) {
2057 emit_floor_f64(ctx
, bld
, Definition(dst
), src
);
2059 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2060 nir_print_instr(&instr
->instr
, stderr
);
2061 fprintf(stderr
, "\n");
2065 case nir_op_fceil
: {
2066 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2067 if (dst
.regClass() == v2b
) {
2068 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f16
, dst
);
2069 } else if (dst
.regClass() == v1
) {
2070 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f32
, dst
);
2071 } else if (dst
.regClass() == v2
) {
2072 if (ctx
->options
->chip_class
>= GFX7
) {
2073 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_ceil_f64
, dst
);
2075 /* GFX6 doesn't support V_CEIL_F64, lower it. */
2076 /* trunc = trunc(src0)
2077 * if (src0 > 0.0 && src0 != trunc)
2080 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src0
);
2081 Temp tmp0
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.def(bld
.lm
), src0
, Operand(0u));
2082 Temp tmp1
= bld
.vopc(aco_opcode::v_cmp_lg_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, trunc
);
2083 Temp cond
= bld
.sop2(aco_opcode::s_and_b64
, bld
.hint_vcc(bld
.def(s2
)), bld
.def(s1
, scc
), tmp0
, tmp1
);
2084 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
);
2085 add
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), bld
.copy(bld
.def(v1
), Operand(0u)), add
);
2086 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), trunc
, add
);
2089 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2090 nir_print_instr(&instr
->instr
, stderr
);
2091 fprintf(stderr
, "\n");
2095 case nir_op_ftrunc
: {
2096 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2097 if (dst
.regClass() == v2b
) {
2098 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f16
, dst
);
2099 } else if (dst
.regClass() == v1
) {
2100 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_trunc_f32
, dst
);
2101 } else if (dst
.regClass() == v2
) {
2102 emit_trunc_f64(ctx
, bld
, Definition(dst
), src
);
2104 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2105 nir_print_instr(&instr
->instr
, stderr
);
2106 fprintf(stderr
, "\n");
2110 case nir_op_fround_even
: {
2111 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2112 if (dst
.regClass() == v2b
) {
2113 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f16
, dst
);
2114 } else if (dst
.regClass() == v1
) {
2115 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f32
, dst
);
2116 } else if (dst
.regClass() == v2
) {
2117 if (ctx
->options
->chip_class
>= GFX7
) {
2118 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_rndne_f64
, dst
);
2120 /* GFX6 doesn't support V_RNDNE_F64, lower it. */
2121 Temp src0_lo
= bld
.tmp(v1
), src0_hi
= bld
.tmp(v1
);
2122 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0_lo
), Definition(src0_hi
), src0
);
2124 Temp bitmask
= bld
.sop1(aco_opcode::s_brev_b32
, bld
.def(s1
), bld
.copy(bld
.def(s1
), Operand(-2u)));
2125 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
));
2126 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
));
2127 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
));
2128 static_cast<VOP3A_instruction
*>(sub
)->neg
[1] = true;
2129 tmp
= sub
->definitions
[0].getTemp();
2131 Temp v
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(-1u), Operand(0x432fffffu
));
2132 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_gt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), src0
, v
);
2133 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2134 Temp cond
= vop3
->definitions
[0].getTemp();
2136 Temp tmp_lo
= bld
.tmp(v1
), tmp_hi
= bld
.tmp(v1
);
2137 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp_lo
), Definition(tmp_hi
), tmp
);
2138 Temp dst0
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_lo
, as_vgpr(ctx
, src0_lo
), cond
);
2139 Temp dst1
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp_hi
, as_vgpr(ctx
, src0_hi
), cond
);
2141 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), dst0
, dst1
);
2144 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2145 nir_print_instr(&instr
->instr
, stderr
);
2146 fprintf(stderr
, "\n");
2152 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
2153 aco_ptr
<Instruction
> norm
;
2154 if (dst
.regClass() == v2b
) {
2155 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3118u
));
2156 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f16
, bld
.def(v1
), half_pi
, src
);
2157 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f16
: aco_opcode::v_cos_f16
;
2158 bld
.vop1(opcode
, Definition(dst
), tmp
);
2159 } else if (dst
.regClass() == v1
) {
2160 Temp half_pi
= bld
.copy(bld
.def(s1
), Operand(0x3e22f983u
));
2161 Temp tmp
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), half_pi
, src
);
2163 /* before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256] */
2164 if (ctx
->options
->chip_class
< GFX9
)
2165 tmp
= bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), tmp
);
2167 aco_opcode opcode
= instr
->op
== nir_op_fsin
? aco_opcode::v_sin_f32
: aco_opcode::v_cos_f32
;
2168 bld
.vop1(opcode
, Definition(dst
), tmp
);
2170 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2171 nir_print_instr(&instr
->instr
, stderr
);
2172 fprintf(stderr
, "\n");
2176 case nir_op_ldexp
: {
2177 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2178 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2179 if (dst
.regClass() == v2b
) {
2180 emit_vop2_instruction(ctx
, instr
, aco_opcode::v_ldexp_f16
, dst
, false);
2181 } else if (dst
.regClass() == v1
) {
2182 bld
.vop3(aco_opcode::v_ldexp_f32
, Definition(dst
), as_vgpr(ctx
, src0
), src1
);
2183 } else if (dst
.regClass() == v2
) {
2184 bld
.vop3(aco_opcode::v_ldexp_f64
, Definition(dst
), as_vgpr(ctx
, src0
), src1
);
2186 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2187 nir_print_instr(&instr
->instr
, stderr
);
2188 fprintf(stderr
, "\n");
2192 case nir_op_frexp_sig
: {
2193 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2194 if (dst
.regClass() == v2b
) {
2195 bld
.vop1(aco_opcode::v_frexp_mant_f16
, Definition(dst
), src
);
2196 } else if (dst
.regClass() == v1
) {
2197 bld
.vop1(aco_opcode::v_frexp_mant_f32
, Definition(dst
), src
);
2198 } else if (dst
.regClass() == v2
) {
2199 bld
.vop1(aco_opcode::v_frexp_mant_f64
, Definition(dst
), src
);
2201 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2202 nir_print_instr(&instr
->instr
, stderr
);
2203 fprintf(stderr
, "\n");
2207 case nir_op_frexp_exp
: {
2208 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2209 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2210 Temp tmp
= bld
.vop1(aco_opcode::v_frexp_exp_i16_f16
, bld
.def(v1
), src
);
2211 tmp
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(v1b
), tmp
, Operand(0u));
2212 convert_int(ctx
, bld
, tmp
, 8, 32, true, dst
);
2213 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2214 bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, Definition(dst
), src
);
2215 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2216 bld
.vop1(aco_opcode::v_frexp_exp_i32_f64
, Definition(dst
), src
);
2218 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2219 nir_print_instr(&instr
->instr
, stderr
);
2220 fprintf(stderr
, "\n");
2224 case nir_op_fsign
: {
2225 Temp src
= as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]));
2226 if (dst
.regClass() == v2b
) {
2227 Temp one
= bld
.copy(bld
.def(v1
), Operand(0x3c00u
));
2228 Temp minus_one
= bld
.copy(bld
.def(v1
), Operand(0xbc00u
));
2229 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2230 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), one
, src
, cond
);
2231 cond
= bld
.vopc(aco_opcode::v_cmp_le_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2232 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), minus_one
, src
, cond
);
2233 } else if (dst
.regClass() == v1
) {
2234 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2235 src
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0x3f800000u
), src
, cond
);
2236 cond
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2237 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0xbf800000u
), src
, cond
);
2238 } else if (dst
.regClass() == v2
) {
2239 Temp cond
= bld
.vopc(aco_opcode::v_cmp_nlt_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2240 Temp tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0x3FF00000u
));
2241 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, emit_extract_vector(ctx
, src
, 1, v1
), cond
);
2243 cond
= bld
.vopc(aco_opcode::v_cmp_le_f64
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), src
);
2244 tmp
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0xBFF00000u
));
2245 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), tmp
, upper
, cond
);
2247 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2249 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2250 nir_print_instr(&instr
->instr
, stderr
);
2251 fprintf(stderr
, "\n");
2256 case nir_op_f2f16_rtne
: {
2257 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2258 if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2259 src
= bld
.vop1(aco_opcode::v_cvt_f32_f64
, bld
.def(v1
), src
);
2260 bld
.vop1(aco_opcode::v_cvt_f16_f32
, Definition(dst
), src
);
2263 case nir_op_f2f16_rtz
: {
2264 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2265 if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2266 src
= bld
.vop1(aco_opcode::v_cvt_f32_f64
, bld
.def(v1
), src
);
2267 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src
, Operand(0u));
2270 case nir_op_f2f32
: {
2271 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2272 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f16
, dst
);
2273 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2274 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_f32_f64
, dst
);
2276 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2277 nir_print_instr(&instr
->instr
, stderr
);
2278 fprintf(stderr
, "\n");
2282 case nir_op_f2f64
: {
2283 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2284 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2285 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2286 bld
.vop1(aco_opcode::v_cvt_f64_f32
, Definition(dst
), src
);
2289 case nir_op_i2f16
: {
2290 assert(dst
.regClass() == v2b
);
2291 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2292 if (instr
->src
[0].src
.ssa
->bit_size
== 8)
2293 src
= convert_int(ctx
, bld
, src
, 8, 16, true);
2294 else if (instr
->src
[0].src
.ssa
->bit_size
== 64)
2295 src
= convert_int(ctx
, bld
, src
, 64, 32, false);
2296 bld
.vop1(aco_opcode::v_cvt_f16_i16
, Definition(dst
), src
);
2299 case nir_op_i2f32
: {
2300 assert(dst
.size() == 1);
2301 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2302 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2303 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2304 bld
.vop1(aco_opcode::v_cvt_f32_i32
, Definition(dst
), src
);
2307 case nir_op_i2f64
: {
2308 if (instr
->src
[0].src
.ssa
->bit_size
<= 32) {
2309 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2310 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2311 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2312 bld
.vop1(aco_opcode::v_cvt_f64_i32
, Definition(dst
), src
);
2313 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2314 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2315 RegClass rc
= RegClass(src
.type(), 1);
2316 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
2317 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
2318 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
2319 upper
= bld
.vop1(aco_opcode::v_cvt_f64_i32
, bld
.def(v2
), upper
);
2320 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
2321 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
2324 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2325 nir_print_instr(&instr
->instr
, stderr
);
2326 fprintf(stderr
, "\n");
2330 case nir_op_u2f16
: {
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, false);
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_u16
, Definition(dst
), src
);
2340 case nir_op_u2f32
: {
2341 assert(dst
.size() == 1);
2342 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2343 if (instr
->src
[0].src
.ssa
->bit_size
== 8) {
2344 bld
.vop1(aco_opcode::v_cvt_f32_ubyte0
, Definition(dst
), src
);
2346 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2347 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, true);
2348 bld
.vop1(aco_opcode::v_cvt_f32_u32
, Definition(dst
), src
);
2352 case nir_op_u2f64
: {
2353 if (instr
->src
[0].src
.ssa
->bit_size
<= 32) {
2354 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2355 if (instr
->src
[0].src
.ssa
->bit_size
<= 16)
2356 src
= convert_int(ctx
, bld
, src
, instr
->src
[0].src
.ssa
->bit_size
, 32, false);
2357 bld
.vop1(aco_opcode::v_cvt_f64_u32
, Definition(dst
), src
);
2358 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2359 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2360 RegClass rc
= RegClass(src
.type(), 1);
2361 Temp lower
= bld
.tmp(rc
), upper
= bld
.tmp(rc
);
2362 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), src
);
2363 lower
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), lower
);
2364 upper
= bld
.vop1(aco_opcode::v_cvt_f64_u32
, bld
.def(v2
), upper
);
2365 upper
= bld
.vop3(aco_opcode::v_ldexp_f64
, bld
.def(v2
), upper
, Operand(32u));
2366 bld
.vop3(aco_opcode::v_add_f64
, Definition(dst
), lower
, upper
);
2368 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2369 nir_print_instr(&instr
->instr
, stderr
);
2370 fprintf(stderr
, "\n");
2375 case nir_op_f2i16
: {
2376 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2377 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i16_f16
, dst
);
2378 else if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2379 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f32
, dst
);
2381 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f64
, dst
);
2385 case nir_op_f2u16
: {
2386 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2387 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u16_f16
, dst
);
2388 else if (instr
->src
[0].src
.ssa
->bit_size
== 32)
2389 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f32
, dst
);
2391 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f64
, dst
);
2394 case nir_op_f2i32
: {
2395 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2396 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2397 Temp tmp
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2398 if (dst
.type() == RegType::vgpr
) {
2399 bld
.vop1(aco_opcode::v_cvt_i32_f32
, Definition(dst
), tmp
);
2401 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2402 bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), tmp
));
2404 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2405 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f32
, dst
);
2406 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2407 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_i32_f64
, dst
);
2409 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2410 nir_print_instr(&instr
->instr
, stderr
);
2411 fprintf(stderr
, "\n");
2415 case nir_op_f2u32
: {
2416 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2417 if (instr
->src
[0].src
.ssa
->bit_size
== 16) {
2418 Temp tmp
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2419 if (dst
.type() == RegType::vgpr
) {
2420 bld
.vop1(aco_opcode::v_cvt_u32_f32
, Definition(dst
), tmp
);
2422 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
),
2423 bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), tmp
));
2425 } else if (instr
->src
[0].src
.ssa
->bit_size
== 32) {
2426 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f32
, dst
);
2427 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2428 emit_vop1_instruction(ctx
, instr
, aco_opcode::v_cvt_u32_f64
, dst
);
2430 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2431 nir_print_instr(&instr
->instr
, stderr
);
2432 fprintf(stderr
, "\n");
2436 case nir_op_f2i64
: {
2437 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2438 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2439 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2441 if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::vgpr
) {
2442 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2443 exponent
= bld
.vop3(aco_opcode::v_med3_i32
, bld
.def(v1
), Operand(0x0u
), exponent
, Operand(64u));
2444 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2445 Temp sign
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(31u), src
);
2446 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2447 mantissa
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(7u), mantissa
);
2448 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2449 Temp new_exponent
= bld
.tmp(v1
);
2450 Temp borrow
= bld
.vsub32(Definition(new_exponent
), Operand(63u), exponent
, true).def(1).getTemp();
2451 if (ctx
->program
->chip_class
>= GFX8
)
2452 mantissa
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2454 mantissa
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2455 Temp saturate
= bld
.vop1(aco_opcode::v_bfrev_b32
, bld
.def(v1
), Operand(0xfffffffeu
));
2456 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2457 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2458 lower
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, Operand(0xffffffffu
), borrow
);
2459 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, saturate
, borrow
);
2460 lower
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, lower
);
2461 upper
= bld
.vop2(aco_opcode::v_xor_b32
, bld
.def(v1
), sign
, upper
);
2462 Temp new_lower
= bld
.tmp(v1
);
2463 borrow
= bld
.vsub32(Definition(new_lower
), lower
, sign
, true).def(1).getTemp();
2464 Temp new_upper
= bld
.vsub32(bld
.def(v1
), upper
, sign
, false, borrow
);
2465 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), new_lower
, new_upper
);
2467 } else if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::sgpr
) {
2468 if (src
.type() == RegType::vgpr
)
2469 src
= bld
.as_uniform(src
);
2470 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2471 exponent
= bld
.sop2(aco_opcode::s_sub_i32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2472 exponent
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2473 exponent
= bld
.sop2(aco_opcode::s_min_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(64u), exponent
);
2474 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2475 Temp sign
= bld
.sop2(aco_opcode::s_ashr_i32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(31u));
2476 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2477 mantissa
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, Operand(7u));
2478 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2479 exponent
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(63u), exponent
);
2480 mantissa
= bld
.sop2(aco_opcode::s_lshr_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent
);
2481 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), exponent
, Operand(0xffffffffu
)); // exp >= 64
2482 Temp saturate
= bld
.sop1(aco_opcode::s_brev_b64
, bld
.def(s2
), Operand(0xfffffffeu
));
2483 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), saturate
, mantissa
, cond
);
2484 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2485 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2486 lower
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, lower
);
2487 upper
= bld
.sop2(aco_opcode::s_xor_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sign
, upper
);
2488 Temp borrow
= bld
.tmp(s1
);
2489 lower
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.scc(Definition(borrow
)), lower
, sign
);
2490 upper
= bld
.sop2(aco_opcode::s_subb_u32
, bld
.def(s1
), bld
.def(s1
, scc
), upper
, sign
, borrow
);
2491 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2493 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2494 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2495 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2496 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2497 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2498 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2499 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2500 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2501 Temp upper
= bld
.vop1(aco_opcode::v_cvt_i32_f64
, bld
.def(v1
), floor
);
2502 if (dst
.type() == RegType::sgpr
) {
2503 lower
= bld
.as_uniform(lower
);
2504 upper
= bld
.as_uniform(upper
);
2506 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2509 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2510 nir_print_instr(&instr
->instr
, stderr
);
2511 fprintf(stderr
, "\n");
2515 case nir_op_f2u64
: {
2516 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2517 if (instr
->src
[0].src
.ssa
->bit_size
== 16)
2518 src
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src
);
2520 if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::vgpr
) {
2521 Temp exponent
= bld
.vop1(aco_opcode::v_frexp_exp_i32_f32
, bld
.def(v1
), src
);
2522 Temp exponent_in_range
= bld
.vopc(aco_opcode::v_cmp_ge_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(64u), exponent
);
2523 exponent
= bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
), Operand(0x0u
), exponent
);
2524 Temp mantissa
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffu
), src
);
2525 mantissa
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(0x800000u
), mantissa
);
2526 Temp exponent_small
= bld
.vsub32(bld
.def(v1
), Operand(24u), exponent
);
2527 Temp small
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), exponent_small
, mantissa
);
2528 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), Operand(0u), mantissa
);
2529 Temp new_exponent
= bld
.tmp(v1
);
2530 Temp cond_small
= bld
.vsub32(Definition(new_exponent
), exponent
, Operand(24u), true).def(1).getTemp();
2531 if (ctx
->program
->chip_class
>= GFX8
)
2532 mantissa
= bld
.vop3(aco_opcode::v_lshlrev_b64
, bld
.def(v2
), new_exponent
, mantissa
);
2534 mantissa
= bld
.vop3(aco_opcode::v_lshl_b64
, bld
.def(v2
), mantissa
, new_exponent
);
2535 Temp lower
= bld
.tmp(v1
), upper
= bld
.tmp(v1
);
2536 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2537 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), lower
, small
, cond_small
);
2538 upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), upper
, Operand(0u), cond_small
);
2539 lower
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), lower
, exponent_in_range
);
2540 upper
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xffffffffu
), upper
, exponent_in_range
);
2541 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2543 } else if (instr
->src
[0].src
.ssa
->bit_size
<= 32 && dst
.type() == RegType::sgpr
) {
2544 if (src
.type() == RegType::vgpr
)
2545 src
= bld
.as_uniform(src
);
2546 Temp exponent
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), src
, Operand(0x80017u
));
2547 exponent
= bld
.sop2(aco_opcode::s_sub_i32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(126u));
2548 exponent
= bld
.sop2(aco_opcode::s_max_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0u), exponent
);
2549 Temp mantissa
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x7fffffu
), src
);
2550 mantissa
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0x800000u
), mantissa
);
2551 Temp exponent_small
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(24u), exponent
);
2552 Temp small
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), mantissa
, exponent_small
);
2553 mantissa
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), mantissa
);
2554 Temp exponent_large
= bld
.sop2(aco_opcode::s_sub_u32
, bld
.def(s1
), bld
.def(s1
, scc
), exponent
, Operand(24u));
2555 mantissa
= bld
.sop2(aco_opcode::s_lshl_b64
, bld
.def(s2
), bld
.def(s1
, scc
), mantissa
, exponent_large
);
2556 Temp cond
= bld
.sopc(aco_opcode::s_cmp_ge_i32
, bld
.def(s1
, scc
), Operand(64u), exponent
);
2557 mantissa
= bld
.sop2(aco_opcode::s_cselect_b64
, bld
.def(s2
), mantissa
, Operand(0xffffffffu
), cond
);
2558 Temp lower
= bld
.tmp(s1
), upper
= bld
.tmp(s1
);
2559 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lower
), Definition(upper
), mantissa
);
2560 Temp cond_small
= bld
.sopc(aco_opcode::s_cmp_le_i32
, bld
.def(s1
, scc
), exponent
, Operand(24u));
2561 lower
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), small
, lower
, cond_small
);
2562 upper
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), Operand(0u), upper
, cond_small
);
2563 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2565 } else if (instr
->src
[0].src
.ssa
->bit_size
== 64) {
2566 Temp vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0x3df00000u
));
2567 Temp trunc
= emit_trunc_f64(ctx
, bld
, bld
.def(v2
), src
);
2568 Temp mul
= bld
.vop3(aco_opcode::v_mul_f64
, bld
.def(v2
), trunc
, vec
);
2569 vec
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(0u), Operand(0xc1f00000u
));
2570 Temp floor
= emit_floor_f64(ctx
, bld
, bld
.def(v2
), mul
);
2571 Temp fma
= bld
.vop3(aco_opcode::v_fma_f64
, bld
.def(v2
), floor
, vec
, trunc
);
2572 Temp lower
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), fma
);
2573 Temp upper
= bld
.vop1(aco_opcode::v_cvt_u32_f64
, bld
.def(v1
), floor
);
2574 if (dst
.type() == RegType::sgpr
) {
2575 lower
= bld
.as_uniform(lower
);
2576 upper
= bld
.as_uniform(upper
);
2578 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lower
, upper
);
2581 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2582 nir_print_instr(&instr
->instr
, stderr
);
2583 fprintf(stderr
, "\n");
2587 case nir_op_b2f16
: {
2588 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2589 assert(src
.regClass() == bld
.lm
);
2591 if (dst
.regClass() == s1
) {
2592 src
= bool_to_scalar_condition(ctx
, src
);
2593 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3c00u
), src
);
2594 } else if (dst
.regClass() == v2b
) {
2595 Temp one
= bld
.copy(bld
.def(v1
), Operand(0x3c00u
));
2596 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), one
, src
);
2598 unreachable("Wrong destination register class for nir_op_b2f16.");
2602 case nir_op_b2f32
: {
2603 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2604 assert(src
.regClass() == bld
.lm
);
2606 if (dst
.regClass() == s1
) {
2607 src
= bool_to_scalar_condition(ctx
, src
);
2608 bld
.sop2(aco_opcode::s_mul_i32
, Definition(dst
), Operand(0x3f800000u
), src
);
2609 } else if (dst
.regClass() == v1
) {
2610 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(0x3f800000u
), src
);
2612 unreachable("Wrong destination register class for nir_op_b2f32.");
2616 case nir_op_b2f64
: {
2617 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2618 assert(src
.regClass() == bld
.lm
);
2620 if (dst
.regClass() == s2
) {
2621 src
= bool_to_scalar_condition(ctx
, src
);
2622 bld
.sop2(aco_opcode::s_cselect_b64
, Definition(dst
), Operand(0x3f800000u
), Operand(0u), bld
.scc(src
));
2623 } else if (dst
.regClass() == v2
) {
2624 Temp one
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v2
), Operand(0x3FF00000u
));
2625 Temp upper
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), one
, src
);
2626 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), Operand(0u), upper
);
2628 unreachable("Wrong destination register class for nir_op_b2f64.");
2635 case nir_op_i2i64
: {
2636 convert_int(ctx
, bld
, get_alu_src(ctx
, instr
->src
[0]),
2637 instr
->src
[0].src
.ssa
->bit_size
, instr
->dest
.dest
.ssa
.bit_size
, true, dst
);
2643 case nir_op_u2u64
: {
2644 convert_int(ctx
, bld
, get_alu_src(ctx
, instr
->src
[0]),
2645 instr
->src
[0].src
.ssa
->bit_size
, instr
->dest
.dest
.ssa
.bit_size
, false, dst
);
2649 case nir_op_b2i32
: {
2650 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2651 assert(src
.regClass() == bld
.lm
);
2653 if (dst
.regClass() == s1
) {
2654 // TODO: in a post-RA optimization, we can check if src is in VCC, and directly use VCCNZ
2655 bool_to_scalar_condition(ctx
, src
, dst
);
2656 } else if (dst
.regClass() == v1
) {
2657 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand(1u), src
);
2659 unreachable("Invalid register class for b2i32");
2665 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2666 assert(dst
.regClass() == bld
.lm
);
2668 if (src
.type() == RegType::vgpr
) {
2669 assert(src
.regClass() == v1
|| src
.regClass() == v2
);
2670 assert(dst
.regClass() == bld
.lm
);
2671 bld
.vopc(src
.size() == 2 ? aco_opcode::v_cmp_lg_u64
: aco_opcode::v_cmp_lg_u32
,
2672 Definition(dst
), Operand(0u), src
).def(0).setHint(vcc
);
2674 assert(src
.regClass() == s1
|| src
.regClass() == s2
);
2676 if (src
.regClass() == s2
&& ctx
->program
->chip_class
<= GFX7
) {
2677 tmp
= bld
.sop2(aco_opcode::s_or_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(0u), src
).def(1).getTemp();
2679 tmp
= bld
.sopc(src
.size() == 2 ? aco_opcode::s_cmp_lg_u64
: aco_opcode::s_cmp_lg_u32
,
2680 bld
.scc(bld
.def(s1
)), Operand(0u), src
);
2682 bool_to_vector_condition(ctx
, tmp
, dst
);
2686 case nir_op_pack_64_2x32_split
: {
2687 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2688 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2690 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2693 case nir_op_unpack_64_2x32_split_x
:
2694 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2696 case nir_op_unpack_64_2x32_split_y
:
2697 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2699 case nir_op_unpack_32_2x16_split_x
:
2700 if (dst
.type() == RegType::vgpr
) {
2701 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(dst
.regClass()), get_alu_src(ctx
, instr
->src
[0]));
2703 bld
.copy(Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2706 case nir_op_unpack_32_2x16_split_y
:
2707 if (dst
.type() == RegType::vgpr
) {
2708 bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(dst
.regClass()), Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2710 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)));
2713 case nir_op_pack_32_2x16_split
: {
2714 Temp src0
= get_alu_src(ctx
, instr
->src
[0]);
2715 Temp src1
= get_alu_src(ctx
, instr
->src
[1]);
2716 if (dst
.regClass() == v1
) {
2717 src0
= emit_extract_vector(ctx
, src0
, 0, v2b
);
2718 src1
= emit_extract_vector(ctx
, src1
, 0, v2b
);
2719 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src0
, src1
);
2721 src0
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), src0
, Operand(0xFFFFu
));
2722 src1
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), src1
, Operand(16u));
2723 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), src0
, src1
);
2727 case nir_op_pack_half_2x16
: {
2728 Temp src
= get_alu_src(ctx
, instr
->src
[0], 2);
2730 if (dst
.regClass() == v1
) {
2731 Temp src0
= bld
.tmp(v1
);
2732 Temp src1
= bld
.tmp(v1
);
2733 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src0
), Definition(src1
), src
);
2734 if (!ctx
->block
->fp_mode
.care_about_round32
|| ctx
->block
->fp_mode
.round32
== fp_round_tz
)
2735 bld
.vop3(aco_opcode::v_cvt_pkrtz_f16_f32
, Definition(dst
), src0
, src1
);
2737 bld
.vop3(aco_opcode::v_cvt_pk_u16_u32
, Definition(dst
),
2738 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src0
),
2739 bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), src1
));
2741 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2742 nir_print_instr(&instr
->instr
, stderr
);
2743 fprintf(stderr
, "\n");
2747 case nir_op_unpack_half_2x16_split_x
: {
2748 if (dst
.regClass() == v1
) {
2749 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
), get_alu_src(ctx
, instr
->src
[0]));
2751 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2752 nir_print_instr(&instr
->instr
, stderr
);
2753 fprintf(stderr
, "\n");
2757 case nir_op_unpack_half_2x16_split_y
: {
2758 if (dst
.regClass() == v1
) {
2759 /* TODO: use SDWA here */
2760 bld
.vop1(aco_opcode::v_cvt_f32_f16
, Definition(dst
),
2761 bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), as_vgpr(ctx
, get_alu_src(ctx
, instr
->src
[0]))));
2763 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2764 nir_print_instr(&instr
->instr
, stderr
);
2765 fprintf(stderr
, "\n");
2769 case nir_op_fquantize2f16
: {
2770 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2771 Temp f16
= bld
.vop1(aco_opcode::v_cvt_f16_f32
, bld
.def(v1
), src
);
2774 if (ctx
->program
->chip_class
>= GFX8
) {
2775 Temp mask
= bld
.copy(bld
.def(s1
), Operand(0x36Fu
)); /* value is NOT negative/positive denormal value */
2776 cmp_res
= bld
.vopc_e64(aco_opcode::v_cmp_class_f16
, bld
.hint_vcc(bld
.def(bld
.lm
)), f16
, mask
);
2777 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2779 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
2780 * so compare the result and flush to 0 if it's smaller.
2782 f32
= bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), f16
);
2783 Temp smallest
= bld
.copy(bld
.def(s1
), Operand(0x38800000u
));
2784 Instruction
* vop3
= bld
.vopc_e64(aco_opcode::v_cmp_nlt_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), f32
, smallest
);
2785 static_cast<VOP3A_instruction
*>(vop3
)->abs
[0] = true;
2786 cmp_res
= vop3
->definitions
[0].getTemp();
2789 if (ctx
->block
->fp_mode
.preserve_signed_zero_inf_nan32
|| ctx
->program
->chip_class
< GFX8
) {
2790 Temp copysign_0
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0u), as_vgpr(ctx
, src
));
2791 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), copysign_0
, f32
, cmp_res
);
2793 bld
.vop2(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), f32
, cmp_res
);
2798 Temp bits
= get_alu_src(ctx
, instr
->src
[0]);
2799 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2801 if (dst
.regClass() == s1
) {
2802 bld
.sop2(aco_opcode::s_bfm_b32
, Definition(dst
), bits
, offset
);
2803 } else if (dst
.regClass() == v1
) {
2804 bld
.vop3(aco_opcode::v_bfm_b32
, Definition(dst
), bits
, offset
);
2806 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2807 nir_print_instr(&instr
->instr
, stderr
);
2808 fprintf(stderr
, "\n");
2812 case nir_op_bitfield_select
: {
2813 /* (mask & insert) | (~mask & base) */
2814 Temp bitmask
= get_alu_src(ctx
, instr
->src
[0]);
2815 Temp insert
= get_alu_src(ctx
, instr
->src
[1]);
2816 Temp base
= get_alu_src(ctx
, instr
->src
[2]);
2818 /* dst = (insert & bitmask) | (base & ~bitmask) */
2819 if (dst
.regClass() == s1
) {
2820 aco_ptr
<Instruction
> sop2
;
2821 nir_const_value
* const_bitmask
= nir_src_as_const_value(instr
->src
[0].src
);
2822 nir_const_value
* const_insert
= nir_src_as_const_value(instr
->src
[1].src
);
2824 if (const_insert
&& const_bitmask
) {
2825 lhs
= Operand(const_insert
->u32
& const_bitmask
->u32
);
2827 insert
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), insert
, bitmask
);
2828 lhs
= Operand(insert
);
2832 nir_const_value
* const_base
= nir_src_as_const_value(instr
->src
[2].src
);
2833 if (const_base
&& const_bitmask
) {
2834 rhs
= Operand(const_base
->u32
& ~const_bitmask
->u32
);
2836 base
= bld
.sop2(aco_opcode::s_andn2_b32
, bld
.def(s1
), bld
.def(s1
, scc
), base
, bitmask
);
2837 rhs
= Operand(base
);
2840 bld
.sop2(aco_opcode::s_or_b32
, Definition(dst
), bld
.def(s1
, scc
), rhs
, lhs
);
2842 } else if (dst
.regClass() == v1
) {
2843 if (base
.type() == RegType::sgpr
&& (bitmask
.type() == RegType::sgpr
|| (insert
.type() == RegType::sgpr
)))
2844 base
= as_vgpr(ctx
, base
);
2845 if (insert
.type() == RegType::sgpr
&& bitmask
.type() == RegType::sgpr
)
2846 insert
= as_vgpr(ctx
, insert
);
2848 bld
.vop3(aco_opcode::v_bfi_b32
, Definition(dst
), bitmask
, insert
, base
);
2851 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2852 nir_print_instr(&instr
->instr
, stderr
);
2853 fprintf(stderr
, "\n");
2859 Temp base
= get_alu_src(ctx
, instr
->src
[0]);
2860 Temp offset
= get_alu_src(ctx
, instr
->src
[1]);
2861 Temp bits
= get_alu_src(ctx
, instr
->src
[2]);
2863 if (dst
.type() == RegType::sgpr
) {
2865 nir_const_value
* const_offset
= nir_src_as_const_value(instr
->src
[1].src
);
2866 nir_const_value
* const_bits
= nir_src_as_const_value(instr
->src
[2].src
);
2867 if (const_offset
&& const_bits
) {
2868 uint32_t const_extract
= (const_bits
->u32
<< 16) | const_offset
->u32
;
2869 extract
= Operand(const_extract
);
2873 width
= Operand(const_bits
->u32
<< 16);
2875 width
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), bits
, Operand(16u));
2877 extract
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, width
);
2881 if (dst
.regClass() == s1
) {
2882 if (instr
->op
== nir_op_ubfe
)
2883 opcode
= aco_opcode::s_bfe_u32
;
2885 opcode
= aco_opcode::s_bfe_i32
;
2886 } else if (dst
.regClass() == s2
) {
2887 if (instr
->op
== nir_op_ubfe
)
2888 opcode
= aco_opcode::s_bfe_u64
;
2890 opcode
= aco_opcode::s_bfe_i64
;
2892 unreachable("Unsupported BFE bit size");
2895 bld
.sop2(opcode
, Definition(dst
), bld
.def(s1
, scc
), base
, extract
);
2899 if (dst
.regClass() == v1
) {
2900 if (instr
->op
== nir_op_ubfe
)
2901 opcode
= aco_opcode::v_bfe_u32
;
2903 opcode
= aco_opcode::v_bfe_i32
;
2905 unreachable("Unsupported BFE bit size");
2908 emit_vop3a_instruction(ctx
, instr
, opcode
, dst
);
2912 case nir_op_bit_count
: {
2913 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2914 if (src
.regClass() == s1
) {
2915 bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, Definition(dst
), bld
.def(s1
, scc
), src
);
2916 } else if (src
.regClass() == v1
) {
2917 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
), src
, Operand(0u));
2918 } else if (src
.regClass() == v2
) {
2919 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, Definition(dst
),
2920 emit_extract_vector(ctx
, src
, 1, v1
),
2921 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
),
2922 emit_extract_vector(ctx
, src
, 0, v1
), Operand(0u)));
2923 } else if (src
.regClass() == s2
) {
2924 bld
.sop1(aco_opcode::s_bcnt1_i32_b64
, Definition(dst
), bld
.def(s1
, scc
), src
);
2926 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
2927 nir_print_instr(&instr
->instr
, stderr
);
2928 fprintf(stderr
, "\n");
2933 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_lt_f16
, aco_opcode::v_cmp_lt_f32
, aco_opcode::v_cmp_lt_f64
);
2937 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_ge_f16
, aco_opcode::v_cmp_ge_f32
, aco_opcode::v_cmp_ge_f64
);
2941 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_eq_f16
, aco_opcode::v_cmp_eq_f32
, aco_opcode::v_cmp_eq_f64
);
2945 emit_comparison(ctx
, instr
, dst
, aco_opcode::v_cmp_neq_f16
, aco_opcode::v_cmp_neq_f32
, aco_opcode::v_cmp_neq_f64
);
2949 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
);
2953 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
);
2957 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2958 emit_boolean_logic(ctx
, instr
, Builder::s_xnor
, dst
);
2960 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
,
2961 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_eq_u64
: aco_opcode::num_opcodes
);
2965 if (instr
->src
[0].src
.ssa
->bit_size
== 1)
2966 emit_boolean_logic(ctx
, instr
, Builder::s_xor
, dst
);
2968 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
,
2969 ctx
->program
->chip_class
>= GFX8
? aco_opcode::s_cmp_lg_u64
: aco_opcode::num_opcodes
);
2973 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
);
2977 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
);
2982 case nir_op_fddx_fine
:
2983 case nir_op_fddy_fine
:
2984 case nir_op_fddx_coarse
:
2985 case nir_op_fddy_coarse
: {
2986 Temp src
= get_alu_src(ctx
, instr
->src
[0]);
2987 uint16_t dpp_ctrl1
, dpp_ctrl2
;
2988 if (instr
->op
== nir_op_fddx_fine
) {
2989 dpp_ctrl1
= dpp_quad_perm(0, 0, 2, 2);
2990 dpp_ctrl2
= dpp_quad_perm(1, 1, 3, 3);
2991 } else if (instr
->op
== nir_op_fddy_fine
) {
2992 dpp_ctrl1
= dpp_quad_perm(0, 1, 0, 1);
2993 dpp_ctrl2
= dpp_quad_perm(2, 3, 2, 3);
2995 dpp_ctrl1
= dpp_quad_perm(0, 0, 0, 0);
2996 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddx_coarse
)
2997 dpp_ctrl2
= dpp_quad_perm(1, 1, 1, 1);
2999 dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
3003 if (ctx
->program
->chip_class
>= GFX8
) {
3004 Temp tl
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl1
);
3005 tmp
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), src
, tl
, dpp_ctrl2
);
3007 Temp tl
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl1
);
3008 Temp tr
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl2
);
3009 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), tr
, tl
);
3011 emit_wqm(ctx
, tmp
, dst
, true);
3015 fprintf(stderr
, "Unknown NIR ALU instr: ");
3016 nir_print_instr(&instr
->instr
, stderr
);
3017 fprintf(stderr
, "\n");
3021 void visit_load_const(isel_context
*ctx
, nir_load_const_instr
*instr
)
3023 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
3025 // TODO: we really want to have the resulting type as this would allow for 64bit literals
3026 // which get truncated the lsb if double and msb if int
3027 // for now, we only use s_mov_b64 with 64bit inline constants
3028 assert(instr
->def
.num_components
== 1 && "Vector load_const should be lowered to scalar.");
3029 assert(dst
.type() == RegType::sgpr
);
3031 Builder
bld(ctx
->program
, ctx
->block
);
3033 if (instr
->def
.bit_size
== 1) {
3034 assert(dst
.regClass() == bld
.lm
);
3035 int val
= instr
->value
[0].b
? -1 : 0;
3036 Operand op
= bld
.lm
.size() == 1 ? Operand((uint32_t) val
) : Operand((uint64_t) val
);
3037 bld
.sop1(Builder::s_mov
, Definition(dst
), op
);
3038 } else if (instr
->def
.bit_size
== 8) {
3039 /* ensure that the value is correctly represented in the low byte of the register */
3040 bld
.sopk(aco_opcode::s_movk_i32
, Definition(dst
), instr
->value
[0].u8
);
3041 } else if (instr
->def
.bit_size
== 16) {
3042 /* ensure that the value is correctly represented in the low half of the register */
3043 bld
.sopk(aco_opcode::s_movk_i32
, Definition(dst
), instr
->value
[0].u16
);
3044 } else if (dst
.size() == 1) {
3045 bld
.copy(Definition(dst
), Operand(instr
->value
[0].u32
));
3047 assert(dst
.size() != 1);
3048 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
3049 if (instr
->def
.bit_size
== 64)
3050 for (unsigned i
= 0; i
< dst
.size(); i
++)
3051 vec
->operands
[i
] = Operand
{(uint32_t)(instr
->value
[0].u64
>> i
* 32)};
3053 for (unsigned i
= 0; i
< dst
.size(); i
++)
3054 vec
->operands
[i
] = Operand
{instr
->value
[i
].u32
};
3056 vec
->definitions
[0] = Definition(dst
);
3057 ctx
->block
->instructions
.emplace_back(std::move(vec
));
3061 uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
3063 uint32_t new_mask
= 0;
3064 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
3065 if (mask
& (1u << i
))
3066 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
3070 struct LoadEmitInfo
{
3073 unsigned num_components
;
3074 unsigned component_size
;
3075 Temp resource
= Temp(0, s1
);
3076 unsigned component_stride
= 0;
3077 unsigned const_offset
= 0;
3078 unsigned align_mul
= 0;
3079 unsigned align_offset
= 0;
3082 unsigned swizzle_component_size
= 0;
3083 barrier_interaction barrier
= barrier_none
;
3084 bool can_reorder
= true;
3085 Temp soffset
= Temp(0, s1
);
3088 using LoadCallback
= Temp(*)(
3089 Builder
& bld
, const LoadEmitInfo
* info
, Temp offset
, unsigned bytes_needed
,
3090 unsigned align
, unsigned const_offset
, Temp dst_hint
);
3092 template <LoadCallback callback
, bool byte_align_loads
, bool supports_8bit_16bit_loads
, unsigned max_const_offset_plus_one
>
3093 void emit_load(isel_context
*ctx
, Builder
& bld
, const LoadEmitInfo
*info
)
3095 unsigned load_size
= info
->num_components
* info
->component_size
;
3096 unsigned component_size
= info
->component_size
;
3098 unsigned num_vals
= 0;
3099 Temp vals
[info
->dst
.bytes()];
3101 unsigned const_offset
= info
->const_offset
;
3103 unsigned align_mul
= info
->align_mul
? info
->align_mul
: component_size
;
3104 unsigned align_offset
= (info
->align_offset
+ const_offset
) % align_mul
;
3106 unsigned bytes_read
= 0;
3107 while (bytes_read
< load_size
) {
3108 unsigned bytes_needed
= load_size
- bytes_read
;
3110 /* add buffer for unaligned loads */
3111 int byte_align
= align_mul
% 4 == 0 ? align_offset
% 4 : -1;
3114 if ((bytes_needed
> 2 ||
3115 (bytes_needed
== 2 && (align_mul
% 2 || align_offset
% 2)) ||
3116 !supports_8bit_16bit_loads
) && byte_align_loads
) {
3117 if (info
->component_stride
) {
3118 assert(supports_8bit_16bit_loads
&& "unimplemented");
3122 bytes_needed
+= byte_align
== -1 ? 4 - info
->align_mul
: byte_align
;
3123 bytes_needed
= align(bytes_needed
, 4);
3130 if (info
->swizzle_component_size
)
3131 bytes_needed
= MIN2(bytes_needed
, info
->swizzle_component_size
);
3132 if (info
->component_stride
)
3133 bytes_needed
= MIN2(bytes_needed
, info
->component_size
);
3135 bool need_to_align_offset
= byte_align
&& (align_mul
% 4 || align_offset
% 4);
3137 /* reduce constant offset */
3138 Operand offset
= info
->offset
;
3139 unsigned reduced_const_offset
= const_offset
;
3140 bool remove_const_offset_completely
= need_to_align_offset
;
3141 if (const_offset
&& (remove_const_offset_completely
|| const_offset
>= max_const_offset_plus_one
)) {
3142 unsigned to_add
= const_offset
;
3143 if (remove_const_offset_completely
) {
3144 reduced_const_offset
= 0;
3146 to_add
= const_offset
/ max_const_offset_plus_one
* max_const_offset_plus_one
;
3147 reduced_const_offset
%= max_const_offset_plus_one
;
3149 Temp offset_tmp
= offset
.isTemp() ? offset
.getTemp() : Temp();
3150 if (offset
.isConstant()) {
3151 offset
= Operand(offset
.constantValue() + to_add
);
3152 } else if (offset_tmp
.regClass() == s1
) {
3153 offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
3154 offset_tmp
, Operand(to_add
));
3155 } else if (offset_tmp
.regClass() == v1
) {
3156 offset
= bld
.vadd32(bld
.def(v1
), offset_tmp
, Operand(to_add
));
3158 Temp lo
= bld
.tmp(offset_tmp
.type(), 1);
3159 Temp hi
= bld
.tmp(offset_tmp
.type(), 1);
3160 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), offset_tmp
);
3162 if (offset_tmp
.regClass() == s2
) {
3163 Temp carry
= bld
.tmp(s1
);
3164 lo
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), lo
, Operand(to_add
));
3165 hi
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), hi
, carry
);
3166 offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), lo
, hi
);
3168 Temp new_lo
= bld
.tmp(v1
);
3169 Temp carry
= bld
.vadd32(Definition(new_lo
), lo
, Operand(to_add
), true).def(1).getTemp();
3170 hi
= bld
.vadd32(bld
.def(v1
), hi
, Operand(0u), false, carry
);
3171 offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_lo
, hi
);
3176 /* align offset down if needed */
3177 Operand aligned_offset
= offset
;
3178 if (need_to_align_offset
) {
3179 Temp offset_tmp
= offset
.isTemp() ? offset
.getTemp() : Temp();
3180 if (offset
.isConstant()) {
3181 aligned_offset
= Operand(offset
.constantValue() & 0xfffffffcu
);
3182 } else if (offset_tmp
.regClass() == s1
) {
3183 aligned_offset
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfffffffcu
), offset_tmp
);
3184 } else if (offset_tmp
.regClass() == s2
) {
3185 aligned_offset
= bld
.sop2(aco_opcode::s_and_b64
, bld
.def(s2
), bld
.def(s1
, scc
), Operand((uint64_t)0xfffffffffffffffcllu
), offset_tmp
);
3186 } else if (offset_tmp
.regClass() == v1
) {
3187 aligned_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xfffffffcu
), offset_tmp
);
3188 } else if (offset_tmp
.regClass() == v2
) {
3189 Temp hi
= bld
.tmp(v1
), lo
= bld
.tmp(v1
);
3190 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), offset_tmp
);
3191 lo
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xfffffffcu
), lo
);
3192 aligned_offset
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), lo
, hi
);
3195 Temp aligned_offset_tmp
= aligned_offset
.isTemp() ? aligned_offset
.getTemp() :
3196 bld
.copy(bld
.def(s1
), aligned_offset
);
3198 unsigned align
= align_offset
? 1 << (ffs(align_offset
) - 1) : align_mul
;
3199 Temp val
= callback(bld
, info
, aligned_offset_tmp
, bytes_needed
, align
,
3200 reduced_const_offset
, byte_align
? Temp() : info
->dst
);
3202 /* the callback wrote directly to dst */
3203 if (val
== info
->dst
) {
3204 assert(num_vals
== 0);
3205 emit_split_vector(ctx
, info
->dst
, info
->num_components
);
3209 /* shift result right if needed */
3210 if (info
->component_size
< 4 && byte_align_loads
) {
3211 Operand
align((uint32_t)byte_align
);
3212 if (byte_align
== -1) {
3213 if (offset
.isConstant())
3214 align
= Operand(offset
.constantValue() % 4u);
3215 else if (offset
.size() == 2)
3216 align
= Operand(emit_extract_vector(ctx
, offset
.getTemp(), 0, RegClass(offset
.getTemp().type(), 1)));
3221 assert(val
.bytes() >= load_size
&& "unimplemented");
3222 if (val
.type() == RegType::sgpr
)
3223 byte_align_scalar(ctx
, val
, align
, info
->dst
);
3225 byte_align_vector(ctx
, val
, align
, info
->dst
, component_size
);
3229 /* add result to list and advance */
3230 if (info
->component_stride
) {
3231 assert(val
.bytes() == info
->component_size
&& "unimplemented");
3232 const_offset
+= info
->component_stride
;
3233 align_offset
= (align_offset
+ info
->component_stride
) % align_mul
;
3235 const_offset
+= val
.bytes();
3236 align_offset
= (align_offset
+ val
.bytes()) % align_mul
;
3238 bytes_read
+= val
.bytes();
3239 vals
[num_vals
++] = val
;
3242 /* create array of components */
3243 unsigned components_split
= 0;
3244 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> allocated_vec
;
3245 bool has_vgprs
= false;
3246 for (unsigned i
= 0; i
< num_vals
;) {
3248 unsigned num_tmps
= 0;
3249 unsigned tmp_size
= 0;
3250 RegType reg_type
= RegType::sgpr
;
3251 while ((!tmp_size
|| (tmp_size
% component_size
)) && i
< num_vals
) {
3252 if (vals
[i
].type() == RegType::vgpr
)
3253 reg_type
= RegType::vgpr
;
3254 tmp_size
+= vals
[i
].bytes();
3255 tmp
[num_tmps
++] = vals
[i
++];
3258 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(
3259 aco_opcode::p_create_vector
, Format::PSEUDO
, num_tmps
, 1)};
3260 for (unsigned i
= 0; i
< num_vals
; i
++)
3261 vec
->operands
[i
] = Operand(tmp
[i
]);
3262 tmp
[0] = bld
.tmp(RegClass::get(reg_type
, tmp_size
));
3263 vec
->definitions
[0] = Definition(tmp
[0]);
3264 bld
.insert(std::move(vec
));
3267 if (tmp
[0].bytes() % component_size
) {
3269 assert(i
== num_vals
);
3270 RegClass new_rc
= RegClass::get(reg_type
, tmp
[0].bytes() / component_size
* component_size
);
3271 tmp
[0] = bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(new_rc
), tmp
[0], Operand(0u));
3274 RegClass elem_rc
= RegClass::get(reg_type
, component_size
);
3276 unsigned start
= components_split
;
3278 if (tmp_size
== elem_rc
.bytes()) {
3279 allocated_vec
[components_split
++] = tmp
[0];
3281 assert(tmp_size
% elem_rc
.bytes() == 0);
3282 aco_ptr
<Pseudo_instruction
> split
{create_instruction
<Pseudo_instruction
>(
3283 aco_opcode::p_split_vector
, Format::PSEUDO
, 1, tmp_size
/ elem_rc
.bytes())};
3284 for (unsigned i
= 0; i
< split
->definitions
.size(); i
++) {
3285 Temp component
= bld
.tmp(elem_rc
);
3286 allocated_vec
[components_split
++] = component
;
3287 split
->definitions
[i
] = Definition(component
);
3289 split
->operands
[0] = Operand(tmp
[0]);
3290 bld
.insert(std::move(split
));
3293 /* try to p_as_uniform early so we can create more optimizable code and
3294 * also update allocated_vec */
3295 for (unsigned j
= start
; j
< components_split
; j
++) {
3296 if (allocated_vec
[j
].bytes() % 4 == 0 && info
->dst
.type() == RegType::sgpr
)
3297 allocated_vec
[j
] = bld
.as_uniform(allocated_vec
[j
]);
3298 has_vgprs
|= allocated_vec
[j
].type() == RegType::vgpr
;
3302 /* concatenate components and p_as_uniform() result if needed */
3303 if (info
->dst
.type() == RegType::vgpr
|| !has_vgprs
)
3304 ctx
->allocated_vec
.emplace(info
->dst
.id(), allocated_vec
);
3306 int padding_bytes
= MAX2((int)info
->dst
.bytes() - int(allocated_vec
[0].bytes() * info
->num_components
), 0);
3308 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(
3309 aco_opcode::p_create_vector
, Format::PSEUDO
, info
->num_components
+ !!padding_bytes
, 1)};
3310 for (unsigned i
= 0; i
< info
->num_components
; i
++)
3311 vec
->operands
[i
] = Operand(allocated_vec
[i
]);
3313 vec
->operands
[info
->num_components
] = Operand(RegClass::get(RegType::vgpr
, padding_bytes
));
3314 if (info
->dst
.type() == RegType::sgpr
&& has_vgprs
) {
3315 Temp tmp
= bld
.tmp(RegType::vgpr
, info
->dst
.size());
3316 vec
->definitions
[0] = Definition(tmp
);
3317 bld
.insert(std::move(vec
));
3318 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(info
->dst
), tmp
);
3320 vec
->definitions
[0] = Definition(info
->dst
);
3321 bld
.insert(std::move(vec
));
3325 Operand
load_lds_size_m0(Builder
& bld
)
3327 /* TODO: m0 does not need to be initialized on GFX9+ */
3328 return bld
.m0((Temp
)bld
.sopk(aco_opcode::s_movk_i32
, bld
.def(s1
, m0
), 0xffff));
3331 Temp
lds_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3332 Temp offset
, unsigned bytes_needed
,
3333 unsigned align
, unsigned const_offset
,
3336 offset
= offset
.regClass() == s1
? bld
.copy(bld
.def(v1
), offset
) : offset
;
3338 Operand m
= load_lds_size_m0(bld
);
3340 bool large_ds_read
= bld
.program
->chip_class
>= GFX7
;
3341 bool usable_read2
= bld
.program
->chip_class
>= GFX7
;
3346 //TODO: use ds_read_u8_d16_hi/ds_read_u16_d16_hi if beneficial
3347 if (bytes_needed
>= 16 && align
% 16 == 0 && large_ds_read
) {
3349 op
= aco_opcode::ds_read_b128
;
3350 } else if (bytes_needed
>= 16 && align
% 8 == 0 && const_offset
% 8 == 0 && usable_read2
) {
3353 op
= aco_opcode::ds_read2_b64
;
3354 } else if (bytes_needed
>= 12 && align
% 16 == 0 && large_ds_read
) {
3356 op
= aco_opcode::ds_read_b96
;
3357 } else if (bytes_needed
>= 8 && align
% 8 == 0) {
3359 op
= aco_opcode::ds_read_b64
;
3360 } else if (bytes_needed
>= 8 && align
% 4 == 0 && const_offset
% 4 == 0) {
3363 op
= aco_opcode::ds_read2_b32
;
3364 } else if (bytes_needed
>= 4 && align
% 4 == 0) {
3366 op
= aco_opcode::ds_read_b32
;
3367 } else if (bytes_needed
>= 2 && align
% 2 == 0) {
3369 op
= aco_opcode::ds_read_u16
;
3372 op
= aco_opcode::ds_read_u8
;
3375 unsigned max_offset_plus_one
= read2
? 254 * (size
/ 2u) + 1 : 65536;
3376 if (const_offset
>= max_offset_plus_one
) {
3377 offset
= bld
.vadd32(bld
.def(v1
), offset
, Operand(const_offset
/ max_offset_plus_one
));
3378 const_offset
%= max_offset_plus_one
;
3382 const_offset
/= (size
/ 2u);
3384 RegClass rc
= RegClass(RegType::vgpr
, DIV_ROUND_UP(size
, 4));
3385 Temp val
= rc
== info
->dst
.regClass() && dst_hint
.id() ? dst_hint
: bld
.tmp(rc
);
3387 bld
.ds(op
, Definition(val
), offset
, m
, const_offset
, const_offset
+ 1);
3389 bld
.ds(op
, Definition(val
), offset
, m
, const_offset
);
3392 val
= bld
.pseudo(aco_opcode::p_extract_vector
, bld
.def(RegClass::get(RegType::vgpr
, size
)), val
, Operand(0u));
3397 static auto emit_lds_load
= emit_load
<lds_load_callback
, false, true, UINT32_MAX
>;
3399 Temp
smem_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3400 Temp offset
, unsigned bytes_needed
,
3401 unsigned align
, unsigned const_offset
,
3406 if (bytes_needed
<= 4) {
3408 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dword
: aco_opcode::s_load_dword
;
3409 } else if (bytes_needed
<= 8) {
3411 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx2
: aco_opcode::s_load_dwordx2
;
3412 } else if (bytes_needed
<= 16) {
3414 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx4
: aco_opcode::s_load_dwordx4
;
3415 } else if (bytes_needed
<= 32) {
3417 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx8
: aco_opcode::s_load_dwordx8
;
3420 op
= info
->resource
.id() ? aco_opcode::s_buffer_load_dwordx16
: aco_opcode::s_load_dwordx16
;
3422 aco_ptr
<SMEM_instruction
> load
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 2, 1)};
3423 if (info
->resource
.id()) {
3424 load
->operands
[0] = Operand(info
->resource
);
3425 load
->operands
[1] = Operand(offset
);
3427 load
->operands
[0] = Operand(offset
);
3428 load
->operands
[1] = Operand(0u);
3430 RegClass
rc(RegType::sgpr
, size
);
3431 Temp val
= dst_hint
.id() && dst_hint
.regClass() == rc
? dst_hint
: bld
.tmp(rc
);
3432 load
->definitions
[0] = Definition(val
);
3433 load
->glc
= info
->glc
;
3434 load
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3435 load
->barrier
= info
->barrier
;
3436 load
->can_reorder
= false; // FIXME: currently, it doesn't seem beneficial due to how our scheduler works
3437 bld
.insert(std::move(load
));
3441 static auto emit_smem_load
= emit_load
<smem_load_callback
, true, false, 1024>;
3443 Temp
mubuf_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3444 Temp offset
, unsigned bytes_needed
,
3445 unsigned align_
, unsigned const_offset
,
3448 Operand vaddr
= offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3449 Operand soffset
= offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
3451 if (info
->soffset
.id()) {
3452 if (soffset
.isTemp())
3453 vaddr
= bld
.copy(bld
.def(v1
), soffset
);
3454 soffset
= Operand(info
->soffset
);
3457 unsigned bytes_size
= 0;
3459 if (bytes_needed
== 1) {
3461 op
= aco_opcode::buffer_load_ubyte
;
3462 } else if (bytes_needed
== 2) {
3464 op
= aco_opcode::buffer_load_ushort
;
3465 } else if (bytes_needed
<= 4) {
3467 op
= aco_opcode::buffer_load_dword
;
3468 } else if (bytes_needed
<= 8) {
3470 op
= aco_opcode::buffer_load_dwordx2
;
3471 } else if (bytes_needed
<= 12 && bld
.program
->chip_class
> GFX6
) {
3473 op
= aco_opcode::buffer_load_dwordx3
;
3476 op
= aco_opcode::buffer_load_dwordx4
;
3478 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3479 mubuf
->operands
[0] = Operand(info
->resource
);
3480 mubuf
->operands
[1] = vaddr
;
3481 mubuf
->operands
[2] = soffset
;
3482 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
3483 mubuf
->glc
= info
->glc
;
3484 mubuf
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3485 mubuf
->barrier
= info
->barrier
;
3486 mubuf
->can_reorder
= info
->can_reorder
;
3487 mubuf
->offset
= const_offset
;
3488 RegClass rc
= RegClass::get(RegType::vgpr
, align(bytes_size
, 4));
3489 Temp val
= dst_hint
.id() && rc
== dst_hint
.regClass() ? dst_hint
: bld
.tmp(rc
);
3490 mubuf
->definitions
[0] = Definition(val
);
3491 bld
.insert(std::move(mubuf
));
3496 static auto emit_mubuf_load
= emit_load
<mubuf_load_callback
, true, true, 4096>;
3498 Temp
get_gfx6_global_rsrc(Builder
& bld
, Temp addr
)
3500 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3501 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3503 if (addr
.type() == RegType::vgpr
)
3504 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), Operand(0u), Operand(0u), Operand(-1u), Operand(rsrc_conf
));
3505 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), addr
, Operand(-1u), Operand(rsrc_conf
));
3508 Temp
global_load_callback(Builder
& bld
, const LoadEmitInfo
*info
,
3509 Temp offset
, unsigned bytes_needed
,
3510 unsigned align_
, unsigned const_offset
,
3513 unsigned bytes_size
= 0;
3514 bool mubuf
= bld
.program
->chip_class
== GFX6
;
3515 bool global
= bld
.program
->chip_class
>= GFX9
;
3517 if (bytes_needed
== 1) {
3519 op
= mubuf
? aco_opcode::buffer_load_ubyte
: global
? aco_opcode::global_load_ubyte
: aco_opcode::flat_load_ubyte
;
3520 } else if (bytes_needed
== 2) {
3522 op
= mubuf
? aco_opcode::buffer_load_ushort
: global
? aco_opcode::global_load_ushort
: aco_opcode::flat_load_ushort
;
3523 } else if (bytes_needed
<= 4) {
3525 op
= mubuf
? aco_opcode::buffer_load_dword
: global
? aco_opcode::global_load_dword
: aco_opcode::flat_load_dword
;
3526 } else if (bytes_needed
<= 8) {
3528 op
= mubuf
? aco_opcode::buffer_load_dwordx2
: global
? aco_opcode::global_load_dwordx2
: aco_opcode::flat_load_dwordx2
;
3529 } else if (bytes_needed
<= 12 && !mubuf
) {
3531 op
= global
? aco_opcode::global_load_dwordx3
: aco_opcode::flat_load_dwordx3
;
3534 op
= mubuf
? aco_opcode::buffer_load_dwordx4
: global
? aco_opcode::global_load_dwordx4
: aco_opcode::flat_load_dwordx4
;
3536 RegClass rc
= RegClass::get(RegType::vgpr
, align(bytes_size
, 4));
3537 Temp val
= dst_hint
.id() && rc
== dst_hint
.regClass() ? dst_hint
: bld
.tmp(rc
);
3539 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
3540 mubuf
->operands
[0] = Operand(get_gfx6_global_rsrc(bld
, offset
));
3541 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
3542 mubuf
->operands
[2] = Operand(0u);
3543 mubuf
->glc
= info
->glc
;
3546 mubuf
->addr64
= offset
.type() == RegType::vgpr
;
3547 mubuf
->disable_wqm
= false;
3548 mubuf
->barrier
= info
->barrier
;
3549 mubuf
->definitions
[0] = Definition(val
);
3550 bld
.insert(std::move(mubuf
));
3552 offset
= offset
.regClass() == s2
? bld
.copy(bld
.def(v2
), offset
) : offset
;
3554 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 2, 1)};
3555 flat
->operands
[0] = Operand(offset
);
3556 flat
->operands
[1] = Operand(s1
);
3557 flat
->glc
= info
->glc
;
3558 flat
->dlc
= info
->glc
&& bld
.program
->chip_class
>= GFX10
;
3559 flat
->barrier
= info
->barrier
;
3561 flat
->definitions
[0] = Definition(val
);
3562 bld
.insert(std::move(flat
));
3568 static auto emit_global_load
= emit_load
<global_load_callback
, true, true, 1>;
3570 Temp
load_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp dst
,
3571 Temp address
, unsigned base_offset
, unsigned align
)
3573 assert(util_is_power_of_two_nonzero(align
));
3575 Builder
bld(ctx
->program
, ctx
->block
);
3577 unsigned num_components
= dst
.bytes() / elem_size_bytes
;
3578 LoadEmitInfo info
= {Operand(as_vgpr(ctx
, address
)), dst
, num_components
, elem_size_bytes
};
3579 info
.align_mul
= align
;
3580 info
.align_offset
= 0;
3581 info
.barrier
= barrier_shared
;
3582 info
.can_reorder
= false;
3583 info
.const_offset
= base_offset
;
3584 emit_lds_load(ctx
, bld
, &info
);
3589 void split_store_data(isel_context
*ctx
, RegType dst_type
, unsigned count
, Temp
*dst
, unsigned *offsets
, Temp src
)
3594 Builder
bld(ctx
->program
, ctx
->block
);
3596 ASSERTED
bool is_subdword
= false;
3597 for (unsigned i
= 0; i
< count
; i
++)
3598 is_subdword
|= offsets
[i
] % 4;
3599 is_subdword
|= (src
.bytes() - offsets
[count
- 1]) % 4;
3600 assert(!is_subdword
|| dst_type
== RegType::vgpr
);
3602 /* count == 1 fast path */
3604 if (dst_type
== RegType::sgpr
)
3605 dst
[0] = bld
.as_uniform(src
);
3607 dst
[0] = as_vgpr(ctx
, src
);
3611 for (unsigned i
= 0; i
< count
- 1; i
++)
3612 dst
[i
] = bld
.tmp(RegClass::get(dst_type
, offsets
[i
+ 1] - offsets
[i
]));
3613 dst
[count
- 1] = bld
.tmp(RegClass::get(dst_type
, src
.bytes() - offsets
[count
- 1]));
3615 if (is_subdword
&& src
.type() == RegType::sgpr
) {
3616 src
= as_vgpr(ctx
, src
);
3618 /* use allocated_vec if possible */
3619 auto it
= ctx
->allocated_vec
.find(src
.id());
3620 if (it
!= ctx
->allocated_vec
.end()) {
3621 unsigned total_size
= 0;
3622 for (unsigned i
= 0; it
->second
[i
].bytes() && (i
< NIR_MAX_VEC_COMPONENTS
); i
++)
3623 total_size
+= it
->second
[i
].bytes();
3624 if (total_size
!= src
.bytes())
3627 unsigned elem_size
= it
->second
[0].bytes();
3629 for (unsigned i
= 0; i
< count
; i
++) {
3630 if (offsets
[i
] % elem_size
|| dst
[i
].bytes() % elem_size
)
3634 for (unsigned i
= 0; i
< count
; i
++) {
3635 unsigned start_idx
= offsets
[i
] / elem_size
;
3636 unsigned op_count
= dst
[i
].bytes() / elem_size
;
3637 if (op_count
== 1) {
3638 if (dst_type
== RegType::sgpr
)
3639 dst
[i
] = bld
.as_uniform(it
->second
[start_idx
]);
3641 dst
[i
] = as_vgpr(ctx
, it
->second
[start_idx
]);
3645 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, op_count
, 1)};
3646 for (unsigned j
= 0; j
< op_count
; j
++) {
3647 Temp tmp
= it
->second
[start_idx
+ j
];
3648 if (dst_type
== RegType::sgpr
)
3649 tmp
= bld
.as_uniform(tmp
);
3650 vec
->operands
[j
] = Operand(tmp
);
3652 vec
->definitions
[0] = Definition(dst
[i
]);
3653 bld
.insert(std::move(vec
));
3659 if (dst_type
== RegType::sgpr
)
3660 src
= bld
.as_uniform(src
);
3664 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
, Format::PSEUDO
, 1, count
)};
3665 split
->operands
[0] = Operand(src
);
3666 for (unsigned i
= 0; i
< count
; i
++)
3667 split
->definitions
[i
] = Definition(dst
[i
]);
3668 bld
.insert(std::move(split
));
3671 bool scan_write_mask(uint32_t mask
, uint32_t todo_mask
,
3672 int *start
, int *count
)
3674 unsigned start_elem
= ffs(todo_mask
) - 1;
3675 bool skip
= !(mask
& (1 << start_elem
));
3677 mask
= ~mask
& todo_mask
;
3681 u_bit_scan_consecutive_range(&mask
, start
, count
);
3686 void advance_write_mask(uint32_t *todo_mask
, int start
, int count
)
3688 *todo_mask
&= ~u_bit_consecutive(0, count
) << start
;
3691 void store_lds(isel_context
*ctx
, unsigned elem_size_bytes
, Temp data
, uint32_t wrmask
,
3692 Temp address
, unsigned base_offset
, unsigned align
)
3694 assert(util_is_power_of_two_nonzero(align
));
3695 assert(util_is_power_of_two_nonzero(elem_size_bytes
) && elem_size_bytes
<= 8);
3697 Builder
bld(ctx
->program
, ctx
->block
);
3698 bool large_ds_write
= ctx
->options
->chip_class
>= GFX7
;
3699 bool usable_write2
= ctx
->options
->chip_class
>= GFX7
;
3701 unsigned write_count
= 0;
3702 Temp write_datas
[32];
3703 unsigned offsets
[32];
3704 aco_opcode opcodes
[32];
3706 wrmask
= widen_mask(wrmask
, elem_size_bytes
);
3708 uint32_t todo
= u_bit_consecutive(0, data
.bytes());
3711 if (!scan_write_mask(wrmask
, todo
, &offset
, &bytes
)) {
3712 offsets
[write_count
] = offset
;
3713 opcodes
[write_count
] = aco_opcode::num_opcodes
;
3715 advance_write_mask(&todo
, offset
, bytes
);
3719 bool aligned2
= offset
% 2 == 0 && align
% 2 == 0;
3720 bool aligned4
= offset
% 4 == 0 && align
% 4 == 0;
3721 bool aligned8
= offset
% 8 == 0 && align
% 8 == 0;
3722 bool aligned16
= offset
% 16 == 0 && align
% 16 == 0;
3724 //TODO: use ds_write_b8_d16_hi/ds_write_b16_d16_hi if beneficial
3725 aco_opcode op
= aco_opcode::num_opcodes
;
3726 if (bytes
>= 16 && aligned16
&& large_ds_write
) {
3727 op
= aco_opcode::ds_write_b128
;
3729 } else if (bytes
>= 12 && aligned16
&& large_ds_write
) {
3730 op
= aco_opcode::ds_write_b96
;
3732 } else if (bytes
>= 8 && aligned8
) {
3733 op
= aco_opcode::ds_write_b64
;
3735 } else if (bytes
>= 4 && aligned4
) {
3736 op
= aco_opcode::ds_write_b32
;
3738 } else if (bytes
>= 2 && aligned2
) {
3739 op
= aco_opcode::ds_write_b16
;
3741 } else if (bytes
>= 1) {
3742 op
= aco_opcode::ds_write_b8
;
3748 offsets
[write_count
] = offset
;
3749 opcodes
[write_count
] = op
;
3751 advance_write_mask(&todo
, offset
, bytes
);
3754 Operand m
= load_lds_size_m0(bld
);
3756 split_store_data(ctx
, RegType::vgpr
, write_count
, write_datas
, offsets
, data
);
3758 for (unsigned i
= 0; i
< write_count
; i
++) {
3759 aco_opcode op
= opcodes
[i
];
3760 if (op
== aco_opcode::num_opcodes
)
3763 Temp data
= write_datas
[i
];
3765 unsigned second
= write_count
;
3766 if (usable_write2
&& (op
== aco_opcode::ds_write_b32
|| op
== aco_opcode::ds_write_b64
)) {
3767 for (second
= i
+ 1; second
< write_count
; second
++) {
3768 if (opcodes
[second
] == op
&& (offsets
[second
] - offsets
[i
]) % data
.bytes() == 0) {
3769 op
= data
.bytes() == 4 ? aco_opcode::ds_write2_b32
: aco_opcode::ds_write2_b64
;
3770 opcodes
[second
] = aco_opcode::num_opcodes
;
3776 bool write2
= op
== aco_opcode::ds_write2_b32
|| op
== aco_opcode::ds_write2_b64
;
3777 unsigned write2_off
= (offsets
[second
] - offsets
[i
]) / data
.bytes();
3779 unsigned inline_offset
= base_offset
+ offsets
[i
];
3780 unsigned max_offset
= write2
? (255 - write2_off
) * data
.bytes() : 65535;
3781 Temp address_offset
= address
;
3782 if (inline_offset
> max_offset
) {
3783 address_offset
= bld
.vadd32(bld
.def(v1
), Operand(base_offset
), address_offset
);
3784 inline_offset
= offsets
[i
];
3786 assert(inline_offset
<= max_offset
); /* offsets[i] shouldn't be large enough for this to happen */
3789 Temp second_data
= write_datas
[second
];
3790 inline_offset
/= data
.bytes();
3791 bld
.ds(op
, address_offset
, data
, second_data
, m
, inline_offset
, inline_offset
+ write2_off
);
3793 bld
.ds(op
, address_offset
, data
, m
, inline_offset
);
3798 unsigned calculate_lds_alignment(isel_context
*ctx
, unsigned const_offset
)
3800 unsigned align
= 16;
3802 align
= std::min(align
, 1u << (ffs(const_offset
) - 1));
3808 aco_opcode
get_buffer_store_op(bool smem
, unsigned bytes
)
3813 return aco_opcode::buffer_store_byte
;
3816 return aco_opcode::buffer_store_short
;
3818 return smem
? aco_opcode::s_buffer_store_dword
: aco_opcode::buffer_store_dword
;
3820 return smem
? aco_opcode::s_buffer_store_dwordx2
: aco_opcode::buffer_store_dwordx2
;
3823 return aco_opcode::buffer_store_dwordx3
;
3825 return smem
? aco_opcode::s_buffer_store_dwordx4
: aco_opcode::buffer_store_dwordx4
;
3827 unreachable("Unexpected store size");
3828 return aco_opcode::num_opcodes
;
3831 void split_buffer_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool smem
, RegType dst_type
,
3832 Temp data
, unsigned writemask
, int swizzle_element_size
,
3833 unsigned *write_count
, Temp
*write_datas
, unsigned *offsets
)
3835 unsigned write_count_with_skips
= 0;
3838 /* determine how to split the data */
3839 unsigned todo
= u_bit_consecutive(0, data
.bytes());
3842 skips
[write_count_with_skips
] = !scan_write_mask(writemask
, todo
, &offset
, &bytes
);
3843 offsets
[write_count_with_skips
] = offset
;
3844 if (skips
[write_count_with_skips
]) {
3845 advance_write_mask(&todo
, offset
, bytes
);
3846 write_count_with_skips
++;
3850 /* only supported sizes are 1, 2, 4, 8, 12 and 16 bytes and can't be
3851 * larger than swizzle_element_size */
3852 bytes
= MIN2(bytes
, swizzle_element_size
);
3854 bytes
= bytes
> 4 ? bytes
& ~0x3 : MIN2(bytes
, 2);
3856 /* SMEM and GFX6 VMEM can't emit 12-byte stores */
3857 if ((ctx
->program
->chip_class
== GFX6
|| smem
) && bytes
== 12)
3860 /* dword or larger stores have to be dword-aligned */
3861 unsigned align_mul
= instr
? nir_intrinsic_align_mul(instr
) : 4;
3862 unsigned align_offset
= (instr
? nir_intrinsic_align_offset(instr
) : 0) + offset
;
3863 bool dword_aligned
= align_offset
% 4 == 0 && align_mul
% 4 == 0;
3865 bytes
= MIN2(bytes
, (align_offset
% 2 == 0 && align_mul
% 2 == 0) ? 2 : 1);
3867 advance_write_mask(&todo
, offset
, bytes
);
3868 write_count_with_skips
++;
3871 /* actually split data */
3872 split_store_data(ctx
, dst_type
, write_count_with_skips
, write_datas
, offsets
, data
);
3875 for (unsigned i
= 0; i
< write_count_with_skips
; i
++) {
3878 write_datas
[*write_count
] = write_datas
[i
];
3879 offsets
[*write_count
] = offsets
[i
];
3884 Temp
create_vec_from_array(isel_context
*ctx
, Temp arr
[], unsigned cnt
, RegType reg_type
, unsigned elem_size_bytes
,
3885 unsigned split_cnt
= 0u, Temp dst
= Temp())
3887 Builder
bld(ctx
->program
, ctx
->block
);
3888 unsigned dword_size
= elem_size_bytes
/ 4;
3891 dst
= bld
.tmp(RegClass(reg_type
, cnt
* dword_size
));
3893 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> allocated_vec
;
3894 aco_ptr
<Pseudo_instruction
> instr
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, cnt
, 1)};
3895 instr
->definitions
[0] = Definition(dst
);
3897 for (unsigned i
= 0; i
< cnt
; ++i
) {
3899 assert(arr
[i
].size() == dword_size
);
3900 allocated_vec
[i
] = arr
[i
];
3901 instr
->operands
[i
] = Operand(arr
[i
]);
3903 Temp zero
= bld
.copy(bld
.def(RegClass(reg_type
, dword_size
)), Operand(0u, dword_size
== 2));
3904 allocated_vec
[i
] = zero
;
3905 instr
->operands
[i
] = Operand(zero
);
3909 bld
.insert(std::move(instr
));
3912 emit_split_vector(ctx
, dst
, split_cnt
);
3914 ctx
->allocated_vec
.emplace(dst
.id(), allocated_vec
); /* emit_split_vector already does this */
3919 inline unsigned resolve_excess_vmem_const_offset(Builder
&bld
, Temp
&voffset
, unsigned const_offset
)
3921 if (const_offset
>= 4096) {
3922 unsigned excess_const_offset
= const_offset
/ 4096u * 4096u;
3923 const_offset
%= 4096u;
3926 voffset
= bld
.copy(bld
.def(v1
), Operand(excess_const_offset
));
3927 else if (unlikely(voffset
.regClass() == s1
))
3928 voffset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(excess_const_offset
), Operand(voffset
));
3929 else if (likely(voffset
.regClass() == v1
))
3930 voffset
= bld
.vadd32(bld
.def(v1
), Operand(voffset
), Operand(excess_const_offset
));
3932 unreachable("Unsupported register class of voffset");
3935 return const_offset
;
3938 void emit_single_mubuf_store(isel_context
*ctx
, Temp descriptor
, Temp voffset
, Temp soffset
, Temp vdata
,
3939 unsigned const_offset
= 0u, bool allow_reorder
= true, bool slc
= false)
3942 assert(vdata
.size() != 3 || ctx
->program
->chip_class
!= GFX6
);
3943 assert(vdata
.size() >= 1 && vdata
.size() <= 4);
3945 Builder
bld(ctx
->program
, ctx
->block
);
3946 aco_opcode op
= get_buffer_store_op(false, vdata
.bytes());
3947 const_offset
= resolve_excess_vmem_const_offset(bld
, voffset
, const_offset
);
3949 Operand voffset_op
= voffset
.id() ? Operand(as_vgpr(ctx
, voffset
)) : Operand(v1
);
3950 Operand soffset_op
= soffset
.id() ? Operand(soffset
) : Operand(0u);
3951 Builder::Result r
= bld
.mubuf(op
, Operand(descriptor
), voffset_op
, soffset_op
, Operand(vdata
), const_offset
,
3952 /* offen */ !voffset_op
.isUndefined(), /* idxen*/ false, /* addr64 */ false,
3953 /* disable_wqm */ false, /* glc */ true, /* dlc*/ false, /* slc */ slc
);
3955 static_cast<MUBUF_instruction
*>(r
.instr
)->can_reorder
= allow_reorder
;
3958 void store_vmem_mubuf(isel_context
*ctx
, Temp src
, Temp descriptor
, Temp voffset
, Temp soffset
,
3959 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned write_mask
,
3960 bool allow_combining
= true, bool reorder
= true, bool slc
= false)
3962 Builder
bld(ctx
->program
, ctx
->block
);
3963 assert(elem_size_bytes
== 2 || elem_size_bytes
== 4 || elem_size_bytes
== 8);
3965 write_mask
= widen_mask(write_mask
, elem_size_bytes
);
3967 unsigned write_count
= 0;
3968 Temp write_datas
[32];
3969 unsigned offsets
[32];
3970 split_buffer_store(ctx
, NULL
, false, RegType::vgpr
, src
, write_mask
,
3971 allow_combining
? 16 : 4, &write_count
, write_datas
, offsets
);
3973 for (unsigned i
= 0; i
< write_count
; i
++) {
3974 unsigned const_offset
= offsets
[i
] + base_const_offset
;
3975 emit_single_mubuf_store(ctx
, descriptor
, voffset
, soffset
, write_datas
[i
], const_offset
, reorder
, slc
);
3979 void load_vmem_mubuf(isel_context
*ctx
, Temp dst
, Temp descriptor
, Temp voffset
, Temp soffset
,
3980 unsigned base_const_offset
, unsigned elem_size_bytes
, unsigned num_components
,
3981 unsigned stride
= 0u, bool allow_combining
= true, bool allow_reorder
= true)
3983 assert(elem_size_bytes
== 2 || elem_size_bytes
== 4 || elem_size_bytes
== 8);
3984 assert((num_components
* elem_size_bytes
) == dst
.bytes());
3985 assert(!!stride
!= allow_combining
);
3987 Builder
bld(ctx
->program
, ctx
->block
);
3989 LoadEmitInfo info
= {Operand(voffset
), dst
, num_components
, elem_size_bytes
, descriptor
};
3990 info
.component_stride
= allow_combining
? 0 : stride
;
3992 info
.swizzle_component_size
= allow_combining
? 0 : 4;
3993 info
.align_mul
= MIN2(elem_size_bytes
, 4);
3994 info
.align_offset
= 0;
3995 info
.soffset
= soffset
;
3996 info
.const_offset
= base_const_offset
;
3997 emit_mubuf_load(ctx
, bld
, &info
);
4000 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)
4002 Builder
bld(ctx
->program
, ctx
->block
);
4003 Temp offset
= base_offset
.first
;
4004 unsigned const_offset
= base_offset
.second
;
4006 if (!nir_src_is_const(*off_src
)) {
4007 Temp indirect_offset_arg
= get_ssa_temp(ctx
, off_src
->ssa
);
4010 /* Calculate indirect offset with stride */
4011 if (likely(indirect_offset_arg
.regClass() == v1
))
4012 with_stride
= bld
.v_mul24_imm(bld
.def(v1
), indirect_offset_arg
, stride
);
4013 else if (indirect_offset_arg
.regClass() == s1
)
4014 with_stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), indirect_offset_arg
);
4016 unreachable("Unsupported register class of indirect offset");
4018 /* Add to the supplied base offset */
4019 if (offset
.id() == 0)
4020 offset
= with_stride
;
4021 else if (unlikely(offset
.regClass() == s1
&& with_stride
.regClass() == s1
))
4022 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), with_stride
, offset
);
4023 else if (offset
.size() == 1 && with_stride
.size() == 1)
4024 offset
= bld
.vadd32(bld
.def(v1
), with_stride
, offset
);
4026 unreachable("Unsupported register class of indirect offset");
4028 unsigned const_offset_arg
= nir_src_as_uint(*off_src
);
4029 const_offset
+= const_offset_arg
* stride
;
4032 return std::make_pair(offset
, const_offset
);
4035 std::pair
<Temp
, unsigned> offset_add(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &off1
, const std::pair
<Temp
, unsigned> &off2
)
4037 Builder
bld(ctx
->program
, ctx
->block
);
4040 if (off1
.first
.id() && off2
.first
.id()) {
4041 if (unlikely(off1
.first
.regClass() == s1
&& off2
.first
.regClass() == s1
))
4042 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), off1
.first
, off2
.first
);
4043 else if (off1
.first
.size() == 1 && off2
.first
.size() == 1)
4044 offset
= bld
.vadd32(bld
.def(v1
), off1
.first
, off2
.first
);
4046 unreachable("Unsupported register class of indirect offset");
4048 offset
= off1
.first
.id() ? off1
.first
: off2
.first
;
4051 return std::make_pair(offset
, off1
.second
+ off2
.second
);
4054 std::pair
<Temp
, unsigned> offset_mul(isel_context
*ctx
, const std::pair
<Temp
, unsigned> &offs
, unsigned multiplier
)
4056 Builder
bld(ctx
->program
, ctx
->block
);
4057 unsigned const_offset
= offs
.second
* multiplier
;
4059 if (!offs
.first
.id())
4060 return std::make_pair(offs
.first
, const_offset
);
4062 Temp offset
= unlikely(offs
.first
.regClass() == s1
)
4063 ? bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(multiplier
), offs
.first
)
4064 : bld
.v_mul24_imm(bld
.def(v1
), offs
.first
, multiplier
);
4066 return std::make_pair(offset
, const_offset
);
4069 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
, unsigned component_stride
)
4071 Builder
bld(ctx
->program
, ctx
->block
);
4073 /* base is the driver_location, which is already multiplied by 4, so is in dwords */
4074 unsigned const_offset
= nir_intrinsic_base(instr
) * base_stride
;
4075 /* component is in bytes */
4076 const_offset
+= nir_intrinsic_component(instr
) * component_stride
;
4078 /* offset should be interpreted in relation to the base, so the instruction effectively reads/writes another input/output when it has an offset */
4079 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4080 return offset_add_from_nir(ctx
, std::make_pair(Temp(), const_offset
), off_src
, 4u * base_stride
);
4083 std::pair
<Temp
, unsigned> get_intrinsic_io_basic_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned stride
= 1u)
4085 return get_intrinsic_io_basic_offset(ctx
, instr
, stride
, stride
);
4088 Temp
get_tess_rel_patch_id(isel_context
*ctx
)
4090 Builder
bld(ctx
->program
, ctx
->block
);
4092 switch (ctx
->shader
->info
.stage
) {
4093 case MESA_SHADER_TESS_CTRL
:
4094 return bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffu
),
4095 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
));
4096 case MESA_SHADER_TESS_EVAL
:
4097 return get_arg(ctx
, ctx
->args
->tes_rel_patch_id
);
4099 unreachable("Unsupported stage in get_tess_rel_patch_id");
4103 std::pair
<Temp
, unsigned> get_tcs_per_vertex_input_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4105 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4106 Builder
bld(ctx
->program
, ctx
->block
);
4108 uint32_t tcs_in_patch_stride
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 4;
4109 uint32_t tcs_in_vertex_stride
= ctx
->tcs_num_inputs
* 4;
4111 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
);
4113 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4114 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, tcs_in_vertex_stride
);
4116 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4117 Temp tcs_in_current_patch_offset
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, tcs_in_patch_stride
);
4118 offs
= offset_add(ctx
, offs
, std::make_pair(tcs_in_current_patch_offset
, 0));
4120 return offset_mul(ctx
, offs
, 4u);
4123 std::pair
<Temp
, unsigned> get_tcs_output_lds_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, bool per_vertex
= false)
4125 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4126 Builder
bld(ctx
->program
, ctx
->block
);
4128 uint32_t input_patch_size
= ctx
->args
->options
->key
.tcs
.input_vertices
* ctx
->tcs_num_inputs
* 16;
4129 uint32_t output_vertex_size
= ctx
->tcs_num_outputs
* 16;
4130 uint32_t pervertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
4131 uint32_t output_patch_stride
= pervertex_output_patch_size
+ ctx
->tcs_num_patch_outputs
* 16;
4133 std::pair
<Temp
, unsigned> offs
= instr
4134 ? get_intrinsic_io_basic_offset(ctx
, instr
, 4u)
4135 : std::make_pair(Temp(), 0u);
4137 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4138 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, output_patch_stride
);
4143 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4144 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, output_vertex_size
);
4146 uint32_t output_patch0_offset
= (input_patch_size
* ctx
->tcs_num_patches
);
4147 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_offset
));
4149 uint32_t output_patch0_patch_data_offset
= (input_patch_size
* ctx
->tcs_num_patches
+ pervertex_output_patch_size
);
4150 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, output_patch0_patch_data_offset
));
4156 std::pair
<Temp
, unsigned> get_tcs_per_vertex_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4158 Builder
bld(ctx
->program
, ctx
->block
);
4160 unsigned vertices_per_patch
= ctx
->shader
->info
.tess
.tcs_vertices_out
;
4161 unsigned attr_stride
= vertices_per_patch
* ctx
->tcs_num_patches
;
4163 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u);
4165 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4166 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, vertices_per_patch
* 16u);
4167 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, 0u));
4169 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4170 offs
= offset_add_from_nir(ctx
, offs
, vertex_index_src
, 16u);
4175 std::pair
<Temp
, unsigned> get_tcs_per_patch_output_vmem_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
= nullptr, unsigned const_base_offset
= 0u)
4177 Builder
bld(ctx
->program
, ctx
->block
);
4179 unsigned output_vertex_size
= ctx
->tcs_num_outputs
* 16;
4180 unsigned per_vertex_output_patch_size
= ctx
->shader
->info
.tess
.tcs_vertices_out
* output_vertex_size
;
4181 unsigned per_patch_data_offset
= per_vertex_output_patch_size
* ctx
->tcs_num_patches
;
4182 unsigned attr_stride
= ctx
->tcs_num_patches
;
4184 std::pair
<Temp
, unsigned> offs
= instr
4185 ? get_intrinsic_io_basic_offset(ctx
, instr
, attr_stride
* 4u, 4u)
4186 : std::make_pair(Temp(), 0u);
4188 if (const_base_offset
)
4189 offs
.second
+= const_base_offset
* attr_stride
;
4191 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
4192 Temp patch_off
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, 16u);
4193 offs
= offset_add(ctx
, offs
, std::make_pair(patch_off
, per_patch_data_offset
));
4198 bool tcs_driver_location_matches_api_mask(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
, uint64_t mask
, bool *indirect
)
4200 assert(per_vertex
|| ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4205 unsigned drv_loc
= nir_intrinsic_base(instr
);
4206 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4208 if (!nir_src_is_const(*off_src
)) {
4214 uint64_t slot
= per_vertex
4215 ? ctx
->output_drv_loc_to_var_slot
[ctx
->shader
->info
.stage
][drv_loc
/ 4]
4216 : (ctx
->output_tcs_patch_drv_loc_to_var_slot
[drv_loc
/ 4] - VARYING_SLOT_PATCH0
);
4217 return (((uint64_t) 1) << slot
) & mask
;
4220 bool store_output_to_temps(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4222 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4223 unsigned component
= nir_intrinsic_component(instr
);
4224 unsigned idx
= nir_intrinsic_base(instr
) + component
;
4226 nir_instr
*off_instr
= instr
->src
[1].ssa
->parent_instr
;
4227 if (off_instr
->type
!= nir_instr_type_load_const
)
4230 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4231 idx
+= nir_src_as_uint(instr
->src
[1]) * 4u;
4233 if (instr
->src
[0].ssa
->bit_size
== 64)
4234 write_mask
= widen_mask(write_mask
, 2);
4236 RegClass rc
= instr
->src
[0].ssa
->bit_size
== 16 ? v2b
: v1
;
4238 for (unsigned i
= 0; i
< 8; ++i
) {
4239 if (write_mask
& (1 << i
)) {
4240 ctx
->outputs
.mask
[idx
/ 4u] |= 1 << (idx
% 4u);
4241 ctx
->outputs
.temps
[idx
] = emit_extract_vector(ctx
, src
, i
, rc
);
4249 bool load_input_from_temps(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp dst
)
4251 /* Only TCS per-vertex inputs are supported by this function.
4252 * Per-vertex inputs only match between the VS/TCS invocation id when the number of invocations is the same.
4254 if (ctx
->shader
->info
.stage
!= MESA_SHADER_TESS_CTRL
|| !ctx
->tcs_in_out_eq
)
4257 nir_src
*off_src
= nir_get_io_offset_src(instr
);
4258 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
4259 nir_instr
*vertex_index_instr
= vertex_index_src
->ssa
->parent_instr
;
4260 bool can_use_temps
= nir_src_is_const(*off_src
) &&
4261 vertex_index_instr
->type
== nir_instr_type_intrinsic
&&
4262 nir_instr_as_intrinsic(vertex_index_instr
)->intrinsic
== nir_intrinsic_load_invocation_id
;
4267 unsigned idx
= nir_intrinsic_base(instr
) + nir_intrinsic_component(instr
) + 4 * nir_src_as_uint(*off_src
);
4268 Temp
*src
= &ctx
->inputs
.temps
[idx
];
4269 create_vec_from_array(ctx
, src
, dst
.size(), dst
.regClass().type(), 4u, 0, dst
);
4274 void visit_store_ls_or_es_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4276 Builder
bld(ctx
->program
, ctx
->block
);
4278 if (ctx
->tcs_in_out_eq
&& store_output_to_temps(ctx
, instr
)) {
4279 /* 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. */
4280 bool indirect_write
;
4281 bool temp_only_input
= tcs_driver_location_matches_api_mask(ctx
, instr
, true, ctx
->tcs_temp_only_inputs
, &indirect_write
);
4282 if (temp_only_input
&& !indirect_write
)
4286 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, 4u);
4287 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4288 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4289 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8u;
4291 if (ctx
->stage
== vertex_es
|| ctx
->stage
== tess_eval_es
) {
4292 /* GFX6-8: ES stage is not merged into GS, data is passed from ES to GS in VMEM. */
4293 Temp esgs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_VS
* 16u));
4294 Temp es2gs_offset
= get_arg(ctx
, ctx
->args
->es2gs_offset
);
4295 store_vmem_mubuf(ctx
, src
, esgs_ring
, offs
.first
, es2gs_offset
, offs
.second
, elem_size_bytes
, write_mask
, false, true, true);
4299 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
4300 /* GFX9+: ES stage is merged into GS, data is passed between them using LDS. */
4301 unsigned itemsize
= ctx
->stage
== vertex_geometry_gs
4302 ? ctx
->program
->info
->vs
.es_info
.esgs_itemsize
4303 : ctx
->program
->info
->tes
.es_info
.esgs_itemsize
;
4304 Temp thread_id
= emit_mbcnt(ctx
, bld
.def(v1
));
4305 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));
4306 Temp vertex_idx
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), thread_id
,
4307 bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_idx
), ctx
->program
->wave_size
));
4308 lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, itemsize
);
4309 } else if (ctx
->stage
== vertex_ls
|| ctx
->stage
== vertex_tess_control_hs
) {
4310 /* GFX6-8: VS runs on LS stage when tessellation is used, but LS shares LDS space with HS.
4311 * GFX9+: LS is merged into HS, but still uses the same LDS layout.
4313 Temp vertex_idx
= get_arg(ctx
, ctx
->args
->rel_auto_id
);
4314 lds_base
= bld
.v_mul24_imm(bld
.def(v1
), vertex_idx
, ctx
->tcs_num_inputs
* 16u);
4316 unreachable("Invalid LS or ES stage");
4319 offs
= offset_add(ctx
, offs
, std::make_pair(lds_base
, 0u));
4320 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
4321 store_lds(ctx
, elem_size_bytes
, src
, write_mask
, offs
.first
, offs
.second
, lds_align
);
4325 bool tcs_output_is_tess_factor(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4330 unsigned off
= nir_intrinsic_base(instr
) * 4u;
4331 return off
== ctx
->tcs_tess_lvl_out_loc
||
4332 off
== ctx
->tcs_tess_lvl_in_loc
;
4336 bool tcs_output_is_read_by_tes(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4338 uint64_t mask
= per_vertex
4339 ? ctx
->program
->info
->tcs
.tes_inputs_read
4340 : ctx
->program
->info
->tcs
.tes_patch_inputs_read
;
4342 bool indirect_write
= false;
4343 bool output_read_by_tes
= tcs_driver_location_matches_api_mask(ctx
, instr
, per_vertex
, mask
, &indirect_write
);
4344 return indirect_write
|| output_read_by_tes
;
4347 bool tcs_output_is_read_by_tcs(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4349 uint64_t mask
= per_vertex
4350 ? ctx
->shader
->info
.outputs_read
4351 : ctx
->shader
->info
.patch_outputs_read
;
4353 bool indirect_write
= false;
4354 bool output_read
= tcs_driver_location_matches_api_mask(ctx
, instr
, per_vertex
, mask
, &indirect_write
);
4355 return indirect_write
|| output_read
;
4358 void visit_store_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4360 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
4361 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4363 Builder
bld(ctx
->program
, ctx
->block
);
4365 Temp store_val
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4366 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4367 unsigned write_mask
= nir_intrinsic_write_mask(instr
);
4369 bool is_tess_factor
= tcs_output_is_tess_factor(ctx
, instr
, per_vertex
);
4370 bool write_to_vmem
= !is_tess_factor
&& tcs_output_is_read_by_tes(ctx
, instr
, per_vertex
);
4371 bool write_to_lds
= is_tess_factor
|| tcs_output_is_read_by_tcs(ctx
, instr
, per_vertex
);
4373 if (write_to_vmem
) {
4374 std::pair
<Temp
, unsigned> vmem_offs
= per_vertex
4375 ? get_tcs_per_vertex_output_vmem_offset(ctx
, instr
)
4376 : get_tcs_per_patch_output_vmem_offset(ctx
, instr
);
4378 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));
4379 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
4380 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);
4384 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
4385 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
4386 store_lds(ctx
, elem_size_bytes
, store_val
, write_mask
, lds_offs
.first
, lds_offs
.second
, lds_align
);
4390 void visit_load_tcs_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
, bool per_vertex
)
4392 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
4393 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4395 Builder
bld(ctx
->program
, ctx
->block
);
4397 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4398 std::pair
<Temp
, unsigned> lds_offs
= get_tcs_output_lds_offset(ctx
, instr
, per_vertex
);
4399 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_offs
.second
);
4400 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
4402 load_lds(ctx
, elem_size_bytes
, dst
, lds_offs
.first
, lds_offs
.second
, lds_align
);
4405 void visit_store_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4407 if (ctx
->stage
== vertex_vs
||
4408 ctx
->stage
== tess_eval_vs
||
4409 ctx
->stage
== fragment_fs
||
4410 ctx
->stage
== ngg_vertex_gs
||
4411 ctx
->stage
== ngg_tess_eval_gs
||
4412 ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
4413 bool stored_to_temps
= store_output_to_temps(ctx
, instr
);
4414 if (!stored_to_temps
) {
4415 fprintf(stderr
, "Unimplemented output offset instruction:\n");
4416 nir_print_instr(instr
->src
[1].ssa
->parent_instr
, stderr
);
4417 fprintf(stderr
, "\n");
4420 } else if (ctx
->stage
== vertex_es
||
4421 ctx
->stage
== vertex_ls
||
4422 ctx
->stage
== tess_eval_es
||
4423 (ctx
->stage
== vertex_tess_control_hs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) ||
4424 (ctx
->stage
== vertex_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) ||
4425 (ctx
->stage
== tess_eval_geometry_gs
&& ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
)) {
4426 visit_store_ls_or_es_output(ctx
, instr
);
4427 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
4428 visit_store_tcs_output(ctx
, instr
, false);
4430 unreachable("Shader stage not implemented");
4434 void visit_load_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4436 visit_load_tcs_output(ctx
, instr
, false);
4439 void emit_interp_instr(isel_context
*ctx
, unsigned idx
, unsigned component
, Temp src
, Temp dst
, Temp prim_mask
)
4441 Temp coord1
= emit_extract_vector(ctx
, src
, 0, v1
);
4442 Temp coord2
= emit_extract_vector(ctx
, src
, 1, v1
);
4444 Builder
bld(ctx
->program
, ctx
->block
);
4446 if (dst
.regClass() == v2b
) {
4447 if (ctx
->program
->has_16bank_lds
) {
4448 assert(ctx
->options
->chip_class
<= GFX8
);
4449 Builder::Result interp_p1
=
4450 bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
),
4451 Operand(2u) /* P0 */, bld
.m0(prim_mask
), idx
, component
);
4452 interp_p1
= bld
.vintrp(aco_opcode::v_interp_p1lv_f16
, bld
.def(v2b
),
4453 coord1
, bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4454 bld
.vintrp(aco_opcode::v_interp_p2_legacy_f16
, Definition(dst
), coord2
,
4455 bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4457 aco_opcode interp_p2_op
= aco_opcode::v_interp_p2_f16
;
4459 if (ctx
->options
->chip_class
== GFX8
)
4460 interp_p2_op
= aco_opcode::v_interp_p2_legacy_f16
;
4462 Builder::Result interp_p1
=
4463 bld
.vintrp(aco_opcode::v_interp_p1ll_f16
, bld
.def(v1
),
4464 coord1
, bld
.m0(prim_mask
), idx
, component
);
4465 bld
.vintrp(interp_p2_op
, Definition(dst
), coord2
, bld
.m0(prim_mask
),
4466 interp_p1
, idx
, component
);
4469 Builder::Result interp_p1
=
4470 bld
.vintrp(aco_opcode::v_interp_p1_f32
, bld
.def(v1
), coord1
,
4471 bld
.m0(prim_mask
), idx
, component
);
4473 if (ctx
->program
->has_16bank_lds
)
4474 interp_p1
.instr
->operands
[0].setLateKill(true);
4476 bld
.vintrp(aco_opcode::v_interp_p2_f32
, Definition(dst
), coord2
,
4477 bld
.m0(prim_mask
), interp_p1
, idx
, component
);
4481 void emit_load_frag_coord(isel_context
*ctx
, Temp dst
, unsigned num_components
)
4483 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1));
4484 for (unsigned i
= 0; i
< num_components
; i
++)
4485 vec
->operands
[i
] = Operand(get_arg(ctx
, ctx
->args
->ac
.frag_pos
[i
]));
4486 if (G_0286CC_POS_W_FLOAT_ENA(ctx
->program
->config
->spi_ps_input_ena
)) {
4487 assert(num_components
== 4);
4488 Builder
bld(ctx
->program
, ctx
->block
);
4489 vec
->operands
[3] = bld
.vop1(aco_opcode::v_rcp_f32
, bld
.def(v1
), get_arg(ctx
, ctx
->args
->ac
.frag_pos
[3]));
4492 for (Operand
& op
: vec
->operands
)
4493 op
= op
.isUndefined() ? Operand(0u) : op
;
4495 vec
->definitions
[0] = Definition(dst
);
4496 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4497 emit_split_vector(ctx
, dst
, num_components
);
4501 void visit_load_interpolated_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4503 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4504 Temp coords
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
4505 unsigned idx
= nir_intrinsic_base(instr
);
4506 unsigned component
= nir_intrinsic_component(instr
);
4507 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
4509 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[1]);
4511 assert(offset
->u32
== 0);
4513 /* the lower 15bit of the prim_mask contain the offset into LDS
4514 * while the upper bits contain the number of prims */
4515 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
4516 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
4517 Builder
bld(ctx
->program
, ctx
->block
);
4518 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
4519 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
4520 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
4521 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
4522 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
4525 if (instr
->dest
.ssa
.num_components
== 1) {
4526 emit_interp_instr(ctx
, idx
, component
, coords
, dst
, prim_mask
);
4528 aco_ptr
<Pseudo_instruction
> vec(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, instr
->dest
.ssa
.num_components
, 1));
4529 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; i
++)
4531 Temp tmp
= {ctx
->program
->allocateId(), v1
};
4532 emit_interp_instr(ctx
, idx
, component
+i
, coords
, tmp
, prim_mask
);
4533 vec
->operands
[i
] = Operand(tmp
);
4535 vec
->definitions
[0] = Definition(dst
);
4536 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4540 bool check_vertex_fetch_size(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
4541 unsigned offset
, unsigned stride
, unsigned channels
)
4543 unsigned vertex_byte_size
= vtx_info
->chan_byte_size
* channels
;
4544 if (vtx_info
->chan_byte_size
!= 4 && channels
== 3)
4546 return (ctx
->options
->chip_class
!= GFX6
&& ctx
->options
->chip_class
!= GFX10
) ||
4547 (offset
% vertex_byte_size
== 0 && stride
% vertex_byte_size
== 0);
4550 uint8_t get_fetch_data_format(isel_context
*ctx
, const ac_data_format_info
*vtx_info
,
4551 unsigned offset
, unsigned stride
, unsigned *channels
)
4553 if (!vtx_info
->chan_byte_size
) {
4554 *channels
= vtx_info
->num_channels
;
4555 return vtx_info
->chan_format
;
4558 unsigned num_channels
= *channels
;
4559 if (!check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, *channels
)) {
4560 unsigned new_channels
= num_channels
+ 1;
4561 /* first, assume more loads is worse and try using a larger data format */
4562 while (new_channels
<= 4 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
)) {
4564 /* don't make the attribute potentially out-of-bounds */
4565 if (offset
+ new_channels
* vtx_info
->chan_byte_size
> stride
)
4569 if (new_channels
== 5) {
4570 /* then try decreasing load size (at the cost of more loads) */
4571 new_channels
= *channels
;
4572 while (new_channels
> 1 && !check_vertex_fetch_size(ctx
, vtx_info
, offset
, stride
, new_channels
))
4576 if (new_channels
< *channels
)
4577 *channels
= new_channels
;
4578 num_channels
= new_channels
;
4581 switch (vtx_info
->chan_format
) {
4582 case V_008F0C_BUF_DATA_FORMAT_8
:
4583 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_8
, V_008F0C_BUF_DATA_FORMAT_8_8
,
4584 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_8_8_8_8
}[num_channels
- 1];
4585 case V_008F0C_BUF_DATA_FORMAT_16
:
4586 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_16
, V_008F0C_BUF_DATA_FORMAT_16_16
,
4587 V_008F0C_BUF_DATA_FORMAT_INVALID
, V_008F0C_BUF_DATA_FORMAT_16_16_16_16
}[num_channels
- 1];
4588 case V_008F0C_BUF_DATA_FORMAT_32
:
4589 return (uint8_t[]){V_008F0C_BUF_DATA_FORMAT_32
, V_008F0C_BUF_DATA_FORMAT_32_32
,
4590 V_008F0C_BUF_DATA_FORMAT_32_32_32
, V_008F0C_BUF_DATA_FORMAT_32_32_32_32
}[num_channels
- 1];
4592 unreachable("shouldn't reach here");
4593 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
4596 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
4597 * so we may need to fix it up. */
4598 Temp
adjust_vertex_fetch_alpha(isel_context
*ctx
, unsigned adjustment
, Temp alpha
)
4600 Builder
bld(ctx
->program
, ctx
->block
);
4602 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
4603 alpha
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), alpha
);
4605 /* For the integer-like cases, do a natural sign extension.
4607 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
4608 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
4611 alpha
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(adjustment
== RADV_ALPHA_ADJUST_SNORM
? 7u : 30u), alpha
);
4612 alpha
= bld
.vop2(aco_opcode::v_ashrrev_i32
, bld
.def(v1
), Operand(30u), alpha
);
4614 /* Convert back to the right type. */
4615 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
4616 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
4617 Temp clamp
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0xbf800000u
), alpha
);
4618 alpha
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0xbf800000u
), alpha
, clamp
);
4619 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
4620 alpha
= bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), alpha
);
4626 void visit_load_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4628 Builder
bld(ctx
->program
, ctx
->block
);
4629 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4630 if (ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
) {
4632 nir_instr
*off_instr
= instr
->src
[0].ssa
->parent_instr
;
4633 if (off_instr
->type
!= nir_instr_type_load_const
) {
4634 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
4635 nir_print_instr(off_instr
, stderr
);
4636 fprintf(stderr
, "\n");
4638 uint32_t offset
= nir_instr_as_load_const(off_instr
)->value
[0].u32
;
4640 Temp vertex_buffers
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->vertex_buffers
));
4642 unsigned location
= nir_intrinsic_base(instr
) / 4 - VERT_ATTRIB_GENERIC0
+ offset
;
4643 unsigned component
= nir_intrinsic_component(instr
);
4644 unsigned bitsize
= instr
->dest
.ssa
.bit_size
;
4645 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[location
];
4646 uint32_t attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[location
];
4647 uint32_t attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[location
];
4648 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[location
];
4650 unsigned dfmt
= attrib_format
& 0xf;
4651 unsigned nfmt
= (attrib_format
>> 4) & 0x7;
4652 const struct ac_data_format_info
*vtx_info
= ac_get_data_format_info(dfmt
);
4654 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
) << component
;
4655 unsigned num_channels
= MIN2(util_last_bit(mask
), vtx_info
->num_channels
);
4656 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (location
* 2)) & 3;
4657 bool post_shuffle
= ctx
->options
->key
.vs
.post_shuffle
& (1 << location
);
4659 num_channels
= MAX2(num_channels
, 3);
4661 Operand off
= bld
.copy(bld
.def(s1
), Operand(attrib_binding
* 16u));
4662 Temp list
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), vertex_buffers
, off
);
4665 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << location
)) {
4666 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[location
];
4667 Temp start_instance
= get_arg(ctx
, ctx
->args
->ac
.start_instance
);
4669 Temp instance_id
= get_arg(ctx
, ctx
->args
->ac
.instance_id
);
4671 Temp divided
= bld
.tmp(v1
);
4672 emit_v_div_u32(ctx
, divided
, as_vgpr(ctx
, instance_id
), divisor
);
4673 index
= bld
.vadd32(bld
.def(v1
), start_instance
, divided
);
4675 index
= bld
.vadd32(bld
.def(v1
), start_instance
, instance_id
);
4678 index
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), start_instance
);
4681 index
= bld
.vadd32(bld
.def(v1
),
4682 get_arg(ctx
, ctx
->args
->ac
.base_vertex
),
4683 get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
4686 Temp channels
[num_channels
];
4687 unsigned channel_start
= 0;
4688 bool direct_fetch
= false;
4690 /* skip unused channels at the start */
4691 if (vtx_info
->chan_byte_size
&& !post_shuffle
) {
4692 channel_start
= ffs(mask
) - 1;
4693 for (unsigned i
= 0; i
< channel_start
; i
++)
4694 channels
[i
] = Temp(0, s1
);
4695 } else if (vtx_info
->chan_byte_size
&& post_shuffle
&& !(mask
& 0x8)) {
4696 num_channels
= 3 - (ffs(mask
) - 1);
4700 while (channel_start
< num_channels
) {
4701 unsigned fetch_component
= num_channels
- channel_start
;
4702 unsigned fetch_offset
= attrib_offset
+ channel_start
* vtx_info
->chan_byte_size
;
4703 bool expanded
= false;
4705 /* use MUBUF when possible to avoid possible alignment issues */
4706 /* TODO: we could use SDWA to unpack 8/16-bit attributes without extra instructions */
4707 bool use_mubuf
= (nfmt
== V_008F0C_BUF_NUM_FORMAT_FLOAT
||
4708 nfmt
== V_008F0C_BUF_NUM_FORMAT_UINT
||
4709 nfmt
== V_008F0C_BUF_NUM_FORMAT_SINT
) &&
4710 vtx_info
->chan_byte_size
== 4;
4711 unsigned fetch_dfmt
= V_008F0C_BUF_DATA_FORMAT_INVALID
;
4713 fetch_dfmt
= get_fetch_data_format(ctx
, vtx_info
, fetch_offset
, attrib_stride
, &fetch_component
);
4715 if (fetch_component
== 3 && ctx
->options
->chip_class
== GFX6
) {
4716 /* GFX6 only supports loading vec3 with MTBUF, expand to vec4. */
4717 fetch_component
= 4;
4722 unsigned fetch_bytes
= fetch_component
* bitsize
/ 8;
4724 Temp fetch_index
= index
;
4725 if (attrib_stride
!= 0 && fetch_offset
> attrib_stride
) {
4726 fetch_index
= bld
.vadd32(bld
.def(v1
), Operand(fetch_offset
/ attrib_stride
), fetch_index
);
4727 fetch_offset
= fetch_offset
% attrib_stride
;
4730 Operand
soffset(0u);
4731 if (fetch_offset
>= 4096) {
4732 soffset
= bld
.copy(bld
.def(s1
), Operand(fetch_offset
/ 4096 * 4096));
4733 fetch_offset
%= 4096;
4737 switch (fetch_bytes
) {
4739 assert(!use_mubuf
&& bitsize
== 16);
4740 opcode
= aco_opcode::tbuffer_load_format_d16_x
;
4743 if (bitsize
== 16) {
4745 opcode
= aco_opcode::tbuffer_load_format_d16_xy
;
4747 opcode
= use_mubuf
? aco_opcode::buffer_load_dword
: aco_opcode::tbuffer_load_format_x
;
4751 assert(!use_mubuf
&& bitsize
== 16);
4752 opcode
= aco_opcode::tbuffer_load_format_d16_xyz
;
4755 if (bitsize
== 16) {
4757 opcode
= aco_opcode::tbuffer_load_format_d16_xyzw
;
4759 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx2
: aco_opcode::tbuffer_load_format_xy
;
4763 assert(ctx
->options
->chip_class
>= GFX7
||
4764 (!use_mubuf
&& ctx
->options
->chip_class
== GFX6
));
4765 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx3
: aco_opcode::tbuffer_load_format_xyz
;
4768 opcode
= use_mubuf
? aco_opcode::buffer_load_dwordx4
: aco_opcode::tbuffer_load_format_xyzw
;
4771 unreachable("Unimplemented load_input vector size");
4775 if (channel_start
== 0 && fetch_bytes
== dst
.bytes() && !post_shuffle
&&
4776 !expanded
&& (alpha_adjust
== RADV_ALPHA_ADJUST_NONE
||
4777 num_channels
<= 3)) {
4778 direct_fetch
= true;
4781 fetch_dst
= bld
.tmp(RegClass::get(RegType::vgpr
, fetch_bytes
));
4785 Instruction
*mubuf
= bld
.mubuf(opcode
,
4786 Definition(fetch_dst
), list
, fetch_index
, soffset
,
4787 fetch_offset
, false, true).instr
;
4788 static_cast<MUBUF_instruction
*>(mubuf
)->can_reorder
= true;
4790 Instruction
*mtbuf
= bld
.mtbuf(opcode
,
4791 Definition(fetch_dst
), list
, fetch_index
, soffset
,
4792 fetch_dfmt
, nfmt
, fetch_offset
, false, true).instr
;
4793 static_cast<MTBUF_instruction
*>(mtbuf
)->can_reorder
= true;
4796 emit_split_vector(ctx
, fetch_dst
, fetch_dst
.size());
4798 if (fetch_component
== 1) {
4799 channels
[channel_start
] = fetch_dst
;
4801 for (unsigned i
= 0; i
< MIN2(fetch_component
, num_channels
- channel_start
); i
++)
4802 channels
[channel_start
+ i
] = emit_extract_vector(ctx
, fetch_dst
, i
,
4803 bitsize
== 16 ? v2b
: v1
);
4806 channel_start
+= fetch_component
;
4809 if (!direct_fetch
) {
4810 bool is_float
= nfmt
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
4811 nfmt
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
4813 static const unsigned swizzle_normal
[4] = {0, 1, 2, 3};
4814 static const unsigned swizzle_post_shuffle
[4] = {2, 1, 0, 3};
4815 const unsigned *swizzle
= post_shuffle
? swizzle_post_shuffle
: swizzle_normal
;
4817 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
4818 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
4819 unsigned num_temp
= 0;
4820 for (unsigned i
= 0; i
< dst
.size(); i
++) {
4821 unsigned idx
= i
+ component
;
4822 if (swizzle
[idx
] < num_channels
&& channels
[swizzle
[idx
]].id()) {
4823 Temp channel
= channels
[swizzle
[idx
]];
4824 if (idx
== 3 && alpha_adjust
!= RADV_ALPHA_ADJUST_NONE
)
4825 channel
= adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, channel
);
4826 vec
->operands
[i
] = Operand(channel
);
4830 } else if (is_float
&& idx
== 3) {
4831 vec
->operands
[i
] = Operand(0x3f800000u
);
4832 } else if (!is_float
&& idx
== 3) {
4833 vec
->operands
[i
] = Operand(1u);
4835 vec
->operands
[i
] = Operand(0u);
4838 vec
->definitions
[0] = Definition(dst
);
4839 ctx
->block
->instructions
.emplace_back(std::move(vec
));
4840 emit_split_vector(ctx
, dst
, dst
.size());
4842 if (num_temp
== dst
.size())
4843 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
4845 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
4846 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
4847 nir_instr
*off_instr
= instr
->src
[offset_idx
].ssa
->parent_instr
;
4848 if (off_instr
->type
!= nir_instr_type_load_const
||
4849 nir_instr_as_load_const(off_instr
)->value
[0].u32
!= 0) {
4850 fprintf(stderr
, "Unimplemented nir_intrinsic_load_input offset\n");
4851 nir_print_instr(off_instr
, stderr
);
4852 fprintf(stderr
, "\n");
4855 Temp prim_mask
= get_arg(ctx
, ctx
->args
->ac
.prim_mask
);
4856 nir_const_value
* offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
4858 assert(offset
->u32
== 0);
4860 /* the lower 15bit of the prim_mask contain the offset into LDS
4861 * while the upper bits contain the number of prims */
4862 Temp offset_src
= get_ssa_temp(ctx
, instr
->src
[offset_idx
].ssa
);
4863 assert(offset_src
.regClass() == s1
&& "TODO: divergent offsets...");
4864 Builder
bld(ctx
->program
, ctx
->block
);
4865 Temp stride
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prim_mask
, Operand(16u));
4866 stride
= bld
.sop1(aco_opcode::s_bcnt1_i32_b32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
);
4867 stride
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, Operand(48u));
4868 offset_src
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), stride
, offset_src
);
4869 prim_mask
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
, m0
), bld
.def(s1
, scc
), offset_src
, prim_mask
);
4872 unsigned idx
= nir_intrinsic_base(instr
);
4873 unsigned component
= nir_intrinsic_component(instr
);
4874 unsigned vertex_id
= 2; /* P0 */
4876 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
4877 nir_const_value
* src0
= nir_src_as_const_value(instr
->src
[0]);
4878 switch (src0
->u32
) {
4880 vertex_id
= 2; /* P0 */
4883 vertex_id
= 0; /* P10 */
4886 vertex_id
= 1; /* P20 */
4889 unreachable("invalid vertex index");
4893 if (dst
.size() == 1) {
4894 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(dst
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
);
4896 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
4897 for (unsigned i
= 0; i
< dst
.size(); i
++)
4898 vec
->operands
[i
] = bld
.vintrp(aco_opcode::v_interp_mov_f32
, bld
.def(v1
), Operand(vertex_id
), bld
.m0(prim_mask
), idx
, component
+ i
);
4899 vec
->definitions
[0] = Definition(dst
);
4900 bld
.insert(std::move(vec
));
4903 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4904 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
4905 Temp soffset
= get_arg(ctx
, ctx
->args
->oc_lds
);
4906 std::pair
<Temp
, unsigned> offs
= get_tcs_per_patch_output_vmem_offset(ctx
, instr
);
4907 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8u;
4909 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, soffset
, offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
);
4911 unreachable("Shader stage not implemented");
4915 std::pair
<Temp
, unsigned> get_gs_per_vertex_input_offset(isel_context
*ctx
, nir_intrinsic_instr
*instr
, unsigned base_stride
= 1u)
4917 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4919 Builder
bld(ctx
->program
, ctx
->block
);
4920 nir_src
*vertex_src
= nir_get_io_vertex_index_src(instr
);
4923 if (!nir_src_is_const(*vertex_src
)) {
4924 /* better code could be created, but this case probably doesn't happen
4925 * much in practice */
4926 Temp indirect_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, vertex_src
->ssa
));
4927 for (unsigned i
= 0; i
< ctx
->shader
->info
.gs
.vertices_in
; i
++) {
4930 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
4931 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
/ 2u * 2u]);
4933 elem
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(16u), elem
);
4935 elem
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[i
]);
4938 if (vertex_offset
.id()) {
4939 Temp cond
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
4940 Operand(i
), indirect_vertex
);
4941 vertex_offset
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), vertex_offset
, elem
, cond
);
4943 vertex_offset
= elem
;
4947 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
)
4948 vertex_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
), vertex_offset
);
4950 unsigned vertex
= nir_src_as_uint(*vertex_src
);
4951 if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
)
4952 vertex_offset
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
4953 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
/ 2u * 2u]),
4954 Operand((vertex
% 2u) * 16u), Operand(16u));
4956 vertex_offset
= get_arg(ctx
, ctx
->args
->gs_vtx_offset
[vertex
]);
4959 std::pair
<Temp
, unsigned> offs
= get_intrinsic_io_basic_offset(ctx
, instr
, base_stride
);
4960 offs
= offset_add(ctx
, offs
, std::make_pair(vertex_offset
, 0u));
4961 return offset_mul(ctx
, offs
, 4u);
4964 void visit_load_gs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4966 assert(ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4968 Builder
bld(ctx
->program
, ctx
->block
);
4969 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4970 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4972 if (ctx
->stage
== geometry_gs
) {
4973 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
, ctx
->program
->wave_size
);
4974 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_ESGS_GS
* 16u));
4975 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);
4976 } else if (ctx
->stage
== vertex_geometry_gs
|| ctx
->stage
== tess_eval_geometry_gs
) {
4977 std::pair
<Temp
, unsigned> offs
= get_gs_per_vertex_input_offset(ctx
, instr
);
4978 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
4979 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
4981 unreachable("Unsupported GS stage.");
4985 void visit_load_tcs_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
4987 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4989 Builder
bld(ctx
->program
, ctx
->block
);
4990 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
4992 if (load_input_from_temps(ctx
, instr
, dst
))
4995 std::pair
<Temp
, unsigned> offs
= get_tcs_per_vertex_input_lds_offset(ctx
, instr
);
4996 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
4997 unsigned lds_align
= calculate_lds_alignment(ctx
, offs
.second
);
4999 load_lds(ctx
, elem_size_bytes
, dst
, offs
.first
, offs
.second
, lds_align
);
5002 void visit_load_tes_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5004 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
5006 Builder
bld(ctx
->program
, ctx
->block
);
5008 Temp ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_HS_TESS_OFFCHIP
* 16u));
5009 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
5010 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5012 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
5013 std::pair
<Temp
, unsigned> offs
= get_tcs_per_vertex_output_vmem_offset(ctx
, instr
);
5015 load_vmem_mubuf(ctx
, dst
, ring
, offs
.first
, oc_lds
, offs
.second
, elem_size_bytes
, instr
->dest
.ssa
.num_components
, 0u, true, true);
5018 void visit_load_per_vertex_input(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5020 switch (ctx
->shader
->info
.stage
) {
5021 case MESA_SHADER_GEOMETRY
:
5022 visit_load_gs_per_vertex_input(ctx
, instr
);
5024 case MESA_SHADER_TESS_CTRL
:
5025 visit_load_tcs_per_vertex_input(ctx
, instr
);
5027 case MESA_SHADER_TESS_EVAL
:
5028 visit_load_tes_per_vertex_input(ctx
, instr
);
5031 unreachable("Unimplemented shader stage");
5035 void visit_load_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5037 visit_load_tcs_output(ctx
, instr
, true);
5040 void visit_store_per_vertex_output(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5042 assert(ctx
->stage
== tess_control_hs
|| ctx
->stage
== vertex_tess_control_hs
);
5043 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5045 visit_store_tcs_output(ctx
, instr
, true);
5048 void visit_load_tess_coord(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5050 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
5052 Builder
bld(ctx
->program
, ctx
->block
);
5053 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5055 Operand
tes_u(get_arg(ctx
, ctx
->args
->tes_u
));
5056 Operand
tes_v(get_arg(ctx
, ctx
->args
->tes_v
));
5059 if (ctx
->shader
->info
.tess
.primitive_mode
== GL_TRIANGLES
) {
5060 Temp tmp
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), tes_u
, tes_v
);
5061 tmp
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0x3f800000u
/* 1.0f */), tmp
);
5062 tes_w
= Operand(tmp
);
5065 Temp tess_coord
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), tes_u
, tes_v
, tes_w
);
5066 emit_split_vector(ctx
, tess_coord
, 3);
5069 Temp
load_desc_ptr(isel_context
*ctx
, unsigned desc_set
)
5071 if (ctx
->program
->info
->need_indirect_descriptor_sets
) {
5072 Builder
bld(ctx
->program
, ctx
->block
);
5073 Temp ptr64
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->descriptor_sets
[0]));
5074 Operand off
= bld
.copy(bld
.def(s1
), Operand(desc_set
<< 2));
5075 return bld
.smem(aco_opcode::s_load_dword
, bld
.def(s1
), ptr64
, off
);//, false, false, false);
5078 return get_arg(ctx
, ctx
->args
->descriptor_sets
[desc_set
]);
5082 void visit_load_resource(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5084 Builder
bld(ctx
->program
, ctx
->block
);
5085 Temp index
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5086 if (!nir_dest_is_divergent(instr
->dest
))
5087 index
= bld
.as_uniform(index
);
5088 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
5089 unsigned binding
= nir_intrinsic_binding(instr
);
5092 radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
5093 radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
5094 unsigned offset
= layout
->binding
[binding
].offset
;
5096 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
5097 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
5098 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+ layout
->binding
[binding
].dynamic_offset_offset
;
5099 desc_ptr
= get_arg(ctx
, ctx
->args
->ac
.push_constants
);
5100 offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
5103 desc_ptr
= load_desc_ptr(ctx
, desc_set
);
5104 stride
= layout
->binding
[binding
].size
;
5107 nir_const_value
* nir_const_index
= nir_src_as_const_value(instr
->src
[0]);
5108 unsigned const_index
= nir_const_index
? nir_const_index
->u32
: 0;
5110 if (nir_const_index
) {
5111 const_index
= const_index
* stride
;
5112 } else if (index
.type() == RegType::vgpr
) {
5113 bool index24bit
= layout
->binding
[binding
].array_size
<= 0x1000000;
5114 index
= bld
.v_mul_imm(bld
.def(v1
), index
, stride
, index24bit
);
5116 index
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), Operand(index
));
5120 if (nir_const_index
) {
5121 const_index
= const_index
+ offset
;
5122 } else if (index
.type() == RegType::vgpr
) {
5123 index
= bld
.vadd32(bld
.def(v1
), Operand(offset
), index
);
5125 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), Operand(index
));
5129 if (nir_const_index
&& const_index
== 0) {
5131 } else if (index
.type() == RegType::vgpr
) {
5132 index
= bld
.vadd32(bld
.def(v1
),
5133 nir_const_index
? Operand(const_index
) : Operand(index
),
5136 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
5137 nir_const_index
? Operand(const_index
) : Operand(index
),
5141 bld
.copy(Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), index
);
5144 void load_buffer(isel_context
*ctx
, unsigned num_components
, unsigned component_size
,
5145 Temp dst
, Temp rsrc
, Temp offset
, unsigned align_mul
, unsigned align_offset
,
5146 bool glc
=false, bool readonly
=true, bool allow_smem
=true)
5148 Builder
bld(ctx
->program
, ctx
->block
);
5150 bool use_smem
= dst
.type() != RegType::vgpr
&& (!glc
|| ctx
->options
->chip_class
>= GFX8
) && allow_smem
;
5152 offset
= bld
.as_uniform(offset
);
5154 LoadEmitInfo info
= {Operand(offset
), dst
, num_components
, component_size
, rsrc
};
5156 info
.barrier
= readonly
? barrier_none
: barrier_buffer
;
5157 info
.can_reorder
= readonly
;
5158 info
.align_mul
= align_mul
;
5159 info
.align_offset
= align_offset
;
5161 emit_smem_load(ctx
, bld
, &info
);
5163 emit_mubuf_load(ctx
, bld
, &info
);
5166 void visit_load_ubo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5168 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5169 Temp rsrc
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5171 Builder
bld(ctx
->program
, ctx
->block
);
5173 nir_intrinsic_instr
* idx_instr
= nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
5174 unsigned desc_set
= nir_intrinsic_desc_set(idx_instr
);
5175 unsigned binding
= nir_intrinsic_binding(idx_instr
);
5176 radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[desc_set
].layout
;
5178 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
5179 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5180 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5181 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5182 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
5183 if (ctx
->options
->chip_class
>= GFX10
) {
5184 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5185 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5186 S_008F0C_RESOURCE_LEVEL(1);
5188 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5189 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5191 Temp upper_dwords
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s3
),
5192 Operand(S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
)),
5193 Operand(0xFFFFFFFFu
),
5194 Operand(desc_type
));
5195 rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5196 rsrc
, upper_dwords
);
5198 rsrc
= convert_pointer_to_64_bit(ctx
, rsrc
);
5199 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
5201 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
5202 load_buffer(ctx
, instr
->num_components
, size
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
),
5203 nir_intrinsic_align_mul(instr
), nir_intrinsic_align_offset(instr
));
5206 void visit_load_push_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5208 Builder
bld(ctx
->program
, ctx
->block
);
5209 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5210 unsigned offset
= nir_intrinsic_base(instr
);
5211 unsigned count
= instr
->dest
.ssa
.num_components
;
5212 nir_const_value
*index_cv
= nir_src_as_const_value(instr
->src
[0]);
5214 if (index_cv
&& instr
->dest
.ssa
.bit_size
== 32) {
5215 unsigned start
= (offset
+ index_cv
->u32
) / 4u;
5216 start
-= ctx
->args
->ac
.base_inline_push_consts
;
5217 if (start
+ count
<= ctx
->args
->ac
.num_inline_push_consts
) {
5218 std::array
<Temp
,NIR_MAX_VEC_COMPONENTS
> elems
;
5219 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
5220 for (unsigned i
= 0; i
< count
; ++i
) {
5221 elems
[i
] = get_arg(ctx
, ctx
->args
->ac
.inline_push_consts
[start
+ i
]);
5222 vec
->operands
[i
] = Operand
{elems
[i
]};
5224 vec
->definitions
[0] = Definition(dst
);
5225 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5226 ctx
->allocated_vec
.emplace(dst
.id(), elems
);
5231 Temp index
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[0].ssa
));
5232 if (offset
!= 0) // TODO check if index != 0 as well
5233 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
), index
);
5234 Temp ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->ac
.push_constants
));
5237 bool aligned
= true;
5239 if (instr
->dest
.ssa
.bit_size
== 8) {
5240 aligned
= index_cv
&& (offset
+ index_cv
->u32
) % 4 == 0;
5241 bool fits_in_dword
= count
== 1 || (index_cv
&& ((offset
+ index_cv
->u32
) % 4 + count
) <= 4);
5243 vec
= fits_in_dword
? bld
.tmp(s1
) : bld
.tmp(s2
);
5244 } else if (instr
->dest
.ssa
.bit_size
== 16) {
5245 aligned
= index_cv
&& (offset
+ index_cv
->u32
) % 4 == 0;
5247 vec
= count
== 4 ? bld
.tmp(s4
) : count
> 1 ? bld
.tmp(s2
) : bld
.tmp(s1
);
5252 switch (vec
.size()) {
5254 op
= aco_opcode::s_load_dword
;
5257 op
= aco_opcode::s_load_dwordx2
;
5263 op
= aco_opcode::s_load_dwordx4
;
5269 op
= aco_opcode::s_load_dwordx8
;
5272 unreachable("unimplemented or forbidden load_push_constant.");
5275 bld
.smem(op
, Definition(vec
), ptr
, index
);
5278 Operand byte_offset
= index_cv
? Operand((offset
+ index_cv
->u32
) % 4) : Operand(index
);
5279 byte_align_scalar(ctx
, vec
, byte_offset
, dst
);
5284 emit_split_vector(ctx
, vec
, 4);
5285 RegClass rc
= dst
.size() == 3 ? s1
: s2
;
5286 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
5287 emit_extract_vector(ctx
, vec
, 0, rc
),
5288 emit_extract_vector(ctx
, vec
, 1, rc
),
5289 emit_extract_vector(ctx
, vec
, 2, rc
));
5292 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
5295 void visit_load_constant(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5297 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5299 Builder
bld(ctx
->program
, ctx
->block
);
5301 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5302 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5303 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5304 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
5305 if (ctx
->options
->chip_class
>= GFX10
) {
5306 desc_type
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
5307 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
5308 S_008F0C_RESOURCE_LEVEL(1);
5310 desc_type
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5311 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
5314 unsigned base
= nir_intrinsic_base(instr
);
5315 unsigned range
= nir_intrinsic_range(instr
);
5317 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5318 if (base
&& offset
.type() == RegType::sgpr
)
5319 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), offset
, Operand(base
));
5320 else if (base
&& offset
.type() == RegType::vgpr
)
5321 offset
= bld
.vadd32(bld
.def(v1
), Operand(base
), offset
);
5323 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5324 bld
.sop1(aco_opcode::p_constaddr
, bld
.def(s2
), bld
.def(s1
, scc
), Operand(ctx
->constant_data_offset
)),
5325 Operand(MIN2(base
+ range
, ctx
->shader
->constant_data_size
)),
5326 Operand(desc_type
));
5327 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
5328 // TODO: get alignment information for subdword constants
5329 load_buffer(ctx
, instr
->num_components
, size
, dst
, rsrc
, offset
, size
, 0);
5332 void visit_discard_if(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5334 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
5335 ctx
->cf_info
.exec_potentially_empty_discard
= true;
5337 ctx
->program
->needs_exact
= true;
5339 // TODO: optimize uniform conditions
5340 Builder
bld(ctx
->program
, ctx
->block
);
5341 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
5342 assert(src
.regClass() == bld
.lm
);
5343 src
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
5344 bld
.pseudo(aco_opcode::p_discard_if
, src
);
5345 ctx
->block
->kind
|= block_kind_uses_discard_if
;
5349 void visit_discard(isel_context
* ctx
, nir_intrinsic_instr
*instr
)
5351 Builder
bld(ctx
->program
, ctx
->block
);
5353 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
5354 ctx
->cf_info
.exec_potentially_empty_discard
= true;
5356 bool divergent
= ctx
->cf_info
.parent_if
.is_divergent
||
5357 ctx
->cf_info
.parent_loop
.has_divergent_continue
;
5359 if (ctx
->block
->loop_nest_depth
&&
5360 ((nir_instr_is_last(&instr
->instr
) && !divergent
) || divergent
)) {
5361 /* we handle discards the same way as jump instructions */
5362 append_logical_end(ctx
->block
);
5364 /* in loops, discard behaves like break */
5365 Block
*linear_target
= ctx
->cf_info
.parent_loop
.exit
;
5366 ctx
->block
->kind
|= block_kind_discard
;
5369 /* uniform discard - loop ends here */
5370 assert(nir_instr_is_last(&instr
->instr
));
5371 ctx
->block
->kind
|= block_kind_uniform
;
5372 ctx
->cf_info
.has_branch
= true;
5373 bld
.branch(aco_opcode::p_branch
);
5374 add_linear_edge(ctx
->block
->index
, linear_target
);
5378 /* we add a break right behind the discard() instructions */
5379 ctx
->block
->kind
|= block_kind_break
;
5380 unsigned idx
= ctx
->block
->index
;
5382 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
5383 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = idx
;
5385 /* remove critical edges from linear CFG */
5386 bld
.branch(aco_opcode::p_branch
);
5387 Block
* break_block
= ctx
->program
->create_and_insert_block();
5388 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
5389 break_block
->kind
|= block_kind_uniform
;
5390 add_linear_edge(idx
, break_block
);
5391 add_linear_edge(break_block
->index
, linear_target
);
5392 bld
.reset(break_block
);
5393 bld
.branch(aco_opcode::p_branch
);
5395 Block
* continue_block
= ctx
->program
->create_and_insert_block();
5396 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
5397 add_linear_edge(idx
, continue_block
);
5398 append_logical_start(continue_block
);
5399 ctx
->block
= continue_block
;
5404 /* it can currently happen that NIR doesn't remove the unreachable code */
5405 if (!nir_instr_is_last(&instr
->instr
)) {
5406 ctx
->program
->needs_exact
= true;
5407 /* save exec somewhere temporarily so that it doesn't get
5408 * overwritten before the discard from outer exec masks */
5409 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(0xFFFFFFFF), Operand(exec
, bld
.lm
));
5410 bld
.pseudo(aco_opcode::p_discard_if
, cond
);
5411 ctx
->block
->kind
|= block_kind_uses_discard_if
;
5415 /* This condition is incorrect for uniformly branched discards in a loop
5416 * predicated by a divergent condition, but the above code catches that case
5417 * and the discard would end up turning into a discard_if.
5427 if (!ctx
->cf_info
.parent_if
.is_divergent
) {
5428 /* program just ends here */
5429 ctx
->block
->kind
|= block_kind_uniform
;
5430 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
5431 0 /* enabled mask */, 9 /* dest */,
5432 false /* compressed */, true/* done */, true /* valid mask */);
5433 bld
.sopp(aco_opcode::s_endpgm
);
5434 // TODO: it will potentially be followed by a branch which is dead code to sanitize NIR phis
5436 ctx
->block
->kind
|= block_kind_discard
;
5437 /* branch and linear edge is added by visit_if() */
5441 enum aco_descriptor_type
{
5452 should_declare_array(isel_context
*ctx
, enum glsl_sampler_dim sampler_dim
, bool is_array
) {
5453 if (sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
5455 ac_image_dim dim
= ac_get_sampler_dim(ctx
->options
->chip_class
, sampler_dim
, is_array
);
5456 return dim
== ac_image_cube
||
5457 dim
== ac_image_1darray
||
5458 dim
== ac_image_2darray
||
5459 dim
== ac_image_2darraymsaa
;
5462 Temp
get_sampler_desc(isel_context
*ctx
, nir_deref_instr
*deref_instr
,
5463 enum aco_descriptor_type desc_type
,
5464 const nir_tex_instr
*tex_instr
, bool image
, bool write
)
5466 /* FIXME: we should lower the deref with some new nir_intrinsic_load_desc
5467 std::unordered_map<uint64_t, Temp>::iterator it = ctx->tex_desc.find((uint64_t) desc_type << 32 | deref_instr->dest.ssa.index);
5468 if (it != ctx->tex_desc.end())
5471 Temp index
= Temp();
5472 bool index_set
= false;
5473 unsigned constant_index
= 0;
5474 unsigned descriptor_set
;
5475 unsigned base_index
;
5476 Builder
bld(ctx
->program
, ctx
->block
);
5479 assert(tex_instr
&& !image
);
5481 base_index
= tex_instr
->sampler_index
;
5483 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
5484 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
5488 assert(deref_instr
->deref_type
== nir_deref_type_array
);
5489 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
5491 constant_index
+= array_size
* const_value
->u32
;
5493 Temp indirect
= get_ssa_temp(ctx
, deref_instr
->arr
.index
.ssa
);
5494 if (indirect
.type() == RegType::vgpr
)
5495 indirect
= bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), indirect
);
5497 if (array_size
!= 1)
5498 indirect
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(array_size
), indirect
);
5504 index
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), index
, indirect
);
5508 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
5510 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
5511 base_index
= deref_instr
->var
->data
.binding
;
5514 Temp list
= load_desc_ptr(ctx
, descriptor_set
);
5515 list
= convert_pointer_to_64_bit(ctx
, list
);
5517 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
5518 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
5519 unsigned offset
= binding
->offset
;
5520 unsigned stride
= binding
->size
;
5524 assert(base_index
< layout
->binding_count
);
5526 switch (desc_type
) {
5527 case ACO_DESC_IMAGE
:
5529 opcode
= aco_opcode::s_load_dwordx8
;
5531 case ACO_DESC_FMASK
:
5533 opcode
= aco_opcode::s_load_dwordx8
;
5536 case ACO_DESC_SAMPLER
:
5538 opcode
= aco_opcode::s_load_dwordx4
;
5539 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
5540 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
5542 case ACO_DESC_BUFFER
:
5544 opcode
= aco_opcode::s_load_dwordx4
;
5546 case ACO_DESC_PLANE_0
:
5547 case ACO_DESC_PLANE_1
:
5549 opcode
= aco_opcode::s_load_dwordx8
;
5550 offset
+= 32 * (desc_type
- ACO_DESC_PLANE_0
);
5552 case ACO_DESC_PLANE_2
:
5554 opcode
= aco_opcode::s_load_dwordx4
;
5558 unreachable("invalid desc_type\n");
5561 offset
+= constant_index
* stride
;
5563 if (desc_type
== ACO_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
5564 (!index_set
|| binding
->immutable_samplers_equal
)) {
5565 if (binding
->immutable_samplers_equal
)
5568 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
5569 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
5570 Operand(samplers
[constant_index
* 4 + 0]),
5571 Operand(samplers
[constant_index
* 4 + 1]),
5572 Operand(samplers
[constant_index
* 4 + 2]),
5573 Operand(samplers
[constant_index
* 4 + 3]));
5578 off
= bld
.copy(bld
.def(s1
), Operand(offset
));
5580 off
= Operand((Temp
)bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(offset
),
5581 bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(stride
), index
)));
5584 Temp res
= bld
.smem(opcode
, bld
.def(type
), list
, off
);
5586 if (desc_type
== ACO_DESC_PLANE_2
) {
5588 for (unsigned i
= 0; i
< 8; i
++)
5589 components
[i
] = bld
.tmp(s1
);
5590 bld
.pseudo(aco_opcode::p_split_vector
,
5591 Definition(components
[0]),
5592 Definition(components
[1]),
5593 Definition(components
[2]),
5594 Definition(components
[3]),
5597 Temp desc2
= get_sampler_desc(ctx
, deref_instr
, ACO_DESC_PLANE_1
, tex_instr
, image
, write
);
5598 bld
.pseudo(aco_opcode::p_split_vector
,
5599 bld
.def(s1
), bld
.def(s1
), bld
.def(s1
), bld
.def(s1
),
5600 Definition(components
[4]),
5601 Definition(components
[5]),
5602 Definition(components
[6]),
5603 Definition(components
[7]),
5606 res
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
5607 components
[0], components
[1], components
[2], components
[3],
5608 components
[4], components
[5], components
[6], components
[7]);
5614 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
5617 case GLSL_SAMPLER_DIM_BUF
:
5619 case GLSL_SAMPLER_DIM_1D
:
5620 return array
? 2 : 1;
5621 case GLSL_SAMPLER_DIM_2D
:
5622 return array
? 3 : 2;
5623 case GLSL_SAMPLER_DIM_MS
:
5624 return array
? 4 : 3;
5625 case GLSL_SAMPLER_DIM_3D
:
5626 case GLSL_SAMPLER_DIM_CUBE
:
5628 case GLSL_SAMPLER_DIM_RECT
:
5629 case GLSL_SAMPLER_DIM_SUBPASS
:
5631 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
5640 /* Adjust the sample index according to FMASK.
5642 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
5643 * which is the identity mapping. Each nibble says which physical sample
5644 * should be fetched to get that sample.
5646 * For example, 0x11111100 means there are only 2 samples stored and
5647 * the second sample covers 3/4 of the pixel. When reading samples 0
5648 * and 1, return physical sample 0 (determined by the first two 0s
5649 * in FMASK), otherwise return physical sample 1.
5651 * The sample index should be adjusted as follows:
5652 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
5654 static Temp
adjust_sample_index_using_fmask(isel_context
*ctx
, bool da
, std::vector
<Temp
>& coords
, Operand sample_index
, Temp fmask_desc_ptr
)
5656 Builder
bld(ctx
->program
, ctx
->block
);
5657 Temp fmask
= bld
.tmp(v1
);
5658 unsigned dim
= ctx
->options
->chip_class
>= GFX10
5659 ? ac_get_sampler_dim(ctx
->options
->chip_class
, GLSL_SAMPLER_DIM_2D
, da
)
5662 Temp coord
= da
? bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v3
), coords
[0], coords
[1], coords
[2]) :
5663 bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), coords
[0], coords
[1]);
5664 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(aco_opcode::image_load
, Format::MIMG
, 3, 1)};
5665 load
->operands
[0] = Operand(fmask_desc_ptr
);
5666 load
->operands
[1] = Operand(s4
); /* no sampler */
5667 load
->operands
[2] = Operand(coord
);
5668 load
->definitions
[0] = Definition(fmask
);
5675 load
->can_reorder
= true; /* fmask images shouldn't be modified */
5676 ctx
->block
->instructions
.emplace_back(std::move(load
));
5678 Operand sample_index4
;
5679 if (sample_index
.isConstant()) {
5680 if (sample_index
.constantValue() < 16) {
5681 sample_index4
= Operand(sample_index
.constantValue() << 2);
5683 sample_index4
= Operand(0u);
5685 } else if (sample_index
.regClass() == s1
) {
5686 sample_index4
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), sample_index
, Operand(2u));
5688 assert(sample_index
.regClass() == v1
);
5689 sample_index4
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), sample_index
);
5693 if (sample_index4
.isConstant() && sample_index4
.constantValue() == 0)
5694 final_sample
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(15u), fmask
);
5695 else if (sample_index4
.isConstant() && sample_index4
.constantValue() == 28)
5696 final_sample
= bld
.vop2(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), Operand(28u), fmask
);
5698 final_sample
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), fmask
, sample_index4
, Operand(4u));
5700 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
5701 * resource descriptor is 0 (invalid),
5703 Temp compare
= bld
.tmp(bld
.lm
);
5704 bld
.vopc_e64(aco_opcode::v_cmp_lg_u32
, Definition(compare
),
5705 Operand(0u), emit_extract_vector(ctx
, fmask_desc_ptr
, 1, s1
)).def(0).setHint(vcc
);
5707 Temp sample_index_v
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), sample_index
);
5709 /* Replace the MSAA sample index. */
5710 return bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), sample_index_v
, final_sample
, compare
);
5713 static Temp
get_image_coords(isel_context
*ctx
, const nir_intrinsic_instr
*instr
, const struct glsl_type
*type
)
5716 Temp src0
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
5717 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5718 bool is_array
= glsl_sampler_type_is_array(type
);
5719 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
5720 assert(!add_frag_pos
&& "Input attachments should be lowered.");
5721 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
|| dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
5722 bool gfx9_1d
= ctx
->options
->chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
5723 int count
= image_type_to_components_count(dim
, is_array
);
5724 std::vector
<Temp
> coords(count
);
5725 Builder
bld(ctx
->program
, ctx
->block
);
5729 Temp src2
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
5730 /* get sample index */
5731 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
5732 nir_const_value
*sample_cv
= nir_src_as_const_value(instr
->src
[2]);
5733 Operand sample_index
= sample_cv
? Operand(sample_cv
->u32
) : Operand(emit_extract_vector(ctx
, src2
, 0, v1
));
5734 std::vector
<Temp
> fmask_load_address
;
5735 for (unsigned i
= 0; i
< (is_array
? 3 : 2); i
++)
5736 fmask_load_address
.emplace_back(emit_extract_vector(ctx
, src0
, i
, v1
));
5738 Temp fmask_desc_ptr
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_FMASK
, nullptr, false, false);
5739 coords
[count
] = adjust_sample_index_using_fmask(ctx
, is_array
, fmask_load_address
, sample_index
, fmask_desc_ptr
);
5741 coords
[count
] = emit_extract_vector(ctx
, src2
, 0, v1
);
5746 coords
[0] = emit_extract_vector(ctx
, src0
, 0, v1
);
5747 coords
.resize(coords
.size() + 1);
5748 coords
[1] = bld
.copy(bld
.def(v1
), Operand(0u));
5750 coords
[2] = emit_extract_vector(ctx
, src0
, 1, v1
);
5752 for (int i
= 0; i
< count
; i
++)
5753 coords
[i
] = emit_extract_vector(ctx
, src0
, i
, v1
);
5756 if (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
5757 instr
->intrinsic
== nir_intrinsic_image_deref_store
) {
5758 int lod_index
= instr
->intrinsic
== nir_intrinsic_image_deref_load
? 3 : 4;
5759 bool level_zero
= nir_src_is_const(instr
->src
[lod_index
]) && nir_src_as_uint(instr
->src
[lod_index
]) == 0;
5762 coords
.emplace_back(get_ssa_temp(ctx
, instr
->src
[lod_index
].ssa
));
5765 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, coords
.size(), 1)};
5766 for (unsigned i
= 0; i
< coords
.size(); i
++)
5767 vec
->operands
[i
] = Operand(coords
[i
]);
5768 Temp res
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, coords
.size())};
5769 vec
->definitions
[0] = Definition(res
);
5770 ctx
->block
->instructions
.emplace_back(std::move(vec
));
5775 void visit_image_load(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5777 Builder
bld(ctx
->program
, ctx
->block
);
5778 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5779 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5780 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5781 bool is_array
= glsl_sampler_type_is_array(type
);
5782 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
5784 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
5785 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
5786 unsigned num_channels
= util_last_bit(mask
);
5787 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
5788 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
5791 switch (num_channels
) {
5793 opcode
= aco_opcode::buffer_load_format_x
;
5796 opcode
= aco_opcode::buffer_load_format_xy
;
5799 opcode
= aco_opcode::buffer_load_format_xyz
;
5802 opcode
= aco_opcode::buffer_load_format_xyzw
;
5805 unreachable(">4 channel buffer image load");
5807 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 3, 1)};
5808 load
->operands
[0] = Operand(rsrc
);
5809 load
->operands
[1] = Operand(vindex
);
5810 load
->operands
[2] = Operand((uint32_t) 0);
5812 if (num_channels
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
5815 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_channels
)};
5816 load
->definitions
[0] = Definition(tmp
);
5818 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
5819 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
5820 load
->barrier
= barrier_image
;
5821 ctx
->block
->instructions
.emplace_back(std::move(load
));
5823 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, (1 << num_channels
) - 1);
5827 Temp coords
= get_image_coords(ctx
, instr
, type
);
5828 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
5830 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
5831 unsigned num_components
= util_bitcount(dmask
);
5833 if (num_components
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
5836 tmp
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, num_components
)};
5838 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
5839 aco_opcode opcode
= level_zero
? aco_opcode::image_load
: aco_opcode::image_load_mip
;
5841 aco_ptr
<MIMG_instruction
> load
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1)};
5842 load
->operands
[0] = Operand(resource
);
5843 load
->operands
[1] = Operand(s4
); /* no sampler */
5844 load
->operands
[2] = Operand(coords
);
5845 load
->definitions
[0] = Definition(tmp
);
5846 load
->glc
= var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
) ? 1 : 0;
5847 load
->dlc
= load
->glc
&& ctx
->options
->chip_class
>= GFX10
;
5848 load
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5849 load
->dmask
= dmask
;
5851 load
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
5852 load
->barrier
= barrier_image
;
5853 ctx
->block
->instructions
.emplace_back(std::move(load
));
5855 expand_vector(ctx
, tmp
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
5859 void visit_image_store(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5861 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5862 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5863 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5864 bool is_array
= glsl_sampler_type_is_array(type
);
5865 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
5867 bool glc
= ctx
->options
->chip_class
== GFX6
|| var
->data
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
) ? 1 : 0;
5869 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
5870 Temp rsrc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
5871 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
5873 switch (data
.size()) {
5875 opcode
= aco_opcode::buffer_store_format_x
;
5878 opcode
= aco_opcode::buffer_store_format_xy
;
5881 opcode
= aco_opcode::buffer_store_format_xyz
;
5884 opcode
= aco_opcode::buffer_store_format_xyzw
;
5887 unreachable(">4 channel buffer image store");
5889 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
5890 store
->operands
[0] = Operand(rsrc
);
5891 store
->operands
[1] = Operand(vindex
);
5892 store
->operands
[2] = Operand((uint32_t) 0);
5893 store
->operands
[3] = Operand(data
);
5894 store
->idxen
= true;
5897 store
->disable_wqm
= true;
5898 store
->barrier
= barrier_image
;
5899 ctx
->program
->needs_exact
= true;
5900 ctx
->block
->instructions
.emplace_back(std::move(store
));
5904 assert(data
.type() == RegType::vgpr
);
5905 Temp coords
= get_image_coords(ctx
, instr
, type
);
5906 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
5908 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
5909 aco_opcode opcode
= level_zero
? aco_opcode::image_store
: aco_opcode::image_store_mip
;
5911 aco_ptr
<MIMG_instruction
> store
{create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 0)};
5912 store
->operands
[0] = Operand(resource
);
5913 store
->operands
[1] = Operand(data
);
5914 store
->operands
[2] = Operand(coords
);
5917 store
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
5918 store
->dmask
= (1 << data
.size()) - 1;
5920 store
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
5921 store
->disable_wqm
= true;
5922 store
->barrier
= barrier_image
;
5923 ctx
->program
->needs_exact
= true;
5924 ctx
->block
->instructions
.emplace_back(std::move(store
));
5928 void visit_image_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
5930 /* return the previous value if dest is ever used */
5931 bool return_previous
= false;
5932 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
5933 return_previous
= true;
5936 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
5937 return_previous
= true;
5941 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
5942 const struct glsl_type
*type
= glsl_without_array(var
->type
);
5943 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
5944 bool is_array
= glsl_sampler_type_is_array(type
);
5945 Builder
bld(ctx
->program
, ctx
->block
);
5947 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[3].ssa
));
5948 assert(data
.size() == 1 && "64bit ssbo atomics not yet implemented.");
5950 if (instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
)
5951 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), get_ssa_temp(ctx
, instr
->src
[4].ssa
), data
);
5953 aco_opcode buf_op
, image_op
;
5954 switch (instr
->intrinsic
) {
5955 case nir_intrinsic_image_deref_atomic_add
:
5956 buf_op
= aco_opcode::buffer_atomic_add
;
5957 image_op
= aco_opcode::image_atomic_add
;
5959 case nir_intrinsic_image_deref_atomic_umin
:
5960 buf_op
= aco_opcode::buffer_atomic_umin
;
5961 image_op
= aco_opcode::image_atomic_umin
;
5963 case nir_intrinsic_image_deref_atomic_imin
:
5964 buf_op
= aco_opcode::buffer_atomic_smin
;
5965 image_op
= aco_opcode::image_atomic_smin
;
5967 case nir_intrinsic_image_deref_atomic_umax
:
5968 buf_op
= aco_opcode::buffer_atomic_umax
;
5969 image_op
= aco_opcode::image_atomic_umax
;
5971 case nir_intrinsic_image_deref_atomic_imax
:
5972 buf_op
= aco_opcode::buffer_atomic_smax
;
5973 image_op
= aco_opcode::image_atomic_smax
;
5975 case nir_intrinsic_image_deref_atomic_and
:
5976 buf_op
= aco_opcode::buffer_atomic_and
;
5977 image_op
= aco_opcode::image_atomic_and
;
5979 case nir_intrinsic_image_deref_atomic_or
:
5980 buf_op
= aco_opcode::buffer_atomic_or
;
5981 image_op
= aco_opcode::image_atomic_or
;
5983 case nir_intrinsic_image_deref_atomic_xor
:
5984 buf_op
= aco_opcode::buffer_atomic_xor
;
5985 image_op
= aco_opcode::image_atomic_xor
;
5987 case nir_intrinsic_image_deref_atomic_exchange
:
5988 buf_op
= aco_opcode::buffer_atomic_swap
;
5989 image_op
= aco_opcode::image_atomic_swap
;
5991 case nir_intrinsic_image_deref_atomic_comp_swap
:
5992 buf_op
= aco_opcode::buffer_atomic_cmpswap
;
5993 image_op
= aco_opcode::image_atomic_cmpswap
;
5996 unreachable("visit_image_atomic should only be called with nir_intrinsic_image_deref_atomic_* instructions.");
5999 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6001 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
6002 Temp vindex
= emit_extract_vector(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
), 0, v1
);
6003 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, nullptr, true, true);
6004 //assert(ctx->options->chip_class < GFX9 && "GFX9 stride size workaround not yet implemented.");
6005 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(buf_op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6006 mubuf
->operands
[0] = Operand(resource
);
6007 mubuf
->operands
[1] = Operand(vindex
);
6008 mubuf
->operands
[2] = Operand((uint32_t)0);
6009 mubuf
->operands
[3] = Operand(data
);
6010 if (return_previous
)
6011 mubuf
->definitions
[0] = Definition(dst
);
6013 mubuf
->idxen
= true;
6014 mubuf
->glc
= return_previous
;
6015 mubuf
->dlc
= false; /* Not needed for atomics */
6016 mubuf
->disable_wqm
= true;
6017 mubuf
->barrier
= barrier_image
;
6018 ctx
->program
->needs_exact
= true;
6019 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6023 Temp coords
= get_image_coords(ctx
, instr
, type
);
6024 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, nullptr, true, true);
6025 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(image_op
, Format::MIMG
, 3, return_previous
? 1 : 0)};
6026 mimg
->operands
[0] = Operand(resource
);
6027 mimg
->operands
[1] = Operand(data
);
6028 mimg
->operands
[2] = Operand(coords
);
6029 if (return_previous
)
6030 mimg
->definitions
[0] = Definition(dst
);
6031 mimg
->glc
= return_previous
;
6032 mimg
->dlc
= false; /* Not needed for atomics */
6033 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
6034 mimg
->dmask
= (1 << data
.size()) - 1;
6036 mimg
->da
= should_declare_array(ctx
, dim
, glsl_sampler_type_is_array(type
));
6037 mimg
->disable_wqm
= true;
6038 mimg
->barrier
= barrier_image
;
6039 ctx
->program
->needs_exact
= true;
6040 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
6044 void get_buffer_size(isel_context
*ctx
, Temp desc
, Temp dst
, bool in_elements
)
6046 if (in_elements
&& ctx
->options
->chip_class
== GFX8
) {
6047 /* we only have to divide by 1, 2, 4, 8, 12 or 16 */
6048 Builder
bld(ctx
->program
, ctx
->block
);
6050 Temp size
= emit_extract_vector(ctx
, desc
, 2, s1
);
6052 Temp size_div3
= bld
.vop3(aco_opcode::v_mul_hi_u32
, bld
.def(v1
), bld
.copy(bld
.def(v1
), Operand(0xaaaaaaabu
)), size
);
6053 size_div3
= bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.as_uniform(size_div3
), Operand(1u));
6055 Temp stride
= emit_extract_vector(ctx
, desc
, 1, s1
);
6056 stride
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), stride
, Operand((5u << 16) | 16u));
6058 Temp is12
= bld
.sopc(aco_opcode::s_cmp_eq_i32
, bld
.def(s1
, scc
), stride
, Operand(12u));
6059 size
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
), size_div3
, size
, bld
.scc(is12
));
6061 Temp shr_dst
= dst
.type() == RegType::vgpr
? bld
.tmp(s1
) : dst
;
6062 bld
.sop2(aco_opcode::s_lshr_b32
, Definition(shr_dst
), bld
.def(s1
, scc
),
6063 size
, bld
.sop1(aco_opcode::s_ff1_i32_b32
, bld
.def(s1
), stride
));
6064 if (dst
.type() == RegType::vgpr
)
6065 bld
.copy(Definition(dst
), shr_dst
);
6067 /* TODO: we can probably calculate this faster with v_skip when stride != 12 */
6069 emit_extract_vector(ctx
, desc
, 2, dst
);
6073 void visit_image_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6075 const nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
6076 const struct glsl_type
*type
= glsl_without_array(var
->type
);
6077 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
6078 bool is_array
= glsl_sampler_type_is_array(type
);
6079 Builder
bld(ctx
->program
, ctx
->block
);
6081 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
6082 Temp desc
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_BUFFER
, NULL
, true, false);
6083 return get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
6087 Temp lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
6090 Temp resource
= get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), ACO_DESC_IMAGE
, NULL
, true, false);
6092 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6094 aco_ptr
<MIMG_instruction
> mimg
{create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1)};
6095 mimg
->operands
[0] = Operand(resource
);
6096 mimg
->operands
[1] = Operand(s4
); /* no sampler */
6097 mimg
->operands
[2] = Operand(lod
);
6098 uint8_t& dmask
= mimg
->dmask
;
6099 mimg
->dim
= ac_get_image_dim(ctx
->options
->chip_class
, dim
, is_array
);
6100 mimg
->dmask
= (1 << instr
->dest
.ssa
.num_components
) - 1;
6101 mimg
->da
= glsl_sampler_type_is_array(type
);
6102 mimg
->can_reorder
= true;
6103 Definition
& def
= mimg
->definitions
[0];
6104 ctx
->block
->instructions
.emplace_back(std::move(mimg
));
6106 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
6107 glsl_sampler_type_is_array(type
)) {
6109 assert(instr
->dest
.ssa
.num_components
== 3);
6110 Temp tmp
= {ctx
->program
->allocateId(), v3
};
6111 def
= Definition(tmp
);
6112 emit_split_vector(ctx
, tmp
, 3);
6114 /* divide 3rd value by 6 by multiplying with magic number */
6115 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
6116 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp
, 2, v1
), c
);
6118 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
6119 emit_extract_vector(ctx
, tmp
, 0, v1
),
6120 emit_extract_vector(ctx
, tmp
, 1, v1
),
6123 } else if (ctx
->options
->chip_class
== GFX9
&&
6124 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
6125 glsl_sampler_type_is_array(type
)) {
6126 assert(instr
->dest
.ssa
.num_components
== 2);
6127 def
= Definition(dst
);
6130 def
= Definition(dst
);
6133 emit_split_vector(ctx
, dst
, instr
->dest
.ssa
.num_components
);
6136 void visit_load_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6138 Builder
bld(ctx
->program
, ctx
->block
);
6139 unsigned num_components
= instr
->num_components
;
6141 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6142 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6143 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6145 unsigned access
= nir_intrinsic_access(instr
);
6146 bool glc
= access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
);
6147 unsigned size
= instr
->dest
.ssa
.bit_size
/ 8;
6149 uint32_t flags
= get_all_buffer_resource_flags(ctx
, instr
->src
[0].ssa
, access
);
6150 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6151 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6153 bool allow_smem
= !(flags
& (0 && glc
? has_nonglc_vmem_store
: has_vmem_store
));
6154 allow_smem
|= ((access
& ACCESS_RESTRICT
) && (access
& ACCESS_NON_WRITEABLE
)) || (access
& ACCESS_CAN_REORDER
);
6156 load_buffer(ctx
, num_components
, size
, dst
, rsrc
, get_ssa_temp(ctx
, instr
->src
[1].ssa
),
6157 nir_intrinsic_align_mul(instr
), nir_intrinsic_align_offset(instr
), glc
, false, allow_smem
);
6160 void visit_store_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6162 Builder
bld(ctx
->program
, ctx
->block
);
6163 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6164 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6165 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6166 Temp offset
= get_ssa_temp(ctx
, instr
->src
[2].ssa
);
6168 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6169 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6171 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
6172 uint32_t flags
= get_all_buffer_resource_flags(ctx
, instr
->src
[1].ssa
, nir_intrinsic_access(instr
));
6173 /* GLC bypasses VMEM/SMEM caches, so GLC SMEM loads/stores are coherent with GLC VMEM loads/stores
6174 * TODO: this optimization is disabled for now because we still need to ensure correct ordering
6176 bool allow_smem
= !(flags
& (0 && glc
? has_nonglc_vmem_loadstore
: has_vmem_loadstore
));
6178 bool smem
= !nir_src_is_divergent(instr
->src
[2]) &&
6179 ctx
->options
->chip_class
>= GFX8
&&
6180 (elem_size_bytes
>= 4 || can_subdword_ssbo_store_use_smem(instr
)) &&
6183 offset
= bld
.as_uniform(offset
);
6184 bool smem_nonfs
= smem
&& ctx
->stage
!= fragment_fs
;
6186 unsigned write_count
= 0;
6187 Temp write_datas
[32];
6188 unsigned offsets
[32];
6189 split_buffer_store(ctx
, instr
, smem
, smem_nonfs
? RegType::sgpr
: (smem
? data
.type() : RegType::vgpr
),
6190 data
, writemask
, 16, &write_count
, write_datas
, offsets
);
6192 for (unsigned i
= 0; i
< write_count
; i
++) {
6193 aco_opcode op
= get_buffer_store_op(smem
, write_datas
[i
].bytes());
6194 if (smem
&& ctx
->stage
== fragment_fs
)
6195 op
= aco_opcode::p_fs_buffer_store_smem
;
6198 aco_ptr
<SMEM_instruction
> store
{create_instruction
<SMEM_instruction
>(op
, Format::SMEM
, 3, 0)};
6199 store
->operands
[0] = Operand(rsrc
);
6201 Temp off
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
6202 offset
, Operand(offsets
[i
]));
6203 store
->operands
[1] = Operand(off
);
6205 store
->operands
[1] = Operand(offset
);
6207 if (op
!= aco_opcode::p_fs_buffer_store_smem
)
6208 store
->operands
[1].setFixed(m0
);
6209 store
->operands
[2] = Operand(write_datas
[i
]);
6212 store
->disable_wqm
= true;
6213 store
->barrier
= barrier_buffer
;
6214 ctx
->block
->instructions
.emplace_back(std::move(store
));
6215 ctx
->program
->wb_smem_l1_on_end
= true;
6216 if (op
== aco_opcode::p_fs_buffer_store_smem
) {
6217 ctx
->block
->kind
|= block_kind_needs_lowering
;
6218 ctx
->program
->needs_exact
= true;
6221 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
6222 store
->operands
[0] = Operand(rsrc
);
6223 store
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
6224 store
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
6225 store
->operands
[3] = Operand(write_datas
[i
]);
6226 store
->offset
= offsets
[i
];
6227 store
->offen
= (offset
.type() == RegType::vgpr
);
6230 store
->disable_wqm
= true;
6231 store
->barrier
= barrier_buffer
;
6232 ctx
->program
->needs_exact
= true;
6233 ctx
->block
->instructions
.emplace_back(std::move(store
));
6238 void visit_atomic_ssbo(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6240 /* return the previous value if dest is ever used */
6241 bool return_previous
= false;
6242 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6243 return_previous
= true;
6246 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6247 return_previous
= true;
6251 Builder
bld(ctx
->program
, ctx
->block
);
6252 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6254 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
)
6255 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
6256 get_ssa_temp(ctx
, instr
->src
[3].ssa
), data
);
6258 Temp offset
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6259 Temp rsrc
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6260 rsrc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), rsrc
, Operand(0u));
6262 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6264 aco_opcode op32
, op64
;
6265 switch (instr
->intrinsic
) {
6266 case nir_intrinsic_ssbo_atomic_add
:
6267 op32
= aco_opcode::buffer_atomic_add
;
6268 op64
= aco_opcode::buffer_atomic_add_x2
;
6270 case nir_intrinsic_ssbo_atomic_imin
:
6271 op32
= aco_opcode::buffer_atomic_smin
;
6272 op64
= aco_opcode::buffer_atomic_smin_x2
;
6274 case nir_intrinsic_ssbo_atomic_umin
:
6275 op32
= aco_opcode::buffer_atomic_umin
;
6276 op64
= aco_opcode::buffer_atomic_umin_x2
;
6278 case nir_intrinsic_ssbo_atomic_imax
:
6279 op32
= aco_opcode::buffer_atomic_smax
;
6280 op64
= aco_opcode::buffer_atomic_smax_x2
;
6282 case nir_intrinsic_ssbo_atomic_umax
:
6283 op32
= aco_opcode::buffer_atomic_umax
;
6284 op64
= aco_opcode::buffer_atomic_umax_x2
;
6286 case nir_intrinsic_ssbo_atomic_and
:
6287 op32
= aco_opcode::buffer_atomic_and
;
6288 op64
= aco_opcode::buffer_atomic_and_x2
;
6290 case nir_intrinsic_ssbo_atomic_or
:
6291 op32
= aco_opcode::buffer_atomic_or
;
6292 op64
= aco_opcode::buffer_atomic_or_x2
;
6294 case nir_intrinsic_ssbo_atomic_xor
:
6295 op32
= aco_opcode::buffer_atomic_xor
;
6296 op64
= aco_opcode::buffer_atomic_xor_x2
;
6298 case nir_intrinsic_ssbo_atomic_exchange
:
6299 op32
= aco_opcode::buffer_atomic_swap
;
6300 op64
= aco_opcode::buffer_atomic_swap_x2
;
6302 case nir_intrinsic_ssbo_atomic_comp_swap
:
6303 op32
= aco_opcode::buffer_atomic_cmpswap
;
6304 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
6307 unreachable("visit_atomic_ssbo should only be called with nir_intrinsic_ssbo_atomic_* instructions.");
6309 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6310 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6311 mubuf
->operands
[0] = Operand(rsrc
);
6312 mubuf
->operands
[1] = offset
.type() == RegType::vgpr
? Operand(offset
) : Operand(v1
);
6313 mubuf
->operands
[2] = offset
.type() == RegType::sgpr
? Operand(offset
) : Operand((uint32_t) 0);
6314 mubuf
->operands
[3] = Operand(data
);
6315 if (return_previous
)
6316 mubuf
->definitions
[0] = Definition(dst
);
6318 mubuf
->offen
= (offset
.type() == RegType::vgpr
);
6319 mubuf
->glc
= return_previous
;
6320 mubuf
->dlc
= false; /* Not needed for atomics */
6321 mubuf
->disable_wqm
= true;
6322 mubuf
->barrier
= barrier_buffer
;
6323 ctx
->program
->needs_exact
= true;
6324 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6327 void visit_get_buffer_size(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6329 Temp index
= convert_pointer_to_64_bit(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6330 Builder
bld(ctx
->program
, ctx
->block
);
6331 Temp desc
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), index
, Operand(0u));
6332 get_buffer_size(ctx
, desc
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), false);
6335 void visit_load_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6337 Builder
bld(ctx
->program
, ctx
->block
);
6338 unsigned num_components
= instr
->num_components
;
6339 unsigned component_size
= instr
->dest
.ssa
.bit_size
/ 8;
6341 LoadEmitInfo info
= {Operand(get_ssa_temp(ctx
, instr
->src
[0].ssa
)),
6342 get_ssa_temp(ctx
, &instr
->dest
.ssa
),
6343 num_components
, component_size
};
6344 info
.glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
);
6345 info
.align_mul
= nir_intrinsic_align_mul(instr
);
6346 info
.align_offset
= nir_intrinsic_align_offset(instr
);
6347 info
.barrier
= barrier_buffer
;
6348 info
.can_reorder
= false;
6349 /* VMEM stores don't update the SMEM cache and it's difficult to prove that
6350 * it's safe to use SMEM */
6351 bool can_use_smem
= nir_intrinsic_access(instr
) & ACCESS_NON_WRITEABLE
;
6352 if (info
.dst
.type() == RegType::vgpr
|| (info
.glc
&& ctx
->options
->chip_class
< GFX8
) || !can_use_smem
) {
6353 emit_global_load(ctx
, bld
, &info
);
6355 info
.offset
= Operand(bld
.as_uniform(info
.offset
));
6356 emit_smem_load(ctx
, bld
, &info
);
6360 void visit_store_global(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6362 Builder
bld(ctx
->program
, ctx
->block
);
6363 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6364 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6366 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6367 Temp addr
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
6368 bool glc
= nir_intrinsic_access(instr
) & (ACCESS_VOLATILE
| ACCESS_COHERENT
| ACCESS_NON_READABLE
);
6370 if (ctx
->options
->chip_class
>= GFX7
)
6371 addr
= as_vgpr(ctx
, addr
);
6373 unsigned write_count
= 0;
6374 Temp write_datas
[32];
6375 unsigned offsets
[32];
6376 split_buffer_store(ctx
, instr
, false, RegType::vgpr
, data
, writemask
,
6377 16, &write_count
, write_datas
, offsets
);
6379 for (unsigned i
= 0; i
< write_count
; i
++) {
6380 if (ctx
->options
->chip_class
>= GFX7
) {
6381 unsigned offset
= offsets
[i
];
6382 Temp store_addr
= addr
;
6383 if (offset
> 0 && ctx
->options
->chip_class
< GFX9
) {
6384 Temp addr0
= bld
.tmp(v1
), addr1
= bld
.tmp(v1
);
6385 Temp new_addr0
= bld
.tmp(v1
), new_addr1
= bld
.tmp(v1
);
6386 Temp carry
= bld
.tmp(bld
.lm
);
6387 bld
.pseudo(aco_opcode::p_split_vector
, Definition(addr0
), Definition(addr1
), addr
);
6389 bld
.vop2(aco_opcode::v_add_co_u32
, Definition(new_addr0
), bld
.hint_vcc(Definition(carry
)),
6390 Operand(offset
), addr0
);
6391 bld
.vop2(aco_opcode::v_addc_co_u32
, Definition(new_addr1
), bld
.def(bld
.lm
),
6393 carry
).def(1).setHint(vcc
);
6395 store_addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), new_addr0
, new_addr1
);
6400 bool global
= ctx
->options
->chip_class
>= GFX9
;
6402 switch (write_datas
[i
].bytes()) {
6404 op
= global
? aco_opcode::global_store_byte
: aco_opcode::flat_store_byte
;
6407 op
= global
? aco_opcode::global_store_short
: aco_opcode::flat_store_short
;
6410 op
= global
? aco_opcode::global_store_dword
: aco_opcode::flat_store_dword
;
6413 op
= global
? aco_opcode::global_store_dwordx2
: aco_opcode::flat_store_dwordx2
;
6416 op
= global
? aco_opcode::global_store_dwordx3
: aco_opcode::flat_store_dwordx3
;
6419 op
= global
? aco_opcode::global_store_dwordx4
: aco_opcode::flat_store_dwordx4
;
6422 unreachable("store_global not implemented for this size.");
6425 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, 0)};
6426 flat
->operands
[0] = Operand(store_addr
);
6427 flat
->operands
[1] = Operand(s1
);
6428 flat
->operands
[2] = Operand(write_datas
[i
]);
6431 flat
->offset
= offset
;
6432 flat
->disable_wqm
= true;
6433 flat
->barrier
= barrier_buffer
;
6434 ctx
->program
->needs_exact
= true;
6435 ctx
->block
->instructions
.emplace_back(std::move(flat
));
6437 assert(ctx
->options
->chip_class
== GFX6
);
6439 aco_opcode op
= get_buffer_store_op(false, write_datas
[i
].bytes());
6441 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
6443 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, 0)};
6444 mubuf
->operands
[0] = Operand(rsrc
);
6445 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
6446 mubuf
->operands
[2] = Operand(0u);
6447 mubuf
->operands
[3] = Operand(write_datas
[i
]);
6450 mubuf
->offset
= offsets
[i
];
6451 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
6452 mubuf
->disable_wqm
= true;
6453 mubuf
->barrier
= barrier_buffer
;
6454 ctx
->program
->needs_exact
= true;
6455 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6460 void visit_global_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6462 /* return the previous value if dest is ever used */
6463 bool return_previous
= false;
6464 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6465 return_previous
= true;
6468 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6469 return_previous
= true;
6473 Builder
bld(ctx
->program
, ctx
->block
);
6474 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6475 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6477 if (ctx
->options
->chip_class
>= GFX7
)
6478 addr
= as_vgpr(ctx
, addr
);
6480 if (instr
->intrinsic
== nir_intrinsic_global_atomic_comp_swap
)
6481 data
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(RegType::vgpr
, data
.size() * 2),
6482 get_ssa_temp(ctx
, instr
->src
[2].ssa
), data
);
6484 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6486 aco_opcode op32
, op64
;
6488 if (ctx
->options
->chip_class
>= GFX7
) {
6489 bool global
= ctx
->options
->chip_class
>= GFX9
;
6490 switch (instr
->intrinsic
) {
6491 case nir_intrinsic_global_atomic_add
:
6492 op32
= global
? aco_opcode::global_atomic_add
: aco_opcode::flat_atomic_add
;
6493 op64
= global
? aco_opcode::global_atomic_add_x2
: aco_opcode::flat_atomic_add_x2
;
6495 case nir_intrinsic_global_atomic_imin
:
6496 op32
= global
? aco_opcode::global_atomic_smin
: aco_opcode::flat_atomic_smin
;
6497 op64
= global
? aco_opcode::global_atomic_smin_x2
: aco_opcode::flat_atomic_smin_x2
;
6499 case nir_intrinsic_global_atomic_umin
:
6500 op32
= global
? aco_opcode::global_atomic_umin
: aco_opcode::flat_atomic_umin
;
6501 op64
= global
? aco_opcode::global_atomic_umin_x2
: aco_opcode::flat_atomic_umin_x2
;
6503 case nir_intrinsic_global_atomic_imax
:
6504 op32
= global
? aco_opcode::global_atomic_smax
: aco_opcode::flat_atomic_smax
;
6505 op64
= global
? aco_opcode::global_atomic_smax_x2
: aco_opcode::flat_atomic_smax_x2
;
6507 case nir_intrinsic_global_atomic_umax
:
6508 op32
= global
? aco_opcode::global_atomic_umax
: aco_opcode::flat_atomic_umax
;
6509 op64
= global
? aco_opcode::global_atomic_umax_x2
: aco_opcode::flat_atomic_umax_x2
;
6511 case nir_intrinsic_global_atomic_and
:
6512 op32
= global
? aco_opcode::global_atomic_and
: aco_opcode::flat_atomic_and
;
6513 op64
= global
? aco_opcode::global_atomic_and_x2
: aco_opcode::flat_atomic_and_x2
;
6515 case nir_intrinsic_global_atomic_or
:
6516 op32
= global
? aco_opcode::global_atomic_or
: aco_opcode::flat_atomic_or
;
6517 op64
= global
? aco_opcode::global_atomic_or_x2
: aco_opcode::flat_atomic_or_x2
;
6519 case nir_intrinsic_global_atomic_xor
:
6520 op32
= global
? aco_opcode::global_atomic_xor
: aco_opcode::flat_atomic_xor
;
6521 op64
= global
? aco_opcode::global_atomic_xor_x2
: aco_opcode::flat_atomic_xor_x2
;
6523 case nir_intrinsic_global_atomic_exchange
:
6524 op32
= global
? aco_opcode::global_atomic_swap
: aco_opcode::flat_atomic_swap
;
6525 op64
= global
? aco_opcode::global_atomic_swap_x2
: aco_opcode::flat_atomic_swap_x2
;
6527 case nir_intrinsic_global_atomic_comp_swap
:
6528 op32
= global
? aco_opcode::global_atomic_cmpswap
: aco_opcode::flat_atomic_cmpswap
;
6529 op64
= global
? aco_opcode::global_atomic_cmpswap_x2
: aco_opcode::flat_atomic_cmpswap_x2
;
6532 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6535 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6536 aco_ptr
<FLAT_instruction
> flat
{create_instruction
<FLAT_instruction
>(op
, global
? Format::GLOBAL
: Format::FLAT
, 3, return_previous
? 1 : 0)};
6537 flat
->operands
[0] = Operand(addr
);
6538 flat
->operands
[1] = Operand(s1
);
6539 flat
->operands
[2] = Operand(data
);
6540 if (return_previous
)
6541 flat
->definitions
[0] = Definition(dst
);
6542 flat
->glc
= return_previous
;
6543 flat
->dlc
= false; /* Not needed for atomics */
6545 flat
->disable_wqm
= true;
6546 flat
->barrier
= barrier_buffer
;
6547 ctx
->program
->needs_exact
= true;
6548 ctx
->block
->instructions
.emplace_back(std::move(flat
));
6550 assert(ctx
->options
->chip_class
== GFX6
);
6552 switch (instr
->intrinsic
) {
6553 case nir_intrinsic_global_atomic_add
:
6554 op32
= aco_opcode::buffer_atomic_add
;
6555 op64
= aco_opcode::buffer_atomic_add_x2
;
6557 case nir_intrinsic_global_atomic_imin
:
6558 op32
= aco_opcode::buffer_atomic_smin
;
6559 op64
= aco_opcode::buffer_atomic_smin_x2
;
6561 case nir_intrinsic_global_atomic_umin
:
6562 op32
= aco_opcode::buffer_atomic_umin
;
6563 op64
= aco_opcode::buffer_atomic_umin_x2
;
6565 case nir_intrinsic_global_atomic_imax
:
6566 op32
= aco_opcode::buffer_atomic_smax
;
6567 op64
= aco_opcode::buffer_atomic_smax_x2
;
6569 case nir_intrinsic_global_atomic_umax
:
6570 op32
= aco_opcode::buffer_atomic_umax
;
6571 op64
= aco_opcode::buffer_atomic_umax_x2
;
6573 case nir_intrinsic_global_atomic_and
:
6574 op32
= aco_opcode::buffer_atomic_and
;
6575 op64
= aco_opcode::buffer_atomic_and_x2
;
6577 case nir_intrinsic_global_atomic_or
:
6578 op32
= aco_opcode::buffer_atomic_or
;
6579 op64
= aco_opcode::buffer_atomic_or_x2
;
6581 case nir_intrinsic_global_atomic_xor
:
6582 op32
= aco_opcode::buffer_atomic_xor
;
6583 op64
= aco_opcode::buffer_atomic_xor_x2
;
6585 case nir_intrinsic_global_atomic_exchange
:
6586 op32
= aco_opcode::buffer_atomic_swap
;
6587 op64
= aco_opcode::buffer_atomic_swap_x2
;
6589 case nir_intrinsic_global_atomic_comp_swap
:
6590 op32
= aco_opcode::buffer_atomic_cmpswap
;
6591 op64
= aco_opcode::buffer_atomic_cmpswap_x2
;
6594 unreachable("visit_atomic_global should only be called with nir_intrinsic_global_atomic_* instructions.");
6597 Temp rsrc
= get_gfx6_global_rsrc(bld
, addr
);
6599 aco_opcode op
= instr
->dest
.ssa
.bit_size
== 32 ? op32
: op64
;
6601 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 4, return_previous
? 1 : 0)};
6602 mubuf
->operands
[0] = Operand(rsrc
);
6603 mubuf
->operands
[1] = addr
.type() == RegType::vgpr
? Operand(addr
) : Operand(v1
);
6604 mubuf
->operands
[2] = Operand(0u);
6605 mubuf
->operands
[3] = Operand(data
);
6606 if (return_previous
)
6607 mubuf
->definitions
[0] = Definition(dst
);
6608 mubuf
->glc
= return_previous
;
6611 mubuf
->addr64
= addr
.type() == RegType::vgpr
;
6612 mubuf
->disable_wqm
= true;
6613 mubuf
->barrier
= barrier_buffer
;
6614 ctx
->program
->needs_exact
= true;
6615 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
6619 void emit_memory_barrier(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6620 Builder
bld(ctx
->program
, ctx
->block
);
6621 switch(instr
->intrinsic
) {
6622 case nir_intrinsic_group_memory_barrier
:
6623 case nir_intrinsic_memory_barrier
:
6624 bld
.barrier(aco_opcode::p_memory_barrier_common
);
6626 case nir_intrinsic_memory_barrier_buffer
:
6627 bld
.barrier(aco_opcode::p_memory_barrier_buffer
);
6629 case nir_intrinsic_memory_barrier_image
:
6630 bld
.barrier(aco_opcode::p_memory_barrier_image
);
6632 case nir_intrinsic_memory_barrier_tcs_patch
:
6633 case nir_intrinsic_memory_barrier_shared
:
6634 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
6637 unreachable("Unimplemented memory barrier intrinsic");
6642 void visit_load_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6644 // TODO: implement sparse reads using ds_read2_b32 and nir_ssa_def_components_read()
6645 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6646 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6647 Builder
bld(ctx
->program
, ctx
->block
);
6649 unsigned elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
6650 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
6651 load_lds(ctx
, elem_size_bytes
, dst
, address
, nir_intrinsic_base(instr
), align
);
6654 void visit_store_shared(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6656 unsigned writemask
= nir_intrinsic_write_mask(instr
);
6657 Temp data
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
6658 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6659 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6661 unsigned align
= nir_intrinsic_align_mul(instr
) ? nir_intrinsic_align(instr
) : elem_size_bytes
;
6662 store_lds(ctx
, elem_size_bytes
, data
, writemask
, address
, nir_intrinsic_base(instr
), align
);
6665 void visit_shared_atomic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
6667 unsigned offset
= nir_intrinsic_base(instr
);
6668 Builder
bld(ctx
->program
, ctx
->block
);
6669 Operand m
= load_lds_size_m0(bld
);
6670 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6671 Temp address
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6673 unsigned num_operands
= 3;
6674 aco_opcode op32
, op64
, op32_rtn
, op64_rtn
;
6675 switch(instr
->intrinsic
) {
6676 case nir_intrinsic_shared_atomic_add
:
6677 op32
= aco_opcode::ds_add_u32
;
6678 op64
= aco_opcode::ds_add_u64
;
6679 op32_rtn
= aco_opcode::ds_add_rtn_u32
;
6680 op64_rtn
= aco_opcode::ds_add_rtn_u64
;
6682 case nir_intrinsic_shared_atomic_imin
:
6683 op32
= aco_opcode::ds_min_i32
;
6684 op64
= aco_opcode::ds_min_i64
;
6685 op32_rtn
= aco_opcode::ds_min_rtn_i32
;
6686 op64_rtn
= aco_opcode::ds_min_rtn_i64
;
6688 case nir_intrinsic_shared_atomic_umin
:
6689 op32
= aco_opcode::ds_min_u32
;
6690 op64
= aco_opcode::ds_min_u64
;
6691 op32_rtn
= aco_opcode::ds_min_rtn_u32
;
6692 op64_rtn
= aco_opcode::ds_min_rtn_u64
;
6694 case nir_intrinsic_shared_atomic_imax
:
6695 op32
= aco_opcode::ds_max_i32
;
6696 op64
= aco_opcode::ds_max_i64
;
6697 op32_rtn
= aco_opcode::ds_max_rtn_i32
;
6698 op64_rtn
= aco_opcode::ds_max_rtn_i64
;
6700 case nir_intrinsic_shared_atomic_umax
:
6701 op32
= aco_opcode::ds_max_u32
;
6702 op64
= aco_opcode::ds_max_u64
;
6703 op32_rtn
= aco_opcode::ds_max_rtn_u32
;
6704 op64_rtn
= aco_opcode::ds_max_rtn_u64
;
6706 case nir_intrinsic_shared_atomic_and
:
6707 op32
= aco_opcode::ds_and_b32
;
6708 op64
= aco_opcode::ds_and_b64
;
6709 op32_rtn
= aco_opcode::ds_and_rtn_b32
;
6710 op64_rtn
= aco_opcode::ds_and_rtn_b64
;
6712 case nir_intrinsic_shared_atomic_or
:
6713 op32
= aco_opcode::ds_or_b32
;
6714 op64
= aco_opcode::ds_or_b64
;
6715 op32_rtn
= aco_opcode::ds_or_rtn_b32
;
6716 op64_rtn
= aco_opcode::ds_or_rtn_b64
;
6718 case nir_intrinsic_shared_atomic_xor
:
6719 op32
= aco_opcode::ds_xor_b32
;
6720 op64
= aco_opcode::ds_xor_b64
;
6721 op32_rtn
= aco_opcode::ds_xor_rtn_b32
;
6722 op64_rtn
= aco_opcode::ds_xor_rtn_b64
;
6724 case nir_intrinsic_shared_atomic_exchange
:
6725 op32
= aco_opcode::ds_write_b32
;
6726 op64
= aco_opcode::ds_write_b64
;
6727 op32_rtn
= aco_opcode::ds_wrxchg_rtn_b32
;
6728 op64_rtn
= aco_opcode::ds_wrxchg_rtn_b64
;
6730 case nir_intrinsic_shared_atomic_comp_swap
:
6731 op32
= aco_opcode::ds_cmpst_b32
;
6732 op64
= aco_opcode::ds_cmpst_b64
;
6733 op32_rtn
= aco_opcode::ds_cmpst_rtn_b32
;
6734 op64_rtn
= aco_opcode::ds_cmpst_rtn_b64
;
6738 unreachable("Unhandled shared atomic intrinsic");
6741 /* return the previous value if dest is ever used */
6742 bool return_previous
= false;
6743 nir_foreach_use_safe(use_src
, &instr
->dest
.ssa
) {
6744 return_previous
= true;
6747 nir_foreach_if_use_safe(use_src
, &instr
->dest
.ssa
) {
6748 return_previous
= true;
6753 if (data
.size() == 1) {
6754 assert(instr
->dest
.ssa
.bit_size
== 32);
6755 op
= return_previous
? op32_rtn
: op32
;
6757 assert(instr
->dest
.ssa
.bit_size
== 64);
6758 op
= return_previous
? op64_rtn
: op64
;
6761 if (offset
> 65535) {
6762 address
= bld
.vadd32(bld
.def(v1
), Operand(offset
), address
);
6766 aco_ptr
<DS_instruction
> ds
;
6767 ds
.reset(create_instruction
<DS_instruction
>(op
, Format::DS
, num_operands
, return_previous
? 1 : 0));
6768 ds
->operands
[0] = Operand(address
);
6769 ds
->operands
[1] = Operand(data
);
6770 if (num_operands
== 4)
6771 ds
->operands
[2] = Operand(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
6772 ds
->operands
[num_operands
- 1] = m
;
6773 ds
->offset0
= offset
;
6774 if (return_previous
)
6775 ds
->definitions
[0] = Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
6776 ctx
->block
->instructions
.emplace_back(std::move(ds
));
6779 Temp
get_scratch_resource(isel_context
*ctx
)
6781 Builder
bld(ctx
->program
, ctx
->block
);
6782 Temp scratch_addr
= ctx
->program
->private_segment_buffer
;
6783 if (ctx
->stage
!= compute_cs
)
6784 scratch_addr
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), scratch_addr
, Operand(0u));
6786 uint32_t rsrc_conf
= S_008F0C_ADD_TID_ENABLE(1) |
6787 S_008F0C_INDEX_STRIDE(ctx
->program
->wave_size
== 64 ? 3 : 2);;
6789 if (ctx
->program
->chip_class
>= GFX10
) {
6790 rsrc_conf
|= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
6791 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
6792 S_008F0C_RESOURCE_LEVEL(1);
6793 } else if (ctx
->program
->chip_class
<= GFX7
) { /* dfmt modifies stride on GFX8/GFX9 when ADD_TID_EN=1 */
6794 rsrc_conf
|= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
6795 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
6798 /* older generations need element size = 16 bytes. element size removed in GFX9 */
6799 if (ctx
->program
->chip_class
<= GFX8
)
6800 rsrc_conf
|= S_008F0C_ELEMENT_SIZE(3);
6802 return bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), scratch_addr
, Operand(-1u), Operand(rsrc_conf
));
6805 void visit_load_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6806 Builder
bld(ctx
->program
, ctx
->block
);
6807 Temp rsrc
= get_scratch_resource(ctx
);
6808 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6809 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6811 LoadEmitInfo info
= {Operand(offset
), dst
, instr
->dest
.ssa
.num_components
,
6812 instr
->dest
.ssa
.bit_size
/ 8u, rsrc
};
6813 info
.align_mul
= nir_intrinsic_align_mul(instr
);
6814 info
.align_offset
= nir_intrinsic_align_offset(instr
);
6815 info
.swizzle_component_size
= 16;
6816 info
.can_reorder
= false;
6817 info
.soffset
= ctx
->program
->scratch_offset
;
6818 emit_mubuf_load(ctx
, bld
, &info
);
6821 void visit_store_scratch(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6822 Builder
bld(ctx
->program
, ctx
->block
);
6823 Temp rsrc
= get_scratch_resource(ctx
);
6824 Temp data
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6825 Temp offset
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[1].ssa
));
6827 unsigned elem_size_bytes
= instr
->src
[0].ssa
->bit_size
/ 8;
6828 unsigned writemask
= widen_mask(nir_intrinsic_write_mask(instr
), elem_size_bytes
);
6830 unsigned write_count
= 0;
6831 Temp write_datas
[32];
6832 unsigned offsets
[32];
6833 split_buffer_store(ctx
, instr
, false, RegType::vgpr
, data
, writemask
,
6834 16, &write_count
, write_datas
, offsets
);
6836 for (unsigned i
= 0; i
< write_count
; i
++) {
6837 aco_opcode op
= get_buffer_store_op(false, write_datas
[i
].bytes());
6838 bld
.mubuf(op
, rsrc
, offset
, ctx
->program
->scratch_offset
, write_datas
[i
], offsets
[i
], true);
6842 void visit_load_sample_mask_in(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6843 uint8_t log2_ps_iter_samples
;
6844 if (ctx
->program
->info
->ps
.force_persample
) {
6845 log2_ps_iter_samples
=
6846 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
6848 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
6851 /* The bit pattern matches that used by fixed function fragment
6853 static const unsigned ps_iter_masks
[] = {
6854 0xffff, /* not used */
6860 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
6862 Builder
bld(ctx
->program
, ctx
->block
);
6864 Temp sample_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
6865 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
6866 Temp ps_iter_mask
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(ps_iter_masks
[log2_ps_iter_samples
]));
6867 Temp mask
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), sample_id
, ps_iter_mask
);
6868 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
6869 bld
.vop2(aco_opcode::v_and_b32
, Definition(dst
), mask
, get_arg(ctx
, ctx
->args
->ac
.sample_coverage
));
6872 void visit_emit_vertex_with_counter(isel_context
*ctx
, nir_intrinsic_instr
*instr
) {
6873 Builder
bld(ctx
->program
, ctx
->block
);
6875 unsigned stream
= nir_intrinsic_stream_id(instr
);
6876 Temp next_vertex
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
6877 next_vertex
= bld
.v_mul_imm(bld
.def(v1
), next_vertex
, 4u);
6878 nir_const_value
*next_vertex_cv
= nir_src_as_const_value(instr
->src
[0]);
6881 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), ctx
->program
->private_segment_buffer
, Operand(RING_GSVS_GS
* 16u));
6883 unsigned num_components
=
6884 ctx
->program
->info
->gs
.num_stream_output_components
[stream
];
6885 assert(num_components
);
6887 unsigned stride
= 4u * num_components
* ctx
->shader
->info
.gs
.vertices_out
;
6888 unsigned stream_offset
= 0;
6889 for (unsigned i
= 0; i
< stream
; i
++) {
6890 unsigned prev_stride
= 4u * ctx
->program
->info
->gs
.num_stream_output_components
[i
] * ctx
->shader
->info
.gs
.vertices_out
;
6891 stream_offset
+= prev_stride
* ctx
->program
->wave_size
;
6894 /* Limit on the stride field for <= GFX7. */
6895 assert(stride
< (1 << 14));
6897 Temp gsvs_dwords
[4];
6898 for (unsigned i
= 0; i
< 4; i
++)
6899 gsvs_dwords
[i
] = bld
.tmp(s1
);
6900 bld
.pseudo(aco_opcode::p_split_vector
,
6901 Definition(gsvs_dwords
[0]),
6902 Definition(gsvs_dwords
[1]),
6903 Definition(gsvs_dwords
[2]),
6904 Definition(gsvs_dwords
[3]),
6907 if (stream_offset
) {
6908 Temp stream_offset_tmp
= bld
.copy(bld
.def(s1
), Operand(stream_offset
));
6910 Temp carry
= bld
.tmp(s1
);
6911 gsvs_dwords
[0] = bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.scc(Definition(carry
)), gsvs_dwords
[0], stream_offset_tmp
);
6912 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
));
6915 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
)));
6916 gsvs_dwords
[2] = bld
.copy(bld
.def(s1
), Operand((uint32_t)ctx
->program
->wave_size
));
6918 gsvs_ring
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
6919 gsvs_dwords
[0], gsvs_dwords
[1], gsvs_dwords
[2], gsvs_dwords
[3]);
6921 unsigned offset
= 0;
6922 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; i
++) {
6923 if (ctx
->program
->info
->gs
.output_streams
[i
] != stream
)
6926 for (unsigned j
= 0; j
< 4; j
++) {
6927 if (!(ctx
->program
->info
->gs
.output_usage_mask
[i
] & (1 << j
)))
6930 if (ctx
->outputs
.mask
[i
] & (1 << j
)) {
6931 Operand vaddr_offset
= next_vertex_cv
? Operand(v1
) : Operand(next_vertex
);
6932 unsigned const_offset
= (offset
+ (next_vertex_cv
? next_vertex_cv
->u32
: 0u)) * 4u;
6933 if (const_offset
>= 4096u) {
6934 if (vaddr_offset
.isUndefined())
6935 vaddr_offset
= bld
.copy(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u));
6937 vaddr_offset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), vaddr_offset
);
6938 const_offset
%= 4096u;
6941 aco_ptr
<MTBUF_instruction
> mtbuf
{create_instruction
<MTBUF_instruction
>(aco_opcode::tbuffer_store_format_x
, Format::MTBUF
, 4, 0)};
6942 mtbuf
->operands
[0] = Operand(gsvs_ring
);
6943 mtbuf
->operands
[1] = vaddr_offset
;
6944 mtbuf
->operands
[2] = Operand(get_arg(ctx
, ctx
->args
->gs2vs_offset
));
6945 mtbuf
->operands
[3] = Operand(ctx
->outputs
.temps
[i
* 4u + j
]);
6946 mtbuf
->offen
= !vaddr_offset
.isUndefined();
6947 mtbuf
->dfmt
= V_008F0C_BUF_DATA_FORMAT_32
;
6948 mtbuf
->nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
6949 mtbuf
->offset
= const_offset
;
6952 mtbuf
->barrier
= barrier_gs_data
;
6953 mtbuf
->can_reorder
= true;
6954 bld
.insert(std::move(mtbuf
));
6957 offset
+= ctx
->shader
->info
.gs
.vertices_out
;
6960 /* outputs for the next vertex are undefined and keeping them around can
6961 * create invalid IR with control flow */
6962 ctx
->outputs
.mask
[i
] = 0;
6965 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(false, true, stream
));
6968 Temp
emit_boolean_reduce(isel_context
*ctx
, nir_op op
, unsigned cluster_size
, Temp src
)
6970 Builder
bld(ctx
->program
, ctx
->block
);
6972 if (cluster_size
== 1) {
6974 } if (op
== nir_op_iand
&& cluster_size
== 4) {
6975 //subgroupClusteredAnd(val, 4) -> ~wqm(exec & ~val)
6976 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
6977 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
6978 bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
));
6979 } else if (op
== nir_op_ior
&& cluster_size
== 4) {
6980 //subgroupClusteredOr(val, 4) -> wqm(val & exec)
6981 return bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
6982 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)));
6983 } else if (op
== nir_op_iand
&& cluster_size
== ctx
->program
->wave_size
) {
6984 //subgroupAnd(val) -> (exec & ~val) == 0
6985 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
6986 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
6987 return bld
.sop1(Builder::s_not
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), cond
);
6988 } else if (op
== nir_op_ior
&& cluster_size
== ctx
->program
->wave_size
) {
6989 //subgroupOr(val) -> (val & exec) != 0
6990 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
)).def(1).getTemp();
6991 return bool_to_vector_condition(ctx
, tmp
);
6992 } else if (op
== nir_op_ixor
&& cluster_size
== ctx
->program
->wave_size
) {
6993 //subgroupXor(val) -> s_bcnt1_i32_b64(val & exec) & 1
6994 Temp tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
6995 tmp
= bld
.sop1(Builder::s_bcnt1_i32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
);
6996 tmp
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp
, Operand(1u)).def(1).getTemp();
6997 return bool_to_vector_condition(ctx
, tmp
);
6999 //subgroupClustered{And,Or,Xor}(val, n) ->
7000 //lane_id = v_mbcnt_hi_u32_b32(-1, v_mbcnt_lo_u32_b32(-1, 0)) ; just v_mbcnt_lo_u32_b32 on wave32
7001 //cluster_offset = ~(n - 1) & lane_id
7002 //cluster_mask = ((1 << n) - 1)
7003 //subgroupClusteredAnd():
7004 // return ((val | ~exec) >> cluster_offset) & cluster_mask == cluster_mask
7005 //subgroupClusteredOr():
7006 // return ((val & exec) >> cluster_offset) & cluster_mask != 0
7007 //subgroupClusteredXor():
7008 // return v_bnt_u32_b32(((val & exec) >> cluster_offset) & cluster_mask, 0) & 1 != 0
7009 Temp lane_id
= emit_mbcnt(ctx
, bld
.def(v1
));
7010 Temp cluster_offset
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(~uint32_t(cluster_size
- 1)), lane_id
);
7013 if (op
== nir_op_iand
)
7014 tmp
= bld
.sop2(Builder::s_orn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7016 tmp
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7018 uint32_t cluster_mask
= cluster_size
== 32 ? -1 : (1u << cluster_size
) - 1u;
7020 if (ctx
->program
->chip_class
<= GFX7
)
7021 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), tmp
, cluster_offset
);
7022 else if (ctx
->program
->wave_size
== 64)
7023 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), cluster_offset
, tmp
);
7025 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), cluster_offset
, tmp
);
7026 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
7027 if (cluster_mask
!= 0xffffffff)
7028 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(cluster_mask
), tmp
);
7030 Definition cmp_def
= Definition();
7031 if (op
== nir_op_iand
) {
7032 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(cluster_mask
), tmp
).def(0);
7033 } else if (op
== nir_op_ior
) {
7034 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
7035 } else if (op
== nir_op_ixor
) {
7036 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u),
7037 bld
.vop3(aco_opcode::v_bcnt_u32_b32
, bld
.def(v1
), tmp
, Operand(0u)));
7038 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
).def(0);
7040 cmp_def
.setHint(vcc
);
7041 return cmp_def
.getTemp();
7045 Temp
emit_boolean_exclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
7047 Builder
bld(ctx
->program
, ctx
->block
);
7049 //subgroupExclusiveAnd(val) -> mbcnt(exec & ~val) == 0
7050 //subgroupExclusiveOr(val) -> mbcnt(val & exec) != 0
7051 //subgroupExclusiveXor(val) -> mbcnt(val & exec) & 1 != 0
7053 if (op
== nir_op_iand
)
7054 tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
7056 tmp
= bld
.sop2(Builder::s_and
, bld
.def(s2
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7058 Builder::Result lohi
= bld
.pseudo(aco_opcode::p_split_vector
, bld
.def(s1
), bld
.def(s1
), tmp
);
7059 Temp lo
= lohi
.def(0).getTemp();
7060 Temp hi
= lohi
.def(1).getTemp();
7061 Temp mbcnt
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(lo
), Operand(hi
));
7063 Definition cmp_def
= Definition();
7064 if (op
== nir_op_iand
)
7065 cmp_def
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
7066 else if (op
== nir_op_ior
)
7067 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), mbcnt
).def(0);
7068 else if (op
== nir_op_ixor
)
7069 cmp_def
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u),
7070 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), mbcnt
)).def(0);
7071 cmp_def
.setHint(vcc
);
7072 return cmp_def
.getTemp();
7075 Temp
emit_boolean_inclusive_scan(isel_context
*ctx
, nir_op op
, Temp src
)
7077 Builder
bld(ctx
->program
, ctx
->block
);
7079 //subgroupInclusiveAnd(val) -> subgroupExclusiveAnd(val) && val
7080 //subgroupInclusiveOr(val) -> subgroupExclusiveOr(val) || val
7081 //subgroupInclusiveXor(val) -> subgroupExclusiveXor(val) ^^ val
7082 Temp tmp
= emit_boolean_exclusive_scan(ctx
, op
, src
);
7083 if (op
== nir_op_iand
)
7084 return bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7085 else if (op
== nir_op_ior
)
7086 return bld
.sop2(Builder::s_or
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7087 else if (op
== nir_op_ixor
)
7088 return bld
.sop2(Builder::s_xor
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), tmp
, src
);
7094 void emit_uniform_subgroup(isel_context
*ctx
, nir_intrinsic_instr
*instr
, Temp src
)
7096 Builder
bld(ctx
->program
, ctx
->block
);
7097 Definition
dst(get_ssa_temp(ctx
, &instr
->dest
.ssa
));
7098 if (src
.regClass().type() == RegType::vgpr
) {
7099 bld
.pseudo(aco_opcode::p_as_uniform
, dst
, src
);
7100 } else if (src
.regClass() == s1
) {
7101 bld
.sop1(aco_opcode::s_mov_b32
, dst
, src
);
7102 } else if (src
.regClass() == s2
) {
7103 bld
.sop1(aco_opcode::s_mov_b64
, dst
, src
);
7105 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7106 nir_print_instr(&instr
->instr
, stderr
);
7107 fprintf(stderr
, "\n");
7111 void emit_interp_center(isel_context
*ctx
, Temp dst
, Temp pos1
, Temp pos2
)
7113 Builder
bld(ctx
->program
, ctx
->block
);
7114 Temp persp_center
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
7115 Temp p1
= emit_extract_vector(ctx
, persp_center
, 0, v1
);
7116 Temp p2
= emit_extract_vector(ctx
, persp_center
, 1, v1
);
7118 Temp ddx_1
, ddx_2
, ddy_1
, ddy_2
;
7119 uint32_t dpp_ctrl0
= dpp_quad_perm(0, 0, 0, 0);
7120 uint32_t dpp_ctrl1
= dpp_quad_perm(1, 1, 1, 1);
7121 uint32_t dpp_ctrl2
= dpp_quad_perm(2, 2, 2, 2);
7124 if (ctx
->program
->chip_class
>= GFX8
) {
7125 Temp tl_1
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p1
, dpp_ctrl0
);
7126 ddx_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl1
);
7127 ddy_1
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p1
, tl_1
, dpp_ctrl2
);
7128 Temp tl_2
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), p2
, dpp_ctrl0
);
7129 ddx_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl1
);
7130 ddy_2
= bld
.vop2_dpp(aco_opcode::v_sub_f32
, bld
.def(v1
), p2
, tl_2
, dpp_ctrl2
);
7132 Temp tl_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl0
);
7133 ddx_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl1
);
7134 ddx_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_1
, tl_1
);
7135 ddx_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p1
, (1 << 15) | dpp_ctrl2
);
7136 ddx_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddx_2
, tl_1
);
7137 Temp tl_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl0
);
7138 ddy_1
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl1
);
7139 ddy_1
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_1
, tl_2
);
7140 ddy_2
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), p2
, (1 << 15) | dpp_ctrl2
);
7141 ddy_2
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), ddy_2
, tl_2
);
7144 /* res_k = p_k + ddx_k * pos1 + ddy_k * pos2 */
7145 Temp tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_1
, pos1
, p1
);
7146 Temp tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddx_2
, pos1
, p2
);
7147 tmp1
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_1
, pos2
, tmp1
);
7148 tmp2
= bld
.vop3(aco_opcode::v_mad_f32
, bld
.def(v1
), ddy_2
, pos2
, tmp2
);
7149 Temp wqm1
= bld
.tmp(v1
);
7150 emit_wqm(ctx
, tmp1
, wqm1
, true);
7151 Temp wqm2
= bld
.tmp(v1
);
7152 emit_wqm(ctx
, tmp2
, wqm2
, true);
7153 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), wqm1
, wqm2
);
7157 void visit_intrinsic(isel_context
*ctx
, nir_intrinsic_instr
*instr
)
7159 Builder
bld(ctx
->program
, ctx
->block
);
7160 switch(instr
->intrinsic
) {
7161 case nir_intrinsic_load_barycentric_sample
:
7162 case nir_intrinsic_load_barycentric_pixel
:
7163 case nir_intrinsic_load_barycentric_centroid
: {
7164 glsl_interp_mode mode
= (glsl_interp_mode
)nir_intrinsic_interp_mode(instr
);
7165 Temp bary
= Temp(0, s2
);
7167 case INTERP_MODE_SMOOTH
:
7168 case INTERP_MODE_NONE
:
7169 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
7170 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_center
);
7171 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
7172 bary
= ctx
->persp_centroid
;
7173 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
7174 bary
= get_arg(ctx
, ctx
->args
->ac
.persp_sample
);
7176 case INTERP_MODE_NOPERSPECTIVE
:
7177 if (instr
->intrinsic
== nir_intrinsic_load_barycentric_pixel
)
7178 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_center
);
7179 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_centroid
)
7180 bary
= ctx
->linear_centroid
;
7181 else if (instr
->intrinsic
== nir_intrinsic_load_barycentric_sample
)
7182 bary
= get_arg(ctx
, ctx
->args
->ac
.linear_sample
);
7187 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7188 Temp p1
= emit_extract_vector(ctx
, bary
, 0, v1
);
7189 Temp p2
= emit_extract_vector(ctx
, bary
, 1, v1
);
7190 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7191 Operand(p1
), Operand(p2
));
7192 emit_split_vector(ctx
, dst
, 2);
7195 case nir_intrinsic_load_barycentric_model
: {
7196 Temp model
= get_arg(ctx
, ctx
->args
->ac
.pull_model
);
7198 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7199 Temp p1
= emit_extract_vector(ctx
, model
, 0, v1
);
7200 Temp p2
= emit_extract_vector(ctx
, model
, 1, v1
);
7201 Temp p3
= emit_extract_vector(ctx
, model
, 2, v1
);
7202 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7203 Operand(p1
), Operand(p2
), Operand(p3
));
7204 emit_split_vector(ctx
, dst
, 3);
7207 case nir_intrinsic_load_barycentric_at_sample
: {
7208 uint32_t sample_pos_offset
= RING_PS_SAMPLE_POSITIONS
* 16;
7209 switch (ctx
->options
->key
.fs
.num_samples
) {
7210 case 2: sample_pos_offset
+= 1 << 3; break;
7211 case 4: sample_pos_offset
+= 3 << 3; break;
7212 case 8: sample_pos_offset
+= 7 << 3; break;
7216 Temp addr
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7217 nir_const_value
* const_addr
= nir_src_as_const_value(instr
->src
[0]);
7218 Temp private_segment_buffer
= ctx
->program
->private_segment_buffer
;
7219 if (addr
.type() == RegType::sgpr
) {
7222 sample_pos_offset
+= const_addr
->u32
<< 3;
7223 offset
= Operand(sample_pos_offset
);
7224 } else if (ctx
->options
->chip_class
>= GFX9
) {
7225 offset
= bld
.sop2(aco_opcode::s_lshl3_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
7227 offset
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(3u));
7228 offset
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), bld
.def(s1
, scc
), addr
, Operand(sample_pos_offset
));
7231 Operand off
= bld
.copy(bld
.def(s1
), Operand(offset
));
7232 sample_pos
= bld
.smem(aco_opcode::s_load_dwordx2
, bld
.def(s2
), private_segment_buffer
, off
);
7234 } else if (ctx
->options
->chip_class
>= GFX9
) {
7235 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7236 sample_pos
= bld
.global(aco_opcode::global_load_dwordx2
, bld
.def(v2
), addr
, private_segment_buffer
, sample_pos_offset
);
7237 } else if (ctx
->options
->chip_class
>= GFX7
) {
7238 /* addr += private_segment_buffer + sample_pos_offset */
7239 Temp tmp0
= bld
.tmp(s1
);
7240 Temp tmp1
= bld
.tmp(s1
);
7241 bld
.pseudo(aco_opcode::p_split_vector
, Definition(tmp0
), Definition(tmp1
), private_segment_buffer
);
7242 Definition scc_tmp
= bld
.def(s1
, scc
);
7243 tmp0
= bld
.sop2(aco_opcode::s_add_u32
, bld
.def(s1
), scc_tmp
, tmp0
, Operand(sample_pos_offset
));
7244 tmp1
= bld
.sop2(aco_opcode::s_addc_u32
, bld
.def(s1
), bld
.def(s1
, scc
), tmp1
, Operand(0u), bld
.scc(scc_tmp
.getTemp()));
7245 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7246 Temp pck0
= bld
.tmp(v1
);
7247 Temp carry
= bld
.vadd32(Definition(pck0
), tmp0
, addr
, true).def(1).getTemp();
7248 tmp1
= as_vgpr(ctx
, tmp1
);
7249 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
);
7250 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), pck0
, pck1
);
7252 /* sample_pos = flat_load_dwordx2 addr */
7253 sample_pos
= bld
.flat(aco_opcode::flat_load_dwordx2
, bld
.def(v2
), addr
, Operand(s1
));
7255 assert(ctx
->options
->chip_class
== GFX6
);
7257 uint32_t rsrc_conf
= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
7258 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
7259 Temp rsrc
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
), private_segment_buffer
, Operand(0u), Operand(rsrc_conf
));
7261 addr
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(3u), addr
);
7262 addr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), addr
, Operand(0u));
7264 sample_pos
= bld
.tmp(v2
);
7266 aco_ptr
<MUBUF_instruction
> load
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dwordx2
, Format::MUBUF
, 3, 1)};
7267 load
->definitions
[0] = Definition(sample_pos
);
7268 load
->operands
[0] = Operand(rsrc
);
7269 load
->operands
[1] = Operand(addr
);
7270 load
->operands
[2] = Operand(0u);
7271 load
->offset
= sample_pos_offset
;
7273 load
->addr64
= true;
7276 load
->disable_wqm
= false;
7277 load
->barrier
= barrier_none
;
7278 load
->can_reorder
= true;
7279 ctx
->block
->instructions
.emplace_back(std::move(load
));
7282 /* sample_pos -= 0.5 */
7283 Temp pos1
= bld
.tmp(RegClass(sample_pos
.type(), 1));
7284 Temp pos2
= bld
.tmp(RegClass(sample_pos
.type(), 1));
7285 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), sample_pos
);
7286 pos1
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos1
, Operand(0x3f000000u
));
7287 pos2
= bld
.vop2_e64(aco_opcode::v_sub_f32
, bld
.def(v1
), pos2
, Operand(0x3f000000u
));
7289 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
7292 case nir_intrinsic_load_barycentric_at_offset
: {
7293 Temp offset
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7294 RegClass rc
= RegClass(offset
.type(), 1);
7295 Temp pos1
= bld
.tmp(rc
), pos2
= bld
.tmp(rc
);
7296 bld
.pseudo(aco_opcode::p_split_vector
, Definition(pos1
), Definition(pos2
), offset
);
7297 emit_interp_center(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), pos1
, pos2
);
7300 case nir_intrinsic_load_front_face
: {
7301 bld
.vopc(aco_opcode::v_cmp_lg_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7302 Operand(0u), get_arg(ctx
, ctx
->args
->ac
.front_face
)).def(0).setHint(vcc
);
7305 case nir_intrinsic_load_view_index
: {
7306 if (ctx
->stage
& (sw_vs
| sw_gs
| sw_tcs
| sw_tes
)) {
7307 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7308 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.view_index
)));
7314 case nir_intrinsic_load_layer_id
: {
7315 unsigned idx
= nir_intrinsic_base(instr
);
7316 bld
.vintrp(aco_opcode::v_interp_mov_f32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7317 Operand(2u), bld
.m0(get_arg(ctx
, ctx
->args
->ac
.prim_mask
)), idx
, 0);
7320 case nir_intrinsic_load_frag_coord
: {
7321 emit_load_frag_coord(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 4);
7324 case nir_intrinsic_load_sample_pos
: {
7325 Temp posx
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[0]);
7326 Temp posy
= get_arg(ctx
, ctx
->args
->ac
.frag_pos
[1]);
7327 bld
.pseudo(aco_opcode::p_create_vector
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7328 posx
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posx
) : Operand(0u),
7329 posy
.id() ? bld
.vop1(aco_opcode::v_fract_f32
, bld
.def(v1
), posy
) : Operand(0u));
7332 case nir_intrinsic_load_tess_coord
:
7333 visit_load_tess_coord(ctx
, instr
);
7335 case nir_intrinsic_load_interpolated_input
:
7336 visit_load_interpolated_input(ctx
, instr
);
7338 case nir_intrinsic_store_output
:
7339 visit_store_output(ctx
, instr
);
7341 case nir_intrinsic_load_input
:
7342 case nir_intrinsic_load_input_vertex
:
7343 visit_load_input(ctx
, instr
);
7345 case nir_intrinsic_load_output
:
7346 visit_load_output(ctx
, instr
);
7348 case nir_intrinsic_load_per_vertex_input
:
7349 visit_load_per_vertex_input(ctx
, instr
);
7351 case nir_intrinsic_load_per_vertex_output
:
7352 visit_load_per_vertex_output(ctx
, instr
);
7354 case nir_intrinsic_store_per_vertex_output
:
7355 visit_store_per_vertex_output(ctx
, instr
);
7357 case nir_intrinsic_load_ubo
:
7358 visit_load_ubo(ctx
, instr
);
7360 case nir_intrinsic_load_push_constant
:
7361 visit_load_push_constant(ctx
, instr
);
7363 case nir_intrinsic_load_constant
:
7364 visit_load_constant(ctx
, instr
);
7366 case nir_intrinsic_vulkan_resource_index
:
7367 visit_load_resource(ctx
, instr
);
7369 case nir_intrinsic_discard
:
7370 visit_discard(ctx
, instr
);
7372 case nir_intrinsic_discard_if
:
7373 visit_discard_if(ctx
, instr
);
7375 case nir_intrinsic_load_shared
:
7376 visit_load_shared(ctx
, instr
);
7378 case nir_intrinsic_store_shared
:
7379 visit_store_shared(ctx
, instr
);
7381 case nir_intrinsic_shared_atomic_add
:
7382 case nir_intrinsic_shared_atomic_imin
:
7383 case nir_intrinsic_shared_atomic_umin
:
7384 case nir_intrinsic_shared_atomic_imax
:
7385 case nir_intrinsic_shared_atomic_umax
:
7386 case nir_intrinsic_shared_atomic_and
:
7387 case nir_intrinsic_shared_atomic_or
:
7388 case nir_intrinsic_shared_atomic_xor
:
7389 case nir_intrinsic_shared_atomic_exchange
:
7390 case nir_intrinsic_shared_atomic_comp_swap
:
7391 visit_shared_atomic(ctx
, instr
);
7393 case nir_intrinsic_image_deref_load
:
7394 visit_image_load(ctx
, instr
);
7396 case nir_intrinsic_image_deref_store
:
7397 visit_image_store(ctx
, instr
);
7399 case nir_intrinsic_image_deref_atomic_add
:
7400 case nir_intrinsic_image_deref_atomic_umin
:
7401 case nir_intrinsic_image_deref_atomic_imin
:
7402 case nir_intrinsic_image_deref_atomic_umax
:
7403 case nir_intrinsic_image_deref_atomic_imax
:
7404 case nir_intrinsic_image_deref_atomic_and
:
7405 case nir_intrinsic_image_deref_atomic_or
:
7406 case nir_intrinsic_image_deref_atomic_xor
:
7407 case nir_intrinsic_image_deref_atomic_exchange
:
7408 case nir_intrinsic_image_deref_atomic_comp_swap
:
7409 visit_image_atomic(ctx
, instr
);
7411 case nir_intrinsic_image_deref_size
:
7412 visit_image_size(ctx
, instr
);
7414 case nir_intrinsic_load_ssbo
:
7415 visit_load_ssbo(ctx
, instr
);
7417 case nir_intrinsic_store_ssbo
:
7418 visit_store_ssbo(ctx
, instr
);
7420 case nir_intrinsic_load_global
:
7421 visit_load_global(ctx
, instr
);
7423 case nir_intrinsic_store_global
:
7424 visit_store_global(ctx
, instr
);
7426 case nir_intrinsic_global_atomic_add
:
7427 case nir_intrinsic_global_atomic_imin
:
7428 case nir_intrinsic_global_atomic_umin
:
7429 case nir_intrinsic_global_atomic_imax
:
7430 case nir_intrinsic_global_atomic_umax
:
7431 case nir_intrinsic_global_atomic_and
:
7432 case nir_intrinsic_global_atomic_or
:
7433 case nir_intrinsic_global_atomic_xor
:
7434 case nir_intrinsic_global_atomic_exchange
:
7435 case nir_intrinsic_global_atomic_comp_swap
:
7436 visit_global_atomic(ctx
, instr
);
7438 case nir_intrinsic_ssbo_atomic_add
:
7439 case nir_intrinsic_ssbo_atomic_imin
:
7440 case nir_intrinsic_ssbo_atomic_umin
:
7441 case nir_intrinsic_ssbo_atomic_imax
:
7442 case nir_intrinsic_ssbo_atomic_umax
:
7443 case nir_intrinsic_ssbo_atomic_and
:
7444 case nir_intrinsic_ssbo_atomic_or
:
7445 case nir_intrinsic_ssbo_atomic_xor
:
7446 case nir_intrinsic_ssbo_atomic_exchange
:
7447 case nir_intrinsic_ssbo_atomic_comp_swap
:
7448 visit_atomic_ssbo(ctx
, instr
);
7450 case nir_intrinsic_load_scratch
:
7451 visit_load_scratch(ctx
, instr
);
7453 case nir_intrinsic_store_scratch
:
7454 visit_store_scratch(ctx
, instr
);
7456 case nir_intrinsic_get_buffer_size
:
7457 visit_get_buffer_size(ctx
, instr
);
7459 case nir_intrinsic_control_barrier
: {
7460 if (ctx
->program
->chip_class
== GFX6
&& ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
7461 /* GFX6 only (thanks to a hw bug workaround):
7462 * The real barrier instruction isn’t needed, because an entire patch
7463 * always fits into a single wave.
7468 if (ctx
->program
->workgroup_size
> ctx
->program
->wave_size
)
7469 bld
.sopp(aco_opcode::s_barrier
);
7473 case nir_intrinsic_memory_barrier_tcs_patch
:
7474 case nir_intrinsic_group_memory_barrier
:
7475 case nir_intrinsic_memory_barrier
:
7476 case nir_intrinsic_memory_barrier_buffer
:
7477 case nir_intrinsic_memory_barrier_image
:
7478 case nir_intrinsic_memory_barrier_shared
:
7479 emit_memory_barrier(ctx
, instr
);
7481 case nir_intrinsic_load_num_work_groups
: {
7482 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7483 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.num_work_groups
)));
7484 emit_split_vector(ctx
, dst
, 3);
7487 case nir_intrinsic_load_local_invocation_id
: {
7488 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7489 bld
.copy(Definition(dst
), Operand(get_arg(ctx
, ctx
->args
->ac
.local_invocation_ids
)));
7490 emit_split_vector(ctx
, dst
, 3);
7493 case nir_intrinsic_load_work_group_id
: {
7494 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7495 struct ac_arg
*args
= ctx
->args
->ac
.workgroup_ids
;
7496 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
),
7497 args
[0].used
? Operand(get_arg(ctx
, args
[0])) : Operand(0u),
7498 args
[1].used
? Operand(get_arg(ctx
, args
[1])) : Operand(0u),
7499 args
[2].used
? Operand(get_arg(ctx
, args
[2])) : Operand(0u));
7500 emit_split_vector(ctx
, dst
, 3);
7503 case nir_intrinsic_load_local_invocation_index
: {
7504 Temp id
= emit_mbcnt(ctx
, bld
.def(v1
));
7506 /* The tg_size bits [6:11] contain the subgroup id,
7507 * we need this multiplied by the wave size, and then OR the thread id to it.
7509 if (ctx
->program
->wave_size
== 64) {
7510 /* After the s_and the bits are already multiplied by 64 (left shifted by 6) so we can just feed that to v_or */
7511 Temp tg_num
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(0xfc0u
),
7512 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
7513 bld
.vop2(aco_opcode::v_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, id
);
7515 /* Extract the bit field and multiply the result by 32 (left shift by 5), then do the OR */
7516 Temp tg_num
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
7517 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
7518 bld
.vop3(aco_opcode::v_lshl_or_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), tg_num
, Operand(0x5u
), id
);
7522 case nir_intrinsic_load_subgroup_id
: {
7523 if (ctx
->stage
== compute_cs
) {
7524 bld
.sop2(aco_opcode::s_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
),
7525 get_arg(ctx
, ctx
->args
->ac
.tg_size
), Operand(0x6u
| (0x6u
<< 16)));
7527 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x0u
));
7531 case nir_intrinsic_load_subgroup_invocation
: {
7532 emit_mbcnt(ctx
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)));
7535 case nir_intrinsic_load_num_subgroups
: {
7536 if (ctx
->stage
== compute_cs
)
7537 bld
.sop2(aco_opcode::s_and_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), bld
.def(s1
, scc
), Operand(0x3fu
),
7538 get_arg(ctx
, ctx
->args
->ac
.tg_size
));
7540 bld
.sop1(aco_opcode::s_mov_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), Operand(0x1u
));
7543 case nir_intrinsic_ballot
: {
7544 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7545 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7546 Definition tmp
= bld
.def(dst
.regClass());
7547 Definition lanemask_tmp
= dst
.size() == bld
.lm
.size() ? tmp
: bld
.def(src
.regClass());
7548 if (instr
->src
[0].ssa
->bit_size
== 1) {
7549 assert(src
.regClass() == bld
.lm
);
7550 bld
.sop2(Builder::s_and
, lanemask_tmp
, bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
);
7551 } else if (instr
->src
[0].ssa
->bit_size
== 32 && src
.regClass() == v1
) {
7552 bld
.vopc(aco_opcode::v_cmp_lg_u32
, lanemask_tmp
, Operand(0u), src
);
7553 } else if (instr
->src
[0].ssa
->bit_size
== 64 && src
.regClass() == v2
) {
7554 bld
.vopc(aco_opcode::v_cmp_lg_u64
, lanemask_tmp
, Operand(0u), src
);
7556 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7557 nir_print_instr(&instr
->instr
, stderr
);
7558 fprintf(stderr
, "\n");
7560 if (dst
.size() != bld
.lm
.size()) {
7561 /* Wave32 with ballot size set to 64 */
7562 bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
), lanemask_tmp
.getTemp(), Operand(0u));
7564 emit_wqm(ctx
, tmp
.getTemp(), dst
);
7567 case nir_intrinsic_shuffle
:
7568 case nir_intrinsic_read_invocation
: {
7569 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7570 if (!nir_src_is_divergent(instr
->src
[0])) {
7571 emit_uniform_subgroup(ctx
, instr
, src
);
7573 Temp tid
= get_ssa_temp(ctx
, instr
->src
[1].ssa
);
7574 if (instr
->intrinsic
== nir_intrinsic_read_invocation
|| !nir_src_is_divergent(instr
->src
[1]))
7575 tid
= bld
.as_uniform(tid
);
7576 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7577 if (src
.regClass() == v1b
|| src
.regClass() == v2b
) {
7578 Temp tmp
= bld
.tmp(v1
);
7579 tmp
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), tmp
);
7580 if (dst
.type() == RegType::vgpr
)
7581 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(src
.regClass() == v1b
? v3b
: v2b
), tmp
);
7583 bld
.pseudo(aco_opcode::p_as_uniform
, Definition(dst
), tmp
);
7584 } else if (src
.regClass() == v1
) {
7585 emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, src
), dst
);
7586 } else if (src
.regClass() == v2
) {
7587 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7588 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7589 lo
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, lo
));
7590 hi
= emit_wqm(ctx
, emit_bpermute(ctx
, bld
, tid
, hi
));
7591 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7592 emit_split_vector(ctx
, dst
, 2);
7593 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == s1
) {
7594 assert(src
.regClass() == bld
.lm
);
7595 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
, tid
);
7596 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7597 } else if (instr
->dest
.ssa
.bit_size
== 1 && tid
.regClass() == v1
) {
7598 assert(src
.regClass() == bld
.lm
);
7600 if (ctx
->program
->chip_class
<= GFX7
)
7601 tmp
= bld
.vop3(aco_opcode::v_lshr_b64
, bld
.def(v2
), src
, tid
);
7602 else if (ctx
->program
->wave_size
== 64)
7603 tmp
= bld
.vop3(aco_opcode::v_lshrrev_b64
, bld
.def(v2
), tid
, src
);
7605 tmp
= bld
.vop2_e64(aco_opcode::v_lshrrev_b32
, bld
.def(v1
), tid
, src
);
7606 tmp
= emit_extract_vector(ctx
, tmp
, 0, v1
);
7607 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(1u), tmp
);
7608 emit_wqm(ctx
, bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), tmp
), dst
);
7610 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7611 nir_print_instr(&instr
->instr
, stderr
);
7612 fprintf(stderr
, "\n");
7617 case nir_intrinsic_load_sample_id
: {
7618 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
7619 get_arg(ctx
, ctx
->args
->ac
.ancillary
), Operand(8u), Operand(4u));
7622 case nir_intrinsic_load_sample_mask_in
: {
7623 visit_load_sample_mask_in(ctx
, instr
);
7626 case nir_intrinsic_read_first_invocation
: {
7627 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7628 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7629 if (src
.regClass() == v1b
|| src
.regClass() == v2b
|| src
.regClass() == v1
) {
7631 bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), src
),
7633 } else if (src
.regClass() == v2
) {
7634 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7635 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7636 lo
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), lo
));
7637 hi
= emit_wqm(ctx
, bld
.vop1(aco_opcode::v_readfirstlane_b32
, bld
.def(s1
), hi
));
7638 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7639 emit_split_vector(ctx
, dst
, 2);
7640 } else if (instr
->dest
.ssa
.bit_size
== 1) {
7641 assert(src
.regClass() == bld
.lm
);
7642 Temp tmp
= bld
.sopc(Builder::s_bitcmp1
, bld
.def(s1
, scc
), src
,
7643 bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)));
7644 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7645 } else if (src
.regClass() == s1
) {
7646 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), src
);
7647 } else if (src
.regClass() == s2
) {
7648 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), src
);
7650 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7651 nir_print_instr(&instr
->instr
, stderr
);
7652 fprintf(stderr
, "\n");
7656 case nir_intrinsic_vote_all
: {
7657 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7658 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7659 assert(src
.regClass() == bld
.lm
);
7660 assert(dst
.regClass() == bld
.lm
);
7662 Temp tmp
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), Operand(exec
, bld
.lm
), src
).def(1).getTemp();
7663 Temp cond
= bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
));
7664 bld
.sop1(Builder::s_not
, Definition(dst
), bld
.def(s1
, scc
), cond
);
7667 case nir_intrinsic_vote_any
: {
7668 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7669 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7670 assert(src
.regClass() == bld
.lm
);
7671 assert(dst
.regClass() == bld
.lm
);
7673 Temp tmp
= bool_to_scalar_condition(ctx
, src
);
7674 bool_to_vector_condition(ctx
, emit_wqm(ctx
, tmp
), dst
);
7677 case nir_intrinsic_reduce
:
7678 case nir_intrinsic_inclusive_scan
:
7679 case nir_intrinsic_exclusive_scan
: {
7680 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7681 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7682 nir_op op
= (nir_op
) nir_intrinsic_reduction_op(instr
);
7683 unsigned cluster_size
= instr
->intrinsic
== nir_intrinsic_reduce
?
7684 nir_intrinsic_cluster_size(instr
) : 0;
7685 cluster_size
= util_next_power_of_two(MIN2(cluster_size
? cluster_size
: ctx
->program
->wave_size
, ctx
->program
->wave_size
));
7687 if (!nir_src_is_divergent(instr
->src
[0]) && (op
== nir_op_ior
|| op
== nir_op_iand
)) {
7688 emit_uniform_subgroup(ctx
, instr
, src
);
7689 } else if (instr
->dest
.ssa
.bit_size
== 1) {
7690 if (op
== nir_op_imul
|| op
== nir_op_umin
|| op
== nir_op_imin
)
7692 else if (op
== nir_op_iadd
)
7694 else if (op
== nir_op_umax
|| op
== nir_op_imax
)
7696 assert(op
== nir_op_iand
|| op
== nir_op_ior
|| op
== nir_op_ixor
);
7698 switch (instr
->intrinsic
) {
7699 case nir_intrinsic_reduce
:
7700 emit_wqm(ctx
, emit_boolean_reduce(ctx
, op
, cluster_size
, src
), dst
);
7702 case nir_intrinsic_exclusive_scan
:
7703 emit_wqm(ctx
, emit_boolean_exclusive_scan(ctx
, op
, src
), dst
);
7705 case nir_intrinsic_inclusive_scan
:
7706 emit_wqm(ctx
, emit_boolean_inclusive_scan(ctx
, op
, src
), dst
);
7711 } else if (cluster_size
== 1) {
7712 bld
.copy(Definition(dst
), src
);
7714 unsigned bit_size
= instr
->src
[0].ssa
->bit_size
;
7716 src
= emit_extract_vector(ctx
, src
, 0, RegClass::get(RegType::vgpr
, bit_size
/ 8));
7720 #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;
7721 #define CASEF(name) case nir_op_##name: reduce_op = (bit_size == 32) ? name##32 : (bit_size == 16) ? name##16 : name##64; break;
7736 unreachable("unknown reduction op");
7742 switch (instr
->intrinsic
) {
7743 case nir_intrinsic_reduce
: aco_op
= aco_opcode::p_reduce
; break;
7744 case nir_intrinsic_inclusive_scan
: aco_op
= aco_opcode::p_inclusive_scan
; break;
7745 case nir_intrinsic_exclusive_scan
: aco_op
= aco_opcode::p_exclusive_scan
; break;
7747 unreachable("unknown reduce intrinsic");
7750 aco_ptr
<Pseudo_reduction_instruction
> reduce
{create_instruction
<Pseudo_reduction_instruction
>(aco_op
, Format::PSEUDO_REDUCTION
, 3, 5)};
7751 reduce
->operands
[0] = Operand(src
);
7752 // filled in by aco_reduce_assign.cpp, used internally as part of the
7754 assert(dst
.size() == 1 || dst
.size() == 2);
7755 reduce
->operands
[1] = Operand(RegClass(RegType::vgpr
, dst
.size()).as_linear());
7756 reduce
->operands
[2] = Operand(v1
.as_linear());
7758 Temp tmp_dst
= bld
.tmp(dst
.regClass());
7759 reduce
->definitions
[0] = Definition(tmp_dst
);
7760 reduce
->definitions
[1] = bld
.def(ctx
->program
->lane_mask
); // used internally
7761 reduce
->definitions
[2] = Definition();
7762 reduce
->definitions
[3] = Definition(scc
, s1
);
7763 reduce
->definitions
[4] = Definition();
7764 reduce
->reduce_op
= reduce_op
;
7765 reduce
->cluster_size
= cluster_size
;
7766 ctx
->block
->instructions
.emplace_back(std::move(reduce
));
7768 emit_wqm(ctx
, tmp_dst
, dst
);
7772 case nir_intrinsic_quad_broadcast
: {
7773 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7774 if (!nir_dest_is_divergent(instr
->dest
)) {
7775 emit_uniform_subgroup(ctx
, instr
, src
);
7777 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7778 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
;
7779 uint32_t dpp_ctrl
= dpp_quad_perm(lane
, lane
, lane
, lane
);
7781 if (instr
->dest
.ssa
.bit_size
== 1) {
7782 assert(src
.regClass() == bld
.lm
);
7783 assert(dst
.regClass() == bld
.lm
);
7784 uint32_t half_mask
= 0x11111111u
<< lane
;
7785 Temp mask_tmp
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s2
), Operand(half_mask
), Operand(half_mask
));
7786 Temp tmp
= bld
.tmp(bld
.lm
);
7787 bld
.sop1(Builder::s_wqm
, Definition(tmp
),
7788 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), mask_tmp
,
7789 bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
))));
7790 emit_wqm(ctx
, tmp
, dst
);
7791 } else if (instr
->dest
.ssa
.bit_size
== 8) {
7792 Temp tmp
= bld
.tmp(v1
);
7793 if (ctx
->program
->chip_class
>= GFX8
)
7794 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7796 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), tmp
);
7797 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v3b
), tmp
);
7798 } else if (instr
->dest
.ssa
.bit_size
== 16) {
7799 Temp tmp
= bld
.tmp(v1
);
7800 if (ctx
->program
->chip_class
>= GFX8
)
7801 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7803 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), tmp
);
7804 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v2b
), tmp
);
7805 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7806 if (ctx
->program
->chip_class
>= GFX8
)
7807 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), dst
);
7809 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, (1 << 15) | dpp_ctrl
), dst
);
7810 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7811 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7812 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7813 if (ctx
->program
->chip_class
>= GFX8
) {
7814 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7815 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7817 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, (1 << 15) | dpp_ctrl
));
7818 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, (1 << 15) | dpp_ctrl
));
7820 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7821 emit_split_vector(ctx
, dst
, 2);
7823 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7824 nir_print_instr(&instr
->instr
, stderr
);
7825 fprintf(stderr
, "\n");
7830 case nir_intrinsic_quad_swap_horizontal
:
7831 case nir_intrinsic_quad_swap_vertical
:
7832 case nir_intrinsic_quad_swap_diagonal
:
7833 case nir_intrinsic_quad_swizzle_amd
: {
7834 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7835 if (!nir_dest_is_divergent(instr
->dest
)) {
7836 emit_uniform_subgroup(ctx
, instr
, src
);
7839 uint16_t dpp_ctrl
= 0;
7840 switch (instr
->intrinsic
) {
7841 case nir_intrinsic_quad_swap_horizontal
:
7842 dpp_ctrl
= dpp_quad_perm(1, 0, 3, 2);
7844 case nir_intrinsic_quad_swap_vertical
:
7845 dpp_ctrl
= dpp_quad_perm(2, 3, 0, 1);
7847 case nir_intrinsic_quad_swap_diagonal
:
7848 dpp_ctrl
= dpp_quad_perm(3, 2, 1, 0);
7850 case nir_intrinsic_quad_swizzle_amd
:
7851 dpp_ctrl
= nir_intrinsic_swizzle_mask(instr
);
7856 if (ctx
->program
->chip_class
< GFX8
)
7857 dpp_ctrl
|= (1 << 15);
7859 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7860 if (instr
->dest
.ssa
.bit_size
== 1) {
7861 assert(src
.regClass() == bld
.lm
);
7862 src
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), Operand(0u), Operand((uint32_t)-1), src
);
7863 if (ctx
->program
->chip_class
>= GFX8
)
7864 src
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7866 src
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7867 Temp tmp
= bld
.vopc(aco_opcode::v_cmp_lg_u32
, bld
.def(bld
.lm
), Operand(0u), src
);
7868 emit_wqm(ctx
, tmp
, dst
);
7869 } else if (instr
->dest
.ssa
.bit_size
== 8) {
7870 Temp tmp
= bld
.tmp(v1
);
7871 if (ctx
->program
->chip_class
>= GFX8
)
7872 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7874 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7875 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v3b
), tmp
);
7876 } else if (instr
->dest
.ssa
.bit_size
== 16) {
7877 Temp tmp
= bld
.tmp(v1
);
7878 if (ctx
->program
->chip_class
>= GFX8
)
7879 emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7881 emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
), tmp
);
7882 bld
.pseudo(aco_opcode::p_split_vector
, Definition(dst
), bld
.def(v2b
), tmp
);
7883 } else if (instr
->dest
.ssa
.bit_size
== 32) {
7885 if (ctx
->program
->chip_class
>= GFX8
)
7886 tmp
= bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7888 tmp
= bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, dpp_ctrl
);
7889 emit_wqm(ctx
, tmp
, dst
);
7890 } else if (instr
->dest
.ssa
.bit_size
== 64) {
7891 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7892 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7893 if (ctx
->program
->chip_class
>= GFX8
) {
7894 lo
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7895 hi
= emit_wqm(ctx
, bld
.vop1_dpp(aco_opcode::v_mov_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7897 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, dpp_ctrl
));
7898 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, dpp_ctrl
));
7900 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7901 emit_split_vector(ctx
, dst
, 2);
7903 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7904 nir_print_instr(&instr
->instr
, stderr
);
7905 fprintf(stderr
, "\n");
7909 case nir_intrinsic_masked_swizzle_amd
: {
7910 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7911 if (!nir_dest_is_divergent(instr
->dest
)) {
7912 emit_uniform_subgroup(ctx
, instr
, src
);
7915 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7916 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
7917 if (dst
.regClass() == v1
) {
7919 bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), src
, mask
, 0, false),
7921 } else if (dst
.regClass() == v2
) {
7922 Temp lo
= bld
.tmp(v1
), hi
= bld
.tmp(v1
);
7923 bld
.pseudo(aco_opcode::p_split_vector
, Definition(lo
), Definition(hi
), src
);
7924 lo
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), lo
, mask
, 0, false));
7925 hi
= emit_wqm(ctx
, bld
.ds(aco_opcode::ds_swizzle_b32
, bld
.def(v1
), hi
, mask
, 0, false));
7926 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7927 emit_split_vector(ctx
, dst
, 2);
7929 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7930 nir_print_instr(&instr
->instr
, stderr
);
7931 fprintf(stderr
, "\n");
7935 case nir_intrinsic_write_invocation_amd
: {
7936 Temp src
= as_vgpr(ctx
, get_ssa_temp(ctx
, instr
->src
[0].ssa
));
7937 Temp val
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[1].ssa
));
7938 Temp lane
= bld
.as_uniform(get_ssa_temp(ctx
, instr
->src
[2].ssa
));
7939 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7940 if (dst
.regClass() == v1
) {
7941 /* src2 is ignored for writelane. RA assigns the same reg for dst */
7942 emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val
, lane
, src
), dst
);
7943 } else if (dst
.regClass() == v2
) {
7944 Temp src_lo
= bld
.tmp(v1
), src_hi
= bld
.tmp(v1
);
7945 Temp val_lo
= bld
.tmp(s1
), val_hi
= bld
.tmp(s1
);
7946 bld
.pseudo(aco_opcode::p_split_vector
, Definition(src_lo
), Definition(src_hi
), src
);
7947 bld
.pseudo(aco_opcode::p_split_vector
, Definition(val_lo
), Definition(val_hi
), val
);
7948 Temp lo
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_lo
, lane
, src_hi
));
7949 Temp hi
= emit_wqm(ctx
, bld
.writelane(bld
.def(v1
), val_hi
, lane
, src_hi
));
7950 bld
.pseudo(aco_opcode::p_create_vector
, Definition(dst
), lo
, hi
);
7951 emit_split_vector(ctx
, dst
, 2);
7953 fprintf(stderr
, "Unimplemented NIR instr bit size: ");
7954 nir_print_instr(&instr
->instr
, stderr
);
7955 fprintf(stderr
, "\n");
7959 case nir_intrinsic_mbcnt_amd
: {
7960 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7961 RegClass rc
= RegClass(src
.type(), 1);
7962 Temp mask_lo
= bld
.tmp(rc
), mask_hi
= bld
.tmp(rc
);
7963 bld
.pseudo(aco_opcode::p_split_vector
, Definition(mask_lo
), Definition(mask_hi
), src
);
7964 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7965 Temp wqm_tmp
= emit_mbcnt(ctx
, bld
.def(v1
), Operand(mask_lo
), Operand(mask_hi
));
7966 emit_wqm(ctx
, wqm_tmp
, dst
);
7969 case nir_intrinsic_load_helper_invocation
: {
7970 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7971 bld
.pseudo(aco_opcode::p_load_helper
, Definition(dst
));
7972 ctx
->block
->kind
|= block_kind_needs_lowering
;
7973 ctx
->program
->needs_exact
= true;
7976 case nir_intrinsic_is_helper_invocation
: {
7977 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
7978 bld
.pseudo(aco_opcode::p_is_helper
, Definition(dst
));
7979 ctx
->block
->kind
|= block_kind_needs_lowering
;
7980 ctx
->program
->needs_exact
= true;
7983 case nir_intrinsic_demote
:
7984 bld
.pseudo(aco_opcode::p_demote_to_helper
, Operand(-1u));
7986 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
7987 ctx
->cf_info
.exec_potentially_empty_discard
= true;
7988 ctx
->block
->kind
|= block_kind_uses_demote
;
7989 ctx
->program
->needs_exact
= true;
7991 case nir_intrinsic_demote_if
: {
7992 Temp src
= get_ssa_temp(ctx
, instr
->src
[0].ssa
);
7993 assert(src
.regClass() == bld
.lm
);
7994 Temp cond
= bld
.sop2(Builder::s_and
, bld
.def(bld
.lm
), bld
.def(s1
, scc
), src
, Operand(exec
, bld
.lm
));
7995 bld
.pseudo(aco_opcode::p_demote_to_helper
, cond
);
7997 if (ctx
->cf_info
.loop_nest_depth
|| ctx
->cf_info
.parent_if
.is_divergent
)
7998 ctx
->cf_info
.exec_potentially_empty_discard
= true;
7999 ctx
->block
->kind
|= block_kind_uses_demote
;
8000 ctx
->program
->needs_exact
= true;
8003 case nir_intrinsic_first_invocation
: {
8004 emit_wqm(ctx
, bld
.sop1(Builder::s_ff1_i32
, bld
.def(s1
), Operand(exec
, bld
.lm
)),
8005 get_ssa_temp(ctx
, &instr
->dest
.ssa
));
8008 case nir_intrinsic_shader_clock
: {
8010 nir_intrinsic_memory_scope(instr
) == NIR_SCOPE_DEVICE
?
8011 aco_opcode::s_memrealtime
: aco_opcode::s_memtime
;
8012 bld
.smem(opcode
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)), false);
8013 emit_split_vector(ctx
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), 2);
8016 case nir_intrinsic_load_vertex_id_zero_base
: {
8017 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8018 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.vertex_id
));
8021 case nir_intrinsic_load_first_vertex
: {
8022 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8023 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.base_vertex
));
8026 case nir_intrinsic_load_base_instance
: {
8027 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8028 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.start_instance
));
8031 case nir_intrinsic_load_instance_id
: {
8032 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8033 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.instance_id
));
8036 case nir_intrinsic_load_draw_id
: {
8037 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8038 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.draw_id
));
8041 case nir_intrinsic_load_invocation_id
: {
8042 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8044 if (ctx
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
) {
8045 if (ctx
->options
->chip_class
>= GFX10
)
8046 bld
.vop2_e64(aco_opcode::v_and_b32
, Definition(dst
), Operand(127u), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
8048 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
));
8049 } else if (ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
8050 bld
.vop3(aco_opcode::v_bfe_u32
, Definition(dst
),
8051 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
), Operand(8u), Operand(5u));
8053 unreachable("Unsupported stage for load_invocation_id");
8058 case nir_intrinsic_load_primitive_id
: {
8059 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8061 switch (ctx
->shader
->info
.stage
) {
8062 case MESA_SHADER_GEOMETRY
:
8063 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
));
8065 case MESA_SHADER_TESS_CTRL
:
8066 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tcs_patch_id
));
8068 case MESA_SHADER_TESS_EVAL
:
8069 bld
.copy(Definition(dst
), get_arg(ctx
, ctx
->args
->ac
.tes_patch_id
));
8072 unreachable("Unimplemented shader stage for nir_intrinsic_load_primitive_id");
8077 case nir_intrinsic_load_patch_vertices_in
: {
8078 assert(ctx
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
8079 ctx
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
8081 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8082 bld
.copy(Definition(dst
), Operand(ctx
->args
->options
->key
.tcs
.input_vertices
));
8085 case nir_intrinsic_emit_vertex_with_counter
: {
8086 visit_emit_vertex_with_counter(ctx
, instr
);
8089 case nir_intrinsic_end_primitive_with_counter
: {
8090 unsigned stream
= nir_intrinsic_stream_id(instr
);
8091 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
->gs_wave_id
), -1, sendmsg_gs(true, false, stream
));
8094 case nir_intrinsic_set_vertex_count
: {
8095 /* unused, the HW keeps track of this for us */
8099 fprintf(stderr
, "Unimplemented intrinsic instr: ");
8100 nir_print_instr(&instr
->instr
, stderr
);
8101 fprintf(stderr
, "\n");
8109 void tex_fetch_ptrs(isel_context
*ctx
, nir_tex_instr
*instr
,
8110 Temp
*res_ptr
, Temp
*samp_ptr
, Temp
*fmask_ptr
,
8111 enum glsl_base_type
*stype
)
8113 nir_deref_instr
*texture_deref_instr
= NULL
;
8114 nir_deref_instr
*sampler_deref_instr
= NULL
;
8117 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
8118 switch (instr
->src
[i
].src_type
) {
8119 case nir_tex_src_texture_deref
:
8120 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
8122 case nir_tex_src_sampler_deref
:
8123 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
8125 case nir_tex_src_plane
:
8126 plane
= nir_src_as_int(instr
->src
[i
].src
);
8133 *stype
= glsl_get_sampler_result_type(texture_deref_instr
->type
);
8135 if (!sampler_deref_instr
)
8136 sampler_deref_instr
= texture_deref_instr
;
8139 assert(instr
->op
!= nir_texop_txf_ms
&&
8140 instr
->op
!= nir_texop_samples_identical
);
8141 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
8142 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, (aco_descriptor_type
)(ACO_DESC_PLANE_0
+ plane
), instr
, false, false);
8143 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
8144 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_BUFFER
, instr
, false, false);
8145 } else if (instr
->op
== nir_texop_fragment_mask_fetch
) {
8146 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
8148 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_IMAGE
, instr
, false, false);
8151 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, ACO_DESC_SAMPLER
, instr
, false, false);
8153 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
&& ctx
->options
->chip_class
< GFX8
) {
8154 /* fix sampler aniso on SI/CI: samp[0] = samp[0] & img[7] */
8155 Builder
bld(ctx
->program
, ctx
->block
);
8157 /* to avoid unnecessary moves, we split and recombine sampler and image */
8158 Temp img
[8] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
),
8159 bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
8160 Temp samp
[4] = {bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
), bld
.tmp(s1
)};
8161 bld
.pseudo(aco_opcode::p_split_vector
, Definition(img
[0]), Definition(img
[1]),
8162 Definition(img
[2]), Definition(img
[3]), Definition(img
[4]),
8163 Definition(img
[5]), Definition(img
[6]), Definition(img
[7]), *res_ptr
);
8164 bld
.pseudo(aco_opcode::p_split_vector
, Definition(samp
[0]), Definition(samp
[1]),
8165 Definition(samp
[2]), Definition(samp
[3]), *samp_ptr
);
8167 samp
[0] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), samp
[0], img
[7]);
8168 *res_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s8
),
8169 img
[0], img
[1], img
[2], img
[3],
8170 img
[4], img
[5], img
[6], img
[7]);
8171 *samp_ptr
= bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(s4
),
8172 samp
[0], samp
[1], samp
[2], samp
[3]);
8175 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
8176 instr
->op
== nir_texop_samples_identical
))
8177 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, ACO_DESC_FMASK
, instr
, false, false);
8180 void build_cube_select(isel_context
*ctx
, Temp ma
, Temp id
, Temp deriv
,
8181 Temp
*out_ma
, Temp
*out_sc
, Temp
*out_tc
)
8183 Builder
bld(ctx
->program
, ctx
->block
);
8185 Temp deriv_x
= emit_extract_vector(ctx
, deriv
, 0, v1
);
8186 Temp deriv_y
= emit_extract_vector(ctx
, deriv
, 1, v1
);
8187 Temp deriv_z
= emit_extract_vector(ctx
, deriv
, 2, v1
);
8189 Operand
neg_one(0xbf800000u
);
8190 Operand
one(0x3f800000u
);
8191 Operand
two(0x40000000u
);
8192 Operand
four(0x40800000u
);
8194 Temp is_ma_positive
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), ma
);
8195 Temp sgn_ma
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, one
, is_ma_positive
);
8196 Temp neg_sgn_ma
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
), Operand(0u), sgn_ma
);
8198 Temp is_ma_z
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.hint_vcc(bld
.def(bld
.lm
)), four
, id
);
8199 Temp is_ma_y
= bld
.vopc(aco_opcode::v_cmp_le_f32
, bld
.def(bld
.lm
), two
, id
);
8200 is_ma_y
= bld
.sop2(Builder::s_andn2
, bld
.hint_vcc(bld
.def(bld
.lm
)), is_ma_y
, is_ma_z
);
8201 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
);
8204 Temp tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_z
, deriv_x
, is_not_ma_x
);
8205 Temp sgn
= bld
.vop2_e64(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8206 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_sgn_ma
, sgn_ma
, is_ma_z
),
8208 *out_sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
8211 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_y
, deriv_z
, is_ma_y
);
8212 sgn
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), neg_one
, sgn_ma
, is_ma_y
);
8213 *out_tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tmp
, sgn
);
8216 tmp
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8217 bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), deriv_x
, deriv_y
, is_ma_y
),
8219 tmp
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x7fffffffu
), tmp
);
8220 *out_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), two
, tmp
);
8223 void prepare_cube_coords(isel_context
*ctx
, std::vector
<Temp
>& coords
, Temp
* ddx
, Temp
* ddy
, bool is_deriv
, bool is_array
)
8225 Builder
bld(ctx
->program
, ctx
->block
);
8226 Temp ma
, tc
, sc
, id
;
8229 coords
[3] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[3]);
8231 // see comment in ac_prepare_cube_coords()
8232 if (ctx
->options
->chip_class
<= GFX8
)
8233 coords
[3] = bld
.vop2(aco_opcode::v_max_f32
, bld
.def(v1
), Operand(0u), coords
[3]);
8236 ma
= bld
.vop3(aco_opcode::v_cubema_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8238 aco_ptr
<VOP3A_instruction
> vop3a
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_rcp_f32
, asVOP3(Format::VOP1
), 1, 1)};
8239 vop3a
->operands
[0] = Operand(ma
);
8240 vop3a
->abs
[0] = true;
8241 Temp invma
= bld
.tmp(v1
);
8242 vop3a
->definitions
[0] = Definition(invma
);
8243 ctx
->block
->instructions
.emplace_back(std::move(vop3a
));
8245 sc
= bld
.vop3(aco_opcode::v_cubesc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8247 sc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), sc
, invma
, Operand(0x3fc00000u
/*1.5*/));
8249 tc
= bld
.vop3(aco_opcode::v_cubetc_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8251 tc
= bld
.vop2(aco_opcode::v_madak_f32
, bld
.def(v1
), tc
, invma
, Operand(0x3fc00000u
/*1.5*/));
8253 id
= bld
.vop3(aco_opcode::v_cubeid_f32
, bld
.def(v1
), coords
[0], coords
[1], coords
[2]);
8256 sc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), sc
, invma
);
8257 tc
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), tc
, invma
);
8259 for (unsigned i
= 0; i
< 2; i
++) {
8260 // see comment in ac_prepare_cube_coords()
8262 Temp deriv_sc
, deriv_tc
;
8263 build_cube_select(ctx
, ma
, id
, i
? *ddy
: *ddx
,
8264 &deriv_ma
, &deriv_sc
, &deriv_tc
);
8266 deriv_ma
= bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, invma
);
8268 Temp x
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
8269 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_sc
, invma
),
8270 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, sc
));
8271 Temp y
= bld
.vop2(aco_opcode::v_sub_f32
, bld
.def(v1
),
8272 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_tc
, invma
),
8273 bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), deriv_ma
, tc
));
8274 *(i
? ddy
: ddx
) = bld
.pseudo(aco_opcode::p_create_vector
, bld
.def(v2
), x
, y
);
8277 sc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), sc
);
8278 tc
= bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), Operand(0x3fc00000u
/*1.5*/), tc
);
8282 id
= bld
.vop2(aco_opcode::v_madmk_f32
, bld
.def(v1
), coords
[3], id
, Operand(0x41000000u
/*8.0*/));
8289 void get_const_vec(nir_ssa_def
*vec
, nir_const_value
*cv
[4])
8291 if (vec
->parent_instr
->type
!= nir_instr_type_alu
)
8293 nir_alu_instr
*vec_instr
= nir_instr_as_alu(vec
->parent_instr
);
8294 if (vec_instr
->op
!= nir_op_vec(vec
->num_components
))
8297 for (unsigned i
= 0; i
< vec
->num_components
; i
++) {
8298 cv
[i
] = vec_instr
->src
[i
].swizzle
[0] == 0 ?
8299 nir_src_as_const_value(vec_instr
->src
[i
].src
) : NULL
;
8303 void visit_tex(isel_context
*ctx
, nir_tex_instr
*instr
)
8305 Builder
bld(ctx
->program
, ctx
->block
);
8306 bool has_bias
= false, has_lod
= false, level_zero
= false, has_compare
= false,
8307 has_offset
= false, has_ddx
= false, has_ddy
= false, has_derivs
= false, has_sample_index
= false,
8308 has_clamped_lod
= false;
8309 Temp resource
, sampler
, fmask_ptr
, bias
= Temp(), compare
= Temp(), sample_index
= Temp(),
8310 lod
= Temp(), offset
= Temp(), ddx
= Temp(), ddy
= Temp(),
8311 clamped_lod
= Temp();
8312 std::vector
<Temp
> coords
;
8313 std::vector
<Temp
> derivs
;
8314 nir_const_value
*sample_index_cv
= NULL
;
8315 nir_const_value
*const_offset
[4] = {NULL
, NULL
, NULL
, NULL
};
8316 enum glsl_base_type stype
;
8317 tex_fetch_ptrs(ctx
, instr
, &resource
, &sampler
, &fmask_ptr
, &stype
);
8319 bool tg4_integer_workarounds
= ctx
->options
->chip_class
<= GFX8
&& instr
->op
== nir_texop_tg4
&&
8320 (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
);
8321 bool tg4_integer_cube_workaround
= tg4_integer_workarounds
&&
8322 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
8324 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
8325 switch (instr
->src
[i
].src_type
) {
8326 case nir_tex_src_coord
: {
8327 Temp coord
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8328 for (unsigned i
= 0; i
< coord
.size(); i
++)
8329 coords
.emplace_back(emit_extract_vector(ctx
, coord
, i
, v1
));
8332 case nir_tex_src_bias
:
8333 bias
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8336 case nir_tex_src_lod
: {
8337 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
8339 if (val
&& val
->f32
<= 0.0) {
8342 lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8347 case nir_tex_src_min_lod
:
8348 clamped_lod
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8349 has_clamped_lod
= true;
8351 case nir_tex_src_comparator
:
8352 if (instr
->is_shadow
) {
8353 compare
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8357 case nir_tex_src_offset
:
8358 offset
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8359 get_const_vec(instr
->src
[i
].src
.ssa
, const_offset
);
8362 case nir_tex_src_ddx
:
8363 ddx
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8366 case nir_tex_src_ddy
:
8367 ddy
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8370 case nir_tex_src_ms_index
:
8371 sample_index
= get_ssa_temp(ctx
, instr
->src
[i
].src
.ssa
);
8372 sample_index_cv
= nir_src_as_const_value(instr
->src
[i
].src
);
8373 has_sample_index
= true;
8375 case nir_tex_src_texture_offset
:
8376 case nir_tex_src_sampler_offset
:
8382 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
8383 return get_buffer_size(ctx
, resource
, get_ssa_temp(ctx
, &instr
->dest
.ssa
), true);
8385 if (instr
->op
== nir_texop_texture_samples
) {
8386 Temp dword3
= emit_extract_vector(ctx
, resource
, 3, s1
);
8388 Temp samples_log2
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), dword3
, Operand(16u | 4u<<16));
8389 Temp samples
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(1u), samples_log2
);
8390 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 */));
8392 Operand default_sample
= Operand(1u);
8393 if (ctx
->options
->robust_buffer_access
) {
8394 /* Extract the second dword of the descriptor, if it's
8395 * all zero, then it's a null descriptor.
8397 Temp dword1
= emit_extract_vector(ctx
, resource
, 1, s1
);
8398 Temp is_non_null_descriptor
= bld
.sopc(aco_opcode::s_cmp_gt_u32
, bld
.def(s1
, scc
), dword1
, Operand(0u));
8399 default_sample
= Operand(is_non_null_descriptor
);
8402 Temp is_msaa
= bld
.sopc(aco_opcode::s_cmp_ge_u32
, bld
.def(s1
, scc
), type
, Operand(14u));
8403 bld
.sop2(aco_opcode::s_cselect_b32
, Definition(get_ssa_temp(ctx
, &instr
->dest
.ssa
)),
8404 samples
, default_sample
, bld
.scc(is_msaa
));
8408 if (has_offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
8409 aco_ptr
<Instruction
> tmp_instr
;
8410 Temp acc
, pack
= Temp();
8412 uint32_t pack_const
= 0;
8413 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8414 if (!const_offset
[i
])
8416 pack_const
|= (const_offset
[i
]->u32
& 0x3Fu
) << (8u * i
);
8419 if (offset
.type() == RegType::sgpr
) {
8420 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8421 if (const_offset
[i
])
8424 acc
= emit_extract_vector(ctx
, offset
, i
, s1
);
8425 acc
= bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(0x3Fu
));
8428 acc
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), acc
, Operand(8u * i
));
8431 if (pack
== Temp()) {
8434 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), pack
, acc
);
8438 if (pack_const
&& pack
!= Temp())
8439 pack
= bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), Operand(pack_const
), pack
);
8441 for (unsigned i
= 0; i
< offset
.size(); i
++) {
8442 if (const_offset
[i
])
8445 acc
= emit_extract_vector(ctx
, offset
, i
, v1
);
8446 acc
= bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0x3Fu
), acc
);
8449 acc
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(8u * i
), acc
);
8452 if (pack
== Temp()) {
8455 pack
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), pack
, acc
);
8459 if (pack_const
&& pack
!= Temp())
8460 pack
= bld
.sop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(pack_const
), pack
);
8462 if (pack_const
&& pack
== Temp())
8463 offset
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(pack_const
));
8464 else if (pack
== Temp())
8470 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& instr
->coord_components
)
8471 prepare_cube_coords(ctx
, coords
, &ddx
, &ddy
, instr
->op
== nir_texop_txd
, instr
->is_array
&& instr
->op
!= nir_texop_lod
);
8473 /* pack derivatives */
8474 if (has_ddx
|| has_ddy
) {
8475 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& ctx
->options
->chip_class
== GFX9
) {
8476 assert(has_ddx
&& has_ddy
&& ddx
.size() == 1 && ddy
.size() == 1);
8477 Temp zero
= bld
.copy(bld
.def(v1
), Operand(0u));
8478 derivs
= {ddx
, zero
, ddy
, zero
};
8480 for (unsigned i
= 0; has_ddx
&& i
< ddx
.size(); i
++)
8481 derivs
.emplace_back(emit_extract_vector(ctx
, ddx
, i
, v1
));
8482 for (unsigned i
= 0; has_ddy
&& i
< ddy
.size(); i
++)
8483 derivs
.emplace_back(emit_extract_vector(ctx
, ddy
, i
, v1
));
8488 if (instr
->coord_components
> 1 &&
8489 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8491 instr
->op
!= nir_texop_txf
)
8492 coords
[1] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[1]);
8494 if (instr
->coord_components
> 2 &&
8495 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
8496 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
8497 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
8498 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
8500 instr
->op
!= nir_texop_txf
&&
8501 instr
->op
!= nir_texop_txf_ms
&&
8502 instr
->op
!= nir_texop_fragment_fetch
&&
8503 instr
->op
!= nir_texop_fragment_mask_fetch
)
8504 coords
[2] = bld
.vop1(aco_opcode::v_rndne_f32
, bld
.def(v1
), coords
[2]);
8506 if (ctx
->options
->chip_class
== GFX9
&&
8507 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8508 instr
->op
!= nir_texop_lod
&& instr
->coord_components
) {
8509 assert(coords
.size() > 0 && coords
.size() < 3);
8511 coords
.insert(std::next(coords
.begin()), bld
.copy(bld
.def(v1
), instr
->op
== nir_texop_txf
?
8512 Operand((uint32_t) 0) :
8513 Operand((uint32_t) 0x3f000000)));
8516 bool da
= should_declare_array(ctx
, instr
->sampler_dim
, instr
->is_array
);
8518 if (instr
->op
== nir_texop_samples_identical
)
8519 resource
= fmask_ptr
;
8521 else if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
8522 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
8523 instr
->op
!= nir_texop_txs
&&
8524 instr
->op
!= nir_texop_fragment_fetch
&&
8525 instr
->op
!= nir_texop_fragment_mask_fetch
) {
8526 assert(has_sample_index
);
8527 Operand
op(sample_index
);
8528 if (sample_index_cv
)
8529 op
= Operand(sample_index_cv
->u32
);
8530 sample_index
= adjust_sample_index_using_fmask(ctx
, da
, coords
, op
, fmask_ptr
);
8533 if (has_offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
8534 for (unsigned i
= 0; i
< std::min(offset
.size(), instr
->coord_components
); i
++) {
8535 Temp off
= emit_extract_vector(ctx
, offset
, i
, v1
);
8536 coords
[i
] = bld
.vadd32(bld
.def(v1
), coords
[i
], off
);
8541 /* Build tex instruction */
8542 unsigned dmask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
8543 unsigned dim
= ctx
->options
->chip_class
>= GFX10
&& instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
8544 ? ac_get_sampler_dim(ctx
->options
->chip_class
, instr
->sampler_dim
, instr
->is_array
)
8546 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8549 /* gather4 selects the component by dmask and always returns vec4 */
8550 if (instr
->op
== nir_texop_tg4
) {
8551 assert(instr
->dest
.ssa
.num_components
== 4);
8552 if (instr
->is_shadow
)
8555 dmask
= 1 << instr
->component
;
8556 if (tg4_integer_cube_workaround
|| dst
.type() == RegType::sgpr
)
8557 tmp_dst
= bld
.tmp(v4
);
8558 } else if (instr
->op
== nir_texop_samples_identical
) {
8559 tmp_dst
= bld
.tmp(v1
);
8560 } else if (util_bitcount(dmask
) != instr
->dest
.ssa
.num_components
|| dst
.type() == RegType::sgpr
) {
8561 tmp_dst
= bld
.tmp(RegClass(RegType::vgpr
, util_bitcount(dmask
)));
8564 aco_ptr
<MIMG_instruction
> tex
;
8565 if (instr
->op
== nir_texop_txs
|| instr
->op
== nir_texop_query_levels
) {
8567 lod
= bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
8569 bool div_by_6
= instr
->op
== nir_texop_txs
&&
8570 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
8573 if (tmp_dst
.id() == dst
.id() && div_by_6
)
8574 tmp_dst
= bld
.tmp(tmp_dst
.regClass());
8576 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
8577 tex
->operands
[0] = Operand(resource
);
8578 tex
->operands
[1] = Operand(s4
); /* no sampler */
8579 tex
->operands
[2] = Operand(as_vgpr(ctx
,lod
));
8580 if (ctx
->options
->chip_class
== GFX9
&&
8581 instr
->op
== nir_texop_txs
&&
8582 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
8584 tex
->dmask
= (dmask
& 0x1) | ((dmask
& 0x2) << 1);
8585 } else if (instr
->op
== nir_texop_query_levels
) {
8586 tex
->dmask
= 1 << 3;
8591 tex
->definitions
[0] = Definition(tmp_dst
);
8593 tex
->can_reorder
= true;
8594 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8597 /* divide 3rd value by 6 by multiplying with magic number */
8598 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
8599 Temp c
= bld
.copy(bld
.def(s1
), Operand((uint32_t) 0x2AAAAAAB));
8600 Temp by_6
= bld
.vop3(aco_opcode::v_mul_hi_i32
, bld
.def(v1
), emit_extract_vector(ctx
, tmp_dst
, 2, v1
), c
);
8601 assert(instr
->dest
.ssa
.num_components
== 3);
8602 Temp tmp
= dst
.type() == RegType::vgpr
? dst
: bld
.tmp(v3
);
8603 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
8604 emit_extract_vector(ctx
, tmp_dst
, 0, v1
),
8605 emit_extract_vector(ctx
, tmp_dst
, 1, v1
),
8610 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
8614 Temp tg4_compare_cube_wa64
= Temp();
8616 if (tg4_integer_workarounds
) {
8617 tex
.reset(create_instruction
<MIMG_instruction
>(aco_opcode::image_get_resinfo
, Format::MIMG
, 3, 1));
8618 tex
->operands
[0] = Operand(resource
);
8619 tex
->operands
[1] = Operand(s4
); /* no sampler */
8620 tex
->operands
[2] = bld
.vop1(aco_opcode::v_mov_b32
, bld
.def(v1
), Operand(0u));
8624 Temp size
= bld
.tmp(v2
);
8625 tex
->definitions
[0] = Definition(size
);
8626 tex
->can_reorder
= true;
8627 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8628 emit_split_vector(ctx
, size
, size
.size());
8631 for (unsigned i
= 0; i
< 2; i
++) {
8632 half_texel
[i
] = emit_extract_vector(ctx
, size
, i
, v1
);
8633 half_texel
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_i32
, bld
.def(v1
), half_texel
[i
]);
8634 half_texel
[i
] = bld
.vop1(aco_opcode::v_rcp_iflag_f32
, bld
.def(v1
), half_texel
[i
]);
8635 half_texel
[i
] = bld
.vop2(aco_opcode::v_mul_f32
, bld
.def(v1
), Operand(0xbf000000/*-0.5*/), half_texel
[i
]);
8638 Temp new_coords
[2] = {
8639 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[0], half_texel
[0]),
8640 bld
.vop2(aco_opcode::v_add_f32
, bld
.def(v1
), coords
[1], half_texel
[1])
8643 if (tg4_integer_cube_workaround
) {
8644 // see comment in ac_nir_to_llvm.c's lower_gather4_integer()
8645 Temp desc
[resource
.size()];
8646 aco_ptr
<Instruction
> split
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_split_vector
,
8647 Format::PSEUDO
, 1, resource
.size())};
8648 split
->operands
[0] = Operand(resource
);
8649 for (unsigned i
= 0; i
< resource
.size(); i
++) {
8650 desc
[i
] = bld
.tmp(s1
);
8651 split
->definitions
[i
] = Definition(desc
[i
]);
8653 ctx
->block
->instructions
.emplace_back(std::move(split
));
8655 Temp dfmt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], Operand(20u | (6u << 16)));
8656 Temp compare_cube_wa
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), dfmt
,
8657 Operand((uint32_t)V_008F14_IMG_DATA_FORMAT_8_8_8_8
));
8660 if (stype
== GLSL_TYPE_UINT
) {
8661 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
8662 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_USCALED
),
8663 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_UINT
),
8664 bld
.scc(compare_cube_wa
));
8666 nfmt
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.def(s1
),
8667 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SSCALED
),
8668 Operand((uint32_t)V_008F14_IMG_NUM_FORMAT_SINT
),
8669 bld
.scc(compare_cube_wa
));
8671 tg4_compare_cube_wa64
= bld
.tmp(bld
.lm
);
8672 bool_to_vector_condition(ctx
, compare_cube_wa
, tg4_compare_cube_wa64
);
8674 nfmt
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), nfmt
, Operand(26u));
8676 desc
[1] = bld
.sop2(aco_opcode::s_and_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1],
8677 Operand((uint32_t)C_008F14_NUM_FORMAT
));
8678 desc
[1] = bld
.sop2(aco_opcode::s_or_b32
, bld
.def(s1
), bld
.def(s1
, scc
), desc
[1], nfmt
);
8680 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
,
8681 Format::PSEUDO
, resource
.size(), 1)};
8682 for (unsigned i
= 0; i
< resource
.size(); i
++)
8683 vec
->operands
[i
] = Operand(desc
[i
]);
8684 resource
= bld
.tmp(resource
.regClass());
8685 vec
->definitions
[0] = Definition(resource
);
8686 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8688 new_coords
[0] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8689 new_coords
[0], coords
[0], tg4_compare_cube_wa64
);
8690 new_coords
[1] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
8691 new_coords
[1], coords
[1], tg4_compare_cube_wa64
);
8693 coords
[0] = new_coords
[0];
8694 coords
[1] = new_coords
[1];
8697 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
8698 //FIXME: if (ctx->abi->gfx9_stride_size_workaround) return ac_build_buffer_load_format_gfx9_safe()
8700 assert(coords
.size() == 1);
8701 unsigned last_bit
= util_last_bit(nir_ssa_def_components_read(&instr
->dest
.ssa
));
8705 op
= aco_opcode::buffer_load_format_x
; break;
8707 op
= aco_opcode::buffer_load_format_xy
; break;
8709 op
= aco_opcode::buffer_load_format_xyz
; break;
8711 op
= aco_opcode::buffer_load_format_xyzw
; break;
8713 unreachable("Tex instruction loads more than 4 components.");
8716 /* if the instruction return value matches exactly the nir dest ssa, we can use it directly */
8717 if (last_bit
== instr
->dest
.ssa
.num_components
&& dst
.type() == RegType::vgpr
)
8720 tmp_dst
= bld
.tmp(RegType::vgpr
, last_bit
);
8722 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(op
, Format::MUBUF
, 3, 1)};
8723 mubuf
->operands
[0] = Operand(resource
);
8724 mubuf
->operands
[1] = Operand(coords
[0]);
8725 mubuf
->operands
[2] = Operand((uint32_t) 0);
8726 mubuf
->definitions
[0] = Definition(tmp_dst
);
8727 mubuf
->idxen
= true;
8728 mubuf
->can_reorder
= true;
8729 ctx
->block
->instructions
.emplace_back(std::move(mubuf
));
8731 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, (1 << last_bit
) - 1);
8735 /* gather MIMG address components */
8736 std::vector
<Temp
> args
;
8738 args
.emplace_back(offset
);
8740 args
.emplace_back(bias
);
8742 args
.emplace_back(compare
);
8744 args
.insert(args
.end(), derivs
.begin(), derivs
.end());
8746 args
.insert(args
.end(), coords
.begin(), coords
.end());
8747 if (has_sample_index
)
8748 args
.emplace_back(sample_index
);
8750 args
.emplace_back(lod
);
8751 if (has_clamped_lod
)
8752 args
.emplace_back(clamped_lod
);
8754 Temp arg
= bld
.tmp(RegClass(RegType::vgpr
, args
.size()));
8755 aco_ptr
<Instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, args
.size(), 1)};
8756 vec
->definitions
[0] = Definition(arg
);
8757 for (unsigned i
= 0; i
< args
.size(); i
++)
8758 vec
->operands
[i
] = Operand(args
[i
]);
8759 ctx
->block
->instructions
.emplace_back(std::move(vec
));
8762 if (instr
->op
== nir_texop_txf
||
8763 instr
->op
== nir_texop_txf_ms
||
8764 instr
->op
== nir_texop_samples_identical
||
8765 instr
->op
== nir_texop_fragment_fetch
||
8766 instr
->op
== nir_texop_fragment_mask_fetch
) {
8767 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
;
8768 tex
.reset(create_instruction
<MIMG_instruction
>(op
, Format::MIMG
, 3, 1));
8769 tex
->operands
[0] = Operand(resource
);
8770 tex
->operands
[1] = Operand(s4
); /* no sampler */
8771 tex
->operands
[2] = Operand(arg
);
8776 tex
->definitions
[0] = Definition(tmp_dst
);
8777 tex
->can_reorder
= true;
8778 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8780 if (instr
->op
== nir_texop_samples_identical
) {
8781 assert(dmask
== 1 && dst
.regClass() == v1
);
8782 assert(dst
.id() != tmp_dst
.id());
8784 Temp tmp
= bld
.tmp(bld
.lm
);
8785 bld
.vopc(aco_opcode::v_cmp_eq_u32
, Definition(tmp
), Operand(0u), tmp_dst
).def(0).setHint(vcc
);
8786 bld
.vop2_e64(aco_opcode::v_cndmask_b32
, Definition(dst
), Operand(0u), Operand((uint32_t)-1), tmp
);
8789 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, dmask
);
8794 // TODO: would be better to do this by adding offsets, but needs the opcodes ordered.
8795 aco_opcode opcode
= aco_opcode::image_sample
;
8796 if (has_offset
) { /* image_sample_*_o */
8797 if (has_clamped_lod
) {
8799 opcode
= aco_opcode::image_sample_c_cl_o
;
8801 opcode
= aco_opcode::image_sample_c_d_cl_o
;
8803 opcode
= aco_opcode::image_sample_c_b_cl_o
;
8805 opcode
= aco_opcode::image_sample_cl_o
;
8807 opcode
= aco_opcode::image_sample_d_cl_o
;
8809 opcode
= aco_opcode::image_sample_b_cl_o
;
8811 } else if (has_compare
) {
8812 opcode
= aco_opcode::image_sample_c_o
;
8814 opcode
= aco_opcode::image_sample_c_d_o
;
8816 opcode
= aco_opcode::image_sample_c_b_o
;
8818 opcode
= aco_opcode::image_sample_c_lz_o
;
8820 opcode
= aco_opcode::image_sample_c_l_o
;
8822 opcode
= aco_opcode::image_sample_o
;
8824 opcode
= aco_opcode::image_sample_d_o
;
8826 opcode
= aco_opcode::image_sample_b_o
;
8828 opcode
= aco_opcode::image_sample_lz_o
;
8830 opcode
= aco_opcode::image_sample_l_o
;
8832 } else if (has_clamped_lod
) { /* image_sample_*_cl */
8834 opcode
= aco_opcode::image_sample_c_cl
;
8836 opcode
= aco_opcode::image_sample_c_d_cl
;
8838 opcode
= aco_opcode::image_sample_c_b_cl
;
8840 opcode
= aco_opcode::image_sample_cl
;
8842 opcode
= aco_opcode::image_sample_d_cl
;
8844 opcode
= aco_opcode::image_sample_b_cl
;
8846 } else { /* no offset */
8848 opcode
= aco_opcode::image_sample_c
;
8850 opcode
= aco_opcode::image_sample_c_d
;
8852 opcode
= aco_opcode::image_sample_c_b
;
8854 opcode
= aco_opcode::image_sample_c_lz
;
8856 opcode
= aco_opcode::image_sample_c_l
;
8858 opcode
= aco_opcode::image_sample
;
8860 opcode
= aco_opcode::image_sample_d
;
8862 opcode
= aco_opcode::image_sample_b
;
8864 opcode
= aco_opcode::image_sample_lz
;
8866 opcode
= aco_opcode::image_sample_l
;
8870 if (instr
->op
== nir_texop_tg4
) {
8871 if (has_offset
) { /* image_gather4_*_o */
8873 opcode
= aco_opcode::image_gather4_c_lz_o
;
8875 opcode
= aco_opcode::image_gather4_c_l_o
;
8877 opcode
= aco_opcode::image_gather4_c_b_o
;
8879 opcode
= aco_opcode::image_gather4_lz_o
;
8881 opcode
= aco_opcode::image_gather4_l_o
;
8883 opcode
= aco_opcode::image_gather4_b_o
;
8887 opcode
= aco_opcode::image_gather4_c_lz
;
8889 opcode
= aco_opcode::image_gather4_c_l
;
8891 opcode
= aco_opcode::image_gather4_c_b
;
8893 opcode
= aco_opcode::image_gather4_lz
;
8895 opcode
= aco_opcode::image_gather4_l
;
8897 opcode
= aco_opcode::image_gather4_b
;
8900 } else if (instr
->op
== nir_texop_lod
) {
8901 opcode
= aco_opcode::image_get_lod
;
8904 /* we don't need the bias, sample index, compare value or offset to be
8905 * computed in WQM but if the p_create_vector copies the coordinates, then it
8906 * needs to be in WQM */
8907 if (ctx
->stage
== fragment_fs
&&
8908 !has_derivs
&& !has_lod
&& !level_zero
&&
8909 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_MS
&&
8910 instr
->sampler_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
8911 arg
= emit_wqm(ctx
, arg
, bld
.tmp(arg
.regClass()), true);
8913 tex
.reset(create_instruction
<MIMG_instruction
>(opcode
, Format::MIMG
, 3, 1));
8914 tex
->operands
[0] = Operand(resource
);
8915 tex
->operands
[1] = Operand(sampler
);
8916 tex
->operands
[2] = Operand(arg
);
8920 tex
->definitions
[0] = Definition(tmp_dst
);
8921 tex
->can_reorder
= true;
8922 ctx
->block
->instructions
.emplace_back(std::move(tex
));
8924 if (tg4_integer_cube_workaround
) {
8925 assert(tmp_dst
.id() != dst
.id());
8926 assert(tmp_dst
.size() == dst
.size() && dst
.size() == 4);
8928 emit_split_vector(ctx
, tmp_dst
, tmp_dst
.size());
8930 for (unsigned i
= 0; i
< dst
.size(); i
++) {
8931 val
[i
] = emit_extract_vector(ctx
, tmp_dst
, i
, v1
);
8933 if (stype
== GLSL_TYPE_UINT
)
8934 cvt_val
= bld
.vop1(aco_opcode::v_cvt_u32_f32
, bld
.def(v1
), val
[i
]);
8936 cvt_val
= bld
.vop1(aco_opcode::v_cvt_i32_f32
, bld
.def(v1
), val
[i
]);
8937 val
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), val
[i
], cvt_val
, tg4_compare_cube_wa64
);
8939 Temp tmp
= dst
.regClass() == v4
? dst
: bld
.tmp(v4
);
8940 tmp_dst
= bld
.pseudo(aco_opcode::p_create_vector
, Definition(tmp
),
8941 val
[0], val
[1], val
[2], val
[3]);
8943 unsigned mask
= instr
->op
== nir_texop_tg4
? 0xF : dmask
;
8944 expand_vector(ctx
, tmp_dst
, dst
, instr
->dest
.ssa
.num_components
, mask
);
8949 Operand
get_phi_operand(isel_context
*ctx
, nir_ssa_def
*ssa
, RegClass rc
)
8951 Temp tmp
= get_ssa_temp(ctx
, ssa
);
8952 if (ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
)
8955 return Operand(tmp
);
8958 void visit_phi(isel_context
*ctx
, nir_phi_instr
*instr
)
8960 aco_ptr
<Pseudo_instruction
> phi
;
8961 Temp dst
= get_ssa_temp(ctx
, &instr
->dest
.ssa
);
8962 assert(instr
->dest
.ssa
.bit_size
!= 1 || dst
.regClass() == ctx
->program
->lane_mask
);
8964 bool logical
= !dst
.is_linear() || nir_dest_is_divergent(instr
->dest
);
8965 logical
|= ctx
->block
->kind
& block_kind_merge
;
8966 aco_opcode opcode
= logical
? aco_opcode::p_phi
: aco_opcode::p_linear_phi
;
8968 /* we want a sorted list of sources, since the predecessor list is also sorted */
8969 std::map
<unsigned, nir_ssa_def
*> phi_src
;
8970 nir_foreach_phi_src(src
, instr
)
8971 phi_src
[src
->pred
->index
] = src
->src
.ssa
;
8973 std::vector
<unsigned>& preds
= logical
? ctx
->block
->logical_preds
: ctx
->block
->linear_preds
;
8974 unsigned num_operands
= 0;
8975 Operand operands
[std::max(exec_list_length(&instr
->srcs
), (unsigned)preds
.size()) + 1];
8976 unsigned num_defined
= 0;
8977 unsigned cur_pred_idx
= 0;
8978 for (std::pair
<unsigned, nir_ssa_def
*> src
: phi_src
) {
8979 if (cur_pred_idx
< preds
.size()) {
8980 /* handle missing preds (IF merges with discard/break) and extra preds (loop exit with discard) */
8981 unsigned block
= ctx
->cf_info
.nir_to_aco
[src
.first
];
8982 unsigned skipped
= 0;
8983 while (cur_pred_idx
+ skipped
< preds
.size() && preds
[cur_pred_idx
+ skipped
] != block
)
8985 if (cur_pred_idx
+ skipped
< preds
.size()) {
8986 for (unsigned i
= 0; i
< skipped
; i
++)
8987 operands
[num_operands
++] = Operand(dst
.regClass());
8988 cur_pred_idx
+= skipped
;
8993 /* Handle missing predecessors at the end. This shouldn't happen with loop
8994 * headers and we can't ignore these sources for loop header phis. */
8995 if (!(ctx
->block
->kind
& block_kind_loop_header
) && cur_pred_idx
>= preds
.size())
8998 Operand op
= get_phi_operand(ctx
, src
.second
, dst
.regClass());
8999 operands
[num_operands
++] = op
;
9000 num_defined
+= !op
.isUndefined();
9002 /* handle block_kind_continue_or_break at loop exit blocks */
9003 while (cur_pred_idx
++ < preds
.size())
9004 operands
[num_operands
++] = Operand(dst
.regClass());
9006 /* If the loop ends with a break, still add a linear continue edge in case
9007 * that break is divergent or continue_or_break is used. We'll either remove
9008 * this operand later in visit_loop() if it's not necessary or replace the
9009 * undef with something correct. */
9010 if (!logical
&& ctx
->block
->kind
& block_kind_loop_header
) {
9011 nir_loop
*loop
= nir_cf_node_as_loop(instr
->instr
.block
->cf_node
.parent
);
9012 nir_block
*last
= nir_loop_last_block(loop
);
9013 if (last
->successors
[0] != instr
->instr
.block
)
9014 operands
[num_operands
++] = Operand(RegClass());
9017 if (num_defined
== 0) {
9018 Builder
bld(ctx
->program
, ctx
->block
);
9019 if (dst
.regClass() == s1
) {
9020 bld
.sop1(aco_opcode::s_mov_b32
, Definition(dst
), Operand(0u));
9021 } else if (dst
.regClass() == v1
) {
9022 bld
.vop1(aco_opcode::v_mov_b32
, Definition(dst
), Operand(0u));
9024 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
9025 for (unsigned i
= 0; i
< dst
.size(); i
++)
9026 vec
->operands
[i
] = Operand(0u);
9027 vec
->definitions
[0] = Definition(dst
);
9028 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9033 /* we can use a linear phi in some cases if one src is undef */
9034 if (dst
.is_linear() && ctx
->block
->kind
& block_kind_merge
&& num_defined
== 1) {
9035 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, num_operands
, 1));
9037 Block
*linear_else
= &ctx
->program
->blocks
[ctx
->block
->linear_preds
[1]];
9038 Block
*invert
= &ctx
->program
->blocks
[linear_else
->linear_preds
[0]];
9039 assert(invert
->kind
& block_kind_invert
);
9041 unsigned then_block
= invert
->linear_preds
[0];
9043 Block
* insert_block
= NULL
;
9044 for (unsigned i
= 0; i
< num_operands
; i
++) {
9045 Operand op
= operands
[i
];
9046 if (op
.isUndefined())
9048 insert_block
= ctx
->block
->logical_preds
[i
] == then_block
? invert
: ctx
->block
;
9049 phi
->operands
[0] = op
;
9052 assert(insert_block
); /* should be handled by the "num_defined == 0" case above */
9053 phi
->operands
[1] = Operand(dst
.regClass());
9054 phi
->definitions
[0] = Definition(dst
);
9055 insert_block
->instructions
.emplace(insert_block
->instructions
.begin(), std::move(phi
));
9059 /* try to scalarize vector phis */
9060 if (instr
->dest
.ssa
.bit_size
!= 1 && dst
.size() > 1) {
9061 // TODO: scalarize linear phis on divergent ifs
9062 bool can_scalarize
= (opcode
== aco_opcode::p_phi
|| !(ctx
->block
->kind
& block_kind_merge
));
9063 std::array
<Temp
, NIR_MAX_VEC_COMPONENTS
> new_vec
;
9064 for (unsigned i
= 0; can_scalarize
&& (i
< num_operands
); i
++) {
9065 Operand src
= operands
[i
];
9066 if (src
.isTemp() && ctx
->allocated_vec
.find(src
.tempId()) == ctx
->allocated_vec
.end())
9067 can_scalarize
= false;
9069 if (can_scalarize
) {
9070 unsigned num_components
= instr
->dest
.ssa
.num_components
;
9071 assert(dst
.size() % num_components
== 0);
9072 RegClass rc
= RegClass(dst
.type(), dst
.size() / num_components
);
9074 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_components
, 1)};
9075 for (unsigned k
= 0; k
< num_components
; k
++) {
9076 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
9077 for (unsigned i
= 0; i
< num_operands
; i
++) {
9078 Operand src
= operands
[i
];
9079 phi
->operands
[i
] = src
.isTemp() ? Operand(ctx
->allocated_vec
[src
.tempId()][k
]) : Operand(rc
);
9081 Temp phi_dst
= {ctx
->program
->allocateId(), rc
};
9082 phi
->definitions
[0] = Definition(phi_dst
);
9083 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
9084 new_vec
[k
] = phi_dst
;
9085 vec
->operands
[k
] = Operand(phi_dst
);
9087 vec
->definitions
[0] = Definition(dst
);
9088 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9089 ctx
->allocated_vec
.emplace(dst
.id(), new_vec
);
9094 phi
.reset(create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, num_operands
, 1));
9095 for (unsigned i
= 0; i
< num_operands
; i
++)
9096 phi
->operands
[i
] = operands
[i
];
9097 phi
->definitions
[0] = Definition(dst
);
9098 ctx
->block
->instructions
.emplace(ctx
->block
->instructions
.begin(), std::move(phi
));
9102 void visit_undef(isel_context
*ctx
, nir_ssa_undef_instr
*instr
)
9104 Temp dst
= get_ssa_temp(ctx
, &instr
->def
);
9106 assert(dst
.type() == RegType::sgpr
);
9108 if (dst
.size() == 1) {
9109 Builder(ctx
->program
, ctx
->block
).copy(Definition(dst
), Operand(0u));
9111 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, dst
.size(), 1)};
9112 for (unsigned i
= 0; i
< dst
.size(); i
++)
9113 vec
->operands
[i
] = Operand(0u);
9114 vec
->definitions
[0] = Definition(dst
);
9115 ctx
->block
->instructions
.emplace_back(std::move(vec
));
9119 void visit_jump(isel_context
*ctx
, nir_jump_instr
*instr
)
9121 Builder
bld(ctx
->program
, ctx
->block
);
9122 Block
*logical_target
;
9123 append_logical_end(ctx
->block
);
9124 unsigned idx
= ctx
->block
->index
;
9126 switch (instr
->type
) {
9127 case nir_jump_break
:
9128 logical_target
= ctx
->cf_info
.parent_loop
.exit
;
9129 add_logical_edge(idx
, logical_target
);
9130 ctx
->block
->kind
|= block_kind_break
;
9132 if (!ctx
->cf_info
.parent_if
.is_divergent
&&
9133 !ctx
->cf_info
.parent_loop
.has_divergent_continue
) {
9134 /* uniform break - directly jump out of the loop */
9135 ctx
->block
->kind
|= block_kind_uniform
;
9136 ctx
->cf_info
.has_branch
= true;
9137 bld
.branch(aco_opcode::p_branch
);
9138 add_linear_edge(idx
, logical_target
);
9141 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
9142 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
9144 case nir_jump_continue
:
9145 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
9146 add_logical_edge(idx
, logical_target
);
9147 ctx
->block
->kind
|= block_kind_continue
;
9149 if (ctx
->cf_info
.parent_if
.is_divergent
) {
9150 /* for potential uniform breaks after this continue,
9151 we must ensure that they are handled correctly */
9152 ctx
->cf_info
.parent_loop
.has_divergent_continue
= true;
9153 ctx
->cf_info
.parent_loop
.has_divergent_branch
= true;
9154 ctx
->cf_info
.nir_to_aco
[instr
->instr
.block
->index
] = ctx
->block
->index
;
9156 /* uniform continue - directly jump to the loop header */
9157 ctx
->block
->kind
|= block_kind_uniform
;
9158 ctx
->cf_info
.has_branch
= true;
9159 bld
.branch(aco_opcode::p_branch
);
9160 add_linear_edge(idx
, logical_target
);
9165 fprintf(stderr
, "Unknown NIR jump instr: ");
9166 nir_print_instr(&instr
->instr
, stderr
);
9167 fprintf(stderr
, "\n");
9171 if (ctx
->cf_info
.parent_if
.is_divergent
&& !ctx
->cf_info
.exec_potentially_empty_break
) {
9172 ctx
->cf_info
.exec_potentially_empty_break
= true;
9173 ctx
->cf_info
.exec_potentially_empty_break_depth
= ctx
->cf_info
.loop_nest_depth
;
9176 /* remove critical edges from linear CFG */
9177 bld
.branch(aco_opcode::p_branch
);
9178 Block
* break_block
= ctx
->program
->create_and_insert_block();
9179 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9180 break_block
->kind
|= block_kind_uniform
;
9181 add_linear_edge(idx
, break_block
);
9182 /* the loop_header pointer might be invalidated by this point */
9183 if (instr
->type
== nir_jump_continue
)
9184 logical_target
= &ctx
->program
->blocks
[ctx
->cf_info
.parent_loop
.header_idx
];
9185 add_linear_edge(break_block
->index
, logical_target
);
9186 bld
.reset(break_block
);
9187 bld
.branch(aco_opcode::p_branch
);
9189 Block
* continue_block
= ctx
->program
->create_and_insert_block();
9190 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9191 add_linear_edge(idx
, continue_block
);
9192 append_logical_start(continue_block
);
9193 ctx
->block
= continue_block
;
9197 void visit_block(isel_context
*ctx
, nir_block
*block
)
9199 nir_foreach_instr(instr
, block
) {
9200 switch (instr
->type
) {
9201 case nir_instr_type_alu
:
9202 visit_alu_instr(ctx
, nir_instr_as_alu(instr
));
9204 case nir_instr_type_load_const
:
9205 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
9207 case nir_instr_type_intrinsic
:
9208 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
9210 case nir_instr_type_tex
:
9211 visit_tex(ctx
, nir_instr_as_tex(instr
));
9213 case nir_instr_type_phi
:
9214 visit_phi(ctx
, nir_instr_as_phi(instr
));
9216 case nir_instr_type_ssa_undef
:
9217 visit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
9219 case nir_instr_type_deref
:
9221 case nir_instr_type_jump
:
9222 visit_jump(ctx
, nir_instr_as_jump(instr
));
9225 fprintf(stderr
, "Unknown NIR instr type: ");
9226 nir_print_instr(instr
, stderr
);
9227 fprintf(stderr
, "\n");
9232 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9233 ctx
->cf_info
.nir_to_aco
[block
->index
] = ctx
->block
->index
;
9238 static Operand
create_continue_phis(isel_context
*ctx
, unsigned first
, unsigned last
,
9239 aco_ptr
<Instruction
>& header_phi
, Operand
*vals
)
9241 vals
[0] = Operand(header_phi
->definitions
[0].getTemp());
9242 RegClass rc
= vals
[0].regClass();
9244 unsigned loop_nest_depth
= ctx
->program
->blocks
[first
].loop_nest_depth
;
9246 unsigned next_pred
= 1;
9248 for (unsigned idx
= first
+ 1; idx
<= last
; idx
++) {
9249 Block
& block
= ctx
->program
->blocks
[idx
];
9250 if (block
.loop_nest_depth
!= loop_nest_depth
) {
9251 vals
[idx
- first
] = vals
[idx
- 1 - first
];
9255 if (block
.kind
& block_kind_continue
) {
9256 vals
[idx
- first
] = header_phi
->operands
[next_pred
];
9261 bool all_same
= true;
9262 for (unsigned i
= 1; all_same
&& (i
< block
.linear_preds
.size()); i
++)
9263 all_same
= vals
[block
.linear_preds
[i
] - first
] == vals
[block
.linear_preds
[0] - first
];
9267 val
= vals
[block
.linear_preds
[0] - first
];
9269 aco_ptr
<Instruction
> phi(create_instruction
<Pseudo_instruction
>(
9270 aco_opcode::p_linear_phi
, Format::PSEUDO
, block
.linear_preds
.size(), 1));
9271 for (unsigned i
= 0; i
< block
.linear_preds
.size(); i
++)
9272 phi
->operands
[i
] = vals
[block
.linear_preds
[i
] - first
];
9273 val
= Operand(Temp(ctx
->program
->allocateId(), rc
));
9274 phi
->definitions
[0] = Definition(val
.getTemp());
9275 block
.instructions
.emplace(block
.instructions
.begin(), std::move(phi
));
9277 vals
[idx
- first
] = val
;
9280 return vals
[last
- first
];
9283 static void visit_loop(isel_context
*ctx
, nir_loop
*loop
)
9285 //TODO: we might want to wrap the loop around a branch if exec_potentially_empty=true
9286 append_logical_end(ctx
->block
);
9287 ctx
->block
->kind
|= block_kind_loop_preheader
| block_kind_uniform
;
9288 Builder
bld(ctx
->program
, ctx
->block
);
9289 bld
.branch(aco_opcode::p_branch
);
9290 unsigned loop_preheader_idx
= ctx
->block
->index
;
9292 Block loop_exit
= Block();
9293 loop_exit
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9294 loop_exit
.kind
|= (block_kind_loop_exit
| (ctx
->block
->kind
& block_kind_top_level
));
9296 Block
* loop_header
= ctx
->program
->create_and_insert_block();
9297 loop_header
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
+ 1;
9298 loop_header
->kind
|= block_kind_loop_header
;
9299 add_edge(loop_preheader_idx
, loop_header
);
9300 ctx
->block
= loop_header
;
9302 /* emit loop body */
9303 unsigned loop_header_idx
= loop_header
->index
;
9304 loop_info_RAII
loop_raii(ctx
, loop_header_idx
, &loop_exit
);
9305 append_logical_start(ctx
->block
);
9306 bool unreachable
= visit_cf_list(ctx
, &loop
->body
);
9308 //TODO: what if a loop ends with a unconditional or uniformly branched continue and this branch is never taken?
9309 if (!ctx
->cf_info
.has_branch
) {
9310 append_logical_end(ctx
->block
);
9311 if (ctx
->cf_info
.exec_potentially_empty_discard
|| ctx
->cf_info
.exec_potentially_empty_break
) {
9312 /* Discards can result in code running with an empty exec mask.
9313 * This would result in divergent breaks not ever being taken. As a
9314 * workaround, break the loop when the loop mask is empty instead of
9315 * always continuing. */
9316 ctx
->block
->kind
|= (block_kind_continue_or_break
| block_kind_uniform
);
9317 unsigned block_idx
= ctx
->block
->index
;
9319 /* create helper blocks to avoid critical edges */
9320 Block
*break_block
= ctx
->program
->create_and_insert_block();
9321 break_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9322 break_block
->kind
= block_kind_uniform
;
9323 bld
.reset(break_block
);
9324 bld
.branch(aco_opcode::p_branch
);
9325 add_linear_edge(block_idx
, break_block
);
9326 add_linear_edge(break_block
->index
, &loop_exit
);
9328 Block
*continue_block
= ctx
->program
->create_and_insert_block();
9329 continue_block
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9330 continue_block
->kind
= block_kind_uniform
;
9331 bld
.reset(continue_block
);
9332 bld
.branch(aco_opcode::p_branch
);
9333 add_linear_edge(block_idx
, continue_block
);
9334 add_linear_edge(continue_block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9336 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9337 add_logical_edge(block_idx
, &ctx
->program
->blocks
[loop_header_idx
]);
9338 ctx
->block
= &ctx
->program
->blocks
[block_idx
];
9340 ctx
->block
->kind
|= (block_kind_continue
| block_kind_uniform
);
9341 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9342 add_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9344 add_linear_edge(ctx
->block
->index
, &ctx
->program
->blocks
[loop_header_idx
]);
9347 bld
.reset(ctx
->block
);
9348 bld
.branch(aco_opcode::p_branch
);
9351 /* Fixup phis in loop header from unreachable blocks.
9352 * has_branch/has_divergent_branch also indicates if the loop ends with a
9353 * break/continue instruction, but we don't emit those if unreachable=true */
9355 assert(ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
);
9356 bool linear
= ctx
->cf_info
.has_branch
;
9357 bool logical
= ctx
->cf_info
.has_branch
|| ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9358 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
9359 if ((logical
&& instr
->opcode
== aco_opcode::p_phi
) ||
9360 (linear
&& instr
->opcode
== aco_opcode::p_linear_phi
)) {
9361 /* the last operand should be the one that needs to be removed */
9362 instr
->operands
.pop_back();
9363 } else if (!is_phi(instr
)) {
9369 /* Fixup linear phis in loop header from expecting a continue. Both this fixup
9370 * and the previous one shouldn't both happen at once because a break in the
9371 * merge block would get CSE'd */
9372 if (nir_loop_last_block(loop
)->successors
[0] != nir_loop_first_block(loop
)) {
9373 unsigned num_vals
= ctx
->cf_info
.has_branch
? 1 : (ctx
->block
->index
- loop_header_idx
+ 1);
9374 Operand vals
[num_vals
];
9375 for (aco_ptr
<Instruction
>& instr
: ctx
->program
->blocks
[loop_header_idx
].instructions
) {
9376 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
9377 if (ctx
->cf_info
.has_branch
)
9378 instr
->operands
.pop_back();
9380 instr
->operands
.back() = create_continue_phis(ctx
, loop_header_idx
, ctx
->block
->index
, instr
, vals
);
9381 } else if (!is_phi(instr
)) {
9387 ctx
->cf_info
.has_branch
= false;
9389 // TODO: if the loop has not a single exit, we must add one °°
9390 /* emit loop successor block */
9391 ctx
->block
= ctx
->program
->insert_block(std::move(loop_exit
));
9392 append_logical_start(ctx
->block
);
9395 // TODO: check if it is beneficial to not branch on continues
9396 /* trim linear phis in loop header */
9397 for (auto&& instr
: loop_entry
->instructions
) {
9398 if (instr
->opcode
== aco_opcode::p_linear_phi
) {
9399 aco_ptr
<Pseudo_instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, loop_entry
->linear_predecessors
.size(), 1)};
9400 new_phi
->definitions
[0] = instr
->definitions
[0];
9401 for (unsigned i
= 0; i
< new_phi
->operands
.size(); i
++)
9402 new_phi
->operands
[i
] = instr
->operands
[i
];
9403 /* check that the remaining operands are all the same */
9404 for (unsigned i
= new_phi
->operands
.size(); i
< instr
->operands
.size(); i
++)
9405 assert(instr
->operands
[i
].tempId() == instr
->operands
.back().tempId());
9406 instr
.swap(new_phi
);
9407 } else if (instr
->opcode
== aco_opcode::p_phi
) {
9416 static void begin_divergent_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
9420 append_logical_end(ctx
->block
);
9421 ctx
->block
->kind
|= block_kind_branch
;
9423 /* branch to linear then block */
9424 assert(cond
.regClass() == ctx
->program
->lane_mask
);
9425 aco_ptr
<Pseudo_branch_instruction
> branch
;
9426 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_z
, Format::PSEUDO_BRANCH
, 1, 0));
9427 branch
->operands
[0] = Operand(cond
);
9428 ctx
->block
->instructions
.push_back(std::move(branch
));
9430 ic
->BB_if_idx
= ctx
->block
->index
;
9431 ic
->BB_invert
= Block();
9432 ic
->BB_invert
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9433 /* Invert blocks are intentionally not marked as top level because they
9434 * are not part of the logical cfg. */
9435 ic
->BB_invert
.kind
|= block_kind_invert
;
9436 ic
->BB_endif
= Block();
9437 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9438 ic
->BB_endif
.kind
|= (block_kind_merge
| (ctx
->block
->kind
& block_kind_top_level
));
9440 ic
->exec_potentially_empty_discard_old
= ctx
->cf_info
.exec_potentially_empty_discard
;
9441 ic
->exec_potentially_empty_break_old
= ctx
->cf_info
.exec_potentially_empty_break
;
9442 ic
->exec_potentially_empty_break_depth_old
= ctx
->cf_info
.exec_potentially_empty_break_depth
;
9443 ic
->divergent_old
= ctx
->cf_info
.parent_if
.is_divergent
;
9444 ctx
->cf_info
.parent_if
.is_divergent
= true;
9446 /* divergent branches use cbranch_execz */
9447 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9448 ctx
->cf_info
.exec_potentially_empty_break
= false;
9449 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9451 /** emit logical then block */
9452 Block
* BB_then_logical
= ctx
->program
->create_and_insert_block();
9453 BB_then_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9454 add_edge(ic
->BB_if_idx
, BB_then_logical
);
9455 ctx
->block
= BB_then_logical
;
9456 append_logical_start(BB_then_logical
);
9459 static void begin_divergent_if_else(isel_context
*ctx
, if_context
*ic
)
9461 Block
*BB_then_logical
= ctx
->block
;
9462 append_logical_end(BB_then_logical
);
9463 /* branch from logical then block to invert block */
9464 aco_ptr
<Pseudo_branch_instruction
> branch
;
9465 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9466 BB_then_logical
->instructions
.emplace_back(std::move(branch
));
9467 add_linear_edge(BB_then_logical
->index
, &ic
->BB_invert
);
9468 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9469 add_logical_edge(BB_then_logical
->index
, &ic
->BB_endif
);
9470 BB_then_logical
->kind
|= block_kind_uniform
;
9471 assert(!ctx
->cf_info
.has_branch
);
9472 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9473 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9475 /** emit linear then block */
9476 Block
* BB_then_linear
= ctx
->program
->create_and_insert_block();
9477 BB_then_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9478 BB_then_linear
->kind
|= block_kind_uniform
;
9479 add_linear_edge(ic
->BB_if_idx
, BB_then_linear
);
9480 /* branch from linear then block to invert block */
9481 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9482 BB_then_linear
->instructions
.emplace_back(std::move(branch
));
9483 add_linear_edge(BB_then_linear
->index
, &ic
->BB_invert
);
9485 /** emit invert merge block */
9486 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_invert
));
9487 ic
->invert_idx
= ctx
->block
->index
;
9489 /* branch to linear else block (skip else) */
9490 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_cbranch_nz
, Format::PSEUDO_BRANCH
, 1, 0));
9491 branch
->operands
[0] = Operand(ic
->cond
);
9492 ctx
->block
->instructions
.push_back(std::move(branch
));
9494 ic
->exec_potentially_empty_discard_old
|= ctx
->cf_info
.exec_potentially_empty_discard
;
9495 ic
->exec_potentially_empty_break_old
|= ctx
->cf_info
.exec_potentially_empty_break
;
9496 ic
->exec_potentially_empty_break_depth_old
=
9497 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
9498 /* divergent branches use cbranch_execz */
9499 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9500 ctx
->cf_info
.exec_potentially_empty_break
= false;
9501 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9503 /** emit logical else block */
9504 Block
* BB_else_logical
= ctx
->program
->create_and_insert_block();
9505 BB_else_logical
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9506 add_logical_edge(ic
->BB_if_idx
, BB_else_logical
);
9507 add_linear_edge(ic
->invert_idx
, BB_else_logical
);
9508 ctx
->block
= BB_else_logical
;
9509 append_logical_start(BB_else_logical
);
9512 static void end_divergent_if(isel_context
*ctx
, if_context
*ic
)
9514 Block
*BB_else_logical
= ctx
->block
;
9515 append_logical_end(BB_else_logical
);
9517 /* branch from logical else block to endif block */
9518 aco_ptr
<Pseudo_branch_instruction
> branch
;
9519 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9520 BB_else_logical
->instructions
.emplace_back(std::move(branch
));
9521 add_linear_edge(BB_else_logical
->index
, &ic
->BB_endif
);
9522 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9523 add_logical_edge(BB_else_logical
->index
, &ic
->BB_endif
);
9524 BB_else_logical
->kind
|= block_kind_uniform
;
9526 assert(!ctx
->cf_info
.has_branch
);
9527 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
9530 /** emit linear else block */
9531 Block
* BB_else_linear
= ctx
->program
->create_and_insert_block();
9532 BB_else_linear
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9533 BB_else_linear
->kind
|= block_kind_uniform
;
9534 add_linear_edge(ic
->invert_idx
, BB_else_linear
);
9536 /* branch from linear else block to endif block */
9537 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9538 BB_else_linear
->instructions
.emplace_back(std::move(branch
));
9539 add_linear_edge(BB_else_linear
->index
, &ic
->BB_endif
);
9542 /** emit endif merge block */
9543 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
9544 append_logical_start(ctx
->block
);
9547 ctx
->cf_info
.parent_if
.is_divergent
= ic
->divergent_old
;
9548 ctx
->cf_info
.exec_potentially_empty_discard
|= ic
->exec_potentially_empty_discard_old
;
9549 ctx
->cf_info
.exec_potentially_empty_break
|= ic
->exec_potentially_empty_break_old
;
9550 ctx
->cf_info
.exec_potentially_empty_break_depth
=
9551 std::min(ic
->exec_potentially_empty_break_depth_old
, ctx
->cf_info
.exec_potentially_empty_break_depth
);
9552 if (ctx
->cf_info
.loop_nest_depth
== ctx
->cf_info
.exec_potentially_empty_break_depth
&&
9553 !ctx
->cf_info
.parent_if
.is_divergent
) {
9554 ctx
->cf_info
.exec_potentially_empty_break
= false;
9555 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9557 /* uniform control flow never has an empty exec-mask */
9558 if (!ctx
->cf_info
.loop_nest_depth
&& !ctx
->cf_info
.parent_if
.is_divergent
) {
9559 ctx
->cf_info
.exec_potentially_empty_discard
= false;
9560 ctx
->cf_info
.exec_potentially_empty_break
= false;
9561 ctx
->cf_info
.exec_potentially_empty_break_depth
= UINT16_MAX
;
9565 static void begin_uniform_if_then(isel_context
*ctx
, if_context
*ic
, Temp cond
)
9567 assert(cond
.regClass() == s1
);
9569 append_logical_end(ctx
->block
);
9570 ctx
->block
->kind
|= block_kind_uniform
;
9572 aco_ptr
<Pseudo_branch_instruction
> branch
;
9573 aco_opcode branch_opcode
= aco_opcode::p_cbranch_z
;
9574 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(branch_opcode
, Format::PSEUDO_BRANCH
, 1, 0));
9575 branch
->operands
[0] = Operand(cond
);
9576 branch
->operands
[0].setFixed(scc
);
9577 ctx
->block
->instructions
.emplace_back(std::move(branch
));
9579 ic
->BB_if_idx
= ctx
->block
->index
;
9580 ic
->BB_endif
= Block();
9581 ic
->BB_endif
.loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9582 ic
->BB_endif
.kind
|= ctx
->block
->kind
& block_kind_top_level
;
9584 ctx
->cf_info
.has_branch
= false;
9585 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9587 /** emit then block */
9588 Block
* BB_then
= ctx
->program
->create_and_insert_block();
9589 BB_then
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9590 add_edge(ic
->BB_if_idx
, BB_then
);
9591 append_logical_start(BB_then
);
9592 ctx
->block
= BB_then
;
9595 static void begin_uniform_if_else(isel_context
*ctx
, if_context
*ic
)
9597 Block
*BB_then
= ctx
->block
;
9599 ic
->uniform_has_then_branch
= ctx
->cf_info
.has_branch
;
9600 ic
->then_branch_divergent
= ctx
->cf_info
.parent_loop
.has_divergent_branch
;
9602 if (!ic
->uniform_has_then_branch
) {
9603 append_logical_end(BB_then
);
9604 /* branch from then block to endif block */
9605 aco_ptr
<Pseudo_branch_instruction
> branch
;
9606 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9607 BB_then
->instructions
.emplace_back(std::move(branch
));
9608 add_linear_edge(BB_then
->index
, &ic
->BB_endif
);
9609 if (!ic
->then_branch_divergent
)
9610 add_logical_edge(BB_then
->index
, &ic
->BB_endif
);
9611 BB_then
->kind
|= block_kind_uniform
;
9614 ctx
->cf_info
.has_branch
= false;
9615 ctx
->cf_info
.parent_loop
.has_divergent_branch
= false;
9617 /** emit else block */
9618 Block
* BB_else
= ctx
->program
->create_and_insert_block();
9619 BB_else
->loop_nest_depth
= ctx
->cf_info
.loop_nest_depth
;
9620 add_edge(ic
->BB_if_idx
, BB_else
);
9621 append_logical_start(BB_else
);
9622 ctx
->block
= BB_else
;
9625 static void end_uniform_if(isel_context
*ctx
, if_context
*ic
)
9627 Block
*BB_else
= ctx
->block
;
9629 if (!ctx
->cf_info
.has_branch
) {
9630 append_logical_end(BB_else
);
9631 /* branch from then block to endif block */
9632 aco_ptr
<Pseudo_branch_instruction
> branch
;
9633 branch
.reset(create_instruction
<Pseudo_branch_instruction
>(aco_opcode::p_branch
, Format::PSEUDO_BRANCH
, 0, 0));
9634 BB_else
->instructions
.emplace_back(std::move(branch
));
9635 add_linear_edge(BB_else
->index
, &ic
->BB_endif
);
9636 if (!ctx
->cf_info
.parent_loop
.has_divergent_branch
)
9637 add_logical_edge(BB_else
->index
, &ic
->BB_endif
);
9638 BB_else
->kind
|= block_kind_uniform
;
9641 ctx
->cf_info
.has_branch
&= ic
->uniform_has_then_branch
;
9642 ctx
->cf_info
.parent_loop
.has_divergent_branch
&= ic
->then_branch_divergent
;
9644 /** emit endif merge block */
9645 if (!ctx
->cf_info
.has_branch
) {
9646 ctx
->block
= ctx
->program
->insert_block(std::move(ic
->BB_endif
));
9647 append_logical_start(ctx
->block
);
9651 static bool visit_if(isel_context
*ctx
, nir_if
*if_stmt
)
9653 Temp cond
= get_ssa_temp(ctx
, if_stmt
->condition
.ssa
);
9654 Builder
bld(ctx
->program
, ctx
->block
);
9655 aco_ptr
<Pseudo_branch_instruction
> branch
;
9658 if (!nir_src_is_divergent(if_stmt
->condition
)) { /* uniform condition */
9660 * Uniform conditionals are represented in the following way*) :
9662 * The linear and logical CFG:
9665 * BB_THEN (logical) BB_ELSE (logical)
9669 * *) Exceptions may be due to break and continue statements within loops
9670 * If a break/continue happens within uniform control flow, it branches
9671 * to the loop exit/entry block. Otherwise, it branches to the next
9675 // TODO: in a post-RA optimizer, we could check if the condition is in VCC and omit this instruction
9676 assert(cond
.regClass() == ctx
->program
->lane_mask
);
9677 cond
= bool_to_scalar_condition(ctx
, cond
);
9679 begin_uniform_if_then(ctx
, &ic
, cond
);
9680 visit_cf_list(ctx
, &if_stmt
->then_list
);
9682 begin_uniform_if_else(ctx
, &ic
);
9683 visit_cf_list(ctx
, &if_stmt
->else_list
);
9685 end_uniform_if(ctx
, &ic
);
9686 } else { /* non-uniform condition */
9688 * To maintain a logical and linear CFG without critical edges,
9689 * non-uniform conditionals are represented in the following way*) :
9694 * BB_THEN (logical) BB_THEN (linear)
9696 * BB_INVERT (linear)
9698 * BB_ELSE (logical) BB_ELSE (linear)
9705 * BB_THEN (logical) BB_ELSE (logical)
9709 * *) Exceptions may be due to break and continue statements within loops
9712 begin_divergent_if_then(ctx
, &ic
, cond
);
9713 visit_cf_list(ctx
, &if_stmt
->then_list
);
9715 begin_divergent_if_else(ctx
, &ic
);
9716 visit_cf_list(ctx
, &if_stmt
->else_list
);
9718 end_divergent_if(ctx
, &ic
);
9721 return !ctx
->cf_info
.has_branch
&& !ctx
->block
->logical_preds
.empty();
9724 static bool visit_cf_list(isel_context
*ctx
,
9725 struct exec_list
*list
)
9727 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
9728 switch (node
->type
) {
9729 case nir_cf_node_block
:
9730 visit_block(ctx
, nir_cf_node_as_block(node
));
9732 case nir_cf_node_if
:
9733 if (!visit_if(ctx
, nir_cf_node_as_if(node
)))
9736 case nir_cf_node_loop
:
9737 visit_loop(ctx
, nir_cf_node_as_loop(node
));
9740 unreachable("unimplemented cf list type");
9746 static void create_null_export(isel_context
*ctx
)
9748 /* Some shader stages always need to have exports.
9749 * So when there is none, we need to add a null export.
9752 unsigned dest
= (ctx
->program
->stage
& hw_fs
) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS
;
9753 bool vm
= (ctx
->program
->stage
& hw_fs
) || ctx
->program
->chip_class
>= GFX10
;
9754 Builder
bld(ctx
->program
, ctx
->block
);
9755 bld
.exp(aco_opcode::exp
, Operand(v1
), Operand(v1
), Operand(v1
), Operand(v1
),
9756 /* enabled_mask */ 0, dest
, /* compr */ false, /* done */ true, vm
);
9759 static bool export_vs_varying(isel_context
*ctx
, int slot
, bool is_pos
, int *next_pos
)
9761 assert(ctx
->stage
== vertex_vs
||
9762 ctx
->stage
== tess_eval_vs
||
9763 ctx
->stage
== gs_copy_vs
||
9764 ctx
->stage
== ngg_vertex_gs
||
9765 ctx
->stage
== ngg_tess_eval_gs
);
9767 int offset
= (ctx
->stage
& sw_tes
)
9768 ? ctx
->program
->info
->tes
.outinfo
.vs_output_param_offset
[slot
]
9769 : ctx
->program
->info
->vs
.outinfo
.vs_output_param_offset
[slot
];
9770 uint64_t mask
= ctx
->outputs
.mask
[slot
];
9771 if (!is_pos
&& !mask
)
9773 if (!is_pos
&& offset
== AC_EXP_PARAM_UNDEFINED
)
9775 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
9776 exp
->enabled_mask
= mask
;
9777 for (unsigned i
= 0; i
< 4; ++i
) {
9778 if (mask
& (1 << i
))
9779 exp
->operands
[i
] = Operand(ctx
->outputs
.temps
[slot
* 4u + i
]);
9781 exp
->operands
[i
] = Operand(v1
);
9783 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
9784 * Setting valid_mask=1 prevents it and has no other effect.
9786 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& is_pos
&& *next_pos
== 0;
9788 exp
->compressed
= false;
9790 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
9792 exp
->dest
= V_008DFC_SQ_EXP_PARAM
+ offset
;
9793 ctx
->block
->instructions
.emplace_back(std::move(exp
));
9798 static void export_vs_psiz_layer_viewport(isel_context
*ctx
, int *next_pos
)
9800 aco_ptr
<Export_instruction
> exp
{create_instruction
<Export_instruction
>(aco_opcode::exp
, Format::EXP
, 4, 0)};
9801 exp
->enabled_mask
= 0;
9802 for (unsigned i
= 0; i
< 4; ++i
)
9803 exp
->operands
[i
] = Operand(v1
);
9804 if (ctx
->outputs
.mask
[VARYING_SLOT_PSIZ
]) {
9805 exp
->operands
[0] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_PSIZ
* 4u]);
9806 exp
->enabled_mask
|= 0x1;
9808 if (ctx
->outputs
.mask
[VARYING_SLOT_LAYER
]) {
9809 exp
->operands
[2] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_LAYER
* 4u]);
9810 exp
->enabled_mask
|= 0x4;
9812 if (ctx
->outputs
.mask
[VARYING_SLOT_VIEWPORT
]) {
9813 if (ctx
->options
->chip_class
< GFX9
) {
9814 exp
->operands
[3] = Operand(ctx
->outputs
.temps
[VARYING_SLOT_VIEWPORT
* 4u]);
9815 exp
->enabled_mask
|= 0x8;
9817 Builder
bld(ctx
->program
, ctx
->block
);
9819 Temp out
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u),
9820 Operand(ctx
->outputs
.temps
[VARYING_SLOT_VIEWPORT
* 4u]));
9821 if (exp
->operands
[2].isTemp())
9822 out
= bld
.vop2(aco_opcode::v_or_b32
, bld
.def(v1
), Operand(out
), exp
->operands
[2]);
9824 exp
->operands
[2] = Operand(out
);
9825 exp
->enabled_mask
|= 0x4;
9828 exp
->valid_mask
= ctx
->options
->chip_class
>= GFX10
&& *next_pos
== 0;
9830 exp
->compressed
= false;
9831 exp
->dest
= V_008DFC_SQ_EXP_POS
+ (*next_pos
)++;
9832 ctx
->block
->instructions
.emplace_back(std::move(exp
));
9835 static void create_export_phis(isel_context
*ctx
)
9837 /* Used when exports are needed, but the output temps are defined in a preceding block.
9838 * This function will set up phis in order to access the outputs in the next block.
9841 assert(ctx
->block
->instructions
.back()->opcode
== aco_opcode::p_logical_start
);
9842 aco_ptr
<Instruction
> logical_start
= aco_ptr
<Instruction
>(ctx
->block
->instructions
.back().release());
9843 ctx
->block
->instructions
.pop_back();
9845 Builder
bld(ctx
->program
, ctx
->block
);
9847 for (unsigned slot
= 0; slot
<= VARYING_SLOT_VAR31
; ++slot
) {
9848 uint64_t mask
= ctx
->outputs
.mask
[slot
];
9849 for (unsigned i
= 0; i
< 4; ++i
) {
9850 if (!(mask
& (1 << i
)))
9853 Temp old
= ctx
->outputs
.temps
[slot
* 4 + i
];
9854 Temp phi
= bld
.pseudo(aco_opcode::p_phi
, bld
.def(v1
), old
, Operand(v1
));
9855 ctx
->outputs
.temps
[slot
* 4 + i
] = phi
;
9859 bld
.insert(std::move(logical_start
));
9862 static void create_vs_exports(isel_context
*ctx
)
9864 assert(ctx
->stage
== vertex_vs
||
9865 ctx
->stage
== tess_eval_vs
||
9866 ctx
->stage
== gs_copy_vs
||
9867 ctx
->stage
== ngg_vertex_gs
||
9868 ctx
->stage
== ngg_tess_eval_gs
);
9870 radv_vs_output_info
*outinfo
= (ctx
->stage
& sw_tes
)
9871 ? &ctx
->program
->info
->tes
.outinfo
9872 : &ctx
->program
->info
->vs
.outinfo
;
9874 if (outinfo
->export_prim_id
&& !(ctx
->stage
& hw_ngg_gs
)) {
9875 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
9876 ctx
->outputs
.temps
[VARYING_SLOT_PRIMITIVE_ID
* 4u] = get_arg(ctx
, ctx
->args
->vs_prim_id
);
9879 if (ctx
->options
->key
.has_multiview_view_index
) {
9880 ctx
->outputs
.mask
[VARYING_SLOT_LAYER
] |= 0x1;
9881 ctx
->outputs
.temps
[VARYING_SLOT_LAYER
* 4u] = as_vgpr(ctx
, get_arg(ctx
, ctx
->args
->ac
.view_index
));
9884 /* the order these position exports are created is important */
9886 bool exported_pos
= export_vs_varying(ctx
, VARYING_SLOT_POS
, true, &next_pos
);
9887 if (outinfo
->writes_pointsize
|| outinfo
->writes_layer
|| outinfo
->writes_viewport_index
) {
9888 export_vs_psiz_layer_viewport(ctx
, &next_pos
);
9889 exported_pos
= true;
9891 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
9892 exported_pos
|= export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, true, &next_pos
);
9893 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
9894 exported_pos
|= export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, true, &next_pos
);
9896 if (ctx
->export_clip_dists
) {
9897 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 0)
9898 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST0
, false, &next_pos
);
9899 if (ctx
->num_clip_distances
+ ctx
->num_cull_distances
> 4)
9900 export_vs_varying(ctx
, VARYING_SLOT_CLIP_DIST1
, false, &next_pos
);
9903 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
9904 if (i
< VARYING_SLOT_VAR0
&&
9905 i
!= VARYING_SLOT_LAYER
&&
9906 i
!= VARYING_SLOT_PRIMITIVE_ID
&&
9907 i
!= VARYING_SLOT_VIEWPORT
)
9910 export_vs_varying(ctx
, i
, false, NULL
);
9914 create_null_export(ctx
);
9917 static bool export_fs_mrt_z(isel_context
*ctx
)
9919 Builder
bld(ctx
->program
, ctx
->block
);
9920 unsigned enabled_channels
= 0;
9924 for (unsigned i
= 0; i
< 4; ++i
) {
9925 values
[i
] = Operand(v1
);
9928 /* Both stencil and sample mask only need 16-bits. */
9929 if (!ctx
->program
->info
->ps
.writes_z
&&
9930 (ctx
->program
->info
->ps
.writes_stencil
||
9931 ctx
->program
->info
->ps
.writes_sample_mask
)) {
9932 compr
= true; /* COMPR flag */
9934 if (ctx
->program
->info
->ps
.writes_stencil
) {
9935 /* Stencil should be in X[23:16]. */
9936 values
[0] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_STENCIL
* 4u]);
9937 values
[0] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(16u), values
[0]);
9938 enabled_channels
|= 0x3;
9941 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
9942 /* SampleMask should be in Y[15:0]. */
9943 values
[1] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_SAMPLE_MASK
* 4u]);
9944 enabled_channels
|= 0xc;
9947 if (ctx
->program
->info
->ps
.writes_z
) {
9948 values
[0] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_DEPTH
* 4u]);
9949 enabled_channels
|= 0x1;
9952 if (ctx
->program
->info
->ps
.writes_stencil
) {
9953 values
[1] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_STENCIL
* 4u]);
9954 enabled_channels
|= 0x2;
9957 if (ctx
->program
->info
->ps
.writes_sample_mask
) {
9958 values
[2] = Operand(ctx
->outputs
.temps
[FRAG_RESULT_SAMPLE_MASK
* 4u]);
9959 enabled_channels
|= 0x4;
9963 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks at the X
9964 * writemask component.
9966 if (ctx
->options
->chip_class
== GFX6
&&
9967 ctx
->options
->family
!= CHIP_OLAND
&&
9968 ctx
->options
->family
!= CHIP_HAINAN
) {
9969 enabled_channels
|= 0x1;
9972 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
9973 enabled_channels
, V_008DFC_SQ_EXP_MRTZ
, compr
);
9978 static bool export_fs_mrt_color(isel_context
*ctx
, int slot
)
9980 Builder
bld(ctx
->program
, ctx
->block
);
9981 unsigned write_mask
= ctx
->outputs
.mask
[slot
];
9984 for (unsigned i
= 0; i
< 4; ++i
) {
9985 if (write_mask
& (1 << i
)) {
9986 values
[i
] = Operand(ctx
->outputs
.temps
[slot
* 4u + i
]);
9988 values
[i
] = Operand(v1
);
9992 unsigned target
, col_format
;
9993 unsigned enabled_channels
= 0;
9994 aco_opcode compr_op
= (aco_opcode
)0;
9996 slot
-= FRAG_RESULT_DATA0
;
9997 target
= V_008DFC_SQ_EXP_MRT
+ slot
;
9998 col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * slot
)) & 0xf;
10000 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> slot
) & 1;
10001 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> slot
) & 1;
10002 bool is_16bit
= values
[0].regClass() == v2b
;
10004 switch (col_format
)
10006 case V_028714_SPI_SHADER_ZERO
:
10007 enabled_channels
= 0; /* writemask */
10008 target
= V_008DFC_SQ_EXP_NULL
;
10011 case V_028714_SPI_SHADER_32_R
:
10012 enabled_channels
= 1;
10015 case V_028714_SPI_SHADER_32_GR
:
10016 enabled_channels
= 0x3;
10019 case V_028714_SPI_SHADER_32_AR
:
10020 if (ctx
->options
->chip_class
>= GFX10
) {
10021 /* Special case: on GFX10, the outputs are different for 32_AR */
10022 enabled_channels
= 0x3;
10023 values
[1] = values
[3];
10024 values
[3] = Operand(v1
);
10026 enabled_channels
= 0x9;
10030 case V_028714_SPI_SHADER_FP16_ABGR
:
10031 enabled_channels
= 0x5;
10032 compr_op
= aco_opcode::v_cvt_pkrtz_f16_f32
;
10034 if (ctx
->options
->chip_class
>= GFX9
) {
10035 /* Pack the FP16 values together instead of converting them to
10036 * FP32 and back to FP16.
10037 * TODO: use p_create_vector and let the compiler optimizes.
10039 compr_op
= aco_opcode::v_pack_b32_f16
;
10041 for (unsigned i
= 0; i
< 4; i
++) {
10042 if ((write_mask
>> i
) & 1)
10043 values
[i
] = bld
.vop1(aco_opcode::v_cvt_f32_f16
, bld
.def(v1
), values
[i
]);
10049 case V_028714_SPI_SHADER_UNORM16_ABGR
:
10050 enabled_channels
= 0x5;
10051 if (is_16bit
&& ctx
->options
->chip_class
>= GFX9
) {
10052 compr_op
= aco_opcode::v_cvt_pknorm_u16_f16
;
10054 compr_op
= aco_opcode::v_cvt_pknorm_u16_f32
;
10058 case V_028714_SPI_SHADER_SNORM16_ABGR
:
10059 enabled_channels
= 0x5;
10060 if (is_16bit
&& ctx
->options
->chip_class
>= GFX9
) {
10061 compr_op
= aco_opcode::v_cvt_pknorm_i16_f16
;
10063 compr_op
= aco_opcode::v_cvt_pknorm_i16_f32
;
10067 case V_028714_SPI_SHADER_UINT16_ABGR
: {
10068 enabled_channels
= 0x5;
10069 compr_op
= aco_opcode::v_cvt_pk_u16_u32
;
10070 if (is_int8
|| is_int10
) {
10072 uint32_t max_rgb
= is_int8
? 255 : is_int10
? 1023 : 0;
10073 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
10075 for (unsigned i
= 0; i
< 4; i
++) {
10076 if ((write_mask
>> i
) & 1) {
10077 values
[i
] = bld
.vop2(aco_opcode::v_min_u32
, bld
.def(v1
),
10078 i
== 3 && is_int10
? Operand(3u) : Operand(max_rgb_val
),
10082 } else if (is_16bit
) {
10083 for (unsigned i
= 0; i
< 4; i
++) {
10084 if ((write_mask
>> i
) & 1) {
10085 Temp tmp
= convert_int(ctx
, bld
, values
[i
].getTemp(), 16, 32, false);
10086 values
[i
] = Operand(tmp
);
10093 case V_028714_SPI_SHADER_SINT16_ABGR
:
10094 enabled_channels
= 0x5;
10095 compr_op
= aco_opcode::v_cvt_pk_i16_i32
;
10096 if (is_int8
|| is_int10
) {
10098 uint32_t max_rgb
= is_int8
? 127 : is_int10
? 511 : 0;
10099 uint32_t min_rgb
= is_int8
? -128 :is_int10
? -512 : 0;
10100 Temp max_rgb_val
= bld
.copy(bld
.def(s1
), Operand(max_rgb
));
10101 Temp min_rgb_val
= bld
.copy(bld
.def(s1
), Operand(min_rgb
));
10103 for (unsigned i
= 0; i
< 4; i
++) {
10104 if ((write_mask
>> i
) & 1) {
10105 values
[i
] = bld
.vop2(aco_opcode::v_min_i32
, bld
.def(v1
),
10106 i
== 3 && is_int10
? Operand(1u) : Operand(max_rgb_val
),
10108 values
[i
] = bld
.vop2(aco_opcode::v_max_i32
, bld
.def(v1
),
10109 i
== 3 && is_int10
? Operand(-2u) : Operand(min_rgb_val
),
10113 } else if (is_16bit
) {
10114 for (unsigned i
= 0; i
< 4; i
++) {
10115 if ((write_mask
>> i
) & 1) {
10116 Temp tmp
= convert_int(ctx
, bld
, values
[i
].getTemp(), 16, 32, true);
10117 values
[i
] = Operand(tmp
);
10123 case V_028714_SPI_SHADER_32_ABGR
:
10124 enabled_channels
= 0xF;
10131 if (target
== V_008DFC_SQ_EXP_NULL
)
10134 /* Replace NaN by zero (only 32-bit) to fix game bugs if requested. */
10135 if (ctx
->options
->enable_mrt_output_nan_fixup
&&
10137 (col_format
== V_028714_SPI_SHADER_32_R
||
10138 col_format
== V_028714_SPI_SHADER_32_GR
||
10139 col_format
== V_028714_SPI_SHADER_32_AR
||
10140 col_format
== V_028714_SPI_SHADER_32_ABGR
||
10141 col_format
== V_028714_SPI_SHADER_FP16_ABGR
)) {
10142 for (int i
= 0; i
< 4; i
++) {
10143 if (!(write_mask
& (1 << i
)))
10146 Temp isnan
= bld
.vopc(aco_opcode::v_cmp_class_f32
,
10147 bld
.hint_vcc(bld
.def(bld
.lm
)), values
[i
],
10148 bld
.copy(bld
.def(v1
), Operand(3u)));
10149 values
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
), values
[i
],
10150 bld
.copy(bld
.def(v1
), Operand(0u)), isnan
);
10154 if ((bool) compr_op
) {
10155 for (int i
= 0; i
< 2; i
++) {
10156 /* check if at least one of the values to be compressed is enabled */
10157 unsigned enabled
= (write_mask
>> (i
*2) | write_mask
>> (i
*2+1)) & 0x1;
10159 enabled_channels
|= enabled
<< (i
*2);
10160 values
[i
] = bld
.vop3(compr_op
, bld
.def(v1
),
10161 values
[i
*2].isUndefined() ? Operand(0u) : values
[i
*2],
10162 values
[i
*2+1].isUndefined() ? Operand(0u): values
[i
*2+1]);
10164 values
[i
] = Operand(v1
);
10167 values
[2] = Operand(v1
);
10168 values
[3] = Operand(v1
);
10170 for (int i
= 0; i
< 4; i
++)
10171 values
[i
] = enabled_channels
& (1 << i
) ? values
[i
] : Operand(v1
);
10174 bld
.exp(aco_opcode::exp
, values
[0], values
[1], values
[2], values
[3],
10175 enabled_channels
, target
, (bool) compr_op
);
10179 static void create_fs_exports(isel_context
*ctx
)
10181 bool exported
= false;
10183 /* Export depth, stencil and sample mask. */
10184 if (ctx
->outputs
.mask
[FRAG_RESULT_DEPTH
] ||
10185 ctx
->outputs
.mask
[FRAG_RESULT_STENCIL
] ||
10186 ctx
->outputs
.mask
[FRAG_RESULT_SAMPLE_MASK
])
10187 exported
|= export_fs_mrt_z(ctx
);
10189 /* Export all color render targets. */
10190 for (unsigned i
= FRAG_RESULT_DATA0
; i
< FRAG_RESULT_DATA7
+ 1; ++i
)
10191 if (ctx
->outputs
.mask
[i
])
10192 exported
|= export_fs_mrt_color(ctx
, i
);
10195 create_null_export(ctx
);
10198 static void write_tcs_tess_factors(isel_context
*ctx
)
10200 unsigned outer_comps
;
10201 unsigned inner_comps
;
10203 switch (ctx
->args
->options
->key
.tcs
.primitive_mode
) {
10220 Builder
bld(ctx
->program
, ctx
->block
);
10222 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10223 if (unlikely(ctx
->program
->chip_class
!= GFX6
&& ctx
->program
->workgroup_size
> ctx
->program
->wave_size
))
10224 bld
.sopp(aco_opcode::s_barrier
);
10226 Temp tcs_rel_ids
= get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
);
10227 Temp invocation_id
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), tcs_rel_ids
, Operand(8u), Operand(5u));
10229 Temp invocation_id_is_zero
= bld
.vopc(aco_opcode::v_cmp_eq_u32
, bld
.hint_vcc(bld
.def(bld
.lm
)), Operand(0u), invocation_id
);
10230 if_context ic_invocation_id_is_zero
;
10231 begin_divergent_if_then(ctx
, &ic_invocation_id_is_zero
, invocation_id_is_zero
);
10232 bld
.reset(ctx
->block
);
10234 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));
10236 std::pair
<Temp
, unsigned> lds_base
= get_tcs_output_lds_offset(ctx
);
10237 unsigned stride
= inner_comps
+ outer_comps
;
10238 unsigned lds_align
= calculate_lds_alignment(ctx
, lds_base
.second
);
10242 assert(stride
<= (sizeof(out
) / sizeof(Temp
)));
10244 if (ctx
->args
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
10246 tf_outer_vec
= load_lds(ctx
, 4, bld
.tmp(v2
), lds_base
.first
, lds_base
.second
+ ctx
->tcs_tess_lvl_out_loc
, lds_align
);
10247 out
[1] = emit_extract_vector(ctx
, tf_outer_vec
, 0, v1
);
10248 out
[0] = emit_extract_vector(ctx
, tf_outer_vec
, 1, v1
);
10250 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
);
10251 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
);
10253 for (unsigned i
= 0; i
< outer_comps
; ++i
)
10254 out
[i
] = emit_extract_vector(ctx
, tf_outer_vec
, i
, v1
);
10255 for (unsigned i
= 0; i
< inner_comps
; ++i
)
10256 out
[outer_comps
+ i
] = emit_extract_vector(ctx
, tf_inner_vec
, i
, v1
);
10259 Temp rel_patch_id
= get_tess_rel_patch_id(ctx
);
10260 Temp tf_base
= get_arg(ctx
, ctx
->args
->tess_factor_offset
);
10261 Temp byte_offset
= bld
.v_mul24_imm(bld
.def(v1
), rel_patch_id
, stride
* 4u);
10262 unsigned tf_const_offset
= 0;
10264 if (ctx
->program
->chip_class
<= GFX8
) {
10265 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
);
10266 if_context ic_rel_patch_id_is_zero
;
10267 begin_divergent_if_then(ctx
, &ic_rel_patch_id_is_zero
, rel_patch_id_is_zero
);
10268 bld
.reset(ctx
->block
);
10270 /* Store the dynamic HS control word. */
10271 Temp control_word
= bld
.copy(bld
.def(v1
), Operand(0x80000000u
));
10272 bld
.mubuf(aco_opcode::buffer_store_dword
,
10273 /* SRSRC */ hs_ring_tess_factor
, /* VADDR */ Operand(v1
), /* SOFFSET */ tf_base
, /* VDATA */ control_word
,
10274 /* immediate OFFSET */ 0, /* OFFEN */ false, /* idxen*/ false, /* addr64 */ false,
10275 /* disable_wqm */ false, /* glc */ true);
10276 tf_const_offset
+= 4;
10278 begin_divergent_if_else(ctx
, &ic_rel_patch_id_is_zero
);
10279 end_divergent_if(ctx
, &ic_rel_patch_id_is_zero
);
10280 bld
.reset(ctx
->block
);
10283 assert(stride
== 2 || stride
== 4 || stride
== 6);
10284 Temp tf_vec
= create_vec_from_array(ctx
, out
, stride
, RegType::vgpr
, 4u);
10285 store_vmem_mubuf(ctx
, tf_vec
, hs_ring_tess_factor
, byte_offset
, tf_base
, tf_const_offset
, 4, (1 << stride
) - 1, true, false);
10287 /* Store to offchip for TES to read - only if TES reads them */
10288 if (ctx
->args
->options
->key
.tcs
.tes_reads_tess_factors
) {
10289 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));
10290 Temp oc_lds
= get_arg(ctx
, ctx
->args
->oc_lds
);
10292 std::pair
<Temp
, unsigned> vmem_offs_outer
= get_tcs_per_patch_output_vmem_offset(ctx
, nullptr, ctx
->tcs_tess_lvl_out_loc
);
10293 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);
10295 if (likely(inner_comps
)) {
10296 std::pair
<Temp
, unsigned> vmem_offs_inner
= get_tcs_per_patch_output_vmem_offset(ctx
, nullptr, ctx
->tcs_tess_lvl_in_loc
);
10297 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);
10301 begin_divergent_if_else(ctx
, &ic_invocation_id_is_zero
);
10302 end_divergent_if(ctx
, &ic_invocation_id_is_zero
);
10305 static void emit_stream_output(isel_context
*ctx
,
10306 Temp
const *so_buffers
,
10307 Temp
const *so_write_offset
,
10308 const struct radv_stream_output
*output
)
10310 unsigned num_comps
= util_bitcount(output
->component_mask
);
10311 unsigned writemask
= (1 << num_comps
) - 1;
10312 unsigned loc
= output
->location
;
10313 unsigned buf
= output
->buffer
;
10315 assert(num_comps
&& num_comps
<= 4);
10316 if (!num_comps
|| num_comps
> 4)
10319 unsigned start
= ffs(output
->component_mask
) - 1;
10322 bool all_undef
= true;
10323 assert(ctx
->stage
& hw_vs
);
10324 for (unsigned i
= 0; i
< num_comps
; i
++) {
10325 out
[i
] = ctx
->outputs
.temps
[loc
* 4 + start
+ i
];
10326 all_undef
= all_undef
&& !out
[i
].id();
10331 while (writemask
) {
10333 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
10334 if (count
== 3 && ctx
->options
->chip_class
== GFX6
) {
10335 /* GFX6 doesn't support storing vec3, split it. */
10336 writemask
|= 1u << (start
+ 2);
10340 unsigned offset
= output
->offset
+ start
* 4;
10342 Temp write_data
= {ctx
->program
->allocateId(), RegClass(RegType::vgpr
, count
)};
10343 aco_ptr
<Pseudo_instruction
> vec
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, count
, 1)};
10344 for (int i
= 0; i
< count
; ++i
)
10345 vec
->operands
[i
] = (ctx
->outputs
.mask
[loc
] & 1 << (start
+ i
)) ? Operand(out
[start
+ i
]) : Operand(0u);
10346 vec
->definitions
[0] = Definition(write_data
);
10347 ctx
->block
->instructions
.emplace_back(std::move(vec
));
10352 opcode
= aco_opcode::buffer_store_dword
;
10355 opcode
= aco_opcode::buffer_store_dwordx2
;
10358 opcode
= aco_opcode::buffer_store_dwordx3
;
10361 opcode
= aco_opcode::buffer_store_dwordx4
;
10364 unreachable("Unsupported dword count.");
10367 aco_ptr
<MUBUF_instruction
> store
{create_instruction
<MUBUF_instruction
>(opcode
, Format::MUBUF
, 4, 0)};
10368 store
->operands
[0] = Operand(so_buffers
[buf
]);
10369 store
->operands
[1] = Operand(so_write_offset
[buf
]);
10370 store
->operands
[2] = Operand((uint32_t) 0);
10371 store
->operands
[3] = Operand(write_data
);
10372 if (offset
> 4095) {
10373 /* Don't think this can happen in RADV, but maybe GL? It's easy to do this anyway. */
10374 Builder
bld(ctx
->program
, ctx
->block
);
10375 store
->operands
[0] = bld
.vadd32(bld
.def(v1
), Operand(offset
), Operand(so_write_offset
[buf
]));
10377 store
->offset
= offset
;
10379 store
->offen
= true;
10381 store
->dlc
= false;
10383 store
->can_reorder
= true;
10384 ctx
->block
->instructions
.emplace_back(std::move(store
));
10388 static void emit_streamout(isel_context
*ctx
, unsigned stream
)
10390 Builder
bld(ctx
->program
, ctx
->block
);
10392 Temp so_buffers
[4];
10393 Temp buf_ptr
= convert_pointer_to_64_bit(ctx
, get_arg(ctx
, ctx
->args
->streamout_buffers
));
10394 for (unsigned i
= 0; i
< 4; i
++) {
10395 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
10399 Operand off
= bld
.copy(bld
.def(s1
), Operand(i
* 16u));
10400 so_buffers
[i
] = bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), buf_ptr
, off
);
10403 Temp so_vtx_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10404 get_arg(ctx
, ctx
->args
->streamout_config
), Operand(0x70010u
));
10406 Temp tid
= emit_mbcnt(ctx
, bld
.def(v1
));
10408 Temp can_emit
= bld
.vopc(aco_opcode::v_cmp_gt_i32
, bld
.def(bld
.lm
), so_vtx_count
, tid
);
10411 begin_divergent_if_then(ctx
, &ic
, can_emit
);
10413 bld
.reset(ctx
->block
);
10415 Temp so_write_index
= bld
.vadd32(bld
.def(v1
), get_arg(ctx
, ctx
->args
->streamout_write_idx
), tid
);
10417 Temp so_write_offset
[4];
10419 for (unsigned i
= 0; i
< 4; i
++) {
10420 unsigned stride
= ctx
->program
->info
->so
.strides
[i
];
10425 Temp offset
= bld
.sop2(aco_opcode::s_add_i32
, bld
.def(s1
), bld
.def(s1
, scc
),
10426 get_arg(ctx
, ctx
->args
->streamout_write_idx
),
10427 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
10428 Temp new_offset
= bld
.vadd32(bld
.def(v1
), offset
, tid
);
10430 so_write_offset
[i
] = bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), new_offset
);
10432 Temp offset
= bld
.v_mul_imm(bld
.def(v1
), so_write_index
, stride
* 4u);
10433 Temp offset2
= bld
.sop2(aco_opcode::s_mul_i32
, bld
.def(s1
), Operand(4u),
10434 get_arg(ctx
, ctx
->args
->streamout_offset
[i
]));
10435 so_write_offset
[i
] = bld
.vadd32(bld
.def(v1
), offset
, offset2
);
10439 for (unsigned i
= 0; i
< ctx
->program
->info
->so
.num_outputs
; i
++) {
10440 struct radv_stream_output
*output
=
10441 &ctx
->program
->info
->so
.outputs
[i
];
10442 if (stream
!= output
->stream
)
10445 emit_stream_output(ctx
, so_buffers
, so_write_offset
, output
);
10448 begin_divergent_if_else(ctx
, &ic
);
10449 end_divergent_if(ctx
, &ic
);
10452 } /* end namespace */
10454 void fix_ls_vgpr_init_bug(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
10456 assert(ctx
->shader
->info
.stage
== MESA_SHADER_VERTEX
);
10457 Builder
bld(ctx
->program
, ctx
->block
);
10458 constexpr unsigned hs_idx
= 1u;
10459 Builder::Result hs_thread_count
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10460 get_arg(ctx
, ctx
->args
->merged_wave_info
),
10461 Operand((8u << 16) | (hs_idx
* 8u)));
10462 Temp ls_has_nonzero_hs_threads
= bool_to_vector_condition(ctx
, hs_thread_count
.def(1).getTemp());
10464 /* If there are no HS threads, SPI mistakenly loads the LS VGPRs starting at VGPR 0. */
10466 Temp instance_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10467 get_arg(ctx
, ctx
->args
->rel_auto_id
),
10468 get_arg(ctx
, ctx
->args
->ac
.instance_id
),
10469 ls_has_nonzero_hs_threads
);
10470 Temp rel_auto_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10471 get_arg(ctx
, ctx
->args
->ac
.tcs_rel_ids
),
10472 get_arg(ctx
, ctx
->args
->rel_auto_id
),
10473 ls_has_nonzero_hs_threads
);
10474 Temp vertex_id
= bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10475 get_arg(ctx
, ctx
->args
->ac
.tcs_patch_id
),
10476 get_arg(ctx
, ctx
->args
->ac
.vertex_id
),
10477 ls_has_nonzero_hs_threads
);
10479 ctx
->arg_temps
[ctx
->args
->ac
.instance_id
.arg_index
] = instance_id
;
10480 ctx
->arg_temps
[ctx
->args
->rel_auto_id
.arg_index
] = rel_auto_id
;
10481 ctx
->arg_temps
[ctx
->args
->ac
.vertex_id
.arg_index
] = vertex_id
;
10484 void split_arguments(isel_context
*ctx
, Pseudo_instruction
*startpgm
)
10486 /* Split all arguments except for the first (ring_offsets) and the last
10487 * (exec) so that the dead channels don't stay live throughout the program.
10489 for (int i
= 1; i
< startpgm
->definitions
.size() - 1; i
++) {
10490 if (startpgm
->definitions
[i
].regClass().size() > 1) {
10491 emit_split_vector(ctx
, startpgm
->definitions
[i
].getTemp(),
10492 startpgm
->definitions
[i
].regClass().size());
10497 void handle_bc_optimize(isel_context
*ctx
)
10499 /* needed when SPI_PS_IN_CONTROL.BC_OPTIMIZE_DISABLE is set to 0 */
10500 Builder
bld(ctx
->program
, ctx
->block
);
10501 uint32_t spi_ps_input_ena
= ctx
->program
->config
->spi_ps_input_ena
;
10502 bool uses_center
= G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
);
10503 bool uses_centroid
= G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) || G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
);
10504 ctx
->persp_centroid
= get_arg(ctx
, ctx
->args
->ac
.persp_centroid
);
10505 ctx
->linear_centroid
= get_arg(ctx
, ctx
->args
->ac
.linear_centroid
);
10506 if (uses_center
&& uses_centroid
) {
10507 Temp sel
= bld
.vopc_e64(aco_opcode::v_cmp_lt_i32
, bld
.hint_vcc(bld
.def(bld
.lm
)),
10508 get_arg(ctx
, ctx
->args
->ac
.prim_mask
), Operand(0u));
10510 if (G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
)) {
10512 for (unsigned i
= 0; i
< 2; i
++) {
10513 Temp persp_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_centroid
), i
, v1
);
10514 Temp persp_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.persp_center
), i
, v1
);
10515 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10516 persp_centroid
, persp_center
, sel
);
10518 ctx
->persp_centroid
= bld
.tmp(v2
);
10519 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->persp_centroid
),
10520 Operand(new_coord
[0]), Operand(new_coord
[1]));
10521 emit_split_vector(ctx
, ctx
->persp_centroid
, 2);
10524 if (G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
)) {
10526 for (unsigned i
= 0; i
< 2; i
++) {
10527 Temp linear_centroid
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_centroid
), i
, v1
);
10528 Temp linear_center
= emit_extract_vector(ctx
, get_arg(ctx
, ctx
->args
->ac
.linear_center
), i
, v1
);
10529 new_coord
[i
] = bld
.vop2(aco_opcode::v_cndmask_b32
, bld
.def(v1
),
10530 linear_centroid
, linear_center
, sel
);
10532 ctx
->linear_centroid
= bld
.tmp(v2
);
10533 bld
.pseudo(aco_opcode::p_create_vector
, Definition(ctx
->linear_centroid
),
10534 Operand(new_coord
[0]), Operand(new_coord
[1]));
10535 emit_split_vector(ctx
, ctx
->linear_centroid
, 2);
10540 void setup_fp_mode(isel_context
*ctx
, nir_shader
*shader
)
10542 Program
*program
= ctx
->program
;
10544 unsigned float_controls
= shader
->info
.float_controls_execution_mode
;
10546 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
=
10547 float_controls
& FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
;
10548 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
=
10549 float_controls
& (FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
|
10550 FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
);
10552 program
->next_fp_mode
.must_flush_denorms32
=
10553 float_controls
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
;
10554 program
->next_fp_mode
.must_flush_denorms16_64
=
10555 float_controls
& (FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
|
10556 FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
);
10558 program
->next_fp_mode
.care_about_round32
=
10559 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
);
10561 program
->next_fp_mode
.care_about_round16_64
=
10562 float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
|
10563 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
);
10565 /* default to preserving fp16 and fp64 denorms, since it's free for fp64 and
10566 * the precision seems needed for Wolfenstein: Youngblood to render correctly */
10567 if (program
->next_fp_mode
.must_flush_denorms16_64
)
10568 program
->next_fp_mode
.denorm16_64
= 0;
10570 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
10572 /* preserving fp32 denorms is expensive, so only do it if asked */
10573 if (float_controls
& FLOAT_CONTROLS_DENORM_PRESERVE_FP32
)
10574 program
->next_fp_mode
.denorm32
= fp_denorm_keep
;
10576 program
->next_fp_mode
.denorm32
= 0;
10578 if (float_controls
& FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
)
10579 program
->next_fp_mode
.round32
= fp_round_tz
;
10581 program
->next_fp_mode
.round32
= fp_round_ne
;
10583 if (float_controls
& (FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
| FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
))
10584 program
->next_fp_mode
.round16_64
= fp_round_tz
;
10586 program
->next_fp_mode
.round16_64
= fp_round_ne
;
10588 ctx
->block
->fp_mode
= program
->next_fp_mode
;
10591 void cleanup_cfg(Program
*program
)
10593 /* create linear_succs/logical_succs */
10594 for (Block
& BB
: program
->blocks
) {
10595 for (unsigned idx
: BB
.linear_preds
)
10596 program
->blocks
[idx
].linear_succs
.emplace_back(BB
.index
);
10597 for (unsigned idx
: BB
.logical_preds
)
10598 program
->blocks
[idx
].logical_succs
.emplace_back(BB
.index
);
10602 Temp
merged_wave_info_to_mask(isel_context
*ctx
, unsigned i
)
10604 Builder
bld(ctx
->program
, ctx
->block
);
10606 /* The s_bfm only cares about s0.u[5:0] so we don't need either s_bfe nor s_and here */
10607 Temp count
= i
== 0
10608 ? get_arg(ctx
, ctx
->args
->merged_wave_info
)
10609 : bld
.sop2(aco_opcode::s_lshr_b32
, bld
.def(s1
), bld
.def(s1
, scc
),
10610 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(i
* 8u));
10612 Temp mask
= bld
.sop2(aco_opcode::s_bfm_b64
, bld
.def(s2
), count
, Operand(0u));
10615 if (ctx
->program
->wave_size
== 64) {
10616 /* Special case for 64 active invocations, because 64 doesn't work with s_bfm */
10617 Temp active_64
= bld
.sopc(aco_opcode::s_bitcmp1_b32
, bld
.def(s1
, scc
), count
, Operand(6u /* log2(64) */));
10618 cond
= bld
.sop2(Builder::s_cselect
, bld
.def(bld
.lm
), Operand(-1u), mask
, bld
.scc(active_64
));
10620 /* We use s_bfm_b64 (not _b32) which works with 32, but we need to extract the lower half of the register */
10621 cond
= emit_extract_vector(ctx
, mask
, 0, bld
.lm
);
10627 bool ngg_early_prim_export(isel_context
*ctx
)
10629 /* TODO: Check edge flags, and if they are written, return false. (Needed for OpenGL, not for Vulkan.) */
10633 void ngg_emit_sendmsg_gs_alloc_req(isel_context
*ctx
)
10635 Builder
bld(ctx
->program
, ctx
->block
);
10637 /* It is recommended to do the GS_ALLOC_REQ as soon and as quickly as possible, so we set the maximum priority (3). */
10638 bld
.sopp(aco_opcode::s_setprio
, -1u, 0x3u
);
10640 /* Get the id of the current wave within the threadgroup (workgroup) */
10641 Builder::Result wave_id_in_tg
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10642 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(24u | (4u << 16)));
10644 /* Execute the following code only on the first wave (wave id 0),
10645 * use the SCC def to tell if the wave id is zero or not.
10647 Temp cond
= wave_id_in_tg
.def(1).getTemp();
10649 begin_uniform_if_then(ctx
, &ic
, cond
);
10650 begin_uniform_if_else(ctx
, &ic
);
10651 bld
.reset(ctx
->block
);
10653 /* Number of vertices output by VS/TES */
10654 Temp vtx_cnt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10655 get_arg(ctx
, ctx
->args
->gs_tg_info
), Operand(12u | (9u << 16u)));
10656 /* Number of primitives output by VS/TES */
10657 Temp prm_cnt
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10658 get_arg(ctx
, ctx
->args
->gs_tg_info
), Operand(22u | (9u << 16u)));
10660 /* Put the number of vertices and primitives into m0 for the GS_ALLOC_REQ */
10661 Temp tmp
= bld
.sop2(aco_opcode::s_lshl_b32
, bld
.def(s1
), bld
.def(s1
, scc
), prm_cnt
, Operand(12u));
10662 tmp
= bld
.sop2(aco_opcode::s_or_b32
, bld
.m0(bld
.def(s1
)), bld
.def(s1
, scc
), tmp
, vtx_cnt
);
10664 /* Request the SPI to allocate space for the primitives and vertices that will be exported by the threadgroup. */
10665 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(tmp
), -1, sendmsg_gs_alloc_req
);
10667 end_uniform_if(ctx
, &ic
);
10669 /* After the GS_ALLOC_REQ is done, reset priority to default (0). */
10670 bld
.reset(ctx
->block
);
10671 bld
.sopp(aco_opcode::s_setprio
, -1u, 0x0u
);
10674 Temp
ngg_get_prim_exp_arg(isel_context
*ctx
, unsigned num_vertices
, const Temp vtxindex
[])
10676 Builder
bld(ctx
->program
, ctx
->block
);
10678 if (ctx
->args
->options
->key
.vs_common_out
.as_ngg_passthrough
) {
10679 return get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]);
10682 Temp gs_invocation_id
= get_arg(ctx
, ctx
->args
->ac
.gs_invocation_id
);
10685 for (unsigned i
= 0; i
< num_vertices
; ++i
) {
10686 assert(vtxindex
[i
].id());
10689 tmp
= bld
.vop3(aco_opcode::v_lshl_add_u32
, bld
.def(v1
), vtxindex
[i
], Operand(10u * i
), tmp
);
10693 /* The initial edge flag is always false in tess eval shaders. */
10694 if (ctx
->stage
== ngg_vertex_gs
) {
10695 Temp edgeflag
= bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
), gs_invocation_id
, Operand(8 + i
), Operand(1u));
10696 tmp
= bld
.vop3(aco_opcode::v_lshl_add_u32
, bld
.def(v1
), edgeflag
, Operand(10u * i
+ 9u), tmp
);
10700 /* TODO: Set isnull field in case of merged NGG VS+GS. */
10705 void ngg_emit_prim_export(isel_context
*ctx
, unsigned num_vertices_per_primitive
, const Temp vtxindex
[])
10707 Builder
bld(ctx
->program
, ctx
->block
);
10708 Temp prim_exp_arg
= ngg_get_prim_exp_arg(ctx
, num_vertices_per_primitive
, vtxindex
);
10710 bld
.exp(aco_opcode::exp
, prim_exp_arg
, Operand(v1
), Operand(v1
), Operand(v1
),
10711 1 /* enabled mask */, V_008DFC_SQ_EXP_PRIM
/* dest */,
10712 false /* compressed */, true/* done */, false /* valid mask */);
10715 void ngg_emit_nogs_gsthreads(isel_context
*ctx
)
10717 /* Emit the things that NGG GS threads need to do, for shaders that don't have SW GS.
10718 * These must always come before VS exports.
10720 * It is recommended to do these as early as possible. They can be at the beginning when
10721 * there is no SW GS and the shader doesn't write edge flags.
10725 Temp is_gs_thread
= merged_wave_info_to_mask(ctx
, 1);
10726 begin_divergent_if_then(ctx
, &ic
, is_gs_thread
);
10728 Builder
bld(ctx
->program
, ctx
->block
);
10729 constexpr unsigned max_vertices_per_primitive
= 3;
10730 unsigned num_vertices_per_primitive
= max_vertices_per_primitive
;
10732 if (ctx
->stage
== ngg_vertex_gs
) {
10733 /* TODO: optimize for points & lines */
10734 } else if (ctx
->stage
== ngg_tess_eval_gs
) {
10735 if (ctx
->shader
->info
.tess
.point_mode
)
10736 num_vertices_per_primitive
= 1;
10737 else if (ctx
->shader
->info
.tess
.primitive_mode
== GL_ISOLINES
)
10738 num_vertices_per_primitive
= 2;
10740 unreachable("Unsupported NGG shader stage");
10743 Temp vtxindex
[max_vertices_per_primitive
];
10744 vtxindex
[0] = bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
),
10745 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]));
10746 vtxindex
[1] = num_vertices_per_primitive
< 2 ? Temp(0, v1
) :
10747 bld
.vop3(aco_opcode::v_bfe_u32
, bld
.def(v1
),
10748 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[0]), Operand(16u), Operand(16u));
10749 vtxindex
[2] = num_vertices_per_primitive
< 3 ? Temp(0, v1
) :
10750 bld
.vop2(aco_opcode::v_and_b32
, bld
.def(v1
), Operand(0xffffu
),
10751 get_arg(ctx
, ctx
->args
->gs_vtx_offset
[2]));
10753 /* Export primitive data to the index buffer. */
10754 ngg_emit_prim_export(ctx
, num_vertices_per_primitive
, vtxindex
);
10756 /* Export primitive ID. */
10757 if (ctx
->stage
== ngg_vertex_gs
&& ctx
->args
->options
->key
.vs_common_out
.export_prim_id
) {
10758 /* Copy Primitive IDs from GS threads to the LDS address corresponding to the ES thread of the provoking vertex. */
10759 Temp prim_id
= get_arg(ctx
, ctx
->args
->ac
.gs_prim_id
);
10760 Temp provoking_vtx_index
= vtxindex
[0];
10761 Temp addr
= bld
.v_mul_imm(bld
.def(v1
), provoking_vtx_index
, 4u);
10763 store_lds(ctx
, 4, prim_id
, 0x1u
, addr
, 0u, 4u);
10766 begin_divergent_if_else(ctx
, &ic
);
10767 end_divergent_if(ctx
, &ic
);
10770 void ngg_emit_nogs_output(isel_context
*ctx
)
10772 /* Emits NGG GS output, for stages that don't have SW GS. */
10775 Builder
bld(ctx
->program
, ctx
->block
);
10776 bool late_prim_export
= !ngg_early_prim_export(ctx
);
10778 /* NGG streamout is currently disabled by default. */
10779 assert(!ctx
->args
->shader_info
->so
.num_outputs
);
10781 if (late_prim_export
) {
10782 /* VS exports are output to registers in a predecessor block. Emit phis to get them into this block. */
10783 create_export_phis(ctx
);
10784 /* Do what we need to do in the GS threads. */
10785 ngg_emit_nogs_gsthreads(ctx
);
10787 /* What comes next should be executed on ES threads. */
10788 Temp is_es_thread
= merged_wave_info_to_mask(ctx
, 0);
10789 begin_divergent_if_then(ctx
, &ic
, is_es_thread
);
10790 bld
.reset(ctx
->block
);
10793 /* Export VS outputs */
10794 ctx
->block
->kind
|= block_kind_export_end
;
10795 create_vs_exports(ctx
);
10797 /* Export primitive ID */
10798 if (ctx
->args
->options
->key
.vs_common_out
.export_prim_id
) {
10801 if (ctx
->stage
== ngg_vertex_gs
) {
10802 /* Wait for GS threads to store primitive ID in LDS. */
10803 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10804 bld
.sopp(aco_opcode::s_barrier
);
10806 /* Calculate LDS address where the GS threads stored the primitive ID. */
10807 Temp wave_id_in_tg
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10808 get_arg(ctx
, ctx
->args
->merged_wave_info
), Operand(24u | (4u << 16)));
10809 Temp thread_id_in_wave
= emit_mbcnt(ctx
, bld
.def(v1
));
10810 Temp wave_id_mul
= bld
.v_mul24_imm(bld
.def(v1
), as_vgpr(ctx
, wave_id_in_tg
), ctx
->program
->wave_size
);
10811 Temp thread_id_in_tg
= bld
.vadd32(bld
.def(v1
), Operand(wave_id_mul
), Operand(thread_id_in_wave
));
10812 Temp addr
= bld
.v_mul24_imm(bld
.def(v1
), thread_id_in_tg
, 4u);
10814 /* Load primitive ID from LDS. */
10815 prim_id
= load_lds(ctx
, 4, bld
.tmp(v1
), addr
, 0u, 4u);
10816 } else if (ctx
->stage
== ngg_tess_eval_gs
) {
10817 /* TES: Just use the patch ID as the primitive ID. */
10818 prim_id
= get_arg(ctx
, ctx
->args
->ac
.tes_patch_id
);
10820 unreachable("unsupported NGG shader stage.");
10823 ctx
->outputs
.mask
[VARYING_SLOT_PRIMITIVE_ID
] |= 0x1;
10824 ctx
->outputs
.temps
[VARYING_SLOT_PRIMITIVE_ID
* 4u] = prim_id
;
10826 export_vs_varying(ctx
, VARYING_SLOT_PRIMITIVE_ID
, false, nullptr);
10829 if (late_prim_export
) {
10830 begin_divergent_if_else(ctx
, &ic
);
10831 end_divergent_if(ctx
, &ic
);
10832 bld
.reset(ctx
->block
);
10836 void select_program(Program
*program
,
10837 unsigned shader_count
,
10838 struct nir_shader
*const *shaders
,
10839 ac_shader_config
* config
,
10840 struct radv_shader_args
*args
)
10842 isel_context ctx
= setup_isel_context(program
, shader_count
, shaders
, config
, args
, false);
10843 if_context ic_merged_wave_info
;
10844 bool ngg_no_gs
= ctx
.stage
== ngg_vertex_gs
|| ctx
.stage
== ngg_tess_eval_gs
;
10846 for (unsigned i
= 0; i
< shader_count
; i
++) {
10847 nir_shader
*nir
= shaders
[i
];
10848 init_context(&ctx
, nir
);
10850 setup_fp_mode(&ctx
, nir
);
10853 /* needs to be after init_context() for FS */
10854 Pseudo_instruction
*startpgm
= add_startpgm(&ctx
);
10855 append_logical_start(ctx
.block
);
10857 if (unlikely(args
->options
->has_ls_vgpr_init_bug
&& ctx
.stage
== vertex_tess_control_hs
))
10858 fix_ls_vgpr_init_bug(&ctx
, startpgm
);
10860 split_arguments(&ctx
, startpgm
);
10864 ngg_emit_sendmsg_gs_alloc_req(&ctx
);
10866 if (ngg_early_prim_export(&ctx
))
10867 ngg_emit_nogs_gsthreads(&ctx
);
10870 /* In a merged VS+TCS HS, the VS implementation can be completely empty. */
10871 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
10872 bool empty_shader
= nir_cf_list_is_empty_block(&func
->body
) &&
10873 ((nir
->info
.stage
== MESA_SHADER_VERTEX
&&
10874 (ctx
.stage
== vertex_tess_control_hs
|| ctx
.stage
== vertex_geometry_gs
)) ||
10875 (nir
->info
.stage
== MESA_SHADER_TESS_EVAL
&&
10876 ctx
.stage
== tess_eval_geometry_gs
));
10878 bool check_merged_wave_info
= ctx
.tcs_in_out_eq
? i
== 0 : ((shader_count
>= 2 && !empty_shader
) || ngg_no_gs
);
10879 bool endif_merged_wave_info
= ctx
.tcs_in_out_eq
? i
== 1 : check_merged_wave_info
;
10880 if (check_merged_wave_info
) {
10881 Temp cond
= merged_wave_info_to_mask(&ctx
, i
);
10882 begin_divergent_if_then(&ctx
, &ic_merged_wave_info
, cond
);
10886 Builder
bld(ctx
.program
, ctx
.block
);
10888 bld
.barrier(aco_opcode::p_memory_barrier_shared
);
10889 bld
.sopp(aco_opcode::s_barrier
);
10891 if (ctx
.stage
== vertex_geometry_gs
|| ctx
.stage
== tess_eval_geometry_gs
) {
10892 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));
10894 } else if (ctx
.stage
== geometry_gs
)
10895 ctx
.gs_wave_id
= get_arg(&ctx
, args
->gs_wave_id
);
10897 if (ctx
.stage
== fragment_fs
)
10898 handle_bc_optimize(&ctx
);
10900 visit_cf_list(&ctx
, &func
->body
);
10902 if (ctx
.program
->info
->so
.num_outputs
&& (ctx
.stage
& hw_vs
))
10903 emit_streamout(&ctx
, 0);
10905 if (ctx
.stage
& hw_vs
) {
10906 create_vs_exports(&ctx
);
10907 ctx
.block
->kind
|= block_kind_export_end
;
10908 } else if (ngg_no_gs
&& ngg_early_prim_export(&ctx
)) {
10909 ngg_emit_nogs_output(&ctx
);
10910 } else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
10911 Builder
bld(ctx
.program
, ctx
.block
);
10912 bld
.barrier(aco_opcode::p_memory_barrier_gs_data
);
10913 bld
.sopp(aco_opcode::s_sendmsg
, bld
.m0(ctx
.gs_wave_id
), -1, sendmsg_gs_done(false, false, 0));
10914 } else if (nir
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
10915 write_tcs_tess_factors(&ctx
);
10918 if (ctx
.stage
== fragment_fs
) {
10919 create_fs_exports(&ctx
);
10920 ctx
.block
->kind
|= block_kind_export_end
;
10923 if (endif_merged_wave_info
) {
10924 begin_divergent_if_else(&ctx
, &ic_merged_wave_info
);
10925 end_divergent_if(&ctx
, &ic_merged_wave_info
);
10928 if (ngg_no_gs
&& !ngg_early_prim_export(&ctx
))
10929 ngg_emit_nogs_output(&ctx
);
10931 if (i
== 0 && ctx
.stage
== vertex_tess_control_hs
&& ctx
.tcs_in_out_eq
) {
10932 /* Outputs of the previous stage are inputs to the next stage */
10933 ctx
.inputs
= ctx
.outputs
;
10934 ctx
.outputs
= shader_io_state();
10938 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
10940 append_logical_end(ctx
.block
);
10941 ctx
.block
->kind
|= block_kind_uniform
;
10942 Builder
bld(ctx
.program
, ctx
.block
);
10943 if (ctx
.program
->wb_smem_l1_on_end
)
10944 bld
.smem(aco_opcode::s_dcache_wb
, false);
10945 bld
.sopp(aco_opcode::s_endpgm
);
10947 cleanup_cfg(program
);
10950 void select_gs_copy_shader(Program
*program
, struct nir_shader
*gs_shader
,
10951 ac_shader_config
* config
,
10952 struct radv_shader_args
*args
)
10954 isel_context ctx
= setup_isel_context(program
, 1, &gs_shader
, config
, args
, true);
10956 program
->next_fp_mode
.preserve_signed_zero_inf_nan32
= false;
10957 program
->next_fp_mode
.preserve_signed_zero_inf_nan16_64
= false;
10958 program
->next_fp_mode
.must_flush_denorms32
= false;
10959 program
->next_fp_mode
.must_flush_denorms16_64
= false;
10960 program
->next_fp_mode
.care_about_round32
= false;
10961 program
->next_fp_mode
.care_about_round16_64
= false;
10962 program
->next_fp_mode
.denorm16_64
= fp_denorm_keep
;
10963 program
->next_fp_mode
.denorm32
= 0;
10964 program
->next_fp_mode
.round32
= fp_round_ne
;
10965 program
->next_fp_mode
.round16_64
= fp_round_ne
;
10966 ctx
.block
->fp_mode
= program
->next_fp_mode
;
10968 add_startpgm(&ctx
);
10969 append_logical_start(ctx
.block
);
10971 Builder
bld(ctx
.program
, ctx
.block
);
10973 Temp gsvs_ring
= bld
.smem(aco_opcode::s_load_dwordx4
, bld
.def(s4
), program
->private_segment_buffer
, Operand(RING_GSVS_VS
* 16u));
10975 Operand
stream_id(0u);
10976 if (args
->shader_info
->so
.num_outputs
)
10977 stream_id
= bld
.sop2(aco_opcode::s_bfe_u32
, bld
.def(s1
), bld
.def(s1
, scc
),
10978 get_arg(&ctx
, ctx
.args
->streamout_config
), Operand(0x20018u
));
10980 Temp vtx_offset
= bld
.vop2(aco_opcode::v_lshlrev_b32
, bld
.def(v1
), Operand(2u), get_arg(&ctx
, ctx
.args
->ac
.vertex_id
));
10982 std::stack
<Block
> endif_blocks
;
10984 for (unsigned stream
= 0; stream
< 4; stream
++) {
10985 if (stream_id
.isConstant() && stream
!= stream_id
.constantValue())
10988 unsigned num_components
= args
->shader_info
->gs
.num_stream_output_components
[stream
];
10989 if (stream
> 0 && (!num_components
|| !args
->shader_info
->so
.num_outputs
))
10992 memset(ctx
.outputs
.mask
, 0, sizeof(ctx
.outputs
.mask
));
10994 unsigned BB_if_idx
= ctx
.block
->index
;
10995 Block BB_endif
= Block();
10996 if (!stream_id
.isConstant()) {
10998 Temp cond
= bld
.sopc(aco_opcode::s_cmp_eq_u32
, bld
.def(s1
, scc
), stream_id
, Operand(stream
));
10999 append_logical_end(ctx
.block
);
11000 ctx
.block
->kind
|= block_kind_uniform
;
11001 bld
.branch(aco_opcode::p_cbranch_z
, cond
);
11003 BB_endif
.kind
|= ctx
.block
->kind
& block_kind_top_level
;
11005 ctx
.block
= ctx
.program
->create_and_insert_block();
11006 add_edge(BB_if_idx
, ctx
.block
);
11007 bld
.reset(ctx
.block
);
11008 append_logical_start(ctx
.block
);
11011 unsigned offset
= 0;
11012 for (unsigned i
= 0; i
<= VARYING_SLOT_VAR31
; ++i
) {
11013 if (args
->shader_info
->gs
.output_streams
[i
] != stream
)
11016 unsigned output_usage_mask
= args
->shader_info
->gs
.output_usage_mask
[i
];
11017 unsigned length
= util_last_bit(output_usage_mask
);
11018 for (unsigned j
= 0; j
< length
; ++j
) {
11019 if (!(output_usage_mask
& (1 << j
)))
11022 unsigned const_offset
= offset
* args
->shader_info
->gs
.vertices_out
* 16 * 4;
11023 Temp voffset
= vtx_offset
;
11024 if (const_offset
>= 4096u) {
11025 voffset
= bld
.vadd32(bld
.def(v1
), Operand(const_offset
/ 4096u * 4096u), voffset
);
11026 const_offset
%= 4096u;
11029 aco_ptr
<MUBUF_instruction
> mubuf
{create_instruction
<MUBUF_instruction
>(aco_opcode::buffer_load_dword
, Format::MUBUF
, 3, 1)};
11030 mubuf
->definitions
[0] = bld
.def(v1
);
11031 mubuf
->operands
[0] = Operand(gsvs_ring
);
11032 mubuf
->operands
[1] = Operand(voffset
);
11033 mubuf
->operands
[2] = Operand(0u);
11034 mubuf
->offen
= true;
11035 mubuf
->offset
= const_offset
;
11038 mubuf
->dlc
= args
->options
->chip_class
>= GFX10
;
11039 mubuf
->barrier
= barrier_none
;
11040 mubuf
->can_reorder
= true;
11042 ctx
.outputs
.mask
[i
] |= 1 << j
;
11043 ctx
.outputs
.temps
[i
* 4u + j
] = mubuf
->definitions
[0].getTemp();
11045 bld
.insert(std::move(mubuf
));
11051 if (args
->shader_info
->so
.num_outputs
) {
11052 emit_streamout(&ctx
, stream
);
11053 bld
.reset(ctx
.block
);
11057 create_vs_exports(&ctx
);
11058 ctx
.block
->kind
|= block_kind_export_end
;
11061 if (!stream_id
.isConstant()) {
11062 append_logical_end(ctx
.block
);
11064 /* branch from then block to endif block */
11065 bld
.branch(aco_opcode::p_branch
);
11066 add_edge(ctx
.block
->index
, &BB_endif
);
11067 ctx
.block
->kind
|= block_kind_uniform
;
11069 /* emit else block */
11070 ctx
.block
= ctx
.program
->create_and_insert_block();
11071 add_edge(BB_if_idx
, ctx
.block
);
11072 bld
.reset(ctx
.block
);
11073 append_logical_start(ctx
.block
);
11075 endif_blocks
.push(std::move(BB_endif
));
11079 while (!endif_blocks
.empty()) {
11080 Block BB_endif
= std::move(endif_blocks
.top());
11081 endif_blocks
.pop();
11083 Block
*BB_else
= ctx
.block
;
11085 append_logical_end(BB_else
);
11086 /* branch from else block to endif block */
11087 bld
.branch(aco_opcode::p_branch
);
11088 add_edge(BB_else
->index
, &BB_endif
);
11089 BB_else
->kind
|= block_kind_uniform
;
11091 /** emit endif merge block */
11092 ctx
.block
= program
->insert_block(std::move(BB_endif
));
11093 bld
.reset(ctx
.block
);
11094 append_logical_start(ctx
.block
);
11097 program
->config
->float_mode
= program
->blocks
[0].fp_mode
.val
;
11099 append_logical_end(ctx
.block
);
11100 ctx
.block
->kind
|= block_kind_uniform
;
11101 bld
.sopp(aco_opcode::s_endpgm
);
11103 cleanup_cfg(program
);