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_1f_param_scalar(struct ac_llvm_context
*ctx
,
221 LLVMTypeRef result_type
,
224 if (LLVMGetTypeKind(result_type
) != LLVMVectorTypeKind
)
225 return emit_intrin_1f_param(ctx
, intrin
, result_type
, src0
);
227 LLVMTypeRef elem_type
= LLVMGetElementType(result_type
);
228 LLVMValueRef ret
= LLVMGetUndef(result_type
);
230 /* Scalarize the intrinsic, because vectors are not supported. */
231 for (unsigned i
= 0; i
< LLVMGetVectorSize(result_type
); i
++) {
232 char name
[64], type
[64];
233 LLVMValueRef params
[] = {
234 ac_to_float(ctx
, ac_llvm_extract_elem(ctx
, src0
, i
)),
237 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
238 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
239 assert(length
< sizeof(name
));
240 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
241 ac_build_intrinsic(ctx
, name
, elem_type
, params
,
242 1, AC_FUNC_ATTR_READNONE
),
243 LLVMConstInt(ctx
->i32
, i
, 0), "");
248 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
250 LLVMTypeRef result_type
,
251 LLVMValueRef src0
, LLVMValueRef src1
)
253 char name
[64], type
[64];
254 LLVMValueRef params
[] = {
255 ac_to_float(ctx
, src0
),
256 ac_to_float(ctx
, src1
),
259 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
260 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
261 assert(length
< sizeof(name
));
262 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
265 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
267 LLVMTypeRef result_type
,
268 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
270 char name
[64], type
[64];
271 LLVMValueRef params
[] = {
272 ac_to_float(ctx
, src0
),
273 ac_to_float(ctx
, src1
),
274 ac_to_float(ctx
, src2
),
277 ac_build_type_name_for_intr(LLVMTypeOf(params
[0]), type
, sizeof(type
));
278 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.%s", intrin
, type
);
279 assert(length
< sizeof(name
));
280 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
283 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
284 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
286 LLVMTypeRef src1_type
= LLVMTypeOf(src1
);
287 LLVMTypeRef src2_type
= LLVMTypeOf(src2
);
289 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
291 if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
292 LLVMGetTypeKind(src2_type
) != LLVMPointerTypeKind
) {
293 src2
= LLVMBuildIntToPtr(ctx
->builder
, src2
, src1_type
, "");
294 } else if (LLVMGetTypeKind(src2_type
) == LLVMPointerTypeKind
&&
295 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
296 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src2_type
, "");
299 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
301 return LLVMBuildSelect(ctx
->builder
, v
,
302 ac_to_integer_or_pointer(ctx
, src1
),
303 ac_to_integer_or_pointer(ctx
, src2
), "");
306 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
309 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
312 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
314 LLVMValueRef src0
, LLVMValueRef src1
)
316 LLVMTypeRef ret_type
;
317 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
319 LLVMValueRef params
[] = { src0
, src1
};
320 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
323 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
324 params
, 2, AC_FUNC_ATTR_READNONE
);
326 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
327 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
331 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
335 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
336 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
338 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
342 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
346 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
348 unreachable("Unsupported bit size.");
352 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
355 src0
= ac_to_float(ctx
, src0
);
356 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
357 return LLVMBuildSExt(ctx
->builder
,
358 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
362 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
366 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
370 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
372 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
376 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
378 unreachable("Unsupported bit size.");
382 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
385 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
386 return LLVMBuildSExt(ctx
->builder
,
387 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
391 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
395 LLVMValueRef cond
= NULL
;
397 src0
= ac_to_float(ctx
, src0
);
398 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
400 if (ctx
->chip_class
>= GFX8
) {
401 LLVMValueRef args
[2];
402 /* Check if the result is a denormal - and flush to 0 if so. */
404 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
405 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
408 /* need to convert back up to f32 */
409 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
411 if (ctx
->chip_class
>= GFX8
)
412 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
415 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
416 * so compare the result and flush to 0 if it's smaller.
418 LLVMValueRef temp
, cond2
;
419 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
420 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
,
421 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
423 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealONE
,
424 temp
, ctx
->f32_0
, "");
425 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
426 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
431 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
432 LLVMValueRef src0
, LLVMValueRef src1
)
434 LLVMValueRef dst64
, result
;
435 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
436 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
438 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
439 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
440 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
444 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
445 LLVMValueRef src0
, LLVMValueRef src1
)
447 LLVMValueRef dst64
, result
;
448 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
449 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
451 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
452 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
453 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
457 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
458 LLVMValueRef bits
, LLVMValueRef offset
)
460 /* mask = ((1 << bits) - 1) << offset */
461 return LLVMBuildShl(ctx
->builder
,
462 LLVMBuildSub(ctx
->builder
,
463 LLVMBuildShl(ctx
->builder
,
470 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
471 LLVMValueRef mask
, LLVMValueRef insert
,
475 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
476 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
478 return LLVMBuildXor(ctx
->builder
, base
,
479 LLVMBuildAnd(ctx
->builder
, mask
,
480 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
483 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
485 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
486 LLVMValueRef args
[2]))
488 LLVMValueRef comp
[2];
490 src0
= ac_to_float(ctx
, src0
);
491 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
492 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
494 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
497 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
500 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
501 LLVMValueRef temps
[2], val
;
504 for (i
= 0; i
< 2; i
++) {
505 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
506 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
507 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
508 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
510 return ac_build_gather_values(ctx
, temps
, 2);
513 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
521 if (op
== nir_op_fddx_fine
)
522 mask
= AC_TID_MASK_LEFT
;
523 else if (op
== nir_op_fddy_fine
)
524 mask
= AC_TID_MASK_TOP
;
526 mask
= AC_TID_MASK_TOP_LEFT
;
528 /* for DDX we want to next X pixel, DDY next Y pixel. */
529 if (op
== nir_op_fddx_fine
||
530 op
== nir_op_fddx_coarse
||
536 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
540 struct waterfall_context
{
541 LLVMBasicBlockRef phi_bb
[2];
545 /* To deal with divergent descriptors we can create a loop that handles all
546 * lanes with the same descriptor on a given iteration (henceforth a
549 * These helper create the begin and end of the loop leaving the caller
550 * to implement the body.
553 * - ctx is the usal nir context
554 * - wctx is a temporary struct containing some loop info. Can be left uninitialized.
555 * - value is the possibly divergent value for which we built the loop
556 * - divergent is whether value is actually divergent. If false we just pass
559 static LLVMValueRef
enter_waterfall(struct ac_nir_context
*ctx
,
560 struct waterfall_context
*wctx
,
561 LLVMValueRef value
, bool divergent
)
563 /* If the app claims the value is divergent but it is constant we can
564 * end up with a dynamic index of NULL. */
568 wctx
->use_waterfall
= divergent
;
572 ac_build_bgnloop(&ctx
->ac
, 6000);
574 LLVMValueRef scalar_value
= ac_build_readlane(&ctx
->ac
, value
, NULL
);
576 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, value
,
577 scalar_value
, "uniform_active");
579 wctx
->phi_bb
[0] = LLVMGetInsertBlock(ctx
->ac
.builder
);
580 ac_build_ifcc(&ctx
->ac
, active
, 6001);
585 static LLVMValueRef
exit_waterfall(struct ac_nir_context
*ctx
,
586 struct waterfall_context
*wctx
,
589 LLVMValueRef ret
= NULL
;
590 LLVMValueRef phi_src
[2];
591 LLVMValueRef cc_phi_src
[2] = {
592 LLVMConstInt(ctx
->ac
.i32
, 0, false),
593 LLVMConstInt(ctx
->ac
.i32
, 0xffffffff, false),
596 if (!wctx
->use_waterfall
)
599 wctx
->phi_bb
[1] = LLVMGetInsertBlock(ctx
->ac
.builder
);
601 ac_build_endif(&ctx
->ac
, 6001);
604 phi_src
[0] = LLVMGetUndef(LLVMTypeOf(value
));
607 ret
= ac_build_phi(&ctx
->ac
, LLVMTypeOf(value
), 2, phi_src
, wctx
->phi_bb
);
611 * By using the optimization barrier on the exit decision, we decouple
612 * the operations from the break, and hence avoid LLVM hoisting the
613 * opteration into the break block.
615 LLVMValueRef cc
= ac_build_phi(&ctx
->ac
, ctx
->ac
.i32
, 2, cc_phi_src
, wctx
->phi_bb
);
616 ac_build_optimization_barrier(&ctx
->ac
, &cc
);
618 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, cc
, ctx
->ac
.i32_0
, "uniform_active2");
619 ac_build_ifcc(&ctx
->ac
, active
, 6002);
620 ac_build_break(&ctx
->ac
);
621 ac_build_endif(&ctx
->ac
, 6002);
623 ac_build_endloop(&ctx
->ac
, 6000);
627 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
629 LLVMValueRef src
[4], result
= NULL
;
630 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
631 unsigned src_components
;
632 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
634 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
641 case nir_op_pack_half_2x16
:
642 case nir_op_pack_snorm_2x16
:
643 case nir_op_pack_unorm_2x16
:
646 case nir_op_unpack_half_2x16
:
649 case nir_op_cube_face_coord
:
650 case nir_op_cube_face_index
:
654 src_components
= num_components
;
657 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
658 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
665 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
666 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
667 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
668 /* fneg will be optimized by backend compiler with sign
669 * bit removed via XOR. This is probably a LLVM bug.
671 result
= ac_build_canonicalize(&ctx
->ac
, result
,
672 instr
->dest
.dest
.ssa
.bit_size
);
676 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
679 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
682 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
685 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
686 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
687 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
690 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
691 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
692 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
695 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
698 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
701 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
704 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
707 /* lower_fmod only lower 16-bit and 32-bit fmod */
708 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
709 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
710 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
711 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
712 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
713 ac_to_float_type(&ctx
->ac
, def_type
), result
);
714 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
715 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
718 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
721 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
724 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
727 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
728 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
729 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
732 /* For doubles, we need precise division to pass GLCTS. */
733 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
&&
734 ac_get_type_size(def_type
) == 8) {
735 result
= LLVMBuildFDiv(ctx
->ac
.builder
, ctx
->ac
.f64_1
,
736 ac_to_float(&ctx
->ac
, src
[0]), "");
738 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rcp",
739 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
741 if (ctx
->abi
->clamp_div_by_zero
)
742 result
= ac_build_fmin(&ctx
->ac
, result
,
743 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
746 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
749 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
752 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
755 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
756 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
757 LLVMTypeOf(src
[0]), "");
758 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
759 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
760 LLVMTypeOf(src
[0]), "");
761 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
764 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
765 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
766 LLVMTypeOf(src
[0]), "");
767 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
768 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
769 LLVMTypeOf(src
[0]), "");
770 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
773 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
774 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
775 LLVMTypeOf(src
[0]), "");
776 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
777 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
778 LLVMTypeOf(src
[0]), "");
779 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
782 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
785 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
788 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
791 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
794 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
797 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
800 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
803 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
806 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
809 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
812 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
813 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
814 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
815 /* fabs will be optimized by backend compiler with sign
816 * bit removed via AND.
818 result
= ac_build_canonicalize(&ctx
->ac
, result
,
819 instr
->dest
.dest
.ssa
.bit_size
);
823 result
= emit_iabs(&ctx
->ac
, src
[0]);
826 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
829 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
832 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
835 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
838 result
= ac_build_isign(&ctx
->ac
, src
[0],
839 instr
->dest
.dest
.ssa
.bit_size
);
842 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
843 result
= ac_build_fsign(&ctx
->ac
, src
[0],
844 instr
->dest
.dest
.ssa
.bit_size
);
847 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
848 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
851 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
852 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
855 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
856 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
858 case nir_op_fround_even
:
859 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
860 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
863 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.fract",
864 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
867 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
868 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
871 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
872 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
875 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
876 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
879 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
880 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
883 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
884 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
887 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rsq",
888 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
889 if (ctx
->abi
->clamp_div_by_zero
)
890 result
= ac_build_fmin(&ctx
->ac
, result
,
891 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
893 case nir_op_frexp_exp
:
894 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
895 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
896 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
897 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
898 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
901 case nir_op_frexp_sig
:
902 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
903 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
904 instr
->dest
.dest
.ssa
.bit_size
);
907 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
908 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
911 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
912 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
913 if (ctx
->ac
.chip_class
< GFX9
&&
914 instr
->dest
.dest
.ssa
.bit_size
== 32) {
915 /* Only pre-GFX9 chips do not flush denorms. */
916 result
= ac_build_canonicalize(&ctx
->ac
, result
,
917 instr
->dest
.dest
.ssa
.bit_size
);
921 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
922 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
923 if (ctx
->ac
.chip_class
< GFX9
&&
924 instr
->dest
.dest
.ssa
.bit_size
== 32) {
925 /* Only pre-GFX9 chips do not flush denorms. */
926 result
= ac_build_canonicalize(&ctx
->ac
, result
,
927 instr
->dest
.dest
.ssa
.bit_size
);
931 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
932 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
933 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
936 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
937 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
938 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
939 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
940 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
942 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
945 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
947 case nir_op_bitfield_select
:
948 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
951 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
954 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
956 case nir_op_bitfield_reverse
:
957 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
959 case nir_op_bit_count
:
960 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
965 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
966 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
967 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
973 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
974 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
980 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
981 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
986 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
991 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
993 case nir_op_f2f16_rtz
:
996 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
998 /* For OpenGL, we want fast packing with v_cvt_pkrtz_f16, but if we use it,
999 * all f32->f16 conversions have to round towards zero, because both scalar
1000 * and vec2 down-conversions have to round equally.
1002 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
||
1003 instr
->op
== nir_op_f2f16_rtz
) {
1004 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1006 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
1007 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
1009 /* Fast path conversion. This only works if NIR is vectorized
1012 if (LLVMTypeOf(src
[0]) == ctx
->ac
.v2f32
) {
1013 LLVMValueRef args
[] = {
1014 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 0),
1015 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 1),
1017 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, args
);
1021 assert(ac_get_llvm_num_components(src
[0]) == 1);
1022 LLVMValueRef param
[2] = { src
[0], LLVMGetUndef(ctx
->ac
.f32
) };
1023 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
1024 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
1026 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1027 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1029 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1032 case nir_op_f2f16_rtne
:
1035 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1036 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1037 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1039 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1046 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1047 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1049 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1056 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1057 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1059 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1061 case nir_op_b32csel
:
1062 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1064 case nir_op_find_lsb
:
1065 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1067 case nir_op_ufind_msb
:
1068 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1070 case nir_op_ifind_msb
:
1071 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1073 case nir_op_uadd_carry
:
1074 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1076 case nir_op_usub_borrow
:
1077 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1082 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1085 result
= emit_f2b(&ctx
->ac
, src
[0]);
1091 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1094 result
= emit_i2b(&ctx
->ac
, src
[0]);
1096 case nir_op_fquantize2f16
:
1097 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1099 case nir_op_umul_high
:
1100 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1102 case nir_op_imul_high
:
1103 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1105 case nir_op_pack_half_2x16
:
1106 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1108 case nir_op_pack_snorm_2x16
:
1109 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1111 case nir_op_pack_unorm_2x16
:
1112 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1114 case nir_op_unpack_half_2x16
:
1115 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1119 case nir_op_fddx_fine
:
1120 case nir_op_fddy_fine
:
1121 case nir_op_fddx_coarse
:
1122 case nir_op_fddy_coarse
:
1123 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1126 case nir_op_unpack_64_2x32_split_x
: {
1127 assert(ac_get_llvm_num_components(src
[0]) == 1);
1128 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1131 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1136 case nir_op_unpack_64_2x32_split_y
: {
1137 assert(ac_get_llvm_num_components(src
[0]) == 1);
1138 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1141 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1146 case nir_op_pack_64_2x32_split
: {
1147 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1148 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1152 case nir_op_pack_32_2x16_split
: {
1153 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1154 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1158 case nir_op_unpack_32_2x16_split_x
: {
1159 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1162 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1167 case nir_op_unpack_32_2x16_split_y
: {
1168 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1171 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1176 case nir_op_cube_face_coord
: {
1177 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1178 LLVMValueRef results
[2];
1180 for (unsigned chan
= 0; chan
< 3; chan
++)
1181 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1182 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1183 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1184 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1185 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1186 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1187 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1188 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1189 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1190 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1191 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1192 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1193 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1197 case nir_op_cube_face_index
: {
1198 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1200 for (unsigned chan
= 0; chan
< 3; chan
++)
1201 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1202 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1203 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1208 fprintf(stderr
, "Unknown NIR alu instr: ");
1209 nir_print_instr(&instr
->instr
, stderr
);
1210 fprintf(stderr
, "\n");
1215 assert(instr
->dest
.dest
.is_ssa
);
1216 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1217 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1221 static void visit_load_const(struct ac_nir_context
*ctx
,
1222 const nir_load_const_instr
*instr
)
1224 LLVMValueRef values
[4], value
= NULL
;
1225 LLVMTypeRef element_type
=
1226 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1228 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1229 switch (instr
->def
.bit_size
) {
1231 values
[i
] = LLVMConstInt(element_type
,
1232 instr
->value
[i
].u8
, false);
1235 values
[i
] = LLVMConstInt(element_type
,
1236 instr
->value
[i
].u16
, false);
1239 values
[i
] = LLVMConstInt(element_type
,
1240 instr
->value
[i
].u32
, false);
1243 values
[i
] = LLVMConstInt(element_type
,
1244 instr
->value
[i
].u64
, false);
1248 "unsupported nir load_const bit_size: %d\n",
1249 instr
->def
.bit_size
);
1253 if (instr
->def
.num_components
> 1) {
1254 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1258 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1262 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1265 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1266 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1269 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1270 /* On GFX8, the descriptor contains the size in bytes,
1271 * but TXQ must return the size in elements.
1272 * The stride is always non-zero for resources using TXQ.
1274 LLVMValueRef stride
=
1275 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1277 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1278 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1279 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1280 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1282 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1287 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1288 * incorrectly forces nearest filtering if the texture format is integer.
1289 * The only effect it has on Gather4, which always returns 4 texels for
1290 * bilinear filtering, is that the final coordinates are off by 0.5 of
1293 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1294 * or (0.5 / size) from the normalized coordinates.
1296 * However, cube textures with 8_8_8_8 data formats require a different
1297 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1298 * precision in 32-bit data formats, so it needs to be applied dynamically at
1299 * runtime. In this case, return an i1 value that indicates whether the
1300 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1302 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1304 struct ac_image_args
*args
,
1305 const nir_tex_instr
*instr
)
1307 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1308 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1309 LLVMValueRef wa_8888
= NULL
;
1310 LLVMValueRef half_texel
[2];
1311 LLVMValueRef result
;
1313 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1315 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1316 LLVMValueRef formats
;
1317 LLVMValueRef data_format
;
1318 LLVMValueRef wa_formats
;
1320 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1322 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1323 LLVMConstInt(ctx
->i32
, 20, false), "");
1324 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1325 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1326 wa_8888
= LLVMBuildICmp(
1327 ctx
->builder
, LLVMIntEQ
, data_format
,
1328 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1331 uint32_t wa_num_format
=
1332 stype
== GLSL_TYPE_UINT
?
1333 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1334 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1335 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1336 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1338 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1339 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1341 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1342 args
->resource
= LLVMBuildInsertElement(
1343 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1346 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1348 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1350 struct ac_image_args resinfo
= {};
1351 LLVMBasicBlockRef bbs
[2];
1353 LLVMValueRef unnorm
= NULL
;
1354 LLVMValueRef default_offset
= ctx
->f32_0
;
1355 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1357 /* In vulkan, whether the sampler uses unnormalized
1358 * coordinates or not is a dynamic property of the
1359 * sampler. Hence, to figure out whether or not we
1360 * need to divide by the texture size, we need to test
1361 * the sampler at runtime. This tests the bit set by
1362 * radv_init_sampler().
1364 LLVMValueRef sampler0
=
1365 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1366 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1367 LLVMConstInt(ctx
->i32
, 15, false), "");
1368 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1369 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1370 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1373 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1374 if (wa_8888
|| unnorm
) {
1375 assert(!(wa_8888
&& unnorm
));
1376 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1377 /* Skip the texture size query entirely if we don't need it. */
1378 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1379 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1382 /* Query the texture size. */
1383 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1384 resinfo
.opcode
= ac_image_get_resinfo
;
1385 resinfo
.dmask
= 0xf;
1386 resinfo
.lod
= ctx
->i32_0
;
1387 resinfo
.resource
= args
->resource
;
1388 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1389 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1391 /* Compute -0.5 / size. */
1392 for (unsigned c
= 0; c
< 2; c
++) {
1394 LLVMBuildExtractElement(ctx
->builder
, size
,
1395 LLVMConstInt(ctx
->i32
, c
, 0), "");
1396 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1397 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1398 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1399 LLVMConstReal(ctx
->f32
, -0.5), "");
1402 if (wa_8888
|| unnorm
) {
1403 ac_build_endif(ctx
, 2000);
1405 for (unsigned c
= 0; c
< 2; c
++) {
1406 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1407 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1413 for (unsigned c
= 0; c
< 2; c
++) {
1415 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1416 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1419 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1420 result
= ac_build_image_opcode(ctx
, args
);
1422 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1423 LLVMValueRef tmp
, tmp2
;
1425 /* if the cube workaround is in place, f2i the result. */
1426 for (unsigned c
= 0; c
< 4; c
++) {
1427 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1428 if (stype
== GLSL_TYPE_UINT
)
1429 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1431 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1432 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1433 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1434 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1435 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1436 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1442 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1444 nir_deref_instr
*texture_deref_instr
= NULL
;
1446 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1447 switch (instr
->src
[i
].src_type
) {
1448 case nir_tex_src_texture_deref
:
1449 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1455 return texture_deref_instr
;
1458 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1459 const nir_tex_instr
*instr
,
1460 struct ac_image_args
*args
)
1462 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1463 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1465 assert(instr
->dest
.is_ssa
);
1466 return ac_build_buffer_load_format(&ctx
->ac
,
1470 util_last_bit(mask
),
1472 instr
->dest
.ssa
.bit_size
== 16);
1475 args
->opcode
= ac_image_sample
;
1477 switch (instr
->op
) {
1479 case nir_texop_txf_ms
:
1480 case nir_texop_samples_identical
:
1481 args
->opcode
= args
->level_zero
||
1482 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1483 ac_image_load
: ac_image_load_mip
;
1484 args
->level_zero
= false;
1487 case nir_texop_query_levels
:
1488 args
->opcode
= ac_image_get_resinfo
;
1490 args
->lod
= ctx
->ac
.i32_0
;
1491 args
->level_zero
= false;
1494 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1496 args
->level_zero
= true;
1500 args
->opcode
= ac_image_gather4
;
1501 if (!args
->lod
&& !args
->bias
)
1502 args
->level_zero
= true;
1505 args
->opcode
= ac_image_get_lod
;
1507 case nir_texop_fragment_fetch
:
1508 case nir_texop_fragment_mask_fetch
:
1509 args
->opcode
= ac_image_load
;
1510 args
->level_zero
= false;
1516 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1517 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1518 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1519 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1520 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1521 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1522 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1526 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1527 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1528 if ((args
->dim
== ac_image_2darray
||
1529 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1530 args
->coords
[1] = ctx
->ac
.i32_0
;
1534 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1535 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1536 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1537 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1538 /* Prevent texture instructions with implicit derivatives from being
1539 * sinked into branches. */
1540 switch (instr
->op
) {
1544 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1551 return ac_build_image_opcode(&ctx
->ac
, args
);
1554 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1555 nir_intrinsic_instr
*instr
)
1557 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1558 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1560 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1561 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1565 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1566 nir_intrinsic_instr
*instr
)
1568 LLVMValueRef ptr
, addr
;
1569 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1570 unsigned index
= nir_intrinsic_base(instr
);
1572 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1573 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1575 /* Load constant values from user SGPRS when possible, otherwise
1576 * fallback to the default path that loads directly from memory.
1578 if (LLVMIsConstant(src0
) &&
1579 instr
->dest
.ssa
.bit_size
== 32) {
1580 unsigned count
= instr
->dest
.ssa
.num_components
;
1581 unsigned offset
= index
;
1583 offset
+= LLVMConstIntGetZExtValue(src0
);
1586 offset
-= ctx
->args
->base_inline_push_consts
;
1588 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1589 if (offset
+ count
<= num_inline_push_consts
) {
1590 LLVMValueRef push_constants
[num_inline_push_consts
];
1591 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1592 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1593 ctx
->args
->inline_push_consts
[i
]);
1594 return ac_build_gather_values(&ctx
->ac
,
1595 push_constants
+ offset
,
1600 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1601 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1603 if (instr
->dest
.ssa
.bit_size
== 8) {
1604 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1605 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i8
, 4 * load_dwords
);
1606 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1607 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1609 LLVMValueRef params
[3];
1610 if (load_dwords
> 1) {
1611 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.v2i32
, "");
1612 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1613 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1615 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1616 params
[0] = ctx
->ac
.i32_0
;
1620 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1622 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1623 if (instr
->dest
.ssa
.num_components
> 1)
1624 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i8
, instr
->dest
.ssa
.num_components
), "");
1626 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1627 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1628 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i16
, 2 * load_dwords
);
1629 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1630 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1631 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1632 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1633 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1634 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1635 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1636 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1637 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1638 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1639 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1640 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1641 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1642 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1645 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1647 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1650 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1651 const nir_intrinsic_instr
*instr
)
1653 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1655 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1658 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1660 uint32_t new_mask
= 0;
1661 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1662 if (mask
& (1u << i
))
1663 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1667 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1668 unsigned start
, unsigned count
)
1670 LLVMValueRef mask
[] = {
1671 ctx
->i32_0
, ctx
->i32_1
,
1672 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1674 unsigned src_elements
= ac_get_llvm_num_components(src
);
1676 if (count
== src_elements
) {
1679 } else if (count
== 1) {
1680 assert(start
< src_elements
);
1681 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1683 assert(start
+ count
<= src_elements
);
1685 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1686 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1690 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1691 enum gl_access_qualifier access
,
1692 bool may_store_unaligned
,
1693 bool writeonly_memory
)
1695 unsigned cache_policy
= 0;
1697 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1698 * store opcodes not aligned to a dword are affected. The only way to
1699 * get unaligned stores is through shader images.
1701 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1702 /* If this is write-only, don't keep data in L1 to prevent
1703 * evicting L1 cache lines that may be needed by other
1707 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1708 cache_policy
|= ac_glc
;
1711 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1712 cache_policy
|= ac_slc
| ac_glc
;
1714 return cache_policy
;
1717 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1718 struct waterfall_context
*wctx
,
1719 const nir_intrinsic_instr
*instr
,
1722 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1723 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1726 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1727 nir_intrinsic_instr
*instr
)
1729 if (ctx
->ac
.postponed_kill
) {
1730 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1731 ctx
->ac
.postponed_kill
, "");
1732 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1735 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1736 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1737 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1738 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1739 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1740 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1742 struct waterfall_context wctx
;
1743 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1745 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1746 LLVMValueRef base_data
= src_data
;
1747 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1748 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1752 LLVMValueRef data
, offset
;
1753 LLVMTypeRef data_type
;
1755 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1757 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1758 * writes into a 2-element and a 1-element write. */
1760 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1761 writemask
|= 1 << (start
+ 2);
1764 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1766 /* we can only store 4 DWords at the same time.
1767 * can only happen for 64 Bit vectors. */
1768 if (num_bytes
> 16) {
1769 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1774 /* check alignment of 16 Bit stores */
1775 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1776 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1781 /* Due to alignment issues, split stores of 8-bit/16-bit
1784 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1785 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1787 num_bytes
= elem_size_bytes
;
1790 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1792 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1793 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1795 if (num_bytes
== 1) {
1796 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1797 offset
, ctx
->ac
.i32_0
,
1799 } else if (num_bytes
== 2) {
1800 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1801 offset
, ctx
->ac
.i32_0
,
1804 int num_channels
= num_bytes
/ 4;
1806 switch (num_bytes
) {
1807 case 16: /* v4f32 */
1808 data_type
= ctx
->ac
.v4f32
;
1810 case 12: /* v3f32 */
1811 data_type
= ctx
->ac
.v3f32
;
1814 data_type
= ctx
->ac
.v2f32
;
1817 data_type
= ctx
->ac
.f32
;
1820 unreachable("Malformed vector store.");
1822 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1824 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1825 num_channels
, offset
,
1831 exit_waterfall(ctx
, &wctx
, NULL
);
1833 if (ctx
->ac
.postponed_kill
)
1834 ac_build_endif(&ctx
->ac
, 7000);
1837 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1838 LLVMValueRef descriptor
,
1839 LLVMValueRef offset
,
1840 LLVMValueRef compare
,
1841 LLVMValueRef exchange
)
1843 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1844 if (ctx
->abi
->robust_buffer_access
) {
1845 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1847 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1848 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1850 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1852 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1855 LLVMValueRef ptr_parts
[2] = {
1856 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1857 LLVMBuildAnd(ctx
->ac
.builder
,
1858 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1859 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1862 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1863 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1865 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1867 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1868 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1869 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1870 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1872 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1873 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1875 if (ctx
->abi
->robust_buffer_access
) {
1876 ac_build_endif(&ctx
->ac
, -1);
1878 LLVMBasicBlockRef incoming_blocks
[2] = {
1883 LLVMValueRef incoming_values
[2] = {
1884 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1887 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1888 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1895 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1896 nir_intrinsic_instr
*instr
)
1898 if (ctx
->ac
.postponed_kill
) {
1899 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1900 ctx
->ac
.postponed_kill
, "");
1901 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1904 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1906 char name
[64], type
[8];
1907 LLVMValueRef params
[6], descriptor
;
1908 LLVMValueRef result
;
1911 struct waterfall_context wctx
;
1912 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1914 switch (instr
->intrinsic
) {
1915 case nir_intrinsic_ssbo_atomic_add
:
1918 case nir_intrinsic_ssbo_atomic_imin
:
1921 case nir_intrinsic_ssbo_atomic_umin
:
1924 case nir_intrinsic_ssbo_atomic_imax
:
1927 case nir_intrinsic_ssbo_atomic_umax
:
1930 case nir_intrinsic_ssbo_atomic_and
:
1933 case nir_intrinsic_ssbo_atomic_or
:
1936 case nir_intrinsic_ssbo_atomic_xor
:
1939 case nir_intrinsic_ssbo_atomic_exchange
:
1942 case nir_intrinsic_ssbo_atomic_comp_swap
:
1949 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1953 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1954 return_type
== ctx
->ac
.i64
) {
1955 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1956 get_src(ctx
, instr
->src
[1]),
1957 get_src(ctx
, instr
->src
[2]),
1958 get_src(ctx
, instr
->src
[3]));
1960 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1961 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1963 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1964 params
[arg_count
++] = descriptor
;
1966 if (LLVM_VERSION_MAJOR
>= 9) {
1967 /* XXX: The new raw/struct atomic intrinsics are buggy with
1968 * LLVM 8, see r358579.
1970 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1971 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1972 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1974 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1975 snprintf(name
, sizeof(name
),
1976 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1978 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1979 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1980 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1982 assert(return_type
== ctx
->ac
.i32
);
1983 snprintf(name
, sizeof(name
),
1984 "llvm.amdgcn.buffer.atomic.%s", op
);
1987 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1991 result
= exit_waterfall(ctx
, &wctx
, result
);
1992 if (ctx
->ac
.postponed_kill
)
1993 ac_build_endif(&ctx
->ac
, 7001);
1997 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1998 nir_intrinsic_instr
*instr
)
2000 struct waterfall_context wctx
;
2001 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
2003 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2004 int num_components
= instr
->num_components
;
2005 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2006 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
2008 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2009 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
2010 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
2012 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2013 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
2015 LLVMValueRef results
[4];
2016 for (int i
= 0; i
< num_components
;) {
2017 int num_elems
= num_components
- i
;
2018 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
2020 if (num_elems
* elem_size_bytes
> 16)
2021 num_elems
= 16 / elem_size_bytes
;
2022 int load_bytes
= num_elems
* elem_size_bytes
;
2024 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
2028 if (load_bytes
== 1) {
2029 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
2035 } else if (load_bytes
== 2) {
2036 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2043 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2044 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2046 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2047 vindex
, offset
, immoffset
, 0,
2048 cache_policy
, can_speculate
, false);
2051 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2052 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2053 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2055 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2056 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2058 for (unsigned j
= 0; j
< num_elems
; j
++) {
2059 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2064 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2065 return exit_waterfall(ctx
, &wctx
, ret
);
2068 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2069 struct waterfall_context
*wctx
,
2070 const nir_intrinsic_instr
*instr
)
2072 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2073 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2076 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2077 nir_intrinsic_instr
*instr
)
2079 struct waterfall_context wctx
;
2080 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2083 LLVMValueRef rsrc
= rsrc_base
;
2084 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2085 int num_components
= instr
->num_components
;
2087 if (ctx
->abi
->load_ubo
)
2088 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2090 if (instr
->dest
.ssa
.bit_size
== 64)
2091 num_components
*= 2;
2093 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2094 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2095 LLVMValueRef results
[num_components
];
2096 for (unsigned i
= 0; i
< num_components
; ++i
) {
2097 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2100 if (load_bytes
== 1) {
2101 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2108 assert(load_bytes
== 2);
2109 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2117 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2119 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2120 NULL
, 0, 0, true, true);
2122 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2125 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2126 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2128 return exit_waterfall(ctx
, &wctx
, ret
);
2132 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2133 bool vs_in
, unsigned *vertex_index_out
,
2134 LLVMValueRef
*vertex_index_ref
,
2135 unsigned *const_out
, LLVMValueRef
*indir_out
)
2137 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2138 nir_deref_path path
;
2139 unsigned idx_lvl
= 1;
2141 nir_deref_path_init(&path
, instr
, NULL
);
2143 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2144 if (vertex_index_ref
) {
2145 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2146 if (vertex_index_out
)
2147 *vertex_index_out
= 0;
2149 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2154 uint32_t const_offset
= 0;
2155 LLVMValueRef offset
= NULL
;
2157 if (var
->data
.compact
) {
2158 assert(instr
->deref_type
== nir_deref_type_array
);
2159 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2163 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2164 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2165 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2166 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2168 for (unsigned i
= 0; i
< index
; i
++) {
2169 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2170 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2172 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2173 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2174 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2175 const_offset
+= size
*
2176 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2178 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2179 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2181 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2186 unreachable("Uhandled deref type in get_deref_instr_offset");
2190 nir_deref_path_finish(&path
);
2192 if (const_offset
&& offset
)
2193 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2194 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2197 *const_out
= const_offset
;
2198 *indir_out
= offset
;
2201 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2202 nir_intrinsic_instr
*instr
,
2205 LLVMValueRef result
;
2206 LLVMValueRef vertex_index
= NULL
;
2207 LLVMValueRef indir_index
= NULL
;
2208 unsigned const_index
= 0;
2210 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2212 unsigned location
= var
->data
.location
;
2213 unsigned driver_location
= var
->data
.driver_location
;
2214 const bool is_patch
= var
->data
.patch
||
2215 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2216 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2217 const bool is_compact
= var
->data
.compact
;
2219 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2220 false, NULL
, is_patch
? NULL
: &vertex_index
,
2221 &const_index
, &indir_index
);
2223 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2225 LLVMTypeRef src_component_type
;
2226 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2227 src_component_type
= LLVMGetElementType(dest_type
);
2229 src_component_type
= dest_type
;
2231 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2232 vertex_index
, indir_index
,
2233 const_index
, location
, driver_location
,
2234 var
->data
.location_frac
,
2235 instr
->num_components
,
2236 is_patch
, is_compact
, load_inputs
);
2237 if (instr
->dest
.ssa
.bit_size
== 16) {
2238 result
= ac_to_integer(&ctx
->ac
, result
);
2239 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2241 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2245 type_scalar_size_bytes(const struct glsl_type
*type
)
2247 assert(glsl_type_is_vector_or_scalar(type
) ||
2248 glsl_type_is_matrix(type
));
2249 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2252 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2253 nir_intrinsic_instr
*instr
)
2255 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2256 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2258 LLVMValueRef values
[8];
2260 int ve
= instr
->dest
.ssa
.num_components
;
2262 LLVMValueRef indir_index
;
2264 unsigned const_index
;
2265 unsigned stride
= 4;
2266 int mode
= deref
->mode
;
2269 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2270 var
->data
.mode
== nir_var_shader_in
;
2271 idx
= var
->data
.driver_location
;
2272 comp
= var
->data
.location_frac
;
2273 mode
= var
->data
.mode
;
2275 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2276 &const_index
, &indir_index
);
2278 if (var
->data
.compact
) {
2280 const_index
+= comp
;
2285 if (instr
->dest
.ssa
.bit_size
== 64 &&
2286 (deref
->mode
== nir_var_shader_in
||
2287 deref
->mode
== nir_var_shader_out
||
2288 deref
->mode
== nir_var_function_temp
))
2292 case nir_var_shader_in
:
2293 /* TODO: remove this after RADV switches to lowered IO */
2294 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2295 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2296 return load_tess_varyings(ctx
, instr
, true);
2299 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2300 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2301 LLVMValueRef indir_index
;
2302 unsigned const_index
, vertex_index
;
2303 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2304 &const_index
, &indir_index
);
2305 assert(indir_index
== NULL
);
2307 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2308 var
->data
.driver_location
,
2309 var
->data
.location_frac
,
2310 instr
->num_components
, vertex_index
, const_index
, type
);
2313 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2315 unsigned count
= glsl_count_attribute_slots(
2317 ctx
->stage
== MESA_SHADER_VERTEX
);
2319 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2320 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2321 stride
, false, true);
2323 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2327 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2330 case nir_var_function_temp
:
2331 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2333 unsigned count
= glsl_count_attribute_slots(
2336 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2337 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2338 stride
, true, true);
2340 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2344 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2348 case nir_var_shader_out
:
2349 /* TODO: remove this after RADV switches to lowered IO */
2350 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2351 return load_tess_varyings(ctx
, instr
, false);
2354 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2355 var
->data
.fb_fetch_output
&&
2356 ctx
->abi
->emit_fbfetch
)
2357 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2359 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2361 unsigned count
= glsl_count_attribute_slots(
2364 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2365 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2366 stride
, true, true);
2368 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2372 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2373 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2378 case nir_var_mem_global
: {
2379 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2380 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2381 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2382 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2383 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2384 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2385 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2387 if (stride
!= natural_stride
|| split_loads
) {
2388 if (LLVMGetTypeKind(result_type
) == LLVMVectorTypeKind
)
2389 result_type
= LLVMGetElementType(result_type
);
2391 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2392 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2393 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2395 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2396 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2397 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2398 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2400 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2401 LLVMSetOrdering(values
[i
], LLVMAtomicOrderingMonotonic
);
2403 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2405 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2406 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2407 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2408 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2410 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2411 LLVMSetOrdering(val
, LLVMAtomicOrderingMonotonic
);
2416 unreachable("unhandle variable mode");
2418 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2419 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2423 visit_store_var(struct ac_nir_context
*ctx
,
2424 nir_intrinsic_instr
*instr
)
2426 if (ctx
->ac
.postponed_kill
) {
2427 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2428 ctx
->ac
.postponed_kill
, "");
2429 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2432 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2433 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2435 LLVMValueRef temp_ptr
, value
;
2438 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2439 int writemask
= instr
->const_index
[0];
2440 LLVMValueRef indir_index
;
2441 unsigned const_index
;
2444 get_deref_offset(ctx
, deref
, false,
2445 NULL
, NULL
, &const_index
, &indir_index
);
2446 idx
= var
->data
.driver_location
;
2447 comp
= var
->data
.location_frac
;
2449 if (var
->data
.compact
) {
2450 const_index
+= comp
;
2455 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2456 (deref
->mode
== nir_var_shader_out
||
2457 deref
->mode
== nir_var_function_temp
)) {
2459 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2460 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2463 writemask
= widen_mask(writemask
, 2);
2466 writemask
= writemask
<< comp
;
2468 switch (deref
->mode
) {
2469 case nir_var_shader_out
:
2470 /* TODO: remove this after RADV switches to lowered IO */
2471 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2472 LLVMValueRef vertex_index
= NULL
;
2473 LLVMValueRef indir_index
= NULL
;
2474 unsigned const_index
= 0;
2475 const bool is_patch
= var
->data
.patch
||
2476 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2477 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2479 get_deref_offset(ctx
, deref
, false, NULL
,
2480 is_patch
? NULL
: &vertex_index
,
2481 &const_index
, &indir_index
);
2483 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2484 vertex_index
, indir_index
,
2485 const_index
, src
, writemask
,
2486 var
->data
.location_frac
,
2487 var
->data
.driver_location
);
2491 for (unsigned chan
= 0; chan
< 8; chan
++) {
2493 if (!(writemask
& (1 << chan
)))
2496 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2498 if (var
->data
.compact
)
2501 unsigned count
= glsl_count_attribute_slots(
2504 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2505 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2506 stride
, true, true);
2508 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2509 value
, indir_index
, "");
2510 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2511 count
, stride
, tmp_vec
);
2514 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2516 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2520 case nir_var_function_temp
:
2521 for (unsigned chan
= 0; chan
< 8; chan
++) {
2522 if (!(writemask
& (1 << chan
)))
2525 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2527 unsigned count
= glsl_count_attribute_slots(
2530 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2531 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2534 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2535 value
, indir_index
, "");
2536 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2539 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2541 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2546 case nir_var_mem_global
: {
2547 int writemask
= instr
->const_index
[0];
2548 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2549 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2551 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2552 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2553 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2554 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2555 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2557 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2558 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2559 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2561 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2562 stride
== natural_stride
&& !split_stores
) {
2563 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2564 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2565 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2567 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2568 LLVMGetElementType(LLVMTypeOf(address
)), "");
2569 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2571 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2572 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2574 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2575 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMVectorTypeKind
)
2576 val_type
= LLVMGetElementType(val_type
);
2578 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2579 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2580 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2581 for (unsigned chan
= 0; chan
< 4; chan
++) {
2582 if (!(writemask
& (1 << chan
)))
2585 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2587 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2588 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2590 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2591 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2592 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2594 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2595 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2605 if (ctx
->ac
.postponed_kill
)
2606 ac_build_endif(&ctx
->ac
, 7002);
2610 visit_store_output(struct ac_nir_context
*ctx
, nir_intrinsic_instr
*instr
)
2612 if (ctx
->ac
.postponed_kill
) {
2613 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2614 ctx
->ac
.postponed_kill
, "");
2615 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2618 unsigned base
= nir_intrinsic_base(instr
);
2619 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2620 unsigned component
= nir_intrinsic_component(instr
);
2621 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2622 nir_src offset
= *nir_get_io_offset_src(instr
);
2623 LLVMValueRef indir_index
= NULL
;
2625 if (nir_src_is_const(offset
))
2626 assert(nir_src_as_uint(offset
) == 0);
2628 indir_index
= get_src(ctx
, offset
);
2630 switch (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
))) {
2634 writemask
= widen_mask(writemask
, 2);
2635 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2636 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2640 unreachable("unhandled store_output bit size");
2644 writemask
<<= component
;
2646 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2647 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
2648 LLVMValueRef vertex_index
=
2649 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
2651 ctx
->abi
->store_tcs_outputs(ctx
->abi
, NULL
,
2652 vertex_index
, indir_index
,
2654 component
, base
* 4);
2658 /* No indirect indexing is allowed after this point. */
2659 assert(!indir_index
);
2661 for (unsigned chan
= 0; chan
< 8; chan
++) {
2662 if (!(writemask
& (1 << chan
)))
2665 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2666 LLVMBuildStore(ctx
->ac
.builder
, value
,
2667 ctx
->abi
->outputs
[base
* 4 + chan
]);
2670 if (ctx
->ac
.postponed_kill
)
2671 ac_build_endif(&ctx
->ac
, 7002);
2674 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2677 case GLSL_SAMPLER_DIM_BUF
:
2679 case GLSL_SAMPLER_DIM_1D
:
2680 return array
? 2 : 1;
2681 case GLSL_SAMPLER_DIM_2D
:
2682 return array
? 3 : 2;
2683 case GLSL_SAMPLER_DIM_MS
:
2684 return array
? 4 : 3;
2685 case GLSL_SAMPLER_DIM_3D
:
2686 case GLSL_SAMPLER_DIM_CUBE
:
2688 case GLSL_SAMPLER_DIM_RECT
:
2689 case GLSL_SAMPLER_DIM_SUBPASS
:
2691 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2699 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2700 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2701 LLVMValueRef coord_z
,
2702 LLVMValueRef sample_index
,
2703 LLVMValueRef fmask_desc_ptr
)
2705 unsigned sample_chan
= coord_z
? 3 : 2;
2706 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2707 addr
[sample_chan
] = sample_index
;
2709 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2710 return addr
[sample_chan
];
2713 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2715 assert(instr
->src
[0].is_ssa
);
2716 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2719 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2720 const nir_intrinsic_instr
*instr
,
2721 LLVMValueRef dynamic_index
,
2722 enum ac_descriptor_type desc_type
,
2725 nir_deref_instr
*deref_instr
=
2726 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2727 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2729 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2732 static void get_image_coords(struct ac_nir_context
*ctx
,
2733 const nir_intrinsic_instr
*instr
,
2734 LLVMValueRef dynamic_desc_index
,
2735 struct ac_image_args
*args
,
2736 enum glsl_sampler_dim dim
,
2739 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2740 LLVMValueRef masks
[] = {
2741 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2742 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2744 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2747 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2748 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2749 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2750 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2751 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2752 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2753 count
= image_type_to_components_count(dim
, is_array
);
2755 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2756 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2757 LLVMValueRef fmask_load_address
[3];
2759 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2760 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2762 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2764 fmask_load_address
[2] = NULL
;
2766 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2767 fmask_load_address
[0],
2768 fmask_load_address
[1],
2769 fmask_load_address
[2],
2771 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2772 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2774 if (count
== 1 && !gfx9_1d
) {
2775 if (instr
->src
[1].ssa
->num_components
)
2776 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2778 args
->coords
[0] = src0
;
2783 for (chan
= 0; chan
< count
; ++chan
) {
2784 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2789 args
->coords
[2] = args
->coords
[1];
2790 args
->coords
[1] = ctx
->ac
.i32_0
;
2792 args
->coords
[1] = ctx
->ac
.i32_0
;
2795 if (ctx
->ac
.chip_class
== GFX9
&&
2796 dim
== GLSL_SAMPLER_DIM_2D
&&
2798 /* The hw can't bind a slice of a 3D image as a 2D
2799 * image, because it ignores BASE_ARRAY if the target
2800 * is 3D. The workaround is to read BASE_ARRAY and set
2801 * it as the 3rd address operand for all 2D images.
2803 LLVMValueRef first_layer
, const5
, mask
;
2805 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2806 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2807 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2808 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2810 args
->coords
[count
] = first_layer
;
2816 args
->coords
[count
] = sample_index
;
2822 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2823 const nir_intrinsic_instr
*instr
,
2824 LLVMValueRef dynamic_index
,
2825 bool write
, bool atomic
)
2827 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2828 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2829 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2830 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2831 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2833 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2834 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2835 elem_count
, stride
, "");
2837 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2838 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2843 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2844 struct waterfall_context
*wctx
,
2845 const nir_intrinsic_instr
*instr
)
2847 nir_deref_instr
*deref_instr
= NULL
;
2849 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2850 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2852 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2853 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2856 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2857 const nir_intrinsic_instr
*instr
,
2862 enum glsl_sampler_dim dim
;
2863 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2866 dim
= nir_intrinsic_image_dim(instr
);
2867 is_array
= nir_intrinsic_image_array(instr
);
2869 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2870 const struct glsl_type
*type
= image_deref
->type
;
2871 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2872 dim
= glsl_get_sampler_dim(type
);
2873 access
|= var
->data
.access
;
2874 is_array
= glsl_sampler_type_is_array(type
);
2877 struct waterfall_context wctx
;
2878 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2880 struct ac_image_args args
= {};
2882 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2884 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2885 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2886 unsigned num_channels
= util_last_bit(mask
);
2887 LLVMValueRef rsrc
, vindex
;
2889 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2890 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2893 assert(instr
->dest
.is_ssa
);
2894 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2895 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2896 ctx
->ac
.i32_0
, num_channels
,
2899 instr
->dest
.ssa
.bit_size
== 16);
2900 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2902 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2903 res
= ac_to_integer(&ctx
->ac
, res
);
2905 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2907 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2908 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2909 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2910 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2912 args
.lod
= get_src(ctx
, instr
->src
[3]);
2914 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2916 assert(instr
->dest
.is_ssa
);
2917 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
2919 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2921 return exit_waterfall(ctx
, &wctx
, res
);
2924 static void visit_image_store(struct ac_nir_context
*ctx
,
2925 const nir_intrinsic_instr
*instr
,
2928 if (ctx
->ac
.postponed_kill
) {
2929 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2930 ctx
->ac
.postponed_kill
, "");
2931 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2934 enum glsl_sampler_dim dim
;
2935 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2939 dim
= nir_intrinsic_image_dim(instr
);
2940 is_array
= nir_intrinsic_image_array(instr
);
2942 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2943 const struct glsl_type
*type
= image_deref
->type
;
2944 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2945 dim
= glsl_get_sampler_dim(type
);
2946 access
|= var
->data
.access
;
2947 is_array
= glsl_sampler_type_is_array(type
);
2950 struct waterfall_context wctx
;
2951 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2953 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2954 struct ac_image_args args
= {};
2956 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2958 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2959 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2960 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2961 unsigned src_channels
= ac_get_llvm_num_components(src
);
2962 LLVMValueRef vindex
;
2964 if (src_channels
== 3)
2965 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2967 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2968 get_src(ctx
, instr
->src
[1]),
2971 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2972 ctx
->ac
.i32_0
, args
.cache_policy
);
2974 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2976 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2977 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2978 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2979 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2980 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2982 args
.lod
= get_src(ctx
, instr
->src
[4]);
2984 args
.d16
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(args
.data
[0])) == 16;
2986 ac_build_image_opcode(&ctx
->ac
, &args
);
2989 exit_waterfall(ctx
, &wctx
, NULL
);
2990 if (ctx
->ac
.postponed_kill
)
2991 ac_build_endif(&ctx
->ac
, 7003);
2994 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2995 const nir_intrinsic_instr
*instr
,
2998 if (ctx
->ac
.postponed_kill
) {
2999 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3000 ctx
->ac
.postponed_kill
, "");
3001 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
3004 LLVMValueRef params
[7];
3005 int param_count
= 0;
3007 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
3008 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
3009 const char *atomic_name
;
3010 char intrinsic_name
[64];
3011 enum ac_atomic_op atomic_subop
;
3012 ASSERTED
int length
;
3014 enum glsl_sampler_dim dim
;
3017 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
3018 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
3019 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
3020 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
3021 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
3022 assert(format
== GL_R32UI
|| format
== GL_R32I
);
3024 dim
= nir_intrinsic_image_dim(instr
);
3025 is_array
= nir_intrinsic_image_array(instr
);
3027 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3028 dim
= glsl_get_sampler_dim(type
);
3029 is_array
= glsl_sampler_type_is_array(type
);
3032 struct waterfall_context wctx
;
3033 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3035 switch (instr
->intrinsic
) {
3036 case nir_intrinsic_bindless_image_atomic_add
:
3037 case nir_intrinsic_image_deref_atomic_add
:
3038 atomic_name
= "add";
3039 atomic_subop
= ac_atomic_add
;
3041 case nir_intrinsic_bindless_image_atomic_imin
:
3042 case nir_intrinsic_image_deref_atomic_imin
:
3043 atomic_name
= "smin";
3044 atomic_subop
= ac_atomic_smin
;
3046 case nir_intrinsic_bindless_image_atomic_umin
:
3047 case nir_intrinsic_image_deref_atomic_umin
:
3048 atomic_name
= "umin";
3049 atomic_subop
= ac_atomic_umin
;
3051 case nir_intrinsic_bindless_image_atomic_imax
:
3052 case nir_intrinsic_image_deref_atomic_imax
:
3053 atomic_name
= "smax";
3054 atomic_subop
= ac_atomic_smax
;
3056 case nir_intrinsic_bindless_image_atomic_umax
:
3057 case nir_intrinsic_image_deref_atomic_umax
:
3058 atomic_name
= "umax";
3059 atomic_subop
= ac_atomic_umax
;
3061 case nir_intrinsic_bindless_image_atomic_and
:
3062 case nir_intrinsic_image_deref_atomic_and
:
3063 atomic_name
= "and";
3064 atomic_subop
= ac_atomic_and
;
3066 case nir_intrinsic_bindless_image_atomic_or
:
3067 case nir_intrinsic_image_deref_atomic_or
:
3069 atomic_subop
= ac_atomic_or
;
3071 case nir_intrinsic_bindless_image_atomic_xor
:
3072 case nir_intrinsic_image_deref_atomic_xor
:
3073 atomic_name
= "xor";
3074 atomic_subop
= ac_atomic_xor
;
3076 case nir_intrinsic_bindless_image_atomic_exchange
:
3077 case nir_intrinsic_image_deref_atomic_exchange
:
3078 atomic_name
= "swap";
3079 atomic_subop
= ac_atomic_swap
;
3081 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3082 case nir_intrinsic_image_deref_atomic_comp_swap
:
3083 atomic_name
= "cmpswap";
3084 atomic_subop
= 0; /* not used */
3086 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3087 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
3088 atomic_name
= "inc";
3089 atomic_subop
= ac_atomic_inc_wrap
;
3092 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3093 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3094 atomic_name
= "dec";
3095 atomic_subop
= ac_atomic_dec_wrap
;
3102 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
3103 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3105 LLVMValueRef result
;
3106 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3107 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
3108 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
3109 ctx
->ac
.i32_0
, ""); /* vindex */
3110 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3111 if (LLVM_VERSION_MAJOR
>= 9) {
3112 /* XXX: The new raw/struct atomic intrinsics are buggy
3113 * with LLVM 8, see r358579.
3115 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3116 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3118 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3119 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3121 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3123 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3124 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3127 assert(length
< sizeof(intrinsic_name
));
3128 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3129 params
, param_count
, 0);
3131 struct ac_image_args args
= {};
3132 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3133 args
.atomic
= atomic_subop
;
3134 args
.data
[0] = params
[0];
3136 args
.data
[1] = params
[1];
3137 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3138 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3139 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3141 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3144 result
= exit_waterfall(ctx
, &wctx
, result
);
3145 if (ctx
->ac
.postponed_kill
)
3146 ac_build_endif(&ctx
->ac
, 7004);
3150 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3151 nir_intrinsic_instr
*instr
)
3153 struct waterfall_context wctx
;
3154 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3155 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3157 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3159 return exit_waterfall(ctx
, &wctx
, ret
);
3162 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3163 const nir_intrinsic_instr
*instr
,
3168 enum glsl_sampler_dim dim
;
3171 dim
= nir_intrinsic_image_dim(instr
);
3172 is_array
= nir_intrinsic_image_array(instr
);
3174 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3175 dim
= glsl_get_sampler_dim(type
);
3176 is_array
= glsl_sampler_type_is_array(type
);
3179 struct waterfall_context wctx
;
3180 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3182 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3183 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3186 struct ac_image_args args
= { 0 };
3188 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3190 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3191 args
.opcode
= ac_image_get_resinfo
;
3192 assert(nir_src_as_uint(instr
->src
[1]) == 0);
3193 args
.lod
= ctx
->ac
.i32_0
;
3194 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3196 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3198 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3200 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3201 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3202 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3203 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3204 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3207 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3208 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3209 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3213 return exit_waterfall(ctx
, &wctx
, res
);
3216 static void emit_membar(struct ac_llvm_context
*ac
,
3217 const nir_intrinsic_instr
*instr
)
3219 unsigned wait_flags
= 0;
3221 switch (instr
->intrinsic
) {
3222 case nir_intrinsic_memory_barrier
:
3223 case nir_intrinsic_group_memory_barrier
:
3224 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3226 case nir_intrinsic_memory_barrier_buffer
:
3227 case nir_intrinsic_memory_barrier_image
:
3228 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3230 case nir_intrinsic_memory_barrier_shared
:
3231 wait_flags
= AC_WAIT_LGKM
;
3237 ac_build_waitcnt(ac
, wait_flags
);
3240 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3242 /* GFX6 only (thanks to a hw bug workaround):
3243 * The real barrier instruction isn’t needed, because an entire patch
3244 * always fits into a single wave.
3246 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3247 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3250 ac_build_s_barrier(ac
);
3253 static void emit_discard(struct ac_nir_context
*ctx
,
3254 const nir_intrinsic_instr
*instr
)
3258 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3259 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3260 get_src(ctx
, instr
->src
[0]),
3263 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3264 cond
= ctx
->ac
.i1false
;
3267 ac_build_kill_if_false(&ctx
->ac
, cond
);
3270 static void emit_demote(struct ac_nir_context
*ctx
,
3271 const nir_intrinsic_instr
*instr
)
3275 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3276 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3277 get_src(ctx
, instr
->src
[0]),
3280 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3281 cond
= ctx
->ac
.i1false
;
3284 /* Kill immediately while maintaining WQM. */
3285 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3287 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3288 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3289 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3294 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3296 LLVMValueRef result
;
3297 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3298 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3299 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3300 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3302 if (ctx
->ac
.wave_size
== 32)
3303 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3304 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3306 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3310 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3312 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3313 LLVMValueRef result
;
3314 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3315 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3316 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3317 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3319 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3324 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3326 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3327 return LLVMBuildAnd(ctx
->ac
.builder
,
3328 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3329 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3331 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3336 visit_first_invocation(struct ac_nir_context
*ctx
)
3338 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3339 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3341 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3342 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3343 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3344 ctx
->ac
.iN_wavemask
, args
, 2,
3345 AC_FUNC_ATTR_NOUNWIND
|
3346 AC_FUNC_ATTR_READNONE
);
3348 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3352 visit_load_shared(struct ac_nir_context
*ctx
,
3353 const nir_intrinsic_instr
*instr
)
3355 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3357 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3358 instr
->dest
.ssa
.bit_size
);
3360 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3361 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3362 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3363 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3366 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3367 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3371 visit_store_shared(struct ac_nir_context
*ctx
,
3372 const nir_intrinsic_instr
*instr
)
3374 LLVMValueRef derived_ptr
, data
,index
;
3375 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3377 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3378 instr
->src
[0].ssa
->bit_size
);
3379 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3381 int writemask
= nir_intrinsic_write_mask(instr
);
3382 for (int chan
= 0; chan
< 4; chan
++) {
3383 if (!(writemask
& (1 << chan
))) {
3386 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3387 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3388 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3389 LLVMBuildStore(builder
, data
, derived_ptr
);
3393 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3394 const nir_intrinsic_instr
*instr
,
3395 LLVMValueRef ptr
, int src_idx
)
3397 if (ctx
->ac
.postponed_kill
) {
3398 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3399 ctx
->ac
.postponed_kill
, "");
3400 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3403 LLVMValueRef result
;
3404 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3406 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3408 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3409 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3410 if (deref
->mode
== nir_var_mem_global
) {
3411 /* use "singlethread" sync scope to implement relaxed ordering */
3412 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3414 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3415 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3419 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3420 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3421 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3422 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3423 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3425 LLVMAtomicRMWBinOp op
;
3426 switch (instr
->intrinsic
) {
3427 case nir_intrinsic_shared_atomic_add
:
3428 case nir_intrinsic_deref_atomic_add
:
3429 op
= LLVMAtomicRMWBinOpAdd
;
3431 case nir_intrinsic_shared_atomic_umin
:
3432 case nir_intrinsic_deref_atomic_umin
:
3433 op
= LLVMAtomicRMWBinOpUMin
;
3435 case nir_intrinsic_shared_atomic_umax
:
3436 case nir_intrinsic_deref_atomic_umax
:
3437 op
= LLVMAtomicRMWBinOpUMax
;
3439 case nir_intrinsic_shared_atomic_imin
:
3440 case nir_intrinsic_deref_atomic_imin
:
3441 op
= LLVMAtomicRMWBinOpMin
;
3443 case nir_intrinsic_shared_atomic_imax
:
3444 case nir_intrinsic_deref_atomic_imax
:
3445 op
= LLVMAtomicRMWBinOpMax
;
3447 case nir_intrinsic_shared_atomic_and
:
3448 case nir_intrinsic_deref_atomic_and
:
3449 op
= LLVMAtomicRMWBinOpAnd
;
3451 case nir_intrinsic_shared_atomic_or
:
3452 case nir_intrinsic_deref_atomic_or
:
3453 op
= LLVMAtomicRMWBinOpOr
;
3455 case nir_intrinsic_shared_atomic_xor
:
3456 case nir_intrinsic_deref_atomic_xor
:
3457 op
= LLVMAtomicRMWBinOpXor
;
3459 case nir_intrinsic_shared_atomic_exchange
:
3460 case nir_intrinsic_deref_atomic_exchange
:
3461 op
= LLVMAtomicRMWBinOpXchg
;
3463 #if LLVM_VERSION_MAJOR >= 10
3464 case nir_intrinsic_shared_atomic_fadd
:
3465 case nir_intrinsic_deref_atomic_fadd
:
3466 op
= LLVMAtomicRMWBinOpFAdd
;
3475 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_fadd
||
3476 instr
->intrinsic
== nir_intrinsic_deref_atomic_fadd
) {
3477 val
= ac_to_float(&ctx
->ac
, src
);
3479 val
= ac_to_integer(&ctx
->ac
, src
);
3482 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, val
, sync_scope
);
3485 if (ctx
->ac
.postponed_kill
)
3486 ac_build_endif(&ctx
->ac
, 7005);
3490 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3492 LLVMValueRef values
[2];
3493 LLVMValueRef pos
[2];
3495 pos
[0] = ac_to_float(&ctx
->ac
,
3496 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3497 pos
[1] = ac_to_float(&ctx
->ac
,
3498 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3500 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3501 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3502 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3505 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3506 enum glsl_interp_mode interp
, unsigned location
)
3509 case INTERP_MODE_FLAT
:
3512 case INTERP_MODE_SMOOTH
:
3513 case INTERP_MODE_NONE
:
3514 if (location
== INTERP_CENTER
)
3515 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3516 else if (location
== INTERP_CENTROID
)
3517 return ctx
->abi
->persp_centroid
;
3518 else if (location
== INTERP_SAMPLE
)
3519 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3521 case INTERP_MODE_NOPERSPECTIVE
:
3522 if (location
== INTERP_CENTER
)
3523 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3524 else if (location
== INTERP_CENTROID
)
3525 return ctx
->abi
->linear_centroid
;
3526 else if (location
== INTERP_SAMPLE
)
3527 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3533 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3536 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3537 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3540 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3542 LLVMValueRef offset
)
3544 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3545 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3546 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3548 LLVMValueRef ij_out
[2];
3549 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3552 * take the I then J parameters, and the DDX/Y for it, and
3553 * calculate the IJ inputs for the interpolator.
3554 * temp1 = ddx * offset/sample.x + I;
3555 * interp_param.I = ddy * offset/sample.y + temp1;
3556 * temp1 = ddx * offset/sample.x + J;
3557 * interp_param.J = ddy * offset/sample.y + temp1;
3559 for (unsigned i
= 0; i
< 2; i
++) {
3560 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3561 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3562 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3563 ddxy_out
, ix_ll
, "");
3564 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3565 ddxy_out
, iy_ll
, "");
3566 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3567 interp_param
, ix_ll
, "");
3568 LLVMValueRef temp1
, temp2
;
3570 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3573 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3574 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3576 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3577 temp2
, ctx
->ac
.i32
, "");
3579 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3580 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3583 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3586 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3587 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3590 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3592 LLVMValueRef sample_id
)
3594 if (ctx
->abi
->interp_at_sample_force_center
)
3595 return barycentric_center(ctx
, mode
);
3597 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3599 /* fetch sample ID */
3600 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3602 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3603 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3604 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3605 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3606 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3607 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3609 return barycentric_offset(ctx
, mode
, offset
);
3613 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3616 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3617 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3620 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3622 return LLVMBuildBitCast(ctx
->ac
.builder
,
3623 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3627 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3628 LLVMValueRef interp_param
,
3629 unsigned index
, unsigned comp_start
,
3630 unsigned num_components
,
3633 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3634 LLVMValueRef interp_param_f
;
3636 interp_param_f
= LLVMBuildBitCast(ctx
->ac
.builder
,
3637 interp_param
, ctx
->ac
.v2f32
, "");
3638 LLVMValueRef i
= LLVMBuildExtractElement(
3639 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_0
, "");
3640 LLVMValueRef j
= LLVMBuildExtractElement(
3641 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_1
, "");
3643 /* Workaround for issue 2647: kill threads with infinite interpolation coeffs */
3644 if (ctx
->verified_interp
&&
3645 !_mesa_hash_table_search(ctx
->verified_interp
, interp_param
)) {
3646 LLVMValueRef args
[2];
3648 args
[1] = LLVMConstInt(ctx
->ac
.i32
, S_NAN
| Q_NAN
| N_INFINITY
| P_INFINITY
, false);
3649 LLVMValueRef cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f32", ctx
->ac
.i1
,
3650 args
, 2, AC_FUNC_ATTR_READNONE
);
3651 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildNot(ctx
->ac
.builder
, cond
, ""));
3652 _mesa_hash_table_insert(ctx
->verified_interp
, interp_param
, interp_param
);
3655 LLVMValueRef values
[4];
3656 assert(bitsize
== 16 || bitsize
== 32);
3657 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3658 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3659 if (bitsize
== 16) {
3660 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3661 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3663 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3664 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3668 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3671 static LLVMValueRef
visit_load(struct ac_nir_context
*ctx
,
3672 nir_intrinsic_instr
*instr
, bool is_output
)
3674 LLVMValueRef values
[8];
3675 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3676 LLVMTypeRef component_type
;
3677 unsigned base
= nir_intrinsic_base(instr
);
3678 unsigned component
= nir_intrinsic_component(instr
);
3679 unsigned count
= instr
->dest
.ssa
.num_components
*
3680 (instr
->dest
.ssa
.bit_size
== 64 ? 2 : 1);
3681 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3682 LLVMValueRef vertex_index
=
3683 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
3684 nir_src offset
= *nir_get_io_offset_src(instr
);
3685 LLVMValueRef indir_index
= NULL
;
3687 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
3688 component_type
= LLVMGetElementType(dest_type
);
3690 component_type
= dest_type
;
3692 if (nir_src_is_const(offset
))
3693 assert(nir_src_as_uint(offset
) == 0);
3695 indir_index
= get_src(ctx
, offset
);
3697 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3698 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& !is_output
)) {
3699 LLVMValueRef result
=
3700 ctx
->abi
->load_tess_varyings(ctx
->abi
, component_type
,
3701 vertex_index
, indir_index
,
3704 instr
->num_components
,
3705 false, false, !is_output
);
3706 if (instr
->dest
.ssa
.bit_size
== 16) {
3707 result
= ac_to_integer(&ctx
->ac
, result
);
3708 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
3710 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3713 /* No indirect indexing is allowed after this point. */
3714 assert(!indir_index
);
3716 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3717 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3718 assert(nir_src_is_const(*vertex_index_src
));
3720 return ctx
->abi
->load_inputs(ctx
->abi
, 0, base
* 4, component
,
3721 instr
->num_components
,
3722 nir_src_as_uint(*vertex_index_src
),
3726 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& is_output
&&
3727 nir_intrinsic_io_semantics(instr
).fb_fetch_output
)
3728 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
3730 /* Other non-fragment cases have inputs and outputs in temporaries. */
3731 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
3732 for (unsigned chan
= component
; chan
< count
+ component
; chan
++) {
3734 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3735 ctx
->abi
->outputs
[base
* 4 + chan
], "");
3737 values
[chan
] = ctx
->abi
->inputs
[base
* 4 + chan
];
3739 values
[chan
] = LLVMGetUndef(ctx
->ac
.i32
);
3742 LLVMValueRef result
= ac_build_varying_gather_values(&ctx
->ac
, values
, count
, component
);
3743 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3746 /* Fragment shader inputs. */
3747 unsigned vertex_id
= 2; /* P0 */
3749 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3750 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3752 switch (src0
[0].i32
) {
3763 unreachable("Invalid vertex index");
3767 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
, false);
3769 for (unsigned chan
= 0; chan
< count
; chan
++) {
3770 if (component
+ chan
> 4)
3771 attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
+ 1, false);
3772 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3773 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3774 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3777 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3778 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3779 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3780 instr
->dest
.ssa
.bit_size
== 16 ? ctx
->ac
.i16
3784 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, count
);
3785 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3788 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3789 nir_intrinsic_instr
*instr
)
3791 LLVMValueRef result
= NULL
;
3793 switch (instr
->intrinsic
) {
3794 case nir_intrinsic_ballot
:
3795 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3796 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3797 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3799 case nir_intrinsic_read_invocation
:
3800 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3801 get_src(ctx
, instr
->src
[1]));
3803 case nir_intrinsic_read_first_invocation
:
3804 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3806 case nir_intrinsic_load_subgroup_invocation
:
3807 result
= ac_get_thread_id(&ctx
->ac
);
3809 case nir_intrinsic_load_work_group_id
: {
3810 LLVMValueRef values
[3];
3812 for (int i
= 0; i
< 3; i
++) {
3813 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3814 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3817 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3820 case nir_intrinsic_load_base_vertex
:
3821 case nir_intrinsic_load_first_vertex
:
3822 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3824 case nir_intrinsic_load_local_group_size
:
3825 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3827 case nir_intrinsic_load_vertex_id
:
3828 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3829 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3830 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3832 case nir_intrinsic_load_vertex_id_zero_base
: {
3833 result
= ctx
->abi
->vertex_id
;
3836 case nir_intrinsic_load_local_invocation_id
: {
3837 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3840 case nir_intrinsic_load_base_instance
:
3841 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3843 case nir_intrinsic_load_draw_id
:
3844 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3846 case nir_intrinsic_load_view_index
:
3847 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3849 case nir_intrinsic_load_invocation_id
:
3850 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3851 result
= ac_unpack_param(&ctx
->ac
,
3852 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3855 if (ctx
->ac
.chip_class
>= GFX10
) {
3856 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3857 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3858 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3860 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3864 case nir_intrinsic_load_primitive_id
:
3865 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3866 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3867 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3868 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3869 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3870 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3872 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3874 case nir_intrinsic_load_sample_id
:
3875 result
= ac_unpack_param(&ctx
->ac
,
3876 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3879 case nir_intrinsic_load_sample_pos
:
3880 result
= load_sample_pos(ctx
);
3882 case nir_intrinsic_load_sample_mask_in
:
3883 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3885 case nir_intrinsic_load_frag_coord
: {
3886 LLVMValueRef values
[4] = {
3887 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3888 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3889 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3890 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3891 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3893 result
= ac_to_integer(&ctx
->ac
,
3894 ac_build_gather_values(&ctx
->ac
, values
, 4));
3897 case nir_intrinsic_load_layer_id
:
3898 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3900 case nir_intrinsic_load_front_face
:
3901 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3903 case nir_intrinsic_load_helper_invocation
:
3904 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3906 case nir_intrinsic_is_helper_invocation
:
3907 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3909 case nir_intrinsic_load_color0
:
3910 result
= ctx
->abi
->color0
;
3912 case nir_intrinsic_load_color1
:
3913 result
= ctx
->abi
->color1
;
3915 case nir_intrinsic_load_user_data_amd
:
3916 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3917 result
= ctx
->abi
->user_data
;
3919 case nir_intrinsic_load_instance_id
:
3920 result
= ctx
->abi
->instance_id
;
3922 case nir_intrinsic_load_num_work_groups
:
3923 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3925 case nir_intrinsic_load_local_invocation_index
:
3926 result
= visit_load_local_invocation_index(ctx
);
3928 case nir_intrinsic_load_subgroup_id
:
3929 result
= visit_load_subgroup_id(ctx
);
3931 case nir_intrinsic_load_num_subgroups
:
3932 result
= visit_load_num_subgroups(ctx
);
3934 case nir_intrinsic_first_invocation
:
3935 result
= visit_first_invocation(ctx
);
3937 case nir_intrinsic_load_push_constant
:
3938 result
= visit_load_push_constant(ctx
, instr
);
3940 case nir_intrinsic_vulkan_resource_index
: {
3941 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3942 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3943 unsigned binding
= nir_intrinsic_binding(instr
);
3945 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3949 case nir_intrinsic_vulkan_resource_reindex
:
3950 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3952 case nir_intrinsic_store_ssbo
:
3953 visit_store_ssbo(ctx
, instr
);
3955 case nir_intrinsic_load_ssbo
:
3956 result
= visit_load_buffer(ctx
, instr
);
3958 case nir_intrinsic_ssbo_atomic_add
:
3959 case nir_intrinsic_ssbo_atomic_imin
:
3960 case nir_intrinsic_ssbo_atomic_umin
:
3961 case nir_intrinsic_ssbo_atomic_imax
:
3962 case nir_intrinsic_ssbo_atomic_umax
:
3963 case nir_intrinsic_ssbo_atomic_and
:
3964 case nir_intrinsic_ssbo_atomic_or
:
3965 case nir_intrinsic_ssbo_atomic_xor
:
3966 case nir_intrinsic_ssbo_atomic_exchange
:
3967 case nir_intrinsic_ssbo_atomic_comp_swap
:
3968 result
= visit_atomic_ssbo(ctx
, instr
);
3970 case nir_intrinsic_load_ubo
:
3971 result
= visit_load_ubo_buffer(ctx
, instr
);
3973 case nir_intrinsic_get_buffer_size
:
3974 result
= visit_get_buffer_size(ctx
, instr
);
3976 case nir_intrinsic_load_deref
:
3977 result
= visit_load_var(ctx
, instr
);
3979 case nir_intrinsic_store_deref
:
3980 visit_store_var(ctx
, instr
);
3982 case nir_intrinsic_load_input
:
3983 case nir_intrinsic_load_input_vertex
:
3984 case nir_intrinsic_load_per_vertex_input
:
3985 result
= visit_load(ctx
, instr
, false);
3987 case nir_intrinsic_load_output
:
3988 case nir_intrinsic_load_per_vertex_output
:
3989 result
= visit_load(ctx
, instr
, true);
3991 case nir_intrinsic_store_output
:
3992 case nir_intrinsic_store_per_vertex_output
:
3993 visit_store_output(ctx
, instr
);
3995 case nir_intrinsic_load_shared
:
3996 result
= visit_load_shared(ctx
, instr
);
3998 case nir_intrinsic_store_shared
:
3999 visit_store_shared(ctx
, instr
);
4001 case nir_intrinsic_bindless_image_samples
:
4002 case nir_intrinsic_image_deref_samples
:
4003 result
= visit_image_samples(ctx
, instr
);
4005 case nir_intrinsic_bindless_image_load
:
4006 result
= visit_image_load(ctx
, instr
, true);
4008 case nir_intrinsic_image_deref_load
:
4009 result
= visit_image_load(ctx
, instr
, false);
4011 case nir_intrinsic_bindless_image_store
:
4012 visit_image_store(ctx
, instr
, true);
4014 case nir_intrinsic_image_deref_store
:
4015 visit_image_store(ctx
, instr
, false);
4017 case nir_intrinsic_bindless_image_atomic_add
:
4018 case nir_intrinsic_bindless_image_atomic_imin
:
4019 case nir_intrinsic_bindless_image_atomic_umin
:
4020 case nir_intrinsic_bindless_image_atomic_imax
:
4021 case nir_intrinsic_bindless_image_atomic_umax
:
4022 case nir_intrinsic_bindless_image_atomic_and
:
4023 case nir_intrinsic_bindless_image_atomic_or
:
4024 case nir_intrinsic_bindless_image_atomic_xor
:
4025 case nir_intrinsic_bindless_image_atomic_exchange
:
4026 case nir_intrinsic_bindless_image_atomic_comp_swap
:
4027 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
4028 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
4029 result
= visit_image_atomic(ctx
, instr
, true);
4031 case nir_intrinsic_image_deref_atomic_add
:
4032 case nir_intrinsic_image_deref_atomic_imin
:
4033 case nir_intrinsic_image_deref_atomic_umin
:
4034 case nir_intrinsic_image_deref_atomic_imax
:
4035 case nir_intrinsic_image_deref_atomic_umax
:
4036 case nir_intrinsic_image_deref_atomic_and
:
4037 case nir_intrinsic_image_deref_atomic_or
:
4038 case nir_intrinsic_image_deref_atomic_xor
:
4039 case nir_intrinsic_image_deref_atomic_exchange
:
4040 case nir_intrinsic_image_deref_atomic_comp_swap
:
4041 case nir_intrinsic_image_deref_atomic_inc_wrap
:
4042 case nir_intrinsic_image_deref_atomic_dec_wrap
:
4043 result
= visit_image_atomic(ctx
, instr
, false);
4045 case nir_intrinsic_bindless_image_size
:
4046 result
= visit_image_size(ctx
, instr
, true);
4048 case nir_intrinsic_image_deref_size
:
4049 result
= visit_image_size(ctx
, instr
, false);
4051 case nir_intrinsic_shader_clock
:
4052 result
= ac_build_shader_clock(&ctx
->ac
,
4053 nir_intrinsic_memory_scope(instr
));
4055 case nir_intrinsic_discard
:
4056 case nir_intrinsic_discard_if
:
4057 emit_discard(ctx
, instr
);
4059 case nir_intrinsic_demote
:
4060 case nir_intrinsic_demote_if
:
4061 emit_demote(ctx
, instr
);
4063 case nir_intrinsic_memory_barrier
:
4064 case nir_intrinsic_group_memory_barrier
:
4065 case nir_intrinsic_memory_barrier_buffer
:
4066 case nir_intrinsic_memory_barrier_image
:
4067 case nir_intrinsic_memory_barrier_shared
:
4068 emit_membar(&ctx
->ac
, instr
);
4070 case nir_intrinsic_scoped_barrier
: {
4071 assert(!(nir_intrinsic_memory_semantics(instr
) &
4072 (NIR_MEMORY_MAKE_AVAILABLE
| NIR_MEMORY_MAKE_VISIBLE
)));
4074 nir_variable_mode modes
= nir_intrinsic_memory_modes(instr
);
4076 unsigned wait_flags
= 0;
4077 if (modes
& (nir_var_mem_global
| nir_var_mem_ssbo
))
4078 wait_flags
|= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
4079 if (modes
& nir_var_mem_shared
)
4080 wait_flags
|= AC_WAIT_LGKM
;
4083 ac_build_waitcnt(&ctx
->ac
, wait_flags
);
4085 if (nir_intrinsic_execution_scope(instr
) == NIR_SCOPE_WORKGROUP
)
4086 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4089 case nir_intrinsic_memory_barrier_tcs_patch
:
4091 case nir_intrinsic_control_barrier
:
4092 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4094 case nir_intrinsic_shared_atomic_add
:
4095 case nir_intrinsic_shared_atomic_imin
:
4096 case nir_intrinsic_shared_atomic_umin
:
4097 case nir_intrinsic_shared_atomic_imax
:
4098 case nir_intrinsic_shared_atomic_umax
:
4099 case nir_intrinsic_shared_atomic_and
:
4100 case nir_intrinsic_shared_atomic_or
:
4101 case nir_intrinsic_shared_atomic_xor
:
4102 case nir_intrinsic_shared_atomic_exchange
:
4103 case nir_intrinsic_shared_atomic_comp_swap
:
4104 case nir_intrinsic_shared_atomic_fadd
: {
4105 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
4106 instr
->src
[1].ssa
->bit_size
);
4107 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4110 case nir_intrinsic_deref_atomic_add
:
4111 case nir_intrinsic_deref_atomic_imin
:
4112 case nir_intrinsic_deref_atomic_umin
:
4113 case nir_intrinsic_deref_atomic_imax
:
4114 case nir_intrinsic_deref_atomic_umax
:
4115 case nir_intrinsic_deref_atomic_and
:
4116 case nir_intrinsic_deref_atomic_or
:
4117 case nir_intrinsic_deref_atomic_xor
:
4118 case nir_intrinsic_deref_atomic_exchange
:
4119 case nir_intrinsic_deref_atomic_comp_swap
:
4120 case nir_intrinsic_deref_atomic_fadd
: {
4121 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
4122 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4125 case nir_intrinsic_load_barycentric_pixel
:
4126 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
4128 case nir_intrinsic_load_barycentric_centroid
:
4129 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
4131 case nir_intrinsic_load_barycentric_sample
:
4132 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
4134 case nir_intrinsic_load_barycentric_model
:
4135 result
= barycentric_model(ctx
);
4137 case nir_intrinsic_load_barycentric_at_offset
: {
4138 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4139 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
4142 case nir_intrinsic_load_barycentric_at_sample
: {
4143 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
4144 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
4147 case nir_intrinsic_load_interpolated_input
: {
4148 /* We assume any indirect loads have been lowered away */
4149 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
4151 assert(offset
[0].i32
== 0);
4153 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
4154 unsigned index
= nir_intrinsic_base(instr
);
4155 unsigned component
= nir_intrinsic_component(instr
);
4156 result
= load_interpolated_input(ctx
, interp_param
, index
,
4158 instr
->dest
.ssa
.num_components
,
4159 instr
->dest
.ssa
.bit_size
);
4162 case nir_intrinsic_emit_vertex
:
4163 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
4165 case nir_intrinsic_emit_vertex_with_counter
: {
4166 unsigned stream
= nir_intrinsic_stream_id(instr
);
4167 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
4168 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
4173 case nir_intrinsic_end_primitive
:
4174 case nir_intrinsic_end_primitive_with_counter
:
4175 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4177 case nir_intrinsic_load_tess_coord
:
4178 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
4180 case nir_intrinsic_load_tess_level_outer
:
4181 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
4183 case nir_intrinsic_load_tess_level_inner
:
4184 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
4186 case nir_intrinsic_load_tess_level_outer_default
:
4187 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
4189 case nir_intrinsic_load_tess_level_inner_default
:
4190 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
4192 case nir_intrinsic_load_patch_vertices_in
:
4193 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4195 case nir_intrinsic_vote_all
: {
4196 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4197 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4200 case nir_intrinsic_vote_any
: {
4201 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4202 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4205 case nir_intrinsic_shuffle
:
4206 if (ctx
->ac
.chip_class
== GFX8
||
4207 ctx
->ac
.chip_class
== GFX9
||
4208 (ctx
->ac
.chip_class
>= GFX10
&& ctx
->ac
.wave_size
== 32)) {
4209 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4210 get_src(ctx
, instr
->src
[1]));
4212 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4213 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
4214 LLVMTypeRef type
= LLVMTypeOf(src
);
4215 struct waterfall_context wctx
;
4216 LLVMValueRef index_val
;
4218 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
4220 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
4223 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4225 (LLVMValueRef
[]) { src
, index_val
}, 2,
4226 AC_FUNC_ATTR_READNONE
|
4227 AC_FUNC_ATTR_CONVERGENT
);
4229 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4231 result
= exit_waterfall(ctx
, &wctx
, result
);
4234 case nir_intrinsic_reduce
:
4235 result
= ac_build_reduce(&ctx
->ac
,
4236 get_src(ctx
, instr
->src
[0]),
4237 instr
->const_index
[0],
4238 instr
->const_index
[1]);
4240 case nir_intrinsic_inclusive_scan
:
4241 result
= ac_build_inclusive_scan(&ctx
->ac
,
4242 get_src(ctx
, instr
->src
[0]),
4243 instr
->const_index
[0]);
4245 case nir_intrinsic_exclusive_scan
:
4246 result
= ac_build_exclusive_scan(&ctx
->ac
,
4247 get_src(ctx
, instr
->src
[0]),
4248 instr
->const_index
[0]);
4250 case nir_intrinsic_quad_broadcast
: {
4251 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4252 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4253 lane
, lane
, lane
, lane
);
4256 case nir_intrinsic_quad_swap_horizontal
:
4257 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4259 case nir_intrinsic_quad_swap_vertical
:
4260 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4262 case nir_intrinsic_quad_swap_diagonal
:
4263 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4265 case nir_intrinsic_quad_swizzle_amd
: {
4266 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4267 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4268 mask
& 0x3, (mask
>> 2) & 0x3,
4269 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4272 case nir_intrinsic_masked_swizzle_amd
: {
4273 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4274 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4277 case nir_intrinsic_write_invocation_amd
:
4278 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4279 get_src(ctx
, instr
->src
[1]),
4280 get_src(ctx
, instr
->src
[2]));
4282 case nir_intrinsic_mbcnt_amd
:
4283 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4285 case nir_intrinsic_load_scratch
: {
4286 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4287 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4289 LLVMTypeRef comp_type
=
4290 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4291 LLVMTypeRef vec_type
=
4292 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4293 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4294 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4295 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4296 LLVMPointerType(vec_type
, addr_space
), "");
4297 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4300 case nir_intrinsic_store_scratch
: {
4301 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4302 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4304 LLVMTypeRef comp_type
=
4305 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4306 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4307 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4308 LLVMPointerType(comp_type
, addr_space
), "");
4309 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4310 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4313 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4315 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4316 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4317 LLVMTypeRef vec_type
=
4318 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4319 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4321 LLVMPointerType(vec_type
, addr_space
),
4323 LLVMValueRef offset_src
=
4324 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4325 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4329 case nir_intrinsic_load_constant
: {
4330 unsigned base
= nir_intrinsic_base(instr
);
4331 unsigned range
= nir_intrinsic_range(instr
);
4333 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4334 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4335 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4337 /* Clamp the offset to avoid out-of-bound access because global
4338 * instructions can't handle them.
4340 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4341 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4343 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4345 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4347 LLVMTypeRef comp_type
=
4348 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4349 LLVMTypeRef vec_type
=
4350 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4351 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4352 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4353 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4354 LLVMPointerType(vec_type
, addr_space
), "");
4355 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4359 fprintf(stderr
, "Unknown intrinsic: ");
4360 nir_print_instr(&instr
->instr
, stderr
);
4361 fprintf(stderr
, "\n");
4365 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4369 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4370 unsigned base_index
,
4371 unsigned constant_index
,
4372 LLVMValueRef dynamic_index
)
4374 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4375 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4376 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4378 /* Bindless uniforms are 64bit so multiple index by 8 */
4379 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4380 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4382 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4384 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4385 NULL
, 0, 0, true, true);
4387 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4390 struct sampler_desc_address
{
4391 unsigned descriptor_set
;
4392 unsigned base_index
; /* binding in vulkan */
4393 unsigned constant_index
;
4394 LLVMValueRef dynamic_index
;
4399 static struct sampler_desc_address
4400 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4401 nir_deref_instr
*deref_instr
,
4402 const nir_instr
*instr
,
4405 LLVMValueRef index
= NULL
;
4406 unsigned constant_index
= 0;
4407 unsigned descriptor_set
;
4408 unsigned base_index
;
4409 bool bindless
= false;
4414 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4417 index
= get_src(ctx
, img_instr
->src
[0]);
4419 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4420 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4421 nir_tex_src_sampler_handle
);
4422 if (sampSrcIdx
!= -1) {
4425 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4427 assert(tex_instr
&& !image
);
4428 base_index
= tex_instr
->sampler_index
;
4432 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4433 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4434 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4438 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4439 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4441 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4443 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4444 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4449 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4452 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4453 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4454 unsigned sidx
= deref_instr
->strct
.index
;
4455 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4456 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4458 unreachable("Unsupported deref type");
4461 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4463 if (deref_instr
->var
->data
.bindless
) {
4464 /* For now just assert on unhandled variable types */
4465 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4467 base_index
= deref_instr
->var
->data
.driver_location
;
4470 index
= index
? index
: ctx
->ac
.i32_0
;
4471 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4472 constant_index
, index
);
4474 base_index
= deref_instr
->var
->data
.binding
;
4476 return (struct sampler_desc_address
) {
4477 .descriptor_set
= descriptor_set
,
4478 .base_index
= base_index
,
4479 .constant_index
= constant_index
,
4480 .dynamic_index
= index
,
4482 .bindless
= bindless
,
4486 /* Extract any possibly divergent index into a separate value that can be fed
4487 * into get_sampler_desc with the same arguments. */
4488 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4489 nir_deref_instr
*deref_instr
,
4490 const nir_instr
*instr
,
4493 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4494 return addr
.dynamic_index
;
4497 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4498 nir_deref_instr
*deref_instr
,
4499 enum ac_descriptor_type desc_type
,
4500 const nir_instr
*instr
,
4502 bool image
, bool write
)
4504 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4505 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4506 addr
.descriptor_set
,
4508 addr
.constant_index
, index
,
4509 desc_type
, addr
.image
, write
, addr
.bindless
);
4512 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4515 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4516 * filtering manually. The driver sets img7 to a mask clearing
4517 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4518 * s_and_b32 samp0, samp0, img7
4521 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4523 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4524 LLVMValueRef res
, LLVMValueRef samp
)
4526 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4527 LLVMValueRef img7
, samp0
;
4529 if (ctx
->ac
.chip_class
>= GFX8
)
4532 img7
= LLVMBuildExtractElement(builder
, res
,
4533 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4534 samp0
= LLVMBuildExtractElement(builder
, samp
,
4535 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4536 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4537 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4538 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4541 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4542 nir_tex_instr
*instr
,
4543 struct waterfall_context
*wctx
,
4544 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4545 LLVMValueRef
*fmask_ptr
)
4547 nir_deref_instr
*texture_deref_instr
= NULL
;
4548 nir_deref_instr
*sampler_deref_instr
= NULL
;
4551 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4552 switch (instr
->src
[i
].src_type
) {
4553 case nir_tex_src_texture_deref
:
4554 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4556 case nir_tex_src_sampler_deref
:
4557 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4559 case nir_tex_src_plane
:
4560 plane
= nir_src_as_int(instr
->src
[i
].src
);
4567 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4568 &instr
->instr
, false);
4569 if (!sampler_deref_instr
)
4570 sampler_deref_instr
= texture_deref_instr
;
4572 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4573 &instr
->instr
, false);
4574 if (instr
->texture_non_uniform
)
4575 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4577 if (instr
->sampler_non_uniform
)
4578 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4580 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4583 assert(instr
->op
!= nir_texop_txf_ms
&&
4584 instr
->op
!= nir_texop_samples_identical
);
4585 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4587 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4590 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4591 /* The fragment mask is fetched from the compressed
4592 * multisampled surface.
4594 main_descriptor
= AC_DESC_FMASK
;
4597 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4598 texture_dynamic_index
, false, false);
4601 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4602 sampler_dynamic_index
, false, false);
4603 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4604 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4606 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4607 instr
->op
== nir_texop_samples_identical
))
4608 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4609 &instr
->instr
, texture_dynamic_index
, false, false);
4612 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4615 coord
= ac_to_float(ctx
, coord
);
4616 coord
= ac_build_round(ctx
, coord
);
4617 coord
= ac_to_integer(ctx
, coord
);
4621 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4623 LLVMValueRef result
= NULL
;
4624 struct ac_image_args args
= { 0 };
4625 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4626 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4627 unsigned offset_src
= 0;
4628 struct waterfall_context wctx
[2] = {{{0}}};
4630 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4632 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4633 switch (instr
->src
[i
].src_type
) {
4634 case nir_tex_src_coord
: {
4635 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4636 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4637 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4640 case nir_tex_src_projector
:
4642 case nir_tex_src_comparator
:
4643 if (instr
->is_shadow
) {
4644 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4645 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4648 case nir_tex_src_offset
:
4649 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4652 case nir_tex_src_bias
:
4653 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4655 case nir_tex_src_lod
: {
4656 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4657 args
.level_zero
= true;
4659 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4662 case nir_tex_src_ms_index
:
4663 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4665 case nir_tex_src_ms_mcs
:
4667 case nir_tex_src_ddx
:
4668 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4670 case nir_tex_src_ddy
:
4671 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4673 case nir_tex_src_min_lod
:
4674 args
.min_lod
= get_src(ctx
, instr
->src
[i
].src
);
4676 case nir_tex_src_texture_offset
:
4677 case nir_tex_src_sampler_offset
:
4678 case nir_tex_src_plane
:
4684 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4685 result
= get_buffer_size(ctx
, args
.resource
, true);
4689 if (instr
->op
== nir_texop_texture_samples
) {
4690 LLVMValueRef res
, samples
, is_msaa
;
4691 LLVMValueRef default_sample
;
4693 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4694 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4695 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4696 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4697 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4698 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4699 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4700 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4701 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4703 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4704 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4705 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4706 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4707 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4710 if (ctx
->abi
->robust_buffer_access
) {
4711 LLVMValueRef dword1
, is_null_descriptor
;
4713 /* Extract the second dword of the descriptor, if it's
4714 * all zero, then it's a null descriptor.
4716 dword1
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4717 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
4718 is_null_descriptor
=
4719 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, dword1
,
4720 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4722 LLVMBuildSelect(ctx
->ac
.builder
, is_null_descriptor
,
4723 ctx
->ac
.i32_0
, ctx
->ac
.i32_1
, "");
4725 default_sample
= ctx
->ac
.i32_1
;
4728 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4729 default_sample
, "");
4734 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4735 LLVMValueRef offset
[3], pack
;
4736 for (unsigned chan
= 0; chan
< 3; ++chan
)
4737 offset
[chan
] = ctx
->ac
.i32_0
;
4739 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4740 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4741 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4742 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4743 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4745 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4746 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4748 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4749 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4753 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4754 * OpenGL 4.5 spec says:
4756 * "If the texture’s internal format indicates a fixed-point
4757 * depth texture, then D_t and D_ref are clamped to the
4758 * range [0, 1]; otherwise no clamping is performed."
4760 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4761 * so the depth comparison value isn't clamped for Z16 and
4762 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4763 * an explicitly clamped 32-bit float format.
4766 ctx
->ac
.chip_class
>= GFX8
&&
4767 ctx
->ac
.chip_class
<= GFX9
&&
4768 ctx
->abi
->clamp_shadow_reference
) {
4769 LLVMValueRef upgraded
, clamped
;
4771 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4772 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4773 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4774 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4775 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4776 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4777 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4781 /* pack derivatives */
4783 int num_src_deriv_channels
, num_dest_deriv_channels
;
4784 switch (instr
->sampler_dim
) {
4785 case GLSL_SAMPLER_DIM_3D
:
4786 case GLSL_SAMPLER_DIM_CUBE
:
4787 num_src_deriv_channels
= 3;
4788 num_dest_deriv_channels
= 3;
4790 case GLSL_SAMPLER_DIM_2D
:
4792 num_src_deriv_channels
= 2;
4793 num_dest_deriv_channels
= 2;
4795 case GLSL_SAMPLER_DIM_1D
:
4796 num_src_deriv_channels
= 1;
4797 if (ctx
->ac
.chip_class
== GFX9
) {
4798 num_dest_deriv_channels
= 2;
4800 num_dest_deriv_channels
= 1;
4805 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4806 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4807 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4808 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4809 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4811 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4812 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4813 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4817 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4818 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4819 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4820 if (instr
->coord_components
== 3)
4821 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4822 ac_prepare_cube_coords(&ctx
->ac
,
4823 instr
->op
== nir_texop_txd
, instr
->is_array
,
4824 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4827 /* Texture coordinates fixups */
4828 if (instr
->coord_components
> 1 &&
4829 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4831 instr
->op
!= nir_texop_txf
) {
4832 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4835 if (instr
->coord_components
> 2 &&
4836 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4837 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4838 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4839 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4841 instr
->op
!= nir_texop_txf
&&
4842 instr
->op
!= nir_texop_txf_ms
&&
4843 instr
->op
!= nir_texop_fragment_fetch
&&
4844 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4845 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4848 if (ctx
->ac
.chip_class
== GFX9
&&
4849 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4850 instr
->op
!= nir_texop_lod
) {
4851 LLVMValueRef filler
;
4852 if (instr
->op
== nir_texop_txf
)
4853 filler
= ctx
->ac
.i32_0
;
4855 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4857 if (instr
->is_array
)
4858 args
.coords
[2] = args
.coords
[1];
4859 args
.coords
[1] = filler
;
4862 /* Pack sample index */
4863 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4864 instr
->op
== nir_texop_fragment_fetch
))
4865 args
.coords
[instr
->coord_components
] = sample_index
;
4867 if (instr
->op
== nir_texop_samples_identical
) {
4868 struct ac_image_args txf_args
= { 0 };
4869 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4871 txf_args
.dmask
= 0xf;
4872 txf_args
.resource
= fmask_ptr
;
4873 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4874 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4876 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4877 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4881 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4882 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4883 instr
->op
!= nir_texop_txs
&&
4884 instr
->op
!= nir_texop_fragment_fetch
&&
4885 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4886 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4887 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4888 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4889 instr
->is_array
? args
.coords
[2] : NULL
,
4890 args
.coords
[sample_chan
], fmask_ptr
);
4893 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4894 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4895 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4896 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4897 args
.coords
[i
] = LLVMBuildAdd(
4898 ctx
->ac
.builder
, args
.coords
[i
],
4899 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4904 /* DMASK was repurposed for GATHER4. 4 components are always
4905 * returned and DMASK works like a swizzle - it selects
4906 * the component to fetch. The only valid DMASK values are
4907 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4908 * (red,red,red,red) etc.) The ISA document doesn't mention
4912 if (instr
->op
== nir_texop_tg4
) {
4913 if (instr
->is_shadow
)
4916 args
.dmask
= 1 << instr
->component
;
4919 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4920 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4921 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4924 /* Adjust the number of coordinates because we only need (x,y) for 2D
4925 * multisampled images and (x,y,layer) for 2D multisampled layered
4926 * images or for multisampled input attachments.
4928 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4929 if (args
.dim
== ac_image_2dmsaa
) {
4930 args
.dim
= ac_image_2d
;
4932 assert(args
.dim
== ac_image_2darraymsaa
);
4933 args
.dim
= ac_image_2darray
;
4937 assert(instr
->dest
.is_ssa
);
4938 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
4940 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4942 if (instr
->op
== nir_texop_query_levels
)
4943 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4944 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4945 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4946 instr
->op
!= nir_texop_tg4
)
4947 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4948 else if (instr
->op
== nir_texop_txs
&&
4949 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4951 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4952 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4953 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4954 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4955 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4956 } else if (ctx
->ac
.chip_class
== GFX9
&&
4957 instr
->op
== nir_texop_txs
&&
4958 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4960 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4961 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4962 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4964 } else if (instr
->dest
.ssa
.num_components
!= 4)
4965 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4969 assert(instr
->dest
.is_ssa
);
4970 result
= ac_to_integer(&ctx
->ac
, result
);
4972 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4973 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4976 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4980 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4982 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4983 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4985 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4986 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4989 static void visit_post_phi(struct ac_nir_context
*ctx
,
4990 nir_phi_instr
*instr
,
4991 LLVMValueRef llvm_phi
)
4993 nir_foreach_phi_src(src
, instr
) {
4994 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4995 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4997 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
5001 static void phi_post_pass(struct ac_nir_context
*ctx
)
5003 hash_table_foreach(ctx
->phis
, entry
) {
5004 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5005 (LLVMValueRef
)entry
->data
);
5010 static bool is_def_used_in_an_export(const nir_ssa_def
* def
) {
5011 nir_foreach_use(use_src
, def
) {
5012 if (use_src
->parent_instr
->type
== nir_instr_type_intrinsic
) {
5013 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(use_src
->parent_instr
);
5014 if (instr
->intrinsic
== nir_intrinsic_store_deref
)
5016 } else if (use_src
->parent_instr
->type
== nir_instr_type_alu
) {
5017 nir_alu_instr
*instr
= nir_instr_as_alu(use_src
->parent_instr
);
5018 if (instr
->op
== nir_op_vec4
&&
5019 is_def_used_in_an_export(&instr
->dest
.dest
.ssa
)) {
5027 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5028 const nir_ssa_undef_instr
*instr
)
5030 unsigned num_components
= instr
->def
.num_components
;
5031 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5033 if (!ctx
->abi
->convert_undef_to_zero
|| is_def_used_in_an_export(&instr
->def
)) {
5036 if (num_components
== 1)
5037 undef
= LLVMGetUndef(type
);
5039 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5041 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
5043 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
5044 if (num_components
> 1) {
5045 zero
= ac_build_gather_values_extended(
5046 &ctx
->ac
, &zero
, 4, 0, false, false);
5048 ctx
->ssa_defs
[instr
->def
.index
] = zero
;
5052 static void visit_jump(struct ac_llvm_context
*ctx
,
5053 const nir_jump_instr
*instr
)
5055 switch (instr
->type
) {
5056 case nir_jump_break
:
5057 ac_build_break(ctx
);
5059 case nir_jump_continue
:
5060 ac_build_continue(ctx
);
5063 fprintf(stderr
, "Unknown NIR jump instr: ");
5064 nir_print_instr(&instr
->instr
, stderr
);
5065 fprintf(stderr
, "\n");
5071 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
5072 enum glsl_base_type type
)
5076 case GLSL_TYPE_UINT
:
5077 case GLSL_TYPE_BOOL
:
5078 case GLSL_TYPE_SUBROUTINE
:
5080 case GLSL_TYPE_INT8
:
5081 case GLSL_TYPE_UINT8
:
5083 case GLSL_TYPE_INT16
:
5084 case GLSL_TYPE_UINT16
:
5086 case GLSL_TYPE_FLOAT
:
5088 case GLSL_TYPE_FLOAT16
:
5090 case GLSL_TYPE_INT64
:
5091 case GLSL_TYPE_UINT64
:
5093 case GLSL_TYPE_DOUBLE
:
5096 unreachable("unknown GLSL type");
5101 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
5102 const struct glsl_type
*type
)
5104 if (glsl_type_is_scalar(type
)) {
5105 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
5108 if (glsl_type_is_vector(type
)) {
5109 return LLVMVectorType(
5110 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
5111 glsl_get_vector_elements(type
));
5114 if (glsl_type_is_matrix(type
)) {
5115 return LLVMArrayType(
5116 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
5117 glsl_get_matrix_columns(type
));
5120 if (glsl_type_is_array(type
)) {
5121 return LLVMArrayType(
5122 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
5123 glsl_get_length(type
));
5126 assert(glsl_type_is_struct_or_ifc(type
));
5128 LLVMTypeRef member_types
[glsl_get_length(type
)];
5130 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5132 glsl_to_llvm_type(ac
,
5133 glsl_get_struct_field(type
, i
));
5136 return LLVMStructTypeInContext(ac
->context
, member_types
,
5137 glsl_get_length(type
), false);
5140 static void visit_deref(struct ac_nir_context
*ctx
,
5141 nir_deref_instr
*instr
)
5143 if (instr
->mode
!= nir_var_mem_shared
&&
5144 instr
->mode
!= nir_var_mem_global
)
5147 LLVMValueRef result
= NULL
;
5148 switch(instr
->deref_type
) {
5149 case nir_deref_type_var
: {
5150 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
5151 result
= entry
->data
;
5154 case nir_deref_type_struct
:
5155 if (instr
->mode
== nir_var_mem_global
) {
5156 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5157 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
5158 instr
->strct
.index
);
5159 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5160 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
5162 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5163 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
5166 case nir_deref_type_array
:
5167 if (instr
->mode
== nir_var_mem_global
) {
5168 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5169 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
5171 if ((glsl_type_is_matrix(parent
->type
) &&
5172 glsl_matrix_type_is_row_major(parent
->type
)) ||
5173 (glsl_type_is_vector(parent
->type
) && stride
== 0))
5174 stride
= type_scalar_size_bytes(parent
->type
);
5177 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5178 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5179 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5181 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5183 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5185 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5186 get_src(ctx
, instr
->arr
.index
));
5189 case nir_deref_type_ptr_as_array
:
5190 if (instr
->mode
== nir_var_mem_global
) {
5191 unsigned stride
= nir_deref_instr_array_stride(instr
);
5193 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5194 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5195 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5197 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5199 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5201 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5202 get_src(ctx
, instr
->arr
.index
));
5205 case nir_deref_type_cast
: {
5206 result
= get_src(ctx
, instr
->parent
);
5208 /* We can't use the structs from LLVM because the shader
5209 * specifies its own offsets. */
5210 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
5211 if (instr
->mode
== nir_var_mem_shared
)
5212 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
5214 unsigned address_space
;
5216 switch(instr
->mode
) {
5217 case nir_var_mem_shared
:
5218 address_space
= AC_ADDR_SPACE_LDS
;
5220 case nir_var_mem_global
:
5221 address_space
= AC_ADDR_SPACE_GLOBAL
;
5224 unreachable("Unhandled address space");
5227 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
5229 if (LLVMTypeOf(result
) != type
) {
5230 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
5231 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
5234 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
5241 unreachable("Unhandled deref_instr deref type");
5244 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
5247 static void visit_cf_list(struct ac_nir_context
*ctx
,
5248 struct exec_list
*list
);
5250 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5252 nir_foreach_instr(instr
, block
)
5254 switch (instr
->type
) {
5255 case nir_instr_type_alu
:
5256 visit_alu(ctx
, nir_instr_as_alu(instr
));
5258 case nir_instr_type_load_const
:
5259 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5261 case nir_instr_type_intrinsic
:
5262 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5264 case nir_instr_type_tex
:
5265 visit_tex(ctx
, nir_instr_as_tex(instr
));
5267 case nir_instr_type_phi
:
5268 visit_phi(ctx
, nir_instr_as_phi(instr
));
5270 case nir_instr_type_ssa_undef
:
5271 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5273 case nir_instr_type_jump
:
5274 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
5276 case nir_instr_type_deref
:
5277 visit_deref(ctx
, nir_instr_as_deref(instr
));
5280 fprintf(stderr
, "Unknown NIR instr type: ");
5281 nir_print_instr(instr
, stderr
);
5282 fprintf(stderr
, "\n");
5287 _mesa_hash_table_insert(ctx
->defs
, block
,
5288 LLVMGetInsertBlock(ctx
->ac
.builder
));
5291 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5293 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5295 nir_block
*then_block
=
5296 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5298 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5300 visit_cf_list(ctx
, &if_stmt
->then_list
);
5302 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5303 nir_block
*else_block
=
5304 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5306 ac_build_else(&ctx
->ac
, else_block
->index
);
5307 visit_cf_list(ctx
, &if_stmt
->else_list
);
5310 ac_build_endif(&ctx
->ac
, then_block
->index
);
5313 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5315 nir_block
*first_loop_block
=
5316 (nir_block
*) exec_list_get_head(&loop
->body
);
5318 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5320 visit_cf_list(ctx
, &loop
->body
);
5322 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5325 static void visit_cf_list(struct ac_nir_context
*ctx
,
5326 struct exec_list
*list
)
5328 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5330 switch (node
->type
) {
5331 case nir_cf_node_block
:
5332 visit_block(ctx
, nir_cf_node_as_block(node
));
5335 case nir_cf_node_if
:
5336 visit_if(ctx
, nir_cf_node_as_if(node
));
5339 case nir_cf_node_loop
:
5340 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5350 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5351 struct ac_shader_abi
*abi
,
5352 struct nir_shader
*nir
,
5353 struct nir_variable
*variable
,
5354 gl_shader_stage stage
)
5356 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5357 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5359 /* tess ctrl has it's own load/store paths for outputs */
5360 if (stage
== MESA_SHADER_TESS_CTRL
)
5363 if (stage
== MESA_SHADER_VERTEX
||
5364 stage
== MESA_SHADER_TESS_EVAL
||
5365 stage
== MESA_SHADER_GEOMETRY
) {
5366 int idx
= variable
->data
.location
+ variable
->data
.index
;
5367 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5368 int length
= nir
->info
.clip_distance_array_size
+
5369 nir
->info
.cull_distance_array_size
;
5378 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5379 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5380 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5381 for (unsigned chan
= 0; chan
< 4; chan
++) {
5382 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5383 ac_build_alloca_undef(ctx
, type
, "");
5389 setup_locals(struct ac_nir_context
*ctx
,
5390 struct nir_function
*func
)
5393 ctx
->num_locals
= 0;
5394 nir_foreach_function_temp_variable(variable
, func
->impl
) {
5395 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5396 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5397 variable
->data
.location_frac
= 0;
5398 ctx
->num_locals
+= attrib_count
;
5400 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5404 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5405 for (j
= 0; j
< 4; j
++) {
5406 ctx
->locals
[i
* 4 + j
] =
5407 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5413 setup_scratch(struct ac_nir_context
*ctx
,
5414 struct nir_shader
*shader
)
5416 if (shader
->scratch_size
== 0)
5419 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5420 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5425 setup_constant_data(struct ac_nir_context
*ctx
,
5426 struct nir_shader
*shader
)
5428 if (!shader
->constant_data
)
5432 LLVMConstStringInContext(ctx
->ac
.context
,
5433 shader
->constant_data
,
5434 shader
->constant_data_size
,
5436 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5438 /* We want to put the constant data in the CONST address space so that
5439 * we can use scalar loads. However, LLVM versions before 10 put these
5440 * variables in the same section as the code, which is unacceptable
5441 * for RadeonSI as it needs to relocate all the data sections after
5442 * the code sections. See https://reviews.llvm.org/D65813.
5444 unsigned address_space
=
5445 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5447 LLVMValueRef global
=
5448 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5452 LLVMSetInitializer(global
, data
);
5453 LLVMSetGlobalConstant(global
, true);
5454 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5455 ctx
->constant_data
= global
;
5459 setup_shared(struct ac_nir_context
*ctx
,
5460 struct nir_shader
*nir
)
5465 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5466 nir
->info
.cs
.shared_size
);
5469 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5472 LLVMSetAlignment(lds
, 64 * 1024);
5474 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5475 LLVMPointerType(ctx
->ac
.i8
,
5476 AC_ADDR_SPACE_LDS
), "");
5479 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5480 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5482 struct ac_nir_context ctx
= {};
5483 struct nir_function
*func
;
5489 ctx
.stage
= nir
->info
.stage
;
5490 ctx
.info
= &nir
->info
;
5492 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5494 /* TODO: remove this after RADV switches to lowered IO */
5495 if (!nir
->info
.io_lowered
) {
5496 nir_foreach_shader_out_variable(variable
, nir
) {
5497 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5502 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5503 _mesa_key_pointer_equal
);
5504 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5505 _mesa_key_pointer_equal
);
5506 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5507 _mesa_key_pointer_equal
);
5509 if (ctx
.abi
->kill_ps_if_inf_interp
)
5510 ctx
.verified_interp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5511 _mesa_key_pointer_equal
);
5513 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5515 nir_index_ssa_defs(func
->impl
);
5516 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5518 setup_locals(&ctx
, func
);
5519 setup_scratch(&ctx
, nir
);
5520 setup_constant_data(&ctx
, nir
);
5522 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5523 setup_shared(&ctx
, nir
);
5525 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5526 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5527 /* true = don't kill. */
5528 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5531 visit_cf_list(&ctx
, &func
->impl
->body
);
5532 phi_post_pass(&ctx
);
5534 if (ctx
.ac
.postponed_kill
)
5535 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5536 ctx
.ac
.postponed_kill
, ""));
5538 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5539 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5544 ralloc_free(ctx
.defs
);
5545 ralloc_free(ctx
.phis
);
5546 ralloc_free(ctx
.vars
);
5547 if (ctx
.abi
->kill_ps_if_inf_interp
)
5548 ralloc_free(ctx
.verified_interp
);
5552 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5554 bool progress
= false;
5556 /* Lower large variables to scratch first so that we won't bloat the
5557 * shader by generating large if ladders for them. We later lower
5558 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5560 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5561 nir_var_function_temp
,
5563 glsl_get_natural_size_align_bytes
);
5565 /* While it would be nice not to have this flag, we are constrained
5566 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5568 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5570 /* TODO: Indirect indexing of GS inputs is unimplemented.
5572 * TCS and TES load inputs directly from LDS or offchip memory, so
5573 * indirect indexing is trivial.
5575 nir_variable_mode indirect_mask
= 0;
5576 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5577 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5578 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5579 !llvm_has_working_vgpr_indexing
)) {
5580 indirect_mask
|= nir_var_shader_in
;
5582 if (!llvm_has_working_vgpr_indexing
&&
5583 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5584 indirect_mask
|= nir_var_shader_out
;
5586 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5587 * smart enough to handle indirects without causing excess spilling
5588 * causing the gpu to hang.
5590 * See the following thread for more details of the problem:
5591 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5593 indirect_mask
|= nir_var_function_temp
;
5595 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
, UINT32_MAX
);
5600 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5602 if (intrin
->intrinsic
!= nir_intrinsic_store_output
)
5605 unsigned writemask
= nir_intrinsic_write_mask(intrin
) <<
5606 nir_intrinsic_component(intrin
);
5607 unsigned location
= nir_intrinsic_io_semantics(intrin
).location
;
5609 if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5610 return writemask
<< 4;
5611 else if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5618 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5619 unsigned *cond_block_tf_writemask
,
5620 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5622 switch (cf_node
->type
) {
5623 case nir_cf_node_block
: {
5624 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5625 nir_foreach_instr(instr
, block
) {
5626 if (instr
->type
!= nir_instr_type_intrinsic
)
5629 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5630 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5632 /* If we find a barrier in nested control flow put this in the
5633 * too hard basket. In GLSL this is not possible but it is in
5637 *tessfactors_are_def_in_all_invocs
= false;
5641 /* The following case must be prevented:
5642 * gl_TessLevelInner = ...;
5644 * if (gl_InvocationID == 1)
5645 * gl_TessLevelInner = ...;
5647 * If you consider disjoint code segments separated by barriers, each
5648 * such segment that writes tess factor channels should write the same
5649 * channels in all codepaths within that segment.
5651 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5652 /* Accumulate the result: */
5653 *tessfactors_are_def_in_all_invocs
&=
5654 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5656 /* Analyze the next code segment from scratch. */
5657 *upper_block_tf_writemask
= 0;
5658 *cond_block_tf_writemask
= 0;
5661 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5666 case nir_cf_node_if
: {
5667 unsigned then_tessfactor_writemask
= 0;
5668 unsigned else_tessfactor_writemask
= 0;
5670 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5671 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5672 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5673 cond_block_tf_writemask
,
5674 tessfactors_are_def_in_all_invocs
, true);
5677 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5678 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5679 cond_block_tf_writemask
,
5680 tessfactors_are_def_in_all_invocs
, true);
5683 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5684 /* If both statements write the same tess factor channels,
5685 * we can say that the upper block writes them too.
5687 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5688 else_tessfactor_writemask
;
5689 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5690 else_tessfactor_writemask
;
5695 case nir_cf_node_loop
: {
5696 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5697 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5698 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5699 cond_block_tf_writemask
,
5700 tessfactors_are_def_in_all_invocs
, true);
5706 unreachable("unknown cf node type");
5711 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5713 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5715 /* The pass works as follows:
5716 * If all codepaths write tess factors, we can say that all
5717 * invocations define tess factors.
5719 * Each tess factor channel is tracked separately.
5721 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5722 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5724 /* Initial value = true. Here the pass will accumulate results from
5725 * multiple segments surrounded by barriers. If tess factors aren't
5726 * written at all, it's a shader bug and we don't care if this will be
5729 bool tessfactors_are_def_in_all_invocs
= true;
5731 nir_foreach_function(function
, nir
) {
5732 if (function
->impl
) {
5733 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5734 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5735 &cond_block_tf_writemask
,
5736 &tessfactors_are_def_in_all_invocs
,
5742 /* Accumulate the result for the last code segment separated by a
5745 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5746 tessfactors_are_def_in_all_invocs
&=
5747 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5750 return tessfactors_are_def_in_all_invocs
;