2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include <llvm/Config/llvm-config.h>
26 #include "ac_nir_to_llvm.h"
27 #include "ac_llvm_build.h"
28 #include "ac_llvm_util.h"
29 #include "ac_binary.h"
32 #include "nir/nir_deref.h"
33 #include "util/bitscan.h"
34 #include "util/u_math.h"
35 #include "ac_shader_abi.h"
36 #include "ac_shader_util.h"
38 struct ac_nir_context
{
39 struct ac_llvm_context ac
;
40 struct ac_shader_abi
*abi
;
41 const struct ac_shader_args
*args
;
43 gl_shader_stage stage
;
46 LLVMValueRef
*ssa_defs
;
49 LLVMValueRef constant_data
;
51 struct hash_table
*defs
;
52 struct hash_table
*phis
;
53 struct hash_table
*vars
;
54 struct hash_table
*verified_interp
;
56 LLVMValueRef main_function
;
57 LLVMBasicBlockRef continue_block
;
58 LLVMBasicBlockRef break_block
;
64 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
65 nir_deref_instr
*deref_instr
,
66 const nir_instr
*instr
,
69 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
70 nir_deref_instr
*deref_instr
,
71 enum ac_descriptor_type desc_type
,
72 const nir_instr
*instr
,
74 bool image
, bool write
);
77 build_store_values_extended(struct ac_llvm_context
*ac
,
80 unsigned value_stride
,
83 LLVMBuilderRef builder
= ac
->builder
;
86 for (i
= 0; i
< value_count
; i
++) {
87 LLVMValueRef ptr
= values
[i
* value_stride
];
88 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
89 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
90 LLVMBuildStore(builder
, value
, ptr
);
94 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
95 const nir_ssa_def
*def
)
97 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
98 if (def
->num_components
> 1) {
99 type
= LLVMVectorType(type
, def
->num_components
);
104 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
107 return nir
->ssa_defs
[src
.ssa
->index
];
111 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
, unsigned bit_size
)
113 LLVMValueRef ptr
= get_src(ctx
, src
);
114 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
115 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
117 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, bit_size
);
119 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
120 LLVMPointerType(type
, addr_space
), "");
123 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
124 const struct nir_block
*b
)
126 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
127 return (LLVMBasicBlockRef
)entry
->data
;
130 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
132 unsigned num_components
)
134 LLVMValueRef value
= get_src(ctx
, src
.src
);
135 bool need_swizzle
= false;
138 unsigned src_components
= ac_get_llvm_num_components(value
);
139 for (unsigned i
= 0; i
< num_components
; ++i
) {
140 assert(src
.swizzle
[i
] < src_components
);
141 if (src
.swizzle
[i
] != i
)
145 if (need_swizzle
|| num_components
!= src_components
) {
146 LLVMValueRef masks
[] = {
147 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
148 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
149 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
150 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
152 if (src_components
> 1 && num_components
== 1) {
153 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
155 } else if (src_components
== 1 && num_components
> 1) {
156 LLVMValueRef values
[] = {value
, value
, value
, value
};
157 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
159 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
160 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
169 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
170 LLVMIntPredicate pred
, LLVMValueRef src0
,
173 LLVMTypeRef src0_type
= LLVMTypeOf(src0
);
174 LLVMTypeRef src1_type
= LLVMTypeOf(src1
);
176 if (LLVMGetTypeKind(src0_type
) == LLVMPointerTypeKind
&&
177 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
178 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src0_type
, "");
179 } else if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
180 LLVMGetTypeKind(src0_type
) != LLVMPointerTypeKind
) {
181 src0
= LLVMBuildIntToPtr(ctx
->builder
, src0
, src1_type
, "");
184 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
185 return LLVMBuildSelect(ctx
->builder
, result
,
186 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
190 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
191 LLVMRealPredicate pred
, LLVMValueRef src0
,
195 src0
= ac_to_float(ctx
, src0
);
196 src1
= ac_to_float(ctx
, src1
);
197 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
198 return LLVMBuildSelect(ctx
->builder
, result
,
199 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
203 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
205 LLVMTypeRef result_type
,
208 char name
[64], type
[64];
209 LLVMValueRef params
[] = {
210 ac_to_float(ctx
, src0
),
213 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
214 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
215 assert(length
< sizeof(name
));
216 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
219 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
221 LLVMTypeRef result_type
,
222 LLVMValueRef src0
, LLVMValueRef src1
)
224 char name
[64], type
[64];
225 LLVMValueRef params
[] = {
226 ac_to_float(ctx
, src0
),
227 ac_to_float(ctx
, src1
),
230 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
231 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
232 assert(length
< sizeof(name
));
233 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
236 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
238 LLVMTypeRef result_type
,
239 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
241 char name
[64], type
[64];
242 LLVMValueRef params
[] = {
243 ac_to_float(ctx
, src0
),
244 ac_to_float(ctx
, src1
),
245 ac_to_float(ctx
, src2
),
248 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
249 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
250 assert(length
< sizeof(name
));
251 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
254 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
255 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
257 LLVMTypeRef src1_type
= LLVMTypeOf(src1
);
258 LLVMTypeRef src2_type
= LLVMTypeOf(src2
);
260 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
262 if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
263 LLVMGetTypeKind(src2_type
) != LLVMPointerTypeKind
) {
264 src2
= LLVMBuildIntToPtr(ctx
->builder
, src2
, src1_type
, "");
265 } else if (LLVMGetTypeKind(src2_type
) == LLVMPointerTypeKind
&&
266 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
267 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src2_type
, "");
270 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
272 return LLVMBuildSelect(ctx
->builder
, v
,
273 ac_to_integer_or_pointer(ctx
, src1
),
274 ac_to_integer_or_pointer(ctx
, src2
), "");
277 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
280 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
283 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
285 LLVMValueRef src0
, LLVMValueRef src1
)
287 LLVMTypeRef ret_type
;
288 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
290 LLVMValueRef params
[] = { src0
, src1
};
291 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
294 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
295 params
, 2, AC_FUNC_ATTR_READNONE
);
297 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
298 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
302 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
306 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
307 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
309 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
313 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
317 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
319 unreachable("Unsupported bit size.");
323 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
326 src0
= ac_to_float(ctx
, src0
);
327 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
328 return LLVMBuildSExt(ctx
->builder
,
329 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
333 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
337 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
341 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
343 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
347 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
349 unreachable("Unsupported bit size.");
353 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
356 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
357 return LLVMBuildSExt(ctx
->builder
,
358 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
362 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
366 LLVMValueRef cond
= NULL
;
368 src0
= ac_to_float(ctx
, src0
);
369 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
371 if (ctx
->chip_class
>= GFX8
) {
372 LLVMValueRef args
[2];
373 /* Check if the result is a denormal - and flush to 0 if so. */
375 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
376 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
379 /* need to convert back up to f32 */
380 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
382 if (ctx
->chip_class
>= GFX8
)
383 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
386 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
387 * so compare the result and flush to 0 if it's smaller.
389 LLVMValueRef temp
, cond2
;
390 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
391 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
,
392 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
394 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealONE
,
395 temp
, ctx
->f32_0
, "");
396 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
397 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
402 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
403 LLVMValueRef src0
, LLVMValueRef src1
)
405 LLVMValueRef dst64
, result
;
406 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
407 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
409 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
410 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
411 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
415 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
416 LLVMValueRef src0
, LLVMValueRef src1
)
418 LLVMValueRef dst64
, result
;
419 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
420 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
422 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
423 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
424 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
428 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
429 LLVMValueRef bits
, LLVMValueRef offset
)
431 /* mask = ((1 << bits) - 1) << offset */
432 return LLVMBuildShl(ctx
->builder
,
433 LLVMBuildSub(ctx
->builder
,
434 LLVMBuildShl(ctx
->builder
,
441 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
442 LLVMValueRef mask
, LLVMValueRef insert
,
446 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
447 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
449 return LLVMBuildXor(ctx
->builder
, base
,
450 LLVMBuildAnd(ctx
->builder
, mask
,
451 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
454 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
456 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
457 LLVMValueRef args
[2]))
459 LLVMValueRef comp
[2];
461 src0
= ac_to_float(ctx
, src0
);
462 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
463 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
465 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
468 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
471 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
472 LLVMValueRef temps
[2], val
;
475 for (i
= 0; i
< 2; i
++) {
476 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
477 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
478 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
479 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
481 return ac_build_gather_values(ctx
, temps
, 2);
484 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
492 if (op
== nir_op_fddx_fine
)
493 mask
= AC_TID_MASK_LEFT
;
494 else if (op
== nir_op_fddy_fine
)
495 mask
= AC_TID_MASK_TOP
;
497 mask
= AC_TID_MASK_TOP_LEFT
;
499 /* for DDX we want to next X pixel, DDY next Y pixel. */
500 if (op
== nir_op_fddx_fine
||
501 op
== nir_op_fddx_coarse
||
507 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
511 struct waterfall_context
{
512 LLVMBasicBlockRef phi_bb
[2];
516 /* To deal with divergent descriptors we can create a loop that handles all
517 * lanes with the same descriptor on a given iteration (henceforth a
520 * These helper create the begin and end of the loop leaving the caller
521 * to implement the body.
524 * - ctx is the usal nir context
525 * - wctx is a temporary struct containing some loop info. Can be left uninitialized.
526 * - value is the possibly divergent value for which we built the loop
527 * - divergent is whether value is actually divergent. If false we just pass
530 static LLVMValueRef
enter_waterfall(struct ac_nir_context
*ctx
,
531 struct waterfall_context
*wctx
,
532 LLVMValueRef value
, bool divergent
)
534 /* If the app claims the value is divergent but it is constant we can
535 * end up with a dynamic index of NULL. */
539 wctx
->use_waterfall
= divergent
;
543 ac_build_bgnloop(&ctx
->ac
, 6000);
545 LLVMValueRef scalar_value
= ac_build_readlane(&ctx
->ac
, value
, NULL
);
547 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, value
,
548 scalar_value
, "uniform_active");
550 wctx
->phi_bb
[0] = LLVMGetInsertBlock(ctx
->ac
.builder
);
551 ac_build_ifcc(&ctx
->ac
, active
, 6001);
556 static LLVMValueRef
exit_waterfall(struct ac_nir_context
*ctx
,
557 struct waterfall_context
*wctx
,
560 LLVMValueRef ret
= NULL
;
561 LLVMValueRef phi_src
[2];
562 LLVMValueRef cc_phi_src
[2] = {
563 LLVMConstInt(ctx
->ac
.i32
, 0, false),
564 LLVMConstInt(ctx
->ac
.i32
, 0xffffffff, false),
567 if (!wctx
->use_waterfall
)
570 wctx
->phi_bb
[1] = LLVMGetInsertBlock(ctx
->ac
.builder
);
572 ac_build_endif(&ctx
->ac
, 6001);
575 phi_src
[0] = LLVMGetUndef(LLVMTypeOf(value
));
578 ret
= ac_build_phi(&ctx
->ac
, LLVMTypeOf(value
), 2, phi_src
, wctx
->phi_bb
);
582 * By using the optimization barrier on the exit decision, we decouple
583 * the operations from the break, and hence avoid LLVM hoisting the
584 * opteration into the break block.
586 LLVMValueRef cc
= ac_build_phi(&ctx
->ac
, ctx
->ac
.i32
, 2, cc_phi_src
, wctx
->phi_bb
);
587 ac_build_optimization_barrier(&ctx
->ac
, &cc
);
589 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, cc
, ctx
->ac
.i32_0
, "uniform_active2");
590 ac_build_ifcc(&ctx
->ac
, active
, 6002);
591 ac_build_break(&ctx
->ac
);
592 ac_build_endif(&ctx
->ac
, 6002);
594 ac_build_endloop(&ctx
->ac
, 6000);
598 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
600 LLVMValueRef src
[4], result
= NULL
;
601 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
602 unsigned src_components
;
603 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
604 bool saved_inexact
= false;
607 saved_inexact
= ac_disable_inexact_math(ctx
->ac
.builder
);
609 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
616 case nir_op_pack_half_2x16
:
617 case nir_op_pack_snorm_2x16
:
618 case nir_op_pack_unorm_2x16
:
621 case nir_op_unpack_half_2x16
:
624 case nir_op_cube_face_coord
:
625 case nir_op_cube_face_index
:
629 src_components
= num_components
;
632 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
633 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
640 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
641 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
642 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
643 /* fneg will be optimized by backend compiler with sign
644 * bit removed via XOR. This is probably a LLVM bug.
646 result
= ac_build_canonicalize(&ctx
->ac
, result
,
647 instr
->dest
.dest
.ssa
.bit_size
);
651 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
654 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
657 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
660 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
661 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
662 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
665 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
666 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
667 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
670 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
673 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
676 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
679 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
682 /* lower_fmod only lower 16-bit and 32-bit fmod */
683 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
684 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
685 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
686 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
687 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
688 ac_to_float_type(&ctx
->ac
, def_type
), result
);
689 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
690 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
693 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
696 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
699 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
702 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
703 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
704 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
707 /* For doubles, we need precise division to pass GLCTS. */
708 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
&&
709 ac_get_type_size(def_type
) == 8) {
710 result
= LLVMBuildFDiv(ctx
->ac
.builder
, ctx
->ac
.f64_1
,
711 ac_to_float(&ctx
->ac
, src
[0]), "");
713 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rcp",
714 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
718 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
721 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
724 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
727 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
728 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
729 LLVMTypeOf(src
[0]), "");
730 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
731 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
732 LLVMTypeOf(src
[0]), "");
733 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
736 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
737 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
738 LLVMTypeOf(src
[0]), "");
739 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
740 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
741 LLVMTypeOf(src
[0]), "");
742 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
745 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
746 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
747 LLVMTypeOf(src
[0]), "");
748 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
749 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
750 LLVMTypeOf(src
[0]), "");
751 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
754 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
757 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
760 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
763 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
766 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
769 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
772 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
775 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
778 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
781 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
784 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
785 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
786 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
787 /* fabs will be optimized by backend compiler with sign
788 * bit removed via AND.
790 result
= ac_build_canonicalize(&ctx
->ac
, result
,
791 instr
->dest
.dest
.ssa
.bit_size
);
795 result
= emit_iabs(&ctx
->ac
, src
[0]);
798 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
801 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
804 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
807 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
810 result
= ac_build_isign(&ctx
->ac
, src
[0],
811 instr
->dest
.dest
.ssa
.bit_size
);
814 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
815 result
= ac_build_fsign(&ctx
->ac
, src
[0],
816 instr
->dest
.dest
.ssa
.bit_size
);
819 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
820 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
823 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
824 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
827 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
828 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
830 case nir_op_fround_even
:
831 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
832 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
835 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
836 result
= ac_build_fract(&ctx
->ac
, src
[0],
837 instr
->dest
.dest
.ssa
.bit_size
);
840 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
841 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
844 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
845 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
848 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
849 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
852 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
853 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
856 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
857 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
860 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rsq",
861 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
863 case nir_op_frexp_exp
:
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
866 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
867 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
868 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
871 case nir_op_frexp_sig
:
872 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
873 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
874 instr
->dest
.dest
.ssa
.bit_size
);
877 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
878 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
881 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
882 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
883 if (ctx
->ac
.chip_class
< GFX9
&&
884 instr
->dest
.dest
.ssa
.bit_size
== 32) {
885 /* Only pre-GFX9 chips do not flush denorms. */
886 result
= ac_build_canonicalize(&ctx
->ac
, result
,
887 instr
->dest
.dest
.ssa
.bit_size
);
891 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
892 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
893 if (ctx
->ac
.chip_class
< GFX9
&&
894 instr
->dest
.dest
.ssa
.bit_size
== 32) {
895 /* Only pre-GFX9 chips do not flush denorms. */
896 result
= ac_build_canonicalize(&ctx
->ac
, result
,
897 instr
->dest
.dest
.ssa
.bit_size
);
901 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
902 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
903 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
906 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
907 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
908 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
909 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
910 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
912 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
915 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
917 case nir_op_bitfield_select
:
918 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
921 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
924 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
926 case nir_op_bitfield_reverse
:
927 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
929 case nir_op_bit_count
:
930 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
935 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
936 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
937 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
943 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
944 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
950 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
951 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
956 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
961 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
963 case nir_op_f2f16_rtz
:
966 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
968 /* For OpenGL, we want fast packing with v_cvt_pkrtz_f16, but if we use it,
969 * all f32->f16 conversions have to round towards zero, because both scalar
970 * and vec2 down-conversions have to round equally.
972 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
||
973 instr
->op
== nir_op_f2f16_rtz
) {
974 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
976 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
977 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
979 /* Fast path conversion. This only works if NIR is vectorized
982 if (LLVMTypeOf(src
[0]) == ctx
->ac
.v2f32
) {
983 LLVMValueRef args
[] = {
984 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 0),
985 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 1),
987 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, args
);
991 assert(ac_get_llvm_num_components(src
[0]) == 1);
992 LLVMValueRef param
[2] = { src
[0], LLVMGetUndef(ctx
->ac
.f32
) };
993 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
994 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
996 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
997 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
999 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1002 case nir_op_f2f16_rtne
:
1005 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1006 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1007 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1009 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1016 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1017 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1019 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1026 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1027 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1029 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1031 case nir_op_b32csel
:
1032 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1034 case nir_op_find_lsb
:
1035 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1037 case nir_op_ufind_msb
:
1038 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1040 case nir_op_ifind_msb
:
1041 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1043 case nir_op_uadd_carry
:
1044 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1046 case nir_op_usub_borrow
:
1047 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1052 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1055 result
= emit_f2b(&ctx
->ac
, src
[0]);
1061 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1064 result
= emit_i2b(&ctx
->ac
, src
[0]);
1066 case nir_op_fquantize2f16
:
1067 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1069 case nir_op_umul_high
:
1070 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1072 case nir_op_imul_high
:
1073 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1075 case nir_op_pack_half_2x16
:
1076 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1078 case nir_op_pack_snorm_2x16
:
1079 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1081 case nir_op_pack_unorm_2x16
:
1082 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1084 case nir_op_unpack_half_2x16
:
1085 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1089 case nir_op_fddx_fine
:
1090 case nir_op_fddy_fine
:
1091 case nir_op_fddx_coarse
:
1092 case nir_op_fddy_coarse
:
1093 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1096 case nir_op_unpack_64_2x32_split_x
: {
1097 assert(ac_get_llvm_num_components(src
[0]) == 1);
1098 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1101 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1106 case nir_op_unpack_64_2x32_split_y
: {
1107 assert(ac_get_llvm_num_components(src
[0]) == 1);
1108 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1111 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1116 case nir_op_pack_64_2x32_split
: {
1117 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1118 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1122 case nir_op_pack_32_2x16_split
: {
1123 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1124 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1128 case nir_op_unpack_32_2x16_split_x
: {
1129 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1132 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1137 case nir_op_unpack_32_2x16_split_y
: {
1138 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1141 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1146 case nir_op_cube_face_coord
: {
1147 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1148 LLVMValueRef results
[2];
1150 for (unsigned chan
= 0; chan
< 3; chan
++)
1151 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1152 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1153 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1154 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1155 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1156 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1157 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1158 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1159 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1160 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1161 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1162 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1163 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1167 case nir_op_cube_face_index
: {
1168 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1170 for (unsigned chan
= 0; chan
< 3; chan
++)
1171 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1172 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1173 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1178 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1179 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1180 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1181 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1184 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1185 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1188 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1189 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1192 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1193 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1194 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1195 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1198 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1199 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1202 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1203 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1205 case nir_op_fmed3
: {
1206 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1207 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1208 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1209 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1210 instr
->dest
.dest
.ssa
.bit_size
);
1213 case nir_op_imed3
: {
1214 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1215 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1216 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1217 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1220 case nir_op_umed3
: {
1221 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1222 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1223 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1224 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1229 fprintf(stderr
, "Unknown NIR alu instr: ");
1230 nir_print_instr(&instr
->instr
, stderr
);
1231 fprintf(stderr
, "\n");
1236 assert(instr
->dest
.dest
.is_ssa
);
1237 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1238 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1242 ac_restore_inexact_math(ctx
->ac
.builder
, saved_inexact
);
1245 static void visit_load_const(struct ac_nir_context
*ctx
,
1246 const nir_load_const_instr
*instr
)
1248 LLVMValueRef values
[4], value
= NULL
;
1249 LLVMTypeRef element_type
=
1250 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1252 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1253 switch (instr
->def
.bit_size
) {
1255 values
[i
] = LLVMConstInt(element_type
,
1256 instr
->value
[i
].u8
, false);
1259 values
[i
] = LLVMConstInt(element_type
,
1260 instr
->value
[i
].u16
, false);
1263 values
[i
] = LLVMConstInt(element_type
,
1264 instr
->value
[i
].u32
, false);
1267 values
[i
] = LLVMConstInt(element_type
,
1268 instr
->value
[i
].u64
, false);
1272 "unsupported nir load_const bit_size: %d\n",
1273 instr
->def
.bit_size
);
1277 if (instr
->def
.num_components
> 1) {
1278 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1282 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1286 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1289 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1290 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1293 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1294 /* On GFX8, the descriptor contains the size in bytes,
1295 * but TXQ must return the size in elements.
1296 * The stride is always non-zero for resources using TXQ.
1298 LLVMValueRef stride
=
1299 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1301 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1302 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1303 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1304 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1306 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1311 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1312 * incorrectly forces nearest filtering if the texture format is integer.
1313 * The only effect it has on Gather4, which always returns 4 texels for
1314 * bilinear filtering, is that the final coordinates are off by 0.5 of
1317 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1318 * or (0.5 / size) from the normalized coordinates.
1320 * However, cube textures with 8_8_8_8 data formats require a different
1321 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1322 * precision in 32-bit data formats, so it needs to be applied dynamically at
1323 * runtime. In this case, return an i1 value that indicates whether the
1324 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1326 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1328 struct ac_image_args
*args
,
1329 const nir_tex_instr
*instr
)
1331 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1332 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1333 LLVMValueRef wa_8888
= NULL
;
1334 LLVMValueRef half_texel
[2];
1335 LLVMValueRef result
;
1337 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1339 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1340 LLVMValueRef formats
;
1341 LLVMValueRef data_format
;
1342 LLVMValueRef wa_formats
;
1344 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1346 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1347 LLVMConstInt(ctx
->i32
, 20, false), "");
1348 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1349 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1350 wa_8888
= LLVMBuildICmp(
1351 ctx
->builder
, LLVMIntEQ
, data_format
,
1352 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1355 uint32_t wa_num_format
=
1356 stype
== GLSL_TYPE_UINT
?
1357 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1358 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1359 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1360 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1362 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1363 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1365 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1366 args
->resource
= LLVMBuildInsertElement(
1367 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1370 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1372 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1374 struct ac_image_args resinfo
= {};
1375 LLVMBasicBlockRef bbs
[2];
1377 LLVMValueRef unnorm
= NULL
;
1378 LLVMValueRef default_offset
= ctx
->f32_0
;
1379 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1381 /* In vulkan, whether the sampler uses unnormalized
1382 * coordinates or not is a dynamic property of the
1383 * sampler. Hence, to figure out whether or not we
1384 * need to divide by the texture size, we need to test
1385 * the sampler at runtime. This tests the bit set by
1386 * radv_init_sampler().
1388 LLVMValueRef sampler0
=
1389 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1390 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1391 LLVMConstInt(ctx
->i32
, 15, false), "");
1392 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1393 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1394 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1397 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1398 if (wa_8888
|| unnorm
) {
1399 assert(!(wa_8888
&& unnorm
));
1400 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1401 /* Skip the texture size query entirely if we don't need it. */
1402 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1403 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1406 /* Query the texture size. */
1407 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1408 resinfo
.opcode
= ac_image_get_resinfo
;
1409 resinfo
.dmask
= 0xf;
1410 resinfo
.lod
= ctx
->i32_0
;
1411 resinfo
.resource
= args
->resource
;
1412 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1413 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1415 /* Compute -0.5 / size. */
1416 for (unsigned c
= 0; c
< 2; c
++) {
1418 LLVMBuildExtractElement(ctx
->builder
, size
,
1419 LLVMConstInt(ctx
->i32
, c
, 0), "");
1420 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1421 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1422 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1423 LLVMConstReal(ctx
->f32
, -0.5), "");
1426 if (wa_8888
|| unnorm
) {
1427 ac_build_endif(ctx
, 2000);
1429 for (unsigned c
= 0; c
< 2; c
++) {
1430 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1431 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1437 for (unsigned c
= 0; c
< 2; c
++) {
1439 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1440 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1443 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1444 result
= ac_build_image_opcode(ctx
, args
);
1446 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1447 LLVMValueRef tmp
, tmp2
;
1449 /* if the cube workaround is in place, f2i the result. */
1450 for (unsigned c
= 0; c
< 4; c
++) {
1451 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1452 if (stype
== GLSL_TYPE_UINT
)
1453 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1455 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1456 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1457 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1458 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1459 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1460 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1466 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1468 nir_deref_instr
*texture_deref_instr
= NULL
;
1470 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1471 switch (instr
->src
[i
].src_type
) {
1472 case nir_tex_src_texture_deref
:
1473 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1479 return texture_deref_instr
;
1482 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1483 const nir_tex_instr
*instr
,
1484 struct ac_image_args
*args
)
1486 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1487 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1489 assert(instr
->dest
.is_ssa
);
1490 return ac_build_buffer_load_format(&ctx
->ac
,
1494 util_last_bit(mask
),
1496 instr
->dest
.ssa
.bit_size
== 16);
1499 args
->opcode
= ac_image_sample
;
1501 switch (instr
->op
) {
1503 case nir_texop_txf_ms
:
1504 case nir_texop_samples_identical
:
1505 args
->opcode
= args
->level_zero
||
1506 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1507 ac_image_load
: ac_image_load_mip
;
1508 args
->level_zero
= false;
1511 case nir_texop_query_levels
:
1512 args
->opcode
= ac_image_get_resinfo
;
1514 args
->lod
= ctx
->ac
.i32_0
;
1515 args
->level_zero
= false;
1518 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1520 args
->level_zero
= true;
1524 args
->opcode
= ac_image_gather4
;
1525 if (!args
->lod
&& !args
->bias
)
1526 args
->level_zero
= true;
1529 args
->opcode
= ac_image_get_lod
;
1531 case nir_texop_fragment_fetch
:
1532 case nir_texop_fragment_mask_fetch
:
1533 args
->opcode
= ac_image_load
;
1534 args
->level_zero
= false;
1540 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1541 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1542 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1543 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1544 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1545 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1546 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1550 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1551 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1552 if ((args
->dim
== ac_image_2darray
||
1553 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1554 args
->coords
[1] = ctx
->ac
.i32_0
;
1558 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1559 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1560 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1561 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1562 /* Prevent texture instructions with implicit derivatives from being
1563 * sinked into branches. */
1564 switch (instr
->op
) {
1568 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1575 return ac_build_image_opcode(&ctx
->ac
, args
);
1578 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1579 nir_intrinsic_instr
*instr
)
1581 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1582 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1584 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1585 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1589 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1590 nir_intrinsic_instr
*instr
)
1592 LLVMValueRef ptr
, addr
;
1593 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1594 unsigned index
= nir_intrinsic_base(instr
);
1596 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1597 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1599 /* Load constant values from user SGPRS when possible, otherwise
1600 * fallback to the default path that loads directly from memory.
1602 if (LLVMIsConstant(src0
) &&
1603 instr
->dest
.ssa
.bit_size
== 32) {
1604 unsigned count
= instr
->dest
.ssa
.num_components
;
1605 unsigned offset
= index
;
1607 offset
+= LLVMConstIntGetZExtValue(src0
);
1610 offset
-= ctx
->args
->base_inline_push_consts
;
1612 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1613 if (offset
+ count
<= num_inline_push_consts
) {
1614 LLVMValueRef push_constants
[num_inline_push_consts
];
1615 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1616 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1617 ctx
->args
->inline_push_consts
[i
]);
1618 return ac_build_gather_values(&ctx
->ac
,
1619 push_constants
+ offset
,
1624 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1625 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1627 if (instr
->dest
.ssa
.bit_size
== 8) {
1628 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1629 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i8
, 4 * load_dwords
);
1630 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1631 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1633 LLVMValueRef params
[3];
1634 if (load_dwords
> 1) {
1635 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.v2i32
, "");
1636 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1637 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1639 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1640 params
[0] = ctx
->ac
.i32_0
;
1644 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1646 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1647 if (instr
->dest
.ssa
.num_components
> 1)
1648 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i8
, instr
->dest
.ssa
.num_components
), "");
1650 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1651 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1652 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i16
, 2 * load_dwords
);
1653 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1654 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1655 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1656 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1657 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1658 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1659 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1660 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1661 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1662 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1663 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1664 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1665 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1666 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1669 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1671 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1674 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1675 const nir_intrinsic_instr
*instr
)
1677 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1679 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1682 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1684 uint32_t new_mask
= 0;
1685 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1686 if (mask
& (1u << i
))
1687 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1691 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1692 unsigned start
, unsigned count
)
1694 LLVMValueRef mask
[] = {
1695 ctx
->i32_0
, ctx
->i32_1
,
1696 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1698 unsigned src_elements
= ac_get_llvm_num_components(src
);
1700 if (count
== src_elements
) {
1703 } else if (count
== 1) {
1704 assert(start
< src_elements
);
1705 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1707 assert(start
+ count
<= src_elements
);
1709 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1710 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1714 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1715 enum gl_access_qualifier access
,
1716 bool may_store_unaligned
,
1717 bool writeonly_memory
)
1719 unsigned cache_policy
= 0;
1721 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1722 * store opcodes not aligned to a dword are affected. The only way to
1723 * get unaligned stores is through shader images.
1725 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1726 /* If this is write-only, don't keep data in L1 to prevent
1727 * evicting L1 cache lines that may be needed by other
1731 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1732 cache_policy
|= ac_glc
;
1735 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1736 cache_policy
|= ac_slc
| ac_glc
;
1738 return cache_policy
;
1741 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1742 struct waterfall_context
*wctx
,
1743 const nir_intrinsic_instr
*instr
,
1746 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1747 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1750 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1751 nir_intrinsic_instr
*instr
)
1753 if (ctx
->ac
.postponed_kill
) {
1754 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1755 ctx
->ac
.postponed_kill
, "");
1756 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1759 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1760 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1761 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1762 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1763 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1764 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1766 struct waterfall_context wctx
;
1767 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1769 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1770 LLVMValueRef base_data
= src_data
;
1771 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1772 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1776 LLVMValueRef data
, offset
;
1777 LLVMTypeRef data_type
;
1779 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1781 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1782 * writes into a 2-element and a 1-element write. */
1784 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1785 writemask
|= 1 << (start
+ 2);
1788 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1790 /* we can only store 4 DWords at the same time.
1791 * can only happen for 64 Bit vectors. */
1792 if (num_bytes
> 16) {
1793 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1798 /* check alignment of 16 Bit stores */
1799 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1800 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1805 /* Due to alignment issues, split stores of 8-bit/16-bit
1808 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1809 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1811 num_bytes
= elem_size_bytes
;
1814 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1816 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1817 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1819 if (num_bytes
== 1) {
1820 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1821 offset
, ctx
->ac
.i32_0
,
1823 } else if (num_bytes
== 2) {
1824 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1825 offset
, ctx
->ac
.i32_0
,
1828 int num_channels
= num_bytes
/ 4;
1830 switch (num_bytes
) {
1831 case 16: /* v4f32 */
1832 data_type
= ctx
->ac
.v4f32
;
1834 case 12: /* v3f32 */
1835 data_type
= ctx
->ac
.v3f32
;
1838 data_type
= ctx
->ac
.v2f32
;
1841 data_type
= ctx
->ac
.f32
;
1844 unreachable("Malformed vector store.");
1846 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1848 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1849 num_channels
, offset
,
1855 exit_waterfall(ctx
, &wctx
, NULL
);
1857 if (ctx
->ac
.postponed_kill
)
1858 ac_build_endif(&ctx
->ac
, 7000);
1861 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1862 LLVMValueRef descriptor
,
1863 LLVMValueRef offset
,
1864 LLVMValueRef compare
,
1865 LLVMValueRef exchange
)
1867 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1868 if (ctx
->abi
->robust_buffer_access
) {
1869 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1871 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1872 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1874 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1876 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1879 LLVMValueRef ptr_parts
[2] = {
1880 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1881 LLVMBuildAnd(ctx
->ac
.builder
,
1882 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1883 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1886 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1887 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1889 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1891 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1892 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1893 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1894 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1896 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1897 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1899 if (ctx
->abi
->robust_buffer_access
) {
1900 ac_build_endif(&ctx
->ac
, -1);
1902 LLVMBasicBlockRef incoming_blocks
[2] = {
1907 LLVMValueRef incoming_values
[2] = {
1908 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1911 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1912 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1919 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1920 nir_intrinsic_instr
*instr
)
1922 if (ctx
->ac
.postponed_kill
) {
1923 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1924 ctx
->ac
.postponed_kill
, "");
1925 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1928 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1930 char name
[64], type
[8];
1931 LLVMValueRef params
[6], descriptor
;
1932 LLVMValueRef result
;
1935 struct waterfall_context wctx
;
1936 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1938 switch (instr
->intrinsic
) {
1939 case nir_intrinsic_ssbo_atomic_add
:
1942 case nir_intrinsic_ssbo_atomic_imin
:
1945 case nir_intrinsic_ssbo_atomic_umin
:
1948 case nir_intrinsic_ssbo_atomic_imax
:
1951 case nir_intrinsic_ssbo_atomic_umax
:
1954 case nir_intrinsic_ssbo_atomic_and
:
1957 case nir_intrinsic_ssbo_atomic_or
:
1960 case nir_intrinsic_ssbo_atomic_xor
:
1963 case nir_intrinsic_ssbo_atomic_exchange
:
1966 case nir_intrinsic_ssbo_atomic_comp_swap
:
1973 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1977 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1978 return_type
== ctx
->ac
.i64
) {
1979 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1980 get_src(ctx
, instr
->src
[1]),
1981 get_src(ctx
, instr
->src
[2]),
1982 get_src(ctx
, instr
->src
[3]));
1984 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1985 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1987 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1988 params
[arg_count
++] = descriptor
;
1990 if (LLVM_VERSION_MAJOR
>= 9) {
1991 /* XXX: The new raw/struct atomic intrinsics are buggy with
1992 * LLVM 8, see r358579.
1994 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1995 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1996 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1998 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1999 snprintf(name
, sizeof(name
),
2000 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
2002 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
2003 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
2004 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
2006 assert(return_type
== ctx
->ac
.i32
);
2007 snprintf(name
, sizeof(name
),
2008 "llvm.amdgcn.buffer.atomic.%s", op
);
2011 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
2015 result
= exit_waterfall(ctx
, &wctx
, result
);
2016 if (ctx
->ac
.postponed_kill
)
2017 ac_build_endif(&ctx
->ac
, 7001);
2021 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
2022 nir_intrinsic_instr
*instr
)
2024 struct waterfall_context wctx
;
2025 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
2027 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2028 int num_components
= instr
->num_components
;
2029 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2030 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
2032 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2033 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
2034 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
2036 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2037 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
2039 LLVMValueRef results
[4];
2040 for (int i
= 0; i
< num_components
;) {
2041 int num_elems
= num_components
- i
;
2042 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
2044 if (num_elems
* elem_size_bytes
> 16)
2045 num_elems
= 16 / elem_size_bytes
;
2046 int load_bytes
= num_elems
* elem_size_bytes
;
2048 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
2052 if (load_bytes
== 1) {
2053 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
2059 } else if (load_bytes
== 2) {
2060 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2067 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2068 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2070 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2071 vindex
, offset
, immoffset
, 0,
2072 cache_policy
, can_speculate
, false);
2075 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2076 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2077 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2079 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2080 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2082 for (unsigned j
= 0; j
< num_elems
; j
++) {
2083 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2088 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2089 return exit_waterfall(ctx
, &wctx
, ret
);
2092 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2093 struct waterfall_context
*wctx
,
2094 const nir_intrinsic_instr
*instr
)
2096 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2097 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2100 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2101 nir_intrinsic_instr
*instr
)
2103 struct waterfall_context wctx
;
2104 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2107 LLVMValueRef rsrc
= rsrc_base
;
2108 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2109 int num_components
= instr
->num_components
;
2111 if (ctx
->abi
->load_ubo
)
2112 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2114 if (instr
->dest
.ssa
.bit_size
== 64)
2115 num_components
*= 2;
2117 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2118 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2119 LLVMValueRef results
[num_components
];
2120 for (unsigned i
= 0; i
< num_components
; ++i
) {
2121 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2124 if (load_bytes
== 1) {
2125 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2132 assert(load_bytes
== 2);
2133 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2141 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2143 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2144 NULL
, 0, 0, true, true);
2146 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2149 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2150 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2152 return exit_waterfall(ctx
, &wctx
, ret
);
2156 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2157 bool vs_in
, unsigned *vertex_index_out
,
2158 LLVMValueRef
*vertex_index_ref
,
2159 unsigned *const_out
, LLVMValueRef
*indir_out
)
2161 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2162 nir_deref_path path
;
2163 unsigned idx_lvl
= 1;
2165 nir_deref_path_init(&path
, instr
, NULL
);
2167 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2168 if (vertex_index_ref
) {
2169 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2170 if (vertex_index_out
)
2171 *vertex_index_out
= 0;
2173 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2178 uint32_t const_offset
= 0;
2179 LLVMValueRef offset
= NULL
;
2181 if (var
->data
.compact
) {
2182 assert(instr
->deref_type
== nir_deref_type_array
);
2183 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2187 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2188 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2189 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2190 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2192 for (unsigned i
= 0; i
< index
; i
++) {
2193 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2194 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2196 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2197 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2198 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2199 const_offset
+= size
*
2200 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2202 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2203 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2205 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2210 unreachable("Uhandled deref type in get_deref_instr_offset");
2214 nir_deref_path_finish(&path
);
2216 if (const_offset
&& offset
)
2217 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2218 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2221 *const_out
= const_offset
;
2222 *indir_out
= offset
;
2225 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2226 nir_intrinsic_instr
*instr
,
2229 LLVMValueRef result
;
2230 LLVMValueRef vertex_index
= NULL
;
2231 LLVMValueRef indir_index
= NULL
;
2232 unsigned const_index
= 0;
2234 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2236 unsigned location
= var
->data
.location
;
2237 unsigned driver_location
= var
->data
.driver_location
;
2238 const bool is_patch
= var
->data
.patch
||
2239 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2240 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2241 const bool is_compact
= var
->data
.compact
;
2243 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2244 false, NULL
, is_patch
? NULL
: &vertex_index
,
2245 &const_index
, &indir_index
);
2247 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2249 LLVMTypeRef src_component_type
;
2250 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2251 src_component_type
= LLVMGetElementType(dest_type
);
2253 src_component_type
= dest_type
;
2255 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2256 vertex_index
, indir_index
,
2257 const_index
, location
, driver_location
,
2258 var
->data
.location_frac
,
2259 instr
->num_components
,
2260 is_patch
, is_compact
, load_inputs
);
2261 if (instr
->dest
.ssa
.bit_size
== 16) {
2262 result
= ac_to_integer(&ctx
->ac
, result
);
2263 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2265 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2269 type_scalar_size_bytes(const struct glsl_type
*type
)
2271 assert(glsl_type_is_vector_or_scalar(type
) ||
2272 glsl_type_is_matrix(type
));
2273 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2276 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2277 nir_intrinsic_instr
*instr
)
2279 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2280 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2282 LLVMValueRef values
[8];
2284 int ve
= instr
->dest
.ssa
.num_components
;
2286 LLVMValueRef indir_index
;
2288 unsigned const_index
;
2289 unsigned stride
= 4;
2290 int mode
= deref
->mode
;
2293 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2294 var
->data
.mode
== nir_var_shader_in
;
2295 idx
= var
->data
.driver_location
;
2296 comp
= var
->data
.location_frac
;
2297 mode
= var
->data
.mode
;
2299 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2300 &const_index
, &indir_index
);
2302 if (var
->data
.compact
) {
2304 const_index
+= comp
;
2309 if (instr
->dest
.ssa
.bit_size
== 64 &&
2310 (deref
->mode
== nir_var_shader_in
||
2311 deref
->mode
== nir_var_shader_out
||
2312 deref
->mode
== nir_var_function_temp
))
2316 case nir_var_shader_in
:
2317 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2318 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2319 return load_tess_varyings(ctx
, instr
, true);
2322 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2323 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2324 LLVMValueRef indir_index
;
2325 unsigned const_index
, vertex_index
;
2326 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2327 &const_index
, &indir_index
);
2328 assert(indir_index
== NULL
);
2330 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2331 var
->data
.driver_location
,
2332 var
->data
.location_frac
,
2333 instr
->num_components
, vertex_index
, const_index
, type
);
2336 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2338 unsigned count
= glsl_count_attribute_slots(
2340 ctx
->stage
== MESA_SHADER_VERTEX
);
2342 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2343 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2344 stride
, false, true);
2346 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2350 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2353 case nir_var_function_temp
:
2354 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2356 unsigned count
= glsl_count_attribute_slots(
2359 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2360 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2361 stride
, true, true);
2363 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2367 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2371 case nir_var_shader_out
:
2372 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2373 return load_tess_varyings(ctx
, instr
, false);
2376 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2377 var
->data
.fb_fetch_output
&&
2378 ctx
->abi
->emit_fbfetch
)
2379 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2381 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2383 unsigned count
= glsl_count_attribute_slots(
2386 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2387 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2388 stride
, true, true);
2390 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2394 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2395 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2400 case nir_var_mem_global
: {
2401 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2402 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2403 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2404 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2405 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2406 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2407 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2409 if (stride
!= natural_stride
|| split_loads
) {
2410 if (LLVMGetTypeKind(result_type
) == LLVMVectorTypeKind
)
2411 result_type
= LLVMGetElementType(result_type
);
2413 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2414 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2415 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2417 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2418 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2419 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2420 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2422 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2423 LLVMSetOrdering(values
[i
], LLVMAtomicOrderingMonotonic
);
2425 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2427 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2428 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2429 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2430 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2432 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2433 LLVMSetOrdering(val
, LLVMAtomicOrderingMonotonic
);
2438 unreachable("unhandle variable mode");
2440 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2441 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2445 visit_store_var(struct ac_nir_context
*ctx
,
2446 nir_intrinsic_instr
*instr
)
2448 if (ctx
->ac
.postponed_kill
) {
2449 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2450 ctx
->ac
.postponed_kill
, "");
2451 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2454 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2455 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2457 LLVMValueRef temp_ptr
, value
;
2460 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2461 int writemask
= instr
->const_index
[0];
2462 LLVMValueRef indir_index
;
2463 unsigned const_index
;
2466 get_deref_offset(ctx
, deref
, false,
2467 NULL
, NULL
, &const_index
, &indir_index
);
2468 idx
= var
->data
.driver_location
;
2469 comp
= var
->data
.location_frac
;
2471 if (var
->data
.compact
) {
2472 const_index
+= comp
;
2477 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2478 (deref
->mode
== nir_var_shader_out
||
2479 deref
->mode
== nir_var_function_temp
)) {
2481 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2482 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2485 writemask
= widen_mask(writemask
, 2);
2488 writemask
= writemask
<< comp
;
2490 switch (deref
->mode
) {
2491 case nir_var_shader_out
:
2493 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2494 LLVMValueRef vertex_index
= NULL
;
2495 LLVMValueRef indir_index
= NULL
;
2496 unsigned const_index
= 0;
2497 const bool is_patch
= var
->data
.patch
||
2498 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2499 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2501 get_deref_offset(ctx
, deref
, false, NULL
,
2502 is_patch
? NULL
: &vertex_index
,
2503 &const_index
, &indir_index
);
2505 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2506 vertex_index
, indir_index
,
2507 const_index
, src
, writemask
);
2511 for (unsigned chan
= 0; chan
< 8; chan
++) {
2513 if (!(writemask
& (1 << chan
)))
2516 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2518 if (var
->data
.compact
)
2521 unsigned count
= glsl_count_attribute_slots(
2524 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2525 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2526 stride
, true, true);
2528 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2529 value
, indir_index
, "");
2530 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2531 count
, stride
, tmp_vec
);
2534 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2536 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2540 case nir_var_function_temp
:
2541 for (unsigned chan
= 0; chan
< 8; chan
++) {
2542 if (!(writemask
& (1 << chan
)))
2545 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2547 unsigned count
= glsl_count_attribute_slots(
2550 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2551 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2554 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2555 value
, indir_index
, "");
2556 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2559 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2561 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2566 case nir_var_mem_global
: {
2567 int writemask
= instr
->const_index
[0];
2568 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2569 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2571 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2572 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2573 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2574 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2575 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2577 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2578 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2579 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2581 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2582 stride
== natural_stride
&& !split_stores
) {
2583 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2584 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2585 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2587 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2588 LLVMGetElementType(LLVMTypeOf(address
)), "");
2589 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2591 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2592 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2594 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2595 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMVectorTypeKind
)
2596 val_type
= LLVMGetElementType(val_type
);
2598 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2599 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2600 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2601 for (unsigned chan
= 0; chan
< 4; chan
++) {
2602 if (!(writemask
& (1 << chan
)))
2605 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2607 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2608 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2610 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2611 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2612 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2614 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2615 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2625 if (ctx
->ac
.postponed_kill
)
2626 ac_build_endif(&ctx
->ac
, 7002);
2629 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2632 case GLSL_SAMPLER_DIM_BUF
:
2634 case GLSL_SAMPLER_DIM_1D
:
2635 return array
? 2 : 1;
2636 case GLSL_SAMPLER_DIM_2D
:
2637 return array
? 3 : 2;
2638 case GLSL_SAMPLER_DIM_MS
:
2639 return array
? 4 : 3;
2640 case GLSL_SAMPLER_DIM_3D
:
2641 case GLSL_SAMPLER_DIM_CUBE
:
2643 case GLSL_SAMPLER_DIM_RECT
:
2644 case GLSL_SAMPLER_DIM_SUBPASS
:
2646 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2654 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2655 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2656 LLVMValueRef coord_z
,
2657 LLVMValueRef sample_index
,
2658 LLVMValueRef fmask_desc_ptr
)
2660 unsigned sample_chan
= coord_z
? 3 : 2;
2661 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2662 addr
[sample_chan
] = sample_index
;
2664 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2665 return addr
[sample_chan
];
2668 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2670 assert(instr
->src
[0].is_ssa
);
2671 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2674 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2675 const nir_intrinsic_instr
*instr
,
2676 LLVMValueRef dynamic_index
,
2677 enum ac_descriptor_type desc_type
,
2680 nir_deref_instr
*deref_instr
=
2681 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2682 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2684 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2687 static void get_image_coords(struct ac_nir_context
*ctx
,
2688 const nir_intrinsic_instr
*instr
,
2689 LLVMValueRef dynamic_desc_index
,
2690 struct ac_image_args
*args
,
2691 enum glsl_sampler_dim dim
,
2694 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2695 LLVMValueRef masks
[] = {
2696 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2697 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2699 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2702 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2703 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2704 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2705 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2706 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2707 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2708 count
= image_type_to_components_count(dim
, is_array
);
2710 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2711 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2712 LLVMValueRef fmask_load_address
[3];
2714 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2715 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2717 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2719 fmask_load_address
[2] = NULL
;
2721 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2722 fmask_load_address
[0],
2723 fmask_load_address
[1],
2724 fmask_load_address
[2],
2726 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2727 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2729 if (count
== 1 && !gfx9_1d
) {
2730 if (instr
->src
[1].ssa
->num_components
)
2731 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2733 args
->coords
[0] = src0
;
2738 for (chan
= 0; chan
< count
; ++chan
) {
2739 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2744 args
->coords
[2] = args
->coords
[1];
2745 args
->coords
[1] = ctx
->ac
.i32_0
;
2747 args
->coords
[1] = ctx
->ac
.i32_0
;
2750 if (ctx
->ac
.chip_class
== GFX9
&&
2751 dim
== GLSL_SAMPLER_DIM_2D
&&
2753 /* The hw can't bind a slice of a 3D image as a 2D
2754 * image, because it ignores BASE_ARRAY if the target
2755 * is 3D. The workaround is to read BASE_ARRAY and set
2756 * it as the 3rd address operand for all 2D images.
2758 LLVMValueRef first_layer
, const5
, mask
;
2760 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2761 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2762 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2763 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2765 args
->coords
[count
] = first_layer
;
2771 args
->coords
[count
] = sample_index
;
2777 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2778 const nir_intrinsic_instr
*instr
,
2779 LLVMValueRef dynamic_index
,
2780 bool write
, bool atomic
)
2782 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2783 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2784 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2785 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2786 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2788 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2789 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2790 elem_count
, stride
, "");
2792 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2793 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2798 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2799 struct waterfall_context
*wctx
,
2800 const nir_intrinsic_instr
*instr
)
2802 nir_deref_instr
*deref_instr
= NULL
;
2804 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2805 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2807 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2808 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2811 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2812 const nir_intrinsic_instr
*instr
,
2817 enum glsl_sampler_dim dim
;
2818 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2821 dim
= nir_intrinsic_image_dim(instr
);
2822 is_array
= nir_intrinsic_image_array(instr
);
2824 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2825 const struct glsl_type
*type
= image_deref
->type
;
2826 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2827 dim
= glsl_get_sampler_dim(type
);
2828 access
|= var
->data
.access
;
2829 is_array
= glsl_sampler_type_is_array(type
);
2832 struct waterfall_context wctx
;
2833 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2835 struct ac_image_args args
= {};
2837 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2839 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2840 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2841 unsigned num_channels
= util_last_bit(mask
);
2842 LLVMValueRef rsrc
, vindex
;
2844 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2845 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2848 assert(instr
->dest
.is_ssa
);
2849 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2850 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2851 ctx
->ac
.i32_0
, num_channels
,
2854 instr
->dest
.ssa
.bit_size
== 16);
2855 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2857 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2858 res
= ac_to_integer(&ctx
->ac
, res
);
2860 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2862 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2863 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2864 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2865 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2867 args
.lod
= get_src(ctx
, instr
->src
[3]);
2869 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2871 assert(instr
->dest
.is_ssa
);
2872 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
2874 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2876 return exit_waterfall(ctx
, &wctx
, res
);
2879 static void visit_image_store(struct ac_nir_context
*ctx
,
2880 const nir_intrinsic_instr
*instr
,
2883 if (ctx
->ac
.postponed_kill
) {
2884 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2885 ctx
->ac
.postponed_kill
, "");
2886 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2889 enum glsl_sampler_dim dim
;
2890 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2894 dim
= nir_intrinsic_image_dim(instr
);
2895 is_array
= nir_intrinsic_image_array(instr
);
2897 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2898 const struct glsl_type
*type
= image_deref
->type
;
2899 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2900 dim
= glsl_get_sampler_dim(type
);
2901 access
|= var
->data
.access
;
2902 is_array
= glsl_sampler_type_is_array(type
);
2905 struct waterfall_context wctx
;
2906 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2908 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2909 struct ac_image_args args
= {};
2911 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2913 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2914 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2915 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2916 unsigned src_channels
= ac_get_llvm_num_components(src
);
2917 LLVMValueRef vindex
;
2919 if (src_channels
== 3)
2920 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2922 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2923 get_src(ctx
, instr
->src
[1]),
2926 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2927 ctx
->ac
.i32_0
, args
.cache_policy
);
2929 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2931 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2932 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2933 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2934 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2935 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2937 args
.lod
= get_src(ctx
, instr
->src
[4]);
2939 args
.d16
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(args
.data
[0])) == 16;
2941 ac_build_image_opcode(&ctx
->ac
, &args
);
2944 exit_waterfall(ctx
, &wctx
, NULL
);
2945 if (ctx
->ac
.postponed_kill
)
2946 ac_build_endif(&ctx
->ac
, 7003);
2949 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2950 const nir_intrinsic_instr
*instr
,
2953 if (ctx
->ac
.postponed_kill
) {
2954 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2955 ctx
->ac
.postponed_kill
, "");
2956 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
2959 LLVMValueRef params
[7];
2960 int param_count
= 0;
2962 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2963 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2964 const char *atomic_name
;
2965 char intrinsic_name
[64];
2966 enum ac_atomic_op atomic_subop
;
2967 ASSERTED
int length
;
2969 enum glsl_sampler_dim dim
;
2972 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
2973 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
2974 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
2975 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
2976 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
2977 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2979 dim
= nir_intrinsic_image_dim(instr
);
2980 is_array
= nir_intrinsic_image_array(instr
);
2982 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2983 dim
= glsl_get_sampler_dim(type
);
2984 is_array
= glsl_sampler_type_is_array(type
);
2987 struct waterfall_context wctx
;
2988 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2990 switch (instr
->intrinsic
) {
2991 case nir_intrinsic_bindless_image_atomic_add
:
2992 case nir_intrinsic_image_deref_atomic_add
:
2993 atomic_name
= "add";
2994 atomic_subop
= ac_atomic_add
;
2996 case nir_intrinsic_bindless_image_atomic_imin
:
2997 case nir_intrinsic_image_deref_atomic_imin
:
2998 atomic_name
= "smin";
2999 atomic_subop
= ac_atomic_smin
;
3001 case nir_intrinsic_bindless_image_atomic_umin
:
3002 case nir_intrinsic_image_deref_atomic_umin
:
3003 atomic_name
= "umin";
3004 atomic_subop
= ac_atomic_umin
;
3006 case nir_intrinsic_bindless_image_atomic_imax
:
3007 case nir_intrinsic_image_deref_atomic_imax
:
3008 atomic_name
= "smax";
3009 atomic_subop
= ac_atomic_smax
;
3011 case nir_intrinsic_bindless_image_atomic_umax
:
3012 case nir_intrinsic_image_deref_atomic_umax
:
3013 atomic_name
= "umax";
3014 atomic_subop
= ac_atomic_umax
;
3016 case nir_intrinsic_bindless_image_atomic_and
:
3017 case nir_intrinsic_image_deref_atomic_and
:
3018 atomic_name
= "and";
3019 atomic_subop
= ac_atomic_and
;
3021 case nir_intrinsic_bindless_image_atomic_or
:
3022 case nir_intrinsic_image_deref_atomic_or
:
3024 atomic_subop
= ac_atomic_or
;
3026 case nir_intrinsic_bindless_image_atomic_xor
:
3027 case nir_intrinsic_image_deref_atomic_xor
:
3028 atomic_name
= "xor";
3029 atomic_subop
= ac_atomic_xor
;
3031 case nir_intrinsic_bindless_image_atomic_exchange
:
3032 case nir_intrinsic_image_deref_atomic_exchange
:
3033 atomic_name
= "swap";
3034 atomic_subop
= ac_atomic_swap
;
3036 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3037 case nir_intrinsic_image_deref_atomic_comp_swap
:
3038 atomic_name
= "cmpswap";
3039 atomic_subop
= 0; /* not used */
3041 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3042 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
3043 atomic_name
= "inc";
3044 atomic_subop
= ac_atomic_inc_wrap
;
3047 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3048 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3049 atomic_name
= "dec";
3050 atomic_subop
= ac_atomic_dec_wrap
;
3057 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
3058 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3060 LLVMValueRef result
;
3061 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3062 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
3063 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
3064 ctx
->ac
.i32_0
, ""); /* vindex */
3065 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3066 if (LLVM_VERSION_MAJOR
>= 9) {
3067 /* XXX: The new raw/struct atomic intrinsics are buggy
3068 * with LLVM 8, see r358579.
3070 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3071 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3073 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3074 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3076 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3078 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3079 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3082 assert(length
< sizeof(intrinsic_name
));
3083 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3084 params
, param_count
, 0);
3086 struct ac_image_args args
= {};
3087 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3088 args
.atomic
= atomic_subop
;
3089 args
.data
[0] = params
[0];
3091 args
.data
[1] = params
[1];
3092 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3093 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3094 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3096 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3099 result
= exit_waterfall(ctx
, &wctx
, result
);
3100 if (ctx
->ac
.postponed_kill
)
3101 ac_build_endif(&ctx
->ac
, 7004);
3105 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3106 nir_intrinsic_instr
*instr
)
3108 struct waterfall_context wctx
;
3109 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3110 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3112 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3114 return exit_waterfall(ctx
, &wctx
, ret
);
3117 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3118 const nir_intrinsic_instr
*instr
,
3123 enum glsl_sampler_dim dim
;
3126 dim
= nir_intrinsic_image_dim(instr
);
3127 is_array
= nir_intrinsic_image_array(instr
);
3129 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3130 dim
= glsl_get_sampler_dim(type
);
3131 is_array
= glsl_sampler_type_is_array(type
);
3134 struct waterfall_context wctx
;
3135 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3137 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3138 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3141 struct ac_image_args args
= { 0 };
3143 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3145 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3146 args
.opcode
= ac_image_get_resinfo
;
3147 args
.lod
= ctx
->ac
.i32_0
;
3148 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3150 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3152 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3154 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3155 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3156 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3157 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3158 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3161 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3162 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3163 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3167 return exit_waterfall(ctx
, &wctx
, res
);
3170 static void emit_membar(struct ac_llvm_context
*ac
,
3171 const nir_intrinsic_instr
*instr
)
3173 unsigned wait_flags
= 0;
3175 switch (instr
->intrinsic
) {
3176 case nir_intrinsic_memory_barrier
:
3177 case nir_intrinsic_group_memory_barrier
:
3178 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3180 case nir_intrinsic_memory_barrier_buffer
:
3181 case nir_intrinsic_memory_barrier_image
:
3182 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3184 case nir_intrinsic_memory_barrier_shared
:
3185 wait_flags
= AC_WAIT_LGKM
;
3191 ac_build_waitcnt(ac
, wait_flags
);
3194 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3196 /* GFX6 only (thanks to a hw bug workaround):
3197 * The real barrier instruction isn’t needed, because an entire patch
3198 * always fits into a single wave.
3200 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3201 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3204 ac_build_s_barrier(ac
);
3207 static void emit_discard(struct ac_nir_context
*ctx
,
3208 const nir_intrinsic_instr
*instr
)
3212 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3213 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3214 get_src(ctx
, instr
->src
[0]),
3217 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3218 cond
= ctx
->ac
.i1false
;
3221 ac_build_kill_if_false(&ctx
->ac
, cond
);
3224 static void emit_demote(struct ac_nir_context
*ctx
,
3225 const nir_intrinsic_instr
*instr
)
3229 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3230 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3231 get_src(ctx
, instr
->src
[0]),
3234 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3235 cond
= ctx
->ac
.i1false
;
3238 /* Kill immediately while maintaining WQM. */
3239 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3241 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3242 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3243 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3248 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3250 LLVMValueRef result
;
3251 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3252 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3253 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3254 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3256 if (ctx
->ac
.wave_size
== 32)
3257 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3258 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3260 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3264 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3266 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3267 LLVMValueRef result
;
3268 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3269 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3270 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3271 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3273 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3278 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3280 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3281 return LLVMBuildAnd(ctx
->ac
.builder
,
3282 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3283 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3285 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3290 visit_first_invocation(struct ac_nir_context
*ctx
)
3292 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3293 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3295 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3296 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3297 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3298 ctx
->ac
.iN_wavemask
, args
, 2,
3299 AC_FUNC_ATTR_NOUNWIND
|
3300 AC_FUNC_ATTR_READNONE
);
3302 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3306 visit_load_shared(struct ac_nir_context
*ctx
,
3307 const nir_intrinsic_instr
*instr
)
3309 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3311 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3312 instr
->dest
.ssa
.bit_size
);
3314 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3315 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3316 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3317 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3320 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3321 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3325 visit_store_shared(struct ac_nir_context
*ctx
,
3326 const nir_intrinsic_instr
*instr
)
3328 LLVMValueRef derived_ptr
, data
,index
;
3329 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3331 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3332 instr
->src
[0].ssa
->bit_size
);
3333 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3335 int writemask
= nir_intrinsic_write_mask(instr
);
3336 for (int chan
= 0; chan
< 4; chan
++) {
3337 if (!(writemask
& (1 << chan
))) {
3340 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3341 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3342 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3343 LLVMBuildStore(builder
, data
, derived_ptr
);
3347 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3348 const nir_intrinsic_instr
*instr
,
3349 LLVMValueRef ptr
, int src_idx
)
3351 if (ctx
->ac
.postponed_kill
) {
3352 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3353 ctx
->ac
.postponed_kill
, "");
3354 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3357 LLVMValueRef result
;
3358 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3360 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3362 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3363 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3364 if (deref
->mode
== nir_var_mem_global
) {
3365 /* use "singlethread" sync scope to implement relaxed ordering */
3366 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3368 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3369 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3373 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3374 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3375 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3376 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3377 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3379 LLVMAtomicRMWBinOp op
;
3380 switch (instr
->intrinsic
) {
3381 case nir_intrinsic_shared_atomic_add
:
3382 case nir_intrinsic_deref_atomic_add
:
3383 op
= LLVMAtomicRMWBinOpAdd
;
3385 case nir_intrinsic_shared_atomic_umin
:
3386 case nir_intrinsic_deref_atomic_umin
:
3387 op
= LLVMAtomicRMWBinOpUMin
;
3389 case nir_intrinsic_shared_atomic_umax
:
3390 case nir_intrinsic_deref_atomic_umax
:
3391 op
= LLVMAtomicRMWBinOpUMax
;
3393 case nir_intrinsic_shared_atomic_imin
:
3394 case nir_intrinsic_deref_atomic_imin
:
3395 op
= LLVMAtomicRMWBinOpMin
;
3397 case nir_intrinsic_shared_atomic_imax
:
3398 case nir_intrinsic_deref_atomic_imax
:
3399 op
= LLVMAtomicRMWBinOpMax
;
3401 case nir_intrinsic_shared_atomic_and
:
3402 case nir_intrinsic_deref_atomic_and
:
3403 op
= LLVMAtomicRMWBinOpAnd
;
3405 case nir_intrinsic_shared_atomic_or
:
3406 case nir_intrinsic_deref_atomic_or
:
3407 op
= LLVMAtomicRMWBinOpOr
;
3409 case nir_intrinsic_shared_atomic_xor
:
3410 case nir_intrinsic_deref_atomic_xor
:
3411 op
= LLVMAtomicRMWBinOpXor
;
3413 case nir_intrinsic_shared_atomic_exchange
:
3414 case nir_intrinsic_deref_atomic_exchange
:
3415 op
= LLVMAtomicRMWBinOpXchg
;
3417 #if LLVM_VERSION_MAJOR >= 10
3418 case nir_intrinsic_shared_atomic_fadd
:
3419 case nir_intrinsic_deref_atomic_fadd
:
3420 op
= LLVMAtomicRMWBinOpFAdd
;
3429 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_fadd
||
3430 instr
->intrinsic
== nir_intrinsic_deref_atomic_fadd
) {
3431 val
= ac_to_float(&ctx
->ac
, src
);
3433 val
= ac_to_integer(&ctx
->ac
, src
);
3436 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, val
, sync_scope
);
3439 if (ctx
->ac
.postponed_kill
)
3440 ac_build_endif(&ctx
->ac
, 7005);
3444 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3446 LLVMValueRef values
[2];
3447 LLVMValueRef pos
[2];
3449 pos
[0] = ac_to_float(&ctx
->ac
,
3450 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3451 pos
[1] = ac_to_float(&ctx
->ac
,
3452 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3454 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3455 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3456 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3459 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3460 enum glsl_interp_mode interp
, unsigned location
)
3463 case INTERP_MODE_FLAT
:
3466 case INTERP_MODE_SMOOTH
:
3467 case INTERP_MODE_NONE
:
3468 if (location
== INTERP_CENTER
)
3469 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3470 else if (location
== INTERP_CENTROID
)
3471 return ctx
->abi
->persp_centroid
;
3472 else if (location
== INTERP_SAMPLE
)
3473 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3475 case INTERP_MODE_NOPERSPECTIVE
:
3476 if (location
== INTERP_CENTER
)
3477 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3478 else if (location
== INTERP_CENTROID
)
3479 return ctx
->abi
->linear_centroid
;
3480 else if (location
== INTERP_SAMPLE
)
3481 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3487 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3490 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3491 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3494 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3496 LLVMValueRef offset
)
3498 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3499 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3500 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3502 LLVMValueRef ij_out
[2];
3503 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3506 * take the I then J parameters, and the DDX/Y for it, and
3507 * calculate the IJ inputs for the interpolator.
3508 * temp1 = ddx * offset/sample.x + I;
3509 * interp_param.I = ddy * offset/sample.y + temp1;
3510 * temp1 = ddx * offset/sample.x + J;
3511 * interp_param.J = ddy * offset/sample.y + temp1;
3513 for (unsigned i
= 0; i
< 2; i
++) {
3514 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3515 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3516 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3517 ddxy_out
, ix_ll
, "");
3518 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3519 ddxy_out
, iy_ll
, "");
3520 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3521 interp_param
, ix_ll
, "");
3522 LLVMValueRef temp1
, temp2
;
3524 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3527 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3528 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3530 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3531 temp2
, ctx
->ac
.i32
, "");
3533 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3534 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3537 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3540 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3541 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3544 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3546 LLVMValueRef sample_id
)
3548 if (ctx
->abi
->interp_at_sample_force_center
)
3549 return barycentric_center(ctx
, mode
);
3551 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3553 /* fetch sample ID */
3554 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3556 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3557 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3558 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3559 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3560 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3561 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3563 return barycentric_offset(ctx
, mode
, offset
);
3567 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3570 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3571 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3574 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3576 return LLVMBuildBitCast(ctx
->ac
.builder
,
3577 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3581 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3582 LLVMValueRef interp_param
,
3583 unsigned index
, unsigned comp_start
,
3584 unsigned num_components
,
3587 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3588 LLVMValueRef interp_param_f
;
3590 interp_param_f
= LLVMBuildBitCast(ctx
->ac
.builder
,
3591 interp_param
, ctx
->ac
.v2f32
, "");
3592 LLVMValueRef i
= LLVMBuildExtractElement(
3593 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_0
, "");
3594 LLVMValueRef j
= LLVMBuildExtractElement(
3595 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_1
, "");
3597 /* Workaround for issue 2647: kill threads with infinite interpolation coeffs */
3598 if (ctx
->verified_interp
&&
3599 !_mesa_hash_table_search(ctx
->verified_interp
, interp_param
)) {
3600 LLVMValueRef args
[2];
3602 args
[1] = LLVMConstInt(ctx
->ac
.i32
, S_NAN
| Q_NAN
| N_INFINITY
| P_INFINITY
, false);
3603 LLVMValueRef cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f32", ctx
->ac
.i1
,
3604 args
, 2, AC_FUNC_ATTR_READNONE
);
3605 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildNot(ctx
->ac
.builder
, cond
, ""));
3606 _mesa_hash_table_insert(ctx
->verified_interp
, interp_param
, interp_param
);
3609 LLVMValueRef values
[4];
3610 assert(bitsize
== 16 || bitsize
== 32);
3611 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3612 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3613 if (bitsize
== 16) {
3614 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3615 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3617 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3618 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3622 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3625 static LLVMValueRef
load_input(struct ac_nir_context
*ctx
,
3626 nir_intrinsic_instr
*instr
)
3628 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
3630 /* We only lower inputs for fragment shaders ATM */
3631 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
3633 assert(offset
[0].i32
== 0);
3635 unsigned component
= nir_intrinsic_component(instr
);
3636 unsigned index
= nir_intrinsic_base(instr
);
3637 unsigned vertex_id
= 2; /* P0 */
3639 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3640 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3642 switch (src0
[0].i32
) {
3653 unreachable("Invalid vertex index");
3657 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3658 LLVMValueRef values
[8];
3660 /* Each component of a 64-bit value takes up two GL-level channels. */
3661 unsigned num_components
= instr
->dest
.ssa
.num_components
;
3662 unsigned bit_size
= instr
->dest
.ssa
.bit_size
;
3664 bit_size
== 64 ? num_components
* 2 : num_components
;
3666 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3667 if (component
+ chan
> 4)
3668 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3669 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3670 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3671 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3674 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3675 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3676 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3677 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3680 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3681 if (bit_size
== 64) {
3682 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3683 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3684 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3689 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3690 nir_intrinsic_instr
*instr
)
3692 LLVMValueRef result
= NULL
;
3694 switch (instr
->intrinsic
) {
3695 case nir_intrinsic_ballot
:
3696 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3697 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3698 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3700 case nir_intrinsic_read_invocation
:
3701 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3702 get_src(ctx
, instr
->src
[1]));
3704 case nir_intrinsic_read_first_invocation
:
3705 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3707 case nir_intrinsic_load_subgroup_invocation
:
3708 result
= ac_get_thread_id(&ctx
->ac
);
3710 case nir_intrinsic_load_work_group_id
: {
3711 LLVMValueRef values
[3];
3713 for (int i
= 0; i
< 3; i
++) {
3714 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3715 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3718 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3721 case nir_intrinsic_load_base_vertex
:
3722 case nir_intrinsic_load_first_vertex
:
3723 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3725 case nir_intrinsic_load_local_group_size
:
3726 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3728 case nir_intrinsic_load_vertex_id
:
3729 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3730 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3731 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3733 case nir_intrinsic_load_vertex_id_zero_base
: {
3734 result
= ctx
->abi
->vertex_id
;
3737 case nir_intrinsic_load_local_invocation_id
: {
3738 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3741 case nir_intrinsic_load_base_instance
:
3742 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3744 case nir_intrinsic_load_draw_id
:
3745 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3747 case nir_intrinsic_load_view_index
:
3748 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3750 case nir_intrinsic_load_invocation_id
:
3751 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3752 result
= ac_unpack_param(&ctx
->ac
,
3753 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3756 if (ctx
->ac
.chip_class
>= GFX10
) {
3757 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3758 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3759 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3761 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3765 case nir_intrinsic_load_primitive_id
:
3766 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3767 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3768 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3769 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3770 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3771 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3773 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3775 case nir_intrinsic_load_sample_id
:
3776 result
= ac_unpack_param(&ctx
->ac
,
3777 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3780 case nir_intrinsic_load_sample_pos
:
3781 result
= load_sample_pos(ctx
);
3783 case nir_intrinsic_load_sample_mask_in
:
3784 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3786 case nir_intrinsic_load_frag_coord
: {
3787 LLVMValueRef values
[4] = {
3788 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3789 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3790 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3791 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3792 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3794 result
= ac_to_integer(&ctx
->ac
,
3795 ac_build_gather_values(&ctx
->ac
, values
, 4));
3798 case nir_intrinsic_load_layer_id
:
3799 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3801 case nir_intrinsic_load_front_face
:
3802 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3804 case nir_intrinsic_load_helper_invocation
:
3805 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3807 case nir_intrinsic_is_helper_invocation
:
3808 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3810 case nir_intrinsic_load_color0
:
3811 result
= ctx
->abi
->color0
;
3813 case nir_intrinsic_load_color1
:
3814 result
= ctx
->abi
->color1
;
3816 case nir_intrinsic_load_user_data_amd
:
3817 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3818 result
= ctx
->abi
->user_data
;
3820 case nir_intrinsic_load_instance_id
:
3821 result
= ctx
->abi
->instance_id
;
3823 case nir_intrinsic_load_num_work_groups
:
3824 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3826 case nir_intrinsic_load_local_invocation_index
:
3827 result
= visit_load_local_invocation_index(ctx
);
3829 case nir_intrinsic_load_subgroup_id
:
3830 result
= visit_load_subgroup_id(ctx
);
3832 case nir_intrinsic_load_num_subgroups
:
3833 result
= visit_load_num_subgroups(ctx
);
3835 case nir_intrinsic_first_invocation
:
3836 result
= visit_first_invocation(ctx
);
3838 case nir_intrinsic_load_push_constant
:
3839 result
= visit_load_push_constant(ctx
, instr
);
3841 case nir_intrinsic_vulkan_resource_index
: {
3842 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3843 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3844 unsigned binding
= nir_intrinsic_binding(instr
);
3846 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3850 case nir_intrinsic_vulkan_resource_reindex
:
3851 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3853 case nir_intrinsic_store_ssbo
:
3854 visit_store_ssbo(ctx
, instr
);
3856 case nir_intrinsic_load_ssbo
:
3857 result
= visit_load_buffer(ctx
, instr
);
3859 case nir_intrinsic_ssbo_atomic_add
:
3860 case nir_intrinsic_ssbo_atomic_imin
:
3861 case nir_intrinsic_ssbo_atomic_umin
:
3862 case nir_intrinsic_ssbo_atomic_imax
:
3863 case nir_intrinsic_ssbo_atomic_umax
:
3864 case nir_intrinsic_ssbo_atomic_and
:
3865 case nir_intrinsic_ssbo_atomic_or
:
3866 case nir_intrinsic_ssbo_atomic_xor
:
3867 case nir_intrinsic_ssbo_atomic_exchange
:
3868 case nir_intrinsic_ssbo_atomic_comp_swap
:
3869 result
= visit_atomic_ssbo(ctx
, instr
);
3871 case nir_intrinsic_load_ubo
:
3872 result
= visit_load_ubo_buffer(ctx
, instr
);
3874 case nir_intrinsic_get_buffer_size
:
3875 result
= visit_get_buffer_size(ctx
, instr
);
3877 case nir_intrinsic_load_deref
:
3878 result
= visit_load_var(ctx
, instr
);
3880 case nir_intrinsic_store_deref
:
3881 visit_store_var(ctx
, instr
);
3883 case nir_intrinsic_load_shared
:
3884 result
= visit_load_shared(ctx
, instr
);
3886 case nir_intrinsic_store_shared
:
3887 visit_store_shared(ctx
, instr
);
3889 case nir_intrinsic_bindless_image_samples
:
3890 case nir_intrinsic_image_deref_samples
:
3891 result
= visit_image_samples(ctx
, instr
);
3893 case nir_intrinsic_bindless_image_load
:
3894 result
= visit_image_load(ctx
, instr
, true);
3896 case nir_intrinsic_image_deref_load
:
3897 result
= visit_image_load(ctx
, instr
, false);
3899 case nir_intrinsic_bindless_image_store
:
3900 visit_image_store(ctx
, instr
, true);
3902 case nir_intrinsic_image_deref_store
:
3903 visit_image_store(ctx
, instr
, false);
3905 case nir_intrinsic_bindless_image_atomic_add
:
3906 case nir_intrinsic_bindless_image_atomic_imin
:
3907 case nir_intrinsic_bindless_image_atomic_umin
:
3908 case nir_intrinsic_bindless_image_atomic_imax
:
3909 case nir_intrinsic_bindless_image_atomic_umax
:
3910 case nir_intrinsic_bindless_image_atomic_and
:
3911 case nir_intrinsic_bindless_image_atomic_or
:
3912 case nir_intrinsic_bindless_image_atomic_xor
:
3913 case nir_intrinsic_bindless_image_atomic_exchange
:
3914 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3915 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3916 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3917 result
= visit_image_atomic(ctx
, instr
, true);
3919 case nir_intrinsic_image_deref_atomic_add
:
3920 case nir_intrinsic_image_deref_atomic_imin
:
3921 case nir_intrinsic_image_deref_atomic_umin
:
3922 case nir_intrinsic_image_deref_atomic_imax
:
3923 case nir_intrinsic_image_deref_atomic_umax
:
3924 case nir_intrinsic_image_deref_atomic_and
:
3925 case nir_intrinsic_image_deref_atomic_or
:
3926 case nir_intrinsic_image_deref_atomic_xor
:
3927 case nir_intrinsic_image_deref_atomic_exchange
:
3928 case nir_intrinsic_image_deref_atomic_comp_swap
:
3929 case nir_intrinsic_image_deref_atomic_inc_wrap
:
3930 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3931 result
= visit_image_atomic(ctx
, instr
, false);
3933 case nir_intrinsic_bindless_image_size
:
3934 result
= visit_image_size(ctx
, instr
, true);
3936 case nir_intrinsic_image_deref_size
:
3937 result
= visit_image_size(ctx
, instr
, false);
3939 case nir_intrinsic_shader_clock
:
3940 result
= ac_build_shader_clock(&ctx
->ac
,
3941 nir_intrinsic_memory_scope(instr
));
3943 case nir_intrinsic_discard
:
3944 case nir_intrinsic_discard_if
:
3945 emit_discard(ctx
, instr
);
3947 case nir_intrinsic_demote
:
3948 case nir_intrinsic_demote_if
:
3949 emit_demote(ctx
, instr
);
3951 case nir_intrinsic_memory_barrier
:
3952 case nir_intrinsic_group_memory_barrier
:
3953 case nir_intrinsic_memory_barrier_buffer
:
3954 case nir_intrinsic_memory_barrier_image
:
3955 case nir_intrinsic_memory_barrier_shared
:
3956 emit_membar(&ctx
->ac
, instr
);
3958 case nir_intrinsic_scoped_barrier
: {
3959 assert(!(nir_intrinsic_memory_semantics(instr
) &
3960 (NIR_MEMORY_MAKE_AVAILABLE
| NIR_MEMORY_MAKE_VISIBLE
)));
3962 nir_variable_mode modes
= nir_intrinsic_memory_modes(instr
);
3964 unsigned wait_flags
= 0;
3965 if (modes
& (nir_var_mem_global
| nir_var_mem_ssbo
))
3966 wait_flags
|= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3967 if (modes
& nir_var_mem_shared
)
3968 wait_flags
|= AC_WAIT_LGKM
;
3971 ac_build_waitcnt(&ctx
->ac
, wait_flags
);
3973 if (nir_intrinsic_execution_scope(instr
) == NIR_SCOPE_WORKGROUP
)
3974 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3977 case nir_intrinsic_memory_barrier_tcs_patch
:
3979 case nir_intrinsic_control_barrier
:
3980 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3982 case nir_intrinsic_shared_atomic_add
:
3983 case nir_intrinsic_shared_atomic_imin
:
3984 case nir_intrinsic_shared_atomic_umin
:
3985 case nir_intrinsic_shared_atomic_imax
:
3986 case nir_intrinsic_shared_atomic_umax
:
3987 case nir_intrinsic_shared_atomic_and
:
3988 case nir_intrinsic_shared_atomic_or
:
3989 case nir_intrinsic_shared_atomic_xor
:
3990 case nir_intrinsic_shared_atomic_exchange
:
3991 case nir_intrinsic_shared_atomic_comp_swap
:
3992 case nir_intrinsic_shared_atomic_fadd
: {
3993 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3994 instr
->src
[1].ssa
->bit_size
);
3995 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3998 case nir_intrinsic_deref_atomic_add
:
3999 case nir_intrinsic_deref_atomic_imin
:
4000 case nir_intrinsic_deref_atomic_umin
:
4001 case nir_intrinsic_deref_atomic_imax
:
4002 case nir_intrinsic_deref_atomic_umax
:
4003 case nir_intrinsic_deref_atomic_and
:
4004 case nir_intrinsic_deref_atomic_or
:
4005 case nir_intrinsic_deref_atomic_xor
:
4006 case nir_intrinsic_deref_atomic_exchange
:
4007 case nir_intrinsic_deref_atomic_comp_swap
:
4008 case nir_intrinsic_deref_atomic_fadd
: {
4009 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
4010 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4013 case nir_intrinsic_load_barycentric_pixel
:
4014 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
4016 case nir_intrinsic_load_barycentric_centroid
:
4017 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
4019 case nir_intrinsic_load_barycentric_sample
:
4020 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
4022 case nir_intrinsic_load_barycentric_model
:
4023 result
= barycentric_model(ctx
);
4025 case nir_intrinsic_load_barycentric_at_offset
: {
4026 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4027 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
4030 case nir_intrinsic_load_barycentric_at_sample
: {
4031 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
4032 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
4035 case nir_intrinsic_load_interpolated_input
: {
4036 /* We assume any indirect loads have been lowered away */
4037 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
4039 assert(offset
[0].i32
== 0);
4041 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
4042 unsigned index
= nir_intrinsic_base(instr
);
4043 unsigned component
= nir_intrinsic_component(instr
);
4044 result
= load_interpolated_input(ctx
, interp_param
, index
,
4046 instr
->dest
.ssa
.num_components
,
4047 instr
->dest
.ssa
.bit_size
);
4050 case nir_intrinsic_load_input
:
4051 case nir_intrinsic_load_input_vertex
:
4052 result
= load_input(ctx
, instr
);
4054 case nir_intrinsic_emit_vertex
:
4055 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
4057 case nir_intrinsic_emit_vertex_with_counter
: {
4058 unsigned stream
= nir_intrinsic_stream_id(instr
);
4059 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
4060 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
4065 case nir_intrinsic_end_primitive
:
4066 case nir_intrinsic_end_primitive_with_counter
:
4067 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4069 case nir_intrinsic_load_tess_coord
:
4070 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
4072 case nir_intrinsic_load_tess_level_outer
:
4073 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
4075 case nir_intrinsic_load_tess_level_inner
:
4076 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
4078 case nir_intrinsic_load_tess_level_outer_default
:
4079 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
4081 case nir_intrinsic_load_tess_level_inner_default
:
4082 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
4084 case nir_intrinsic_load_patch_vertices_in
:
4085 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4087 case nir_intrinsic_vote_all
: {
4088 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4089 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4092 case nir_intrinsic_vote_any
: {
4093 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4094 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4097 case nir_intrinsic_shuffle
:
4098 if (ctx
->ac
.chip_class
== GFX8
||
4099 ctx
->ac
.chip_class
== GFX9
||
4100 (ctx
->ac
.chip_class
>= GFX10
&& ctx
->ac
.wave_size
== 32)) {
4101 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4102 get_src(ctx
, instr
->src
[1]));
4104 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4105 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
4106 LLVMTypeRef type
= LLVMTypeOf(src
);
4107 struct waterfall_context wctx
;
4108 LLVMValueRef index_val
;
4110 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
4112 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
4115 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4117 (LLVMValueRef
[]) { src
, index_val
}, 2,
4118 AC_FUNC_ATTR_READNONE
|
4119 AC_FUNC_ATTR_CONVERGENT
);
4121 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4123 result
= exit_waterfall(ctx
, &wctx
, result
);
4126 case nir_intrinsic_reduce
:
4127 result
= ac_build_reduce(&ctx
->ac
,
4128 get_src(ctx
, instr
->src
[0]),
4129 instr
->const_index
[0],
4130 instr
->const_index
[1]);
4132 case nir_intrinsic_inclusive_scan
:
4133 result
= ac_build_inclusive_scan(&ctx
->ac
,
4134 get_src(ctx
, instr
->src
[0]),
4135 instr
->const_index
[0]);
4137 case nir_intrinsic_exclusive_scan
:
4138 result
= ac_build_exclusive_scan(&ctx
->ac
,
4139 get_src(ctx
, instr
->src
[0]),
4140 instr
->const_index
[0]);
4142 case nir_intrinsic_quad_broadcast
: {
4143 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4144 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4145 lane
, lane
, lane
, lane
);
4148 case nir_intrinsic_quad_swap_horizontal
:
4149 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4151 case nir_intrinsic_quad_swap_vertical
:
4152 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4154 case nir_intrinsic_quad_swap_diagonal
:
4155 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4157 case nir_intrinsic_quad_swizzle_amd
: {
4158 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4159 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4160 mask
& 0x3, (mask
>> 2) & 0x3,
4161 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4164 case nir_intrinsic_masked_swizzle_amd
: {
4165 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4166 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4169 case nir_intrinsic_write_invocation_amd
:
4170 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4171 get_src(ctx
, instr
->src
[1]),
4172 get_src(ctx
, instr
->src
[2]));
4174 case nir_intrinsic_mbcnt_amd
:
4175 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4177 case nir_intrinsic_load_scratch
: {
4178 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4179 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4181 LLVMTypeRef comp_type
=
4182 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4183 LLVMTypeRef vec_type
=
4184 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4185 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4186 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4187 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4188 LLVMPointerType(vec_type
, addr_space
), "");
4189 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4192 case nir_intrinsic_store_scratch
: {
4193 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4194 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4196 LLVMTypeRef comp_type
=
4197 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4198 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4199 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4200 LLVMPointerType(comp_type
, addr_space
), "");
4201 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4202 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4205 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4207 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4208 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4209 LLVMTypeRef vec_type
=
4210 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4211 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4213 LLVMPointerType(vec_type
, addr_space
),
4215 LLVMValueRef offset_src
=
4216 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4217 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4221 case nir_intrinsic_load_constant
: {
4222 unsigned base
= nir_intrinsic_base(instr
);
4223 unsigned range
= nir_intrinsic_range(instr
);
4225 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4226 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4227 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4229 /* Clamp the offset to avoid out-of-bound access because global
4230 * instructions can't handle them.
4232 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4233 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4235 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4237 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4239 LLVMTypeRef comp_type
=
4240 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4241 LLVMTypeRef vec_type
=
4242 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4243 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4244 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4245 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4246 LLVMPointerType(vec_type
, addr_space
), "");
4247 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4251 fprintf(stderr
, "Unknown intrinsic: ");
4252 nir_print_instr(&instr
->instr
, stderr
);
4253 fprintf(stderr
, "\n");
4257 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4261 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4262 unsigned base_index
,
4263 unsigned constant_index
,
4264 LLVMValueRef dynamic_index
)
4266 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4267 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4268 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4270 /* Bindless uniforms are 64bit so multiple index by 8 */
4271 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4272 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4274 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4276 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4277 NULL
, 0, 0, true, true);
4279 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4282 struct sampler_desc_address
{
4283 unsigned descriptor_set
;
4284 unsigned base_index
; /* binding in vulkan */
4285 unsigned constant_index
;
4286 LLVMValueRef dynamic_index
;
4291 static struct sampler_desc_address
4292 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4293 nir_deref_instr
*deref_instr
,
4294 const nir_instr
*instr
,
4297 LLVMValueRef index
= NULL
;
4298 unsigned constant_index
= 0;
4299 unsigned descriptor_set
;
4300 unsigned base_index
;
4301 bool bindless
= false;
4306 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4309 index
= get_src(ctx
, img_instr
->src
[0]);
4311 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4312 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4313 nir_tex_src_sampler_handle
);
4314 if (sampSrcIdx
!= -1) {
4317 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4319 assert(tex_instr
&& !image
);
4320 base_index
= tex_instr
->sampler_index
;
4324 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4325 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4326 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4330 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4331 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4333 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4335 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4336 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4341 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4344 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4345 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4346 unsigned sidx
= deref_instr
->strct
.index
;
4347 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4348 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4350 unreachable("Unsupported deref type");
4353 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4355 if (deref_instr
->var
->data
.bindless
) {
4356 /* For now just assert on unhandled variable types */
4357 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4359 base_index
= deref_instr
->var
->data
.driver_location
;
4362 index
= index
? index
: ctx
->ac
.i32_0
;
4363 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4364 constant_index
, index
);
4366 base_index
= deref_instr
->var
->data
.binding
;
4368 return (struct sampler_desc_address
) {
4369 .descriptor_set
= descriptor_set
,
4370 .base_index
= base_index
,
4371 .constant_index
= constant_index
,
4372 .dynamic_index
= index
,
4374 .bindless
= bindless
,
4378 /* Extract any possibly divergent index into a separate value that can be fed
4379 * into get_sampler_desc with the same arguments. */
4380 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4381 nir_deref_instr
*deref_instr
,
4382 const nir_instr
*instr
,
4385 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4386 return addr
.dynamic_index
;
4389 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4390 nir_deref_instr
*deref_instr
,
4391 enum ac_descriptor_type desc_type
,
4392 const nir_instr
*instr
,
4394 bool image
, bool write
)
4396 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4397 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4398 addr
.descriptor_set
,
4400 addr
.constant_index
, index
,
4401 desc_type
, addr
.image
, write
, addr
.bindless
);
4404 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4407 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4408 * filtering manually. The driver sets img7 to a mask clearing
4409 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4410 * s_and_b32 samp0, samp0, img7
4413 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4415 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4416 LLVMValueRef res
, LLVMValueRef samp
)
4418 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4419 LLVMValueRef img7
, samp0
;
4421 if (ctx
->ac
.chip_class
>= GFX8
)
4424 img7
= LLVMBuildExtractElement(builder
, res
,
4425 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4426 samp0
= LLVMBuildExtractElement(builder
, samp
,
4427 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4428 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4429 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4430 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4433 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4434 nir_tex_instr
*instr
,
4435 struct waterfall_context
*wctx
,
4436 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4437 LLVMValueRef
*fmask_ptr
)
4439 nir_deref_instr
*texture_deref_instr
= NULL
;
4440 nir_deref_instr
*sampler_deref_instr
= NULL
;
4443 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4444 switch (instr
->src
[i
].src_type
) {
4445 case nir_tex_src_texture_deref
:
4446 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4448 case nir_tex_src_sampler_deref
:
4449 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4451 case nir_tex_src_plane
:
4452 plane
= nir_src_as_int(instr
->src
[i
].src
);
4459 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4460 &instr
->instr
, false);
4461 if (!sampler_deref_instr
)
4462 sampler_deref_instr
= texture_deref_instr
;
4464 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4465 &instr
->instr
, false);
4466 if (instr
->texture_non_uniform
)
4467 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4469 if (instr
->sampler_non_uniform
)
4470 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4472 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4475 assert(instr
->op
!= nir_texop_txf_ms
&&
4476 instr
->op
!= nir_texop_samples_identical
);
4477 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4479 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4482 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4483 /* The fragment mask is fetched from the compressed
4484 * multisampled surface.
4486 main_descriptor
= AC_DESC_FMASK
;
4489 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4490 texture_dynamic_index
, false, false);
4493 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4494 sampler_dynamic_index
, false, false);
4495 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4496 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4498 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4499 instr
->op
== nir_texop_samples_identical
))
4500 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4501 &instr
->instr
, texture_dynamic_index
, false, false);
4504 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4507 coord
= ac_to_float(ctx
, coord
);
4508 coord
= ac_build_round(ctx
, coord
);
4509 coord
= ac_to_integer(ctx
, coord
);
4513 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4515 LLVMValueRef result
= NULL
;
4516 struct ac_image_args args
= { 0 };
4517 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4518 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4519 unsigned offset_src
= 0;
4520 struct waterfall_context wctx
[2] = {{{0}}};
4522 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4524 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4525 switch (instr
->src
[i
].src_type
) {
4526 case nir_tex_src_coord
: {
4527 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4528 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4529 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4532 case nir_tex_src_projector
:
4534 case nir_tex_src_comparator
:
4535 if (instr
->is_shadow
) {
4536 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4537 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4540 case nir_tex_src_offset
:
4541 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4544 case nir_tex_src_bias
:
4545 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4547 case nir_tex_src_lod
: {
4548 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4549 args
.level_zero
= true;
4551 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4554 case nir_tex_src_ms_index
:
4555 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4557 case nir_tex_src_ms_mcs
:
4559 case nir_tex_src_ddx
:
4560 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4562 case nir_tex_src_ddy
:
4563 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4565 case nir_tex_src_min_lod
:
4566 args
.min_lod
= get_src(ctx
, instr
->src
[i
].src
);
4568 case nir_tex_src_texture_offset
:
4569 case nir_tex_src_sampler_offset
:
4570 case nir_tex_src_plane
:
4576 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4577 result
= get_buffer_size(ctx
, args
.resource
, true);
4581 if (instr
->op
== nir_texop_texture_samples
) {
4582 LLVMValueRef res
, samples
, is_msaa
;
4583 LLVMValueRef default_sample
;
4585 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4586 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4587 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4588 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4589 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4590 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4591 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4592 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4593 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4595 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4596 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4597 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4598 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4599 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4602 if (ctx
->abi
->robust_buffer_access
) {
4603 LLVMValueRef dword1
, is_null_descriptor
;
4605 /* Extract the second dword of the descriptor, if it's
4606 * all zero, then it's a null descriptor.
4608 dword1
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4609 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
4610 is_null_descriptor
=
4611 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, dword1
,
4612 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4614 LLVMBuildSelect(ctx
->ac
.builder
, is_null_descriptor
,
4615 ctx
->ac
.i32_0
, ctx
->ac
.i32_1
, "");
4617 default_sample
= ctx
->ac
.i32_1
;
4620 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4621 default_sample
, "");
4626 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4627 LLVMValueRef offset
[3], pack
;
4628 for (unsigned chan
= 0; chan
< 3; ++chan
)
4629 offset
[chan
] = ctx
->ac
.i32_0
;
4631 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4632 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4633 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4634 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4635 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4637 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4638 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4640 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4641 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4645 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4646 * OpenGL 4.5 spec says:
4648 * "If the texture’s internal format indicates a fixed-point
4649 * depth texture, then D_t and D_ref are clamped to the
4650 * range [0, 1]; otherwise no clamping is performed."
4652 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4653 * so the depth comparison value isn't clamped for Z16 and
4654 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4655 * an explicitly clamped 32-bit float format.
4658 ctx
->ac
.chip_class
>= GFX8
&&
4659 ctx
->ac
.chip_class
<= GFX9
&&
4660 ctx
->abi
->clamp_shadow_reference
) {
4661 LLVMValueRef upgraded
, clamped
;
4663 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4664 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4665 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4666 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4667 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4668 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4669 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4673 /* pack derivatives */
4675 int num_src_deriv_channels
, num_dest_deriv_channels
;
4676 switch (instr
->sampler_dim
) {
4677 case GLSL_SAMPLER_DIM_3D
:
4678 case GLSL_SAMPLER_DIM_CUBE
:
4679 num_src_deriv_channels
= 3;
4680 num_dest_deriv_channels
= 3;
4682 case GLSL_SAMPLER_DIM_2D
:
4684 num_src_deriv_channels
= 2;
4685 num_dest_deriv_channels
= 2;
4687 case GLSL_SAMPLER_DIM_1D
:
4688 num_src_deriv_channels
= 1;
4689 if (ctx
->ac
.chip_class
== GFX9
) {
4690 num_dest_deriv_channels
= 2;
4692 num_dest_deriv_channels
= 1;
4697 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4698 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4699 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4700 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4701 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4703 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4704 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4705 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4709 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4710 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4711 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4712 if (instr
->coord_components
== 3)
4713 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4714 ac_prepare_cube_coords(&ctx
->ac
,
4715 instr
->op
== nir_texop_txd
, instr
->is_array
,
4716 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4719 /* Texture coordinates fixups */
4720 if (instr
->coord_components
> 1 &&
4721 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4723 instr
->op
!= nir_texop_txf
) {
4724 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4727 if (instr
->coord_components
> 2 &&
4728 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4729 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4730 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4731 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4733 instr
->op
!= nir_texop_txf
&&
4734 instr
->op
!= nir_texop_txf_ms
&&
4735 instr
->op
!= nir_texop_fragment_fetch
&&
4736 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4737 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4740 if (ctx
->ac
.chip_class
== GFX9
&&
4741 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4742 instr
->op
!= nir_texop_lod
) {
4743 LLVMValueRef filler
;
4744 if (instr
->op
== nir_texop_txf
)
4745 filler
= ctx
->ac
.i32_0
;
4747 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4749 if (instr
->is_array
)
4750 args
.coords
[2] = args
.coords
[1];
4751 args
.coords
[1] = filler
;
4754 /* Pack sample index */
4755 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4756 instr
->op
== nir_texop_fragment_fetch
))
4757 args
.coords
[instr
->coord_components
] = sample_index
;
4759 if (instr
->op
== nir_texop_samples_identical
) {
4760 struct ac_image_args txf_args
= { 0 };
4761 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4763 txf_args
.dmask
= 0xf;
4764 txf_args
.resource
= fmask_ptr
;
4765 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4766 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4768 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4769 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4773 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4774 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4775 instr
->op
!= nir_texop_txs
&&
4776 instr
->op
!= nir_texop_fragment_fetch
&&
4777 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4778 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4779 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4780 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4781 instr
->is_array
? args
.coords
[2] : NULL
,
4782 args
.coords
[sample_chan
], fmask_ptr
);
4785 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4786 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4787 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4788 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4789 args
.coords
[i
] = LLVMBuildAdd(
4790 ctx
->ac
.builder
, args
.coords
[i
],
4791 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4796 /* DMASK was repurposed for GATHER4. 4 components are always
4797 * returned and DMASK works like a swizzle - it selects
4798 * the component to fetch. The only valid DMASK values are
4799 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4800 * (red,red,red,red) etc.) The ISA document doesn't mention
4804 if (instr
->op
== nir_texop_tg4
) {
4805 if (instr
->is_shadow
)
4808 args
.dmask
= 1 << instr
->component
;
4811 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4812 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4813 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4816 /* Adjust the number of coordinates because we only need (x,y) for 2D
4817 * multisampled images and (x,y,layer) for 2D multisampled layered
4818 * images or for multisampled input attachments.
4820 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4821 if (args
.dim
== ac_image_2dmsaa
) {
4822 args
.dim
= ac_image_2d
;
4824 assert(args
.dim
== ac_image_2darraymsaa
);
4825 args
.dim
= ac_image_2darray
;
4829 assert(instr
->dest
.is_ssa
);
4830 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
4832 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4834 if (instr
->op
== nir_texop_query_levels
)
4835 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4836 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4837 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4838 instr
->op
!= nir_texop_tg4
)
4839 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4840 else if (instr
->op
== nir_texop_txs
&&
4841 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4843 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4844 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4845 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4846 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4847 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4848 } else if (ctx
->ac
.chip_class
== GFX9
&&
4849 instr
->op
== nir_texop_txs
&&
4850 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4852 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4853 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4854 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4856 } else if (instr
->dest
.ssa
.num_components
!= 4)
4857 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4861 assert(instr
->dest
.is_ssa
);
4862 result
= ac_to_integer(&ctx
->ac
, result
);
4864 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4865 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4868 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4872 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4874 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4875 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4877 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4878 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4881 static void visit_post_phi(struct ac_nir_context
*ctx
,
4882 nir_phi_instr
*instr
,
4883 LLVMValueRef llvm_phi
)
4885 nir_foreach_phi_src(src
, instr
) {
4886 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4887 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4889 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4893 static void phi_post_pass(struct ac_nir_context
*ctx
)
4895 hash_table_foreach(ctx
->phis
, entry
) {
4896 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4897 (LLVMValueRef
)entry
->data
);
4902 static bool is_def_used_in_an_export(const nir_ssa_def
* def
) {
4903 nir_foreach_use(use_src
, def
) {
4904 if (use_src
->parent_instr
->type
== nir_instr_type_intrinsic
) {
4905 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(use_src
->parent_instr
);
4906 if (instr
->intrinsic
== nir_intrinsic_store_deref
)
4908 } else if (use_src
->parent_instr
->type
== nir_instr_type_alu
) {
4909 nir_alu_instr
*instr
= nir_instr_as_alu(use_src
->parent_instr
);
4910 if (instr
->op
== nir_op_vec4
&&
4911 is_def_used_in_an_export(&instr
->dest
.dest
.ssa
)) {
4919 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4920 const nir_ssa_undef_instr
*instr
)
4922 unsigned num_components
= instr
->def
.num_components
;
4923 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4925 if (!ctx
->abi
->convert_undef_to_zero
|| is_def_used_in_an_export(&instr
->def
)) {
4928 if (num_components
== 1)
4929 undef
= LLVMGetUndef(type
);
4931 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4933 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4935 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
4936 if (num_components
> 1) {
4937 zero
= ac_build_gather_values_extended(
4938 &ctx
->ac
, &zero
, 4, 0, false, false);
4940 ctx
->ssa_defs
[instr
->def
.index
] = zero
;
4944 static void visit_jump(struct ac_llvm_context
*ctx
,
4945 const nir_jump_instr
*instr
)
4947 switch (instr
->type
) {
4948 case nir_jump_break
:
4949 ac_build_break(ctx
);
4951 case nir_jump_continue
:
4952 ac_build_continue(ctx
);
4955 fprintf(stderr
, "Unknown NIR jump instr: ");
4956 nir_print_instr(&instr
->instr
, stderr
);
4957 fprintf(stderr
, "\n");
4963 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4964 enum glsl_base_type type
)
4968 case GLSL_TYPE_UINT
:
4969 case GLSL_TYPE_BOOL
:
4970 case GLSL_TYPE_SUBROUTINE
:
4972 case GLSL_TYPE_INT8
:
4973 case GLSL_TYPE_UINT8
:
4975 case GLSL_TYPE_INT16
:
4976 case GLSL_TYPE_UINT16
:
4978 case GLSL_TYPE_FLOAT
:
4980 case GLSL_TYPE_FLOAT16
:
4982 case GLSL_TYPE_INT64
:
4983 case GLSL_TYPE_UINT64
:
4985 case GLSL_TYPE_DOUBLE
:
4988 unreachable("unknown GLSL type");
4993 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4994 const struct glsl_type
*type
)
4996 if (glsl_type_is_scalar(type
)) {
4997 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
5000 if (glsl_type_is_vector(type
)) {
5001 return LLVMVectorType(
5002 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
5003 glsl_get_vector_elements(type
));
5006 if (glsl_type_is_matrix(type
)) {
5007 return LLVMArrayType(
5008 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
5009 glsl_get_matrix_columns(type
));
5012 if (glsl_type_is_array(type
)) {
5013 return LLVMArrayType(
5014 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
5015 glsl_get_length(type
));
5018 assert(glsl_type_is_struct_or_ifc(type
));
5020 LLVMTypeRef member_types
[glsl_get_length(type
)];
5022 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5024 glsl_to_llvm_type(ac
,
5025 glsl_get_struct_field(type
, i
));
5028 return LLVMStructTypeInContext(ac
->context
, member_types
,
5029 glsl_get_length(type
), false);
5032 static void visit_deref(struct ac_nir_context
*ctx
,
5033 nir_deref_instr
*instr
)
5035 if (instr
->mode
!= nir_var_mem_shared
&&
5036 instr
->mode
!= nir_var_mem_global
)
5039 LLVMValueRef result
= NULL
;
5040 switch(instr
->deref_type
) {
5041 case nir_deref_type_var
: {
5042 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
5043 result
= entry
->data
;
5046 case nir_deref_type_struct
:
5047 if (instr
->mode
== nir_var_mem_global
) {
5048 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5049 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
5050 instr
->strct
.index
);
5051 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5052 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
5054 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5055 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
5058 case nir_deref_type_array
:
5059 if (instr
->mode
== nir_var_mem_global
) {
5060 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5061 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
5063 if ((glsl_type_is_matrix(parent
->type
) &&
5064 glsl_matrix_type_is_row_major(parent
->type
)) ||
5065 (glsl_type_is_vector(parent
->type
) && stride
== 0))
5066 stride
= type_scalar_size_bytes(parent
->type
);
5069 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5070 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5071 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5073 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5075 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5077 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5078 get_src(ctx
, instr
->arr
.index
));
5081 case nir_deref_type_ptr_as_array
:
5082 if (instr
->mode
== nir_var_mem_global
) {
5083 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
5085 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5086 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5087 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5089 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5091 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5093 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5094 get_src(ctx
, instr
->arr
.index
));
5097 case nir_deref_type_cast
: {
5098 result
= get_src(ctx
, instr
->parent
);
5100 /* We can't use the structs from LLVM because the shader
5101 * specifies its own offsets. */
5102 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
5103 if (instr
->mode
== nir_var_mem_shared
)
5104 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
5106 unsigned address_space
;
5108 switch(instr
->mode
) {
5109 case nir_var_mem_shared
:
5110 address_space
= AC_ADDR_SPACE_LDS
;
5112 case nir_var_mem_global
:
5113 address_space
= AC_ADDR_SPACE_GLOBAL
;
5116 unreachable("Unhandled address space");
5119 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
5121 if (LLVMTypeOf(result
) != type
) {
5122 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
5123 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
5126 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
5133 unreachable("Unhandled deref_instr deref type");
5136 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
5139 static void visit_cf_list(struct ac_nir_context
*ctx
,
5140 struct exec_list
*list
);
5142 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5144 nir_foreach_instr(instr
, block
)
5146 switch (instr
->type
) {
5147 case nir_instr_type_alu
:
5148 visit_alu(ctx
, nir_instr_as_alu(instr
));
5150 case nir_instr_type_load_const
:
5151 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5153 case nir_instr_type_intrinsic
:
5154 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5156 case nir_instr_type_tex
:
5157 visit_tex(ctx
, nir_instr_as_tex(instr
));
5159 case nir_instr_type_phi
:
5160 visit_phi(ctx
, nir_instr_as_phi(instr
));
5162 case nir_instr_type_ssa_undef
:
5163 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5165 case nir_instr_type_jump
:
5166 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
5168 case nir_instr_type_deref
:
5169 visit_deref(ctx
, nir_instr_as_deref(instr
));
5172 fprintf(stderr
, "Unknown NIR instr type: ");
5173 nir_print_instr(instr
, stderr
);
5174 fprintf(stderr
, "\n");
5179 _mesa_hash_table_insert(ctx
->defs
, block
,
5180 LLVMGetInsertBlock(ctx
->ac
.builder
));
5183 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5185 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5187 nir_block
*then_block
=
5188 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5190 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5192 visit_cf_list(ctx
, &if_stmt
->then_list
);
5194 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5195 nir_block
*else_block
=
5196 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5198 ac_build_else(&ctx
->ac
, else_block
->index
);
5199 visit_cf_list(ctx
, &if_stmt
->else_list
);
5202 ac_build_endif(&ctx
->ac
, then_block
->index
);
5205 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5207 nir_block
*first_loop_block
=
5208 (nir_block
*) exec_list_get_head(&loop
->body
);
5210 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5212 visit_cf_list(ctx
, &loop
->body
);
5214 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5217 static void visit_cf_list(struct ac_nir_context
*ctx
,
5218 struct exec_list
*list
)
5220 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5222 switch (node
->type
) {
5223 case nir_cf_node_block
:
5224 visit_block(ctx
, nir_cf_node_as_block(node
));
5227 case nir_cf_node_if
:
5228 visit_if(ctx
, nir_cf_node_as_if(node
));
5231 case nir_cf_node_loop
:
5232 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5242 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5243 struct ac_shader_abi
*abi
,
5244 struct nir_shader
*nir
,
5245 struct nir_variable
*variable
,
5246 gl_shader_stage stage
)
5248 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5249 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5251 /* tess ctrl has it's own load/store paths for outputs */
5252 if (stage
== MESA_SHADER_TESS_CTRL
)
5255 if (stage
== MESA_SHADER_VERTEX
||
5256 stage
== MESA_SHADER_TESS_EVAL
||
5257 stage
== MESA_SHADER_GEOMETRY
) {
5258 int idx
= variable
->data
.location
+ variable
->data
.index
;
5259 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5260 int length
= nir
->info
.clip_distance_array_size
+
5261 nir
->info
.cull_distance_array_size
;
5270 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5271 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5272 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5273 for (unsigned chan
= 0; chan
< 4; chan
++) {
5274 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5275 ac_build_alloca_undef(ctx
, type
, "");
5281 setup_locals(struct ac_nir_context
*ctx
,
5282 struct nir_function
*func
)
5285 ctx
->num_locals
= 0;
5286 nir_foreach_function_temp_variable(variable
, func
->impl
) {
5287 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5288 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5289 variable
->data
.location_frac
= 0;
5290 ctx
->num_locals
+= attrib_count
;
5292 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5296 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5297 for (j
= 0; j
< 4; j
++) {
5298 ctx
->locals
[i
* 4 + j
] =
5299 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5305 setup_scratch(struct ac_nir_context
*ctx
,
5306 struct nir_shader
*shader
)
5308 if (shader
->scratch_size
== 0)
5311 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5312 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5317 setup_constant_data(struct ac_nir_context
*ctx
,
5318 struct nir_shader
*shader
)
5320 if (!shader
->constant_data
)
5324 LLVMConstStringInContext(ctx
->ac
.context
,
5325 shader
->constant_data
,
5326 shader
->constant_data_size
,
5328 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5330 /* We want to put the constant data in the CONST address space so that
5331 * we can use scalar loads. However, LLVM versions before 10 put these
5332 * variables in the same section as the code, which is unacceptable
5333 * for RadeonSI as it needs to relocate all the data sections after
5334 * the code sections. See https://reviews.llvm.org/D65813.
5336 unsigned address_space
=
5337 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5339 LLVMValueRef global
=
5340 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5344 LLVMSetInitializer(global
, data
);
5345 LLVMSetGlobalConstant(global
, true);
5346 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5347 ctx
->constant_data
= global
;
5351 setup_shared(struct ac_nir_context
*ctx
,
5352 struct nir_shader
*nir
)
5357 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5358 nir
->info
.cs
.shared_size
);
5361 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5364 LLVMSetAlignment(lds
, 64 * 1024);
5366 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5367 LLVMPointerType(ctx
->ac
.i8
,
5368 AC_ADDR_SPACE_LDS
), "");
5371 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5372 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5374 struct ac_nir_context ctx
= {};
5375 struct nir_function
*func
;
5381 ctx
.stage
= nir
->info
.stage
;
5382 ctx
.info
= &nir
->info
;
5384 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5386 nir_foreach_shader_out_variable(variable
, nir
)
5387 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5390 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5391 _mesa_key_pointer_equal
);
5392 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5393 _mesa_key_pointer_equal
);
5394 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5395 _mesa_key_pointer_equal
);
5397 if (ctx
.abi
->kill_ps_if_inf_interp
)
5398 ctx
.verified_interp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5399 _mesa_key_pointer_equal
);
5401 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5403 nir_index_ssa_defs(func
->impl
);
5404 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5406 setup_locals(&ctx
, func
);
5407 setup_scratch(&ctx
, nir
);
5408 setup_constant_data(&ctx
, nir
);
5410 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5411 setup_shared(&ctx
, nir
);
5413 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5414 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5415 /* true = don't kill. */
5416 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5419 visit_cf_list(&ctx
, &func
->impl
->body
);
5420 phi_post_pass(&ctx
);
5422 if (ctx
.ac
.postponed_kill
)
5423 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5424 ctx
.ac
.postponed_kill
, ""));
5426 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5427 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5432 ralloc_free(ctx
.defs
);
5433 ralloc_free(ctx
.phis
);
5434 ralloc_free(ctx
.vars
);
5435 if (ctx
.abi
->kill_ps_if_inf_interp
)
5436 ralloc_free(ctx
.verified_interp
);
5440 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5442 bool progress
= false;
5444 /* Lower large variables to scratch first so that we won't bloat the
5445 * shader by generating large if ladders for them. We later lower
5446 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5448 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5449 nir_var_function_temp
,
5451 glsl_get_natural_size_align_bytes
);
5453 /* While it would be nice not to have this flag, we are constrained
5454 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5456 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5458 /* TODO: Indirect indexing of GS inputs is unimplemented.
5460 * TCS and TES load inputs directly from LDS or offchip memory, so
5461 * indirect indexing is trivial.
5463 nir_variable_mode indirect_mask
= 0;
5464 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5465 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5466 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5467 !llvm_has_working_vgpr_indexing
)) {
5468 indirect_mask
|= nir_var_shader_in
;
5470 if (!llvm_has_working_vgpr_indexing
&&
5471 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5472 indirect_mask
|= nir_var_shader_out
;
5474 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5475 * smart enough to handle indirects without causing excess spilling
5476 * causing the gpu to hang.
5478 * See the following thread for more details of the problem:
5479 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5481 indirect_mask
|= nir_var_function_temp
;
5483 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
);
5488 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5490 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
5494 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
5496 if (var
->data
.mode
!= nir_var_shader_out
)
5499 unsigned writemask
= 0;
5500 const int location
= var
->data
.location
;
5501 unsigned first_component
= var
->data
.location_frac
;
5502 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
5504 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5505 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
5506 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5507 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
5513 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5514 unsigned *cond_block_tf_writemask
,
5515 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5517 switch (cf_node
->type
) {
5518 case nir_cf_node_block
: {
5519 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5520 nir_foreach_instr(instr
, block
) {
5521 if (instr
->type
!= nir_instr_type_intrinsic
)
5524 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5525 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5527 /* If we find a barrier in nested control flow put this in the
5528 * too hard basket. In GLSL this is not possible but it is in
5532 *tessfactors_are_def_in_all_invocs
= false;
5536 /* The following case must be prevented:
5537 * gl_TessLevelInner = ...;
5539 * if (gl_InvocationID == 1)
5540 * gl_TessLevelInner = ...;
5542 * If you consider disjoint code segments separated by barriers, each
5543 * such segment that writes tess factor channels should write the same
5544 * channels in all codepaths within that segment.
5546 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5547 /* Accumulate the result: */
5548 *tessfactors_are_def_in_all_invocs
&=
5549 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5551 /* Analyze the next code segment from scratch. */
5552 *upper_block_tf_writemask
= 0;
5553 *cond_block_tf_writemask
= 0;
5556 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5561 case nir_cf_node_if
: {
5562 unsigned then_tessfactor_writemask
= 0;
5563 unsigned else_tessfactor_writemask
= 0;
5565 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5566 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5567 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5568 cond_block_tf_writemask
,
5569 tessfactors_are_def_in_all_invocs
, true);
5572 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5573 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5574 cond_block_tf_writemask
,
5575 tessfactors_are_def_in_all_invocs
, true);
5578 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5579 /* If both statements write the same tess factor channels,
5580 * we can say that the upper block writes them too.
5582 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5583 else_tessfactor_writemask
;
5584 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5585 else_tessfactor_writemask
;
5590 case nir_cf_node_loop
: {
5591 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5592 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5593 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5594 cond_block_tf_writemask
,
5595 tessfactors_are_def_in_all_invocs
, true);
5601 unreachable("unknown cf node type");
5606 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5608 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5610 /* The pass works as follows:
5611 * If all codepaths write tess factors, we can say that all
5612 * invocations define tess factors.
5614 * Each tess factor channel is tracked separately.
5616 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5617 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5619 /* Initial value = true. Here the pass will accumulate results from
5620 * multiple segments surrounded by barriers. If tess factors aren't
5621 * written at all, it's a shader bug and we don't care if this will be
5624 bool tessfactors_are_def_in_all_invocs
= true;
5626 nir_foreach_function(function
, nir
) {
5627 if (function
->impl
) {
5628 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5629 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5630 &cond_block_tf_writemask
,
5631 &tessfactors_are_def_in_all_invocs
,
5637 /* Accumulate the result for the last code segment separated by a
5640 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5641 tessfactors_are_def_in_all_invocs
&=
5642 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5645 return tessfactors_are_def_in_all_invocs
;