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 if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
290 LLVMGetTypeKind(src2_type
) != LLVMPointerTypeKind
) {
291 src2
= LLVMBuildIntToPtr(ctx
->builder
, src2
, src1_type
, "");
292 } else if (LLVMGetTypeKind(src2_type
) == LLVMPointerTypeKind
&&
293 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
294 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src2_type
, "");
297 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
298 LLVMConstNull(LLVMTypeOf(src0
)), "");
299 return LLVMBuildSelect(ctx
->builder
, v
,
300 ac_to_integer_or_pointer(ctx
, src1
),
301 ac_to_integer_or_pointer(ctx
, src2
), "");
304 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
307 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
310 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
312 LLVMValueRef src0
, LLVMValueRef src1
)
314 LLVMTypeRef ret_type
;
315 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
317 LLVMValueRef params
[] = { src0
, src1
};
318 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
321 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
322 params
, 2, AC_FUNC_ATTR_READNONE
);
324 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
325 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
329 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
333 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
334 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
336 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
340 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
344 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
346 unreachable("Unsupported bit size.");
350 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
353 src0
= ac_to_float(ctx
, src0
);
354 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
355 return LLVMBuildSExt(ctx
->builder
,
356 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
360 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
364 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
368 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
370 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
374 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
376 unreachable("Unsupported bit size.");
380 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
383 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
384 return LLVMBuildSExt(ctx
->builder
,
385 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
389 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
393 LLVMValueRef cond
= NULL
;
395 src0
= ac_to_float(ctx
, src0
);
396 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
398 if (ctx
->chip_class
>= GFX8
) {
399 LLVMValueRef args
[2];
400 /* Check if the result is a denormal - and flush to 0 if so. */
402 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
403 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
406 /* need to convert back up to f32 */
407 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
409 if (ctx
->chip_class
>= GFX8
)
410 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
413 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
414 * so compare the result and flush to 0 if it's smaller.
416 LLVMValueRef temp
, cond2
;
417 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
418 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
,
419 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
421 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealONE
,
422 temp
, ctx
->f32_0
, "");
423 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
424 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
429 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
430 LLVMValueRef src0
, LLVMValueRef src1
)
432 LLVMValueRef dst64
, result
;
433 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
434 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
436 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
437 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
438 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
442 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
443 LLVMValueRef src0
, LLVMValueRef src1
)
445 LLVMValueRef dst64
, result
;
446 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
447 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
449 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
450 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
451 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
455 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
456 LLVMValueRef bits
, LLVMValueRef offset
)
458 /* mask = ((1 << bits) - 1) << offset */
459 return LLVMBuildShl(ctx
->builder
,
460 LLVMBuildSub(ctx
->builder
,
461 LLVMBuildShl(ctx
->builder
,
468 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
469 LLVMValueRef mask
, LLVMValueRef insert
,
473 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
474 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
476 return LLVMBuildXor(ctx
->builder
, base
,
477 LLVMBuildAnd(ctx
->builder
, mask
,
478 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
481 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
483 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
484 LLVMValueRef args
[2]))
486 LLVMValueRef comp
[2];
488 src0
= ac_to_float(ctx
, src0
);
489 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
490 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
492 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
495 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
498 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
499 LLVMValueRef temps
[2], val
;
502 for (i
= 0; i
< 2; i
++) {
503 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
504 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
505 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
506 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
508 return ac_build_gather_values(ctx
, temps
, 2);
511 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
519 if (op
== nir_op_fddx_fine
)
520 mask
= AC_TID_MASK_LEFT
;
521 else if (op
== nir_op_fddy_fine
)
522 mask
= AC_TID_MASK_TOP
;
524 mask
= AC_TID_MASK_TOP_LEFT
;
526 /* for DDX we want to next X pixel, DDY next Y pixel. */
527 if (op
== nir_op_fddx_fine
||
528 op
== nir_op_fddx_coarse
||
534 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
538 struct waterfall_context
{
539 LLVMBasicBlockRef phi_bb
[2];
543 /* To deal with divergent descriptors we can create a loop that handles all
544 * lanes with the same descriptor on a given iteration (henceforth a
547 * These helper create the begin and end of the loop leaving the caller
548 * to implement the body.
551 * - ctx is the usal nir context
552 * - wctx is a temporary struct containing some loop info. Can be left uninitialized.
553 * - value is the possibly divergent value for which we built the loop
554 * - divergent is whether value is actually divergent. If false we just pass
557 static LLVMValueRef
enter_waterfall(struct ac_nir_context
*ctx
,
558 struct waterfall_context
*wctx
,
559 LLVMValueRef value
, bool divergent
)
561 /* If the app claims the value is divergent but it is constant we can
562 * end up with a dynamic index of NULL. */
566 wctx
->use_waterfall
= divergent
;
570 ac_build_bgnloop(&ctx
->ac
, 6000);
572 LLVMValueRef scalar_value
= ac_build_readlane(&ctx
->ac
, value
, NULL
);
574 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, value
,
575 scalar_value
, "uniform_active");
577 wctx
->phi_bb
[0] = LLVMGetInsertBlock(ctx
->ac
.builder
);
578 ac_build_ifcc(&ctx
->ac
, active
, 6001);
583 static LLVMValueRef
exit_waterfall(struct ac_nir_context
*ctx
,
584 struct waterfall_context
*wctx
,
587 LLVMValueRef ret
= NULL
;
588 LLVMValueRef phi_src
[2];
589 LLVMValueRef cc_phi_src
[2] = {
590 LLVMConstInt(ctx
->ac
.i32
, 0, false),
591 LLVMConstInt(ctx
->ac
.i32
, 0xffffffff, false),
594 if (!wctx
->use_waterfall
)
597 wctx
->phi_bb
[1] = LLVMGetInsertBlock(ctx
->ac
.builder
);
599 ac_build_endif(&ctx
->ac
, 6001);
602 phi_src
[0] = LLVMGetUndef(LLVMTypeOf(value
));
605 ret
= ac_build_phi(&ctx
->ac
, LLVMTypeOf(value
), 2, phi_src
, wctx
->phi_bb
);
609 * By using the optimization barrier on the exit decision, we decouple
610 * the operations from the break, and hence avoid LLVM hoisting the
611 * opteration into the break block.
613 LLVMValueRef cc
= ac_build_phi(&ctx
->ac
, ctx
->ac
.i32
, 2, cc_phi_src
, wctx
->phi_bb
);
614 ac_build_optimization_barrier(&ctx
->ac
, &cc
);
616 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, cc
, ctx
->ac
.i32_0
, "uniform_active2");
617 ac_build_ifcc(&ctx
->ac
, active
, 6002);
618 ac_build_break(&ctx
->ac
);
619 ac_build_endif(&ctx
->ac
, 6002);
621 ac_build_endloop(&ctx
->ac
, 6000);
625 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
627 LLVMValueRef src
[4], result
= NULL
;
628 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
629 unsigned src_components
;
630 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
632 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
639 case nir_op_pack_half_2x16
:
640 case nir_op_pack_snorm_2x16
:
641 case nir_op_pack_unorm_2x16
:
644 case nir_op_unpack_half_2x16
:
647 case nir_op_cube_face_coord
:
648 case nir_op_cube_face_index
:
652 src_components
= num_components
;
655 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
656 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
663 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
664 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
665 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
666 /* fneg will be optimized by backend compiler with sign
667 * bit removed via XOR. This is probably a LLVM bug.
669 result
= ac_build_canonicalize(&ctx
->ac
, result
,
670 instr
->dest
.dest
.ssa
.bit_size
);
674 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
677 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
680 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
683 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
684 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
685 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
688 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
689 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
690 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
693 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
696 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
699 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
702 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
705 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
708 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
711 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
714 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
715 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
716 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
719 /* For doubles, we need precise division to pass GLCTS. */
720 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
&&
721 ac_get_type_size(def_type
) == 8) {
722 result
= LLVMBuildFDiv(ctx
->ac
.builder
, ctx
->ac
.f64_1
,
723 ac_to_float(&ctx
->ac
, src
[0]), "");
725 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rcp",
726 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
728 if (ctx
->abi
->clamp_div_by_zero
)
729 result
= ac_build_fmin(&ctx
->ac
, result
,
730 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
733 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
736 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
739 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
742 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
743 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
744 LLVMTypeOf(src
[0]), "");
745 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
746 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
747 LLVMTypeOf(src
[0]), "");
748 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
751 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
752 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
753 LLVMTypeOf(src
[0]), "");
754 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
755 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
756 LLVMTypeOf(src
[0]), "");
757 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
760 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
761 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
762 LLVMTypeOf(src
[0]), "");
763 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
764 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
765 LLVMTypeOf(src
[0]), "");
766 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
769 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
772 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
775 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
778 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
781 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
784 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
787 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
790 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
793 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
796 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
799 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
800 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
801 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
802 /* fabs will be optimized by backend compiler with sign
803 * bit removed via AND.
805 result
= ac_build_canonicalize(&ctx
->ac
, result
,
806 instr
->dest
.dest
.ssa
.bit_size
);
810 result
= emit_iabs(&ctx
->ac
, src
[0]);
813 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
816 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
819 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
822 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
825 result
= ac_build_isign(&ctx
->ac
, src
[0]);
828 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
829 result
= ac_build_fsign(&ctx
->ac
, src
[0],
830 instr
->dest
.dest
.ssa
.bit_size
);
833 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
834 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
837 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
838 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
841 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
842 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
844 case nir_op_fround_even
:
845 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
846 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
849 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.fract",
850 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
853 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
854 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
857 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
858 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
861 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
865 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
866 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
869 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
870 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
873 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rsq",
874 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
875 if (ctx
->abi
->clamp_div_by_zero
)
876 result
= ac_build_fmin(&ctx
->ac
, result
,
877 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
879 case nir_op_frexp_exp
:
880 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
881 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
882 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
883 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
884 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
887 case nir_op_frexp_sig
:
888 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
889 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
890 instr
->dest
.dest
.ssa
.bit_size
);
893 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
894 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
897 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
898 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
899 if (ctx
->ac
.chip_class
< GFX9
&&
900 instr
->dest
.dest
.ssa
.bit_size
== 32) {
901 /* Only pre-GFX9 chips do not flush denorms. */
902 result
= ac_build_canonicalize(&ctx
->ac
, result
,
903 instr
->dest
.dest
.ssa
.bit_size
);
907 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
908 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
909 if (ctx
->ac
.chip_class
< GFX9
&&
910 instr
->dest
.dest
.ssa
.bit_size
== 32) {
911 /* Only pre-GFX9 chips do not flush denorms. */
912 result
= ac_build_canonicalize(&ctx
->ac
, result
,
913 instr
->dest
.dest
.ssa
.bit_size
);
917 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
918 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
919 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
922 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
923 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
924 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
925 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
926 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
928 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
931 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
933 case nir_op_bitfield_select
:
934 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
937 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
940 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
942 case nir_op_bitfield_reverse
:
943 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
945 case nir_op_bit_count
:
946 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
951 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
952 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
953 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
959 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
960 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
966 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
967 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
972 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
977 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
979 case nir_op_f2f16_rtz
:
982 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
984 /* For OpenGL, we want fast packing with v_cvt_pkrtz_f16, but if we use it,
985 * all f32->f16 conversions have to round towards zero, because both scalar
986 * and vec2 down-conversions have to round equally.
988 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
||
989 instr
->op
== nir_op_f2f16_rtz
) {
990 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
992 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
993 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
995 /* Fast path conversion. This only works if NIR is vectorized
998 if (LLVMTypeOf(src
[0]) == ctx
->ac
.v2f32
) {
999 LLVMValueRef args
[] = {
1000 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 0),
1001 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 1),
1003 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, args
);
1007 assert(ac_get_llvm_num_components(src
[0]) == 1);
1008 LLVMValueRef param
[2] = { src
[0], LLVMGetUndef(ctx
->ac
.f32
) };
1009 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
1010 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
1012 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1013 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1015 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1018 case nir_op_f2f16_rtne
:
1021 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1022 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1023 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1025 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1032 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1033 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1035 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1042 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1043 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1045 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1047 case nir_op_b32csel
:
1048 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1050 case nir_op_find_lsb
:
1051 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1053 case nir_op_ufind_msb
:
1054 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1056 case nir_op_ifind_msb
:
1057 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1059 case nir_op_uadd_carry
:
1060 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1062 case nir_op_usub_borrow
:
1063 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1068 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1071 result
= emit_f2b(&ctx
->ac
, src
[0]);
1077 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1080 result
= emit_i2b(&ctx
->ac
, src
[0]);
1082 case nir_op_fquantize2f16
:
1083 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1085 case nir_op_umul_high
:
1086 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1088 case nir_op_imul_high
:
1089 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1091 case nir_op_pack_half_2x16
:
1092 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1094 case nir_op_pack_snorm_2x16
:
1095 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1097 case nir_op_pack_unorm_2x16
:
1098 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1100 case nir_op_unpack_half_2x16
:
1101 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1105 case nir_op_fddx_fine
:
1106 case nir_op_fddy_fine
:
1107 case nir_op_fddx_coarse
:
1108 case nir_op_fddy_coarse
:
1109 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1112 case nir_op_unpack_64_2x32_split_x
: {
1113 assert(ac_get_llvm_num_components(src
[0]) == 1);
1114 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1117 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1122 case nir_op_unpack_64_2x32_split_y
: {
1123 assert(ac_get_llvm_num_components(src
[0]) == 1);
1124 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1127 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1132 case nir_op_pack_64_2x32_split
: {
1133 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1134 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1138 case nir_op_pack_32_2x16_split
: {
1139 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1140 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1144 case nir_op_unpack_32_2x16_split_x
: {
1145 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1148 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1153 case nir_op_unpack_32_2x16_split_y
: {
1154 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1157 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1162 case nir_op_cube_face_coord
: {
1163 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1164 LLVMValueRef results
[2];
1166 for (unsigned chan
= 0; chan
< 3; chan
++)
1167 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1168 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1169 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1170 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1171 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1172 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1173 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1174 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1175 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1176 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1177 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1178 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1179 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1183 case nir_op_cube_face_index
: {
1184 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1186 for (unsigned chan
= 0; chan
< 3; chan
++)
1187 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1188 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1189 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1194 fprintf(stderr
, "Unknown NIR alu instr: ");
1195 nir_print_instr(&instr
->instr
, stderr
);
1196 fprintf(stderr
, "\n");
1201 assert(instr
->dest
.dest
.is_ssa
);
1202 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1203 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1207 static void visit_load_const(struct ac_nir_context
*ctx
,
1208 const nir_load_const_instr
*instr
)
1210 LLVMValueRef values
[4], value
= NULL
;
1211 LLVMTypeRef element_type
=
1212 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1214 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1215 switch (instr
->def
.bit_size
) {
1217 values
[i
] = LLVMConstInt(element_type
,
1218 instr
->value
[i
].u8
, false);
1221 values
[i
] = LLVMConstInt(element_type
,
1222 instr
->value
[i
].u16
, false);
1225 values
[i
] = LLVMConstInt(element_type
,
1226 instr
->value
[i
].u32
, false);
1229 values
[i
] = LLVMConstInt(element_type
,
1230 instr
->value
[i
].u64
, false);
1234 "unsupported nir load_const bit_size: %d\n",
1235 instr
->def
.bit_size
);
1239 if (instr
->def
.num_components
> 1) {
1240 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1244 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1248 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1251 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1252 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1255 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1256 /* On GFX8, the descriptor contains the size in bytes,
1257 * but TXQ must return the size in elements.
1258 * The stride is always non-zero for resources using TXQ.
1260 LLVMValueRef stride
=
1261 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1263 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1264 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1265 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1266 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1268 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1273 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1274 * incorrectly forces nearest filtering if the texture format is integer.
1275 * The only effect it has on Gather4, which always returns 4 texels for
1276 * bilinear filtering, is that the final coordinates are off by 0.5 of
1279 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1280 * or (0.5 / size) from the normalized coordinates.
1282 * However, cube textures with 8_8_8_8 data formats require a different
1283 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1284 * precision in 32-bit data formats, so it needs to be applied dynamically at
1285 * runtime. In this case, return an i1 value that indicates whether the
1286 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1288 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1290 struct ac_image_args
*args
,
1291 const nir_tex_instr
*instr
)
1293 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1294 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1295 LLVMValueRef wa_8888
= NULL
;
1296 LLVMValueRef half_texel
[2];
1297 LLVMValueRef result
;
1299 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1301 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1302 LLVMValueRef formats
;
1303 LLVMValueRef data_format
;
1304 LLVMValueRef wa_formats
;
1306 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1308 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1309 LLVMConstInt(ctx
->i32
, 20, false), "");
1310 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1311 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1312 wa_8888
= LLVMBuildICmp(
1313 ctx
->builder
, LLVMIntEQ
, data_format
,
1314 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1317 uint32_t wa_num_format
=
1318 stype
== GLSL_TYPE_UINT
?
1319 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1320 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1321 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1322 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1324 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1325 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1327 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1328 args
->resource
= LLVMBuildInsertElement(
1329 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1332 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1334 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1336 struct ac_image_args resinfo
= {};
1337 LLVMBasicBlockRef bbs
[2];
1339 LLVMValueRef unnorm
= NULL
;
1340 LLVMValueRef default_offset
= ctx
->f32_0
;
1341 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1343 /* In vulkan, whether the sampler uses unnormalized
1344 * coordinates or not is a dynamic property of the
1345 * sampler. Hence, to figure out whether or not we
1346 * need to divide by the texture size, we need to test
1347 * the sampler at runtime. This tests the bit set by
1348 * radv_init_sampler().
1350 LLVMValueRef sampler0
=
1351 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1352 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1353 LLVMConstInt(ctx
->i32
, 15, false), "");
1354 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1355 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1356 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1359 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1360 if (wa_8888
|| unnorm
) {
1361 assert(!(wa_8888
&& unnorm
));
1362 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1363 /* Skip the texture size query entirely if we don't need it. */
1364 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1365 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1368 /* Query the texture size. */
1369 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1370 resinfo
.opcode
= ac_image_get_resinfo
;
1371 resinfo
.dmask
= 0xf;
1372 resinfo
.lod
= ctx
->i32_0
;
1373 resinfo
.resource
= args
->resource
;
1374 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1375 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1377 /* Compute -0.5 / size. */
1378 for (unsigned c
= 0; c
< 2; c
++) {
1380 LLVMBuildExtractElement(ctx
->builder
, size
,
1381 LLVMConstInt(ctx
->i32
, c
, 0), "");
1382 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1383 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1384 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1385 LLVMConstReal(ctx
->f32
, -0.5), "");
1388 if (wa_8888
|| unnorm
) {
1389 ac_build_endif(ctx
, 2000);
1391 for (unsigned c
= 0; c
< 2; c
++) {
1392 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1393 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1399 for (unsigned c
= 0; c
< 2; c
++) {
1401 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1402 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1405 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1406 result
= ac_build_image_opcode(ctx
, args
);
1408 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1409 LLVMValueRef tmp
, tmp2
;
1411 /* if the cube workaround is in place, f2i the result. */
1412 for (unsigned c
= 0; c
< 4; c
++) {
1413 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1414 if (stype
== GLSL_TYPE_UINT
)
1415 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1417 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1418 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1419 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1420 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1421 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1422 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1428 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1430 nir_deref_instr
*texture_deref_instr
= NULL
;
1432 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1433 switch (instr
->src
[i
].src_type
) {
1434 case nir_tex_src_texture_deref
:
1435 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1441 return texture_deref_instr
;
1444 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1445 const nir_tex_instr
*instr
,
1446 struct ac_image_args
*args
)
1448 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1449 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1451 assert(instr
->dest
.is_ssa
);
1452 return ac_build_buffer_load_format(&ctx
->ac
,
1456 util_last_bit(mask
),
1458 instr
->dest
.ssa
.bit_size
== 16);
1461 args
->opcode
= ac_image_sample
;
1463 switch (instr
->op
) {
1465 case nir_texop_txf_ms
:
1466 case nir_texop_samples_identical
:
1467 args
->opcode
= args
->level_zero
||
1468 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1469 ac_image_load
: ac_image_load_mip
;
1470 args
->level_zero
= false;
1473 case nir_texop_query_levels
:
1474 args
->opcode
= ac_image_get_resinfo
;
1476 args
->lod
= ctx
->ac
.i32_0
;
1477 args
->level_zero
= false;
1480 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1482 args
->level_zero
= true;
1486 args
->opcode
= ac_image_gather4
;
1487 if (!args
->lod
&& !args
->bias
)
1488 args
->level_zero
= true;
1491 args
->opcode
= ac_image_get_lod
;
1493 case nir_texop_fragment_fetch
:
1494 case nir_texop_fragment_mask_fetch
:
1495 args
->opcode
= ac_image_load
;
1496 args
->level_zero
= false;
1502 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1503 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1504 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1505 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1506 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1507 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1508 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1512 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1513 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1514 if ((args
->dim
== ac_image_2darray
||
1515 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1516 args
->coords
[1] = ctx
->ac
.i32_0
;
1520 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1521 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1522 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1523 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1524 /* Prevent texture instructions with implicit derivatives from being
1525 * sinked into branches. */
1526 switch (instr
->op
) {
1530 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1537 return ac_build_image_opcode(&ctx
->ac
, args
);
1540 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1541 nir_intrinsic_instr
*instr
)
1543 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1544 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1546 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1547 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1551 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1552 nir_intrinsic_instr
*instr
)
1554 LLVMValueRef ptr
, addr
;
1555 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1556 unsigned index
= nir_intrinsic_base(instr
);
1558 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1559 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1561 /* Load constant values from user SGPRS when possible, otherwise
1562 * fallback to the default path that loads directly from memory.
1564 if (LLVMIsConstant(src0
) &&
1565 instr
->dest
.ssa
.bit_size
== 32) {
1566 unsigned count
= instr
->dest
.ssa
.num_components
;
1567 unsigned offset
= index
;
1569 offset
+= LLVMConstIntGetZExtValue(src0
);
1572 offset
-= ctx
->args
->base_inline_push_consts
;
1574 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1575 if (offset
+ count
<= num_inline_push_consts
) {
1576 LLVMValueRef push_constants
[num_inline_push_consts
];
1577 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1578 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1579 ctx
->args
->inline_push_consts
[i
]);
1580 return ac_build_gather_values(&ctx
->ac
,
1581 push_constants
+ offset
,
1586 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1587 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1589 if (instr
->dest
.ssa
.bit_size
== 8) {
1590 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1591 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i8
, 4 * load_dwords
);
1592 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1593 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1595 LLVMValueRef params
[3];
1596 if (load_dwords
> 1) {
1597 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.v2i32
, "");
1598 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1599 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1601 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1602 params
[0] = ctx
->ac
.i32_0
;
1606 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1608 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1609 if (instr
->dest
.ssa
.num_components
> 1)
1610 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i8
, instr
->dest
.ssa
.num_components
), "");
1612 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1613 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1614 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i16
, 2 * load_dwords
);
1615 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1616 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1617 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1618 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1619 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1620 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1621 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1622 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1623 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1624 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1625 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1626 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1627 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1628 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1631 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1633 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1636 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1637 const nir_intrinsic_instr
*instr
)
1639 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1641 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1644 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1646 uint32_t new_mask
= 0;
1647 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1648 if (mask
& (1u << i
))
1649 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1653 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1654 unsigned start
, unsigned count
)
1656 LLVMValueRef mask
[] = {
1657 ctx
->i32_0
, ctx
->i32_1
,
1658 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1660 unsigned src_elements
= ac_get_llvm_num_components(src
);
1662 if (count
== src_elements
) {
1665 } else if (count
== 1) {
1666 assert(start
< src_elements
);
1667 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1669 assert(start
+ count
<= src_elements
);
1671 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1672 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1676 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1677 enum gl_access_qualifier access
,
1678 bool may_store_unaligned
,
1679 bool writeonly_memory
)
1681 unsigned cache_policy
= 0;
1683 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1684 * store opcodes not aligned to a dword are affected. The only way to
1685 * get unaligned stores is through shader images.
1687 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1688 /* If this is write-only, don't keep data in L1 to prevent
1689 * evicting L1 cache lines that may be needed by other
1693 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1694 cache_policy
|= ac_glc
;
1697 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1698 cache_policy
|= ac_slc
| ac_glc
;
1700 return cache_policy
;
1703 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1704 struct waterfall_context
*wctx
,
1705 const nir_intrinsic_instr
*instr
,
1708 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1709 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1712 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1713 nir_intrinsic_instr
*instr
)
1715 if (ctx
->ac
.postponed_kill
) {
1716 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1717 ctx
->ac
.postponed_kill
, "");
1718 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1721 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1722 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1723 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1724 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1725 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1726 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1728 struct waterfall_context wctx
;
1729 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1731 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1732 LLVMValueRef base_data
= src_data
;
1733 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1734 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1738 LLVMValueRef data
, offset
;
1739 LLVMTypeRef data_type
;
1741 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1743 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1744 * writes into a 2-element and a 1-element write. */
1746 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1747 writemask
|= 1 << (start
+ 2);
1750 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1752 /* we can only store 4 DWords at the same time.
1753 * can only happen for 64 Bit vectors. */
1754 if (num_bytes
> 16) {
1755 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1760 /* check alignment of 16 Bit stores */
1761 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1762 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1767 /* Due to alignment issues, split stores of 8-bit/16-bit
1770 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1771 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1773 num_bytes
= elem_size_bytes
;
1776 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1778 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1779 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1781 if (num_bytes
== 1) {
1782 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1783 offset
, ctx
->ac
.i32_0
,
1785 } else if (num_bytes
== 2) {
1786 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1787 offset
, ctx
->ac
.i32_0
,
1790 int num_channels
= num_bytes
/ 4;
1792 switch (num_bytes
) {
1793 case 16: /* v4f32 */
1794 data_type
= ctx
->ac
.v4f32
;
1796 case 12: /* v3f32 */
1797 data_type
= ctx
->ac
.v3f32
;
1800 data_type
= ctx
->ac
.v2f32
;
1803 data_type
= ctx
->ac
.f32
;
1806 unreachable("Malformed vector store.");
1808 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1810 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1811 num_channels
, offset
,
1817 exit_waterfall(ctx
, &wctx
, NULL
);
1819 if (ctx
->ac
.postponed_kill
)
1820 ac_build_endif(&ctx
->ac
, 7000);
1823 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1824 LLVMValueRef descriptor
,
1825 LLVMValueRef offset
,
1826 LLVMValueRef compare
,
1827 LLVMValueRef exchange
)
1829 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1830 if (ctx
->abi
->robust_buffer_access
) {
1831 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1833 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1834 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1836 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1838 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1841 LLVMValueRef ptr_parts
[2] = {
1842 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1843 LLVMBuildAnd(ctx
->ac
.builder
,
1844 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1845 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1848 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1849 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1851 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1853 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1854 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1855 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1856 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1858 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1859 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1861 if (ctx
->abi
->robust_buffer_access
) {
1862 ac_build_endif(&ctx
->ac
, -1);
1864 LLVMBasicBlockRef incoming_blocks
[2] = {
1869 LLVMValueRef incoming_values
[2] = {
1870 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1873 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1874 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1881 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1882 nir_intrinsic_instr
*instr
)
1884 if (ctx
->ac
.postponed_kill
) {
1885 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1886 ctx
->ac
.postponed_kill
, "");
1887 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1890 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1892 char name
[64], type
[8];
1893 LLVMValueRef params
[6], descriptor
;
1894 LLVMValueRef result
;
1897 struct waterfall_context wctx
;
1898 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1900 switch (instr
->intrinsic
) {
1901 case nir_intrinsic_ssbo_atomic_add
:
1904 case nir_intrinsic_ssbo_atomic_imin
:
1907 case nir_intrinsic_ssbo_atomic_umin
:
1910 case nir_intrinsic_ssbo_atomic_imax
:
1913 case nir_intrinsic_ssbo_atomic_umax
:
1916 case nir_intrinsic_ssbo_atomic_and
:
1919 case nir_intrinsic_ssbo_atomic_or
:
1922 case nir_intrinsic_ssbo_atomic_xor
:
1925 case nir_intrinsic_ssbo_atomic_exchange
:
1928 case nir_intrinsic_ssbo_atomic_comp_swap
:
1935 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1939 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1940 return_type
== ctx
->ac
.i64
) {
1941 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1942 get_src(ctx
, instr
->src
[1]),
1943 get_src(ctx
, instr
->src
[2]),
1944 get_src(ctx
, instr
->src
[3]));
1946 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1947 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1949 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1950 params
[arg_count
++] = descriptor
;
1952 if (LLVM_VERSION_MAJOR
>= 9) {
1953 /* XXX: The new raw/struct atomic intrinsics are buggy with
1954 * LLVM 8, see r358579.
1956 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1957 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1958 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1960 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1961 snprintf(name
, sizeof(name
),
1962 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1964 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1965 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1966 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1968 assert(return_type
== ctx
->ac
.i32
);
1969 snprintf(name
, sizeof(name
),
1970 "llvm.amdgcn.buffer.atomic.%s", op
);
1973 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1977 result
= exit_waterfall(ctx
, &wctx
, result
);
1978 if (ctx
->ac
.postponed_kill
)
1979 ac_build_endif(&ctx
->ac
, 7001);
1983 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1984 nir_intrinsic_instr
*instr
)
1986 struct waterfall_context wctx
;
1987 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1989 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1990 int num_components
= instr
->num_components
;
1991 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1992 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1994 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1995 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
1996 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1998 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1999 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
2001 LLVMValueRef results
[4];
2002 for (int i
= 0; i
< num_components
;) {
2003 int num_elems
= num_components
- i
;
2004 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
2006 if (num_elems
* elem_size_bytes
> 16)
2007 num_elems
= 16 / elem_size_bytes
;
2008 int load_bytes
= num_elems
* elem_size_bytes
;
2010 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
2014 if (load_bytes
== 1) {
2015 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
2021 } else if (load_bytes
== 2) {
2022 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2029 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2030 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2032 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2033 vindex
, offset
, immoffset
, 0,
2034 cache_policy
, can_speculate
, false);
2037 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2038 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2039 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2041 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2042 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2044 for (unsigned j
= 0; j
< num_elems
; j
++) {
2045 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2050 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2051 return exit_waterfall(ctx
, &wctx
, ret
);
2054 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2055 struct waterfall_context
*wctx
,
2056 const nir_intrinsic_instr
*instr
)
2058 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2059 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2062 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2063 nir_intrinsic_instr
*instr
)
2065 struct waterfall_context wctx
;
2066 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2069 LLVMValueRef rsrc
= rsrc_base
;
2070 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2071 int num_components
= instr
->num_components
;
2073 if (ctx
->abi
->load_ubo
)
2074 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2076 if (instr
->dest
.ssa
.bit_size
== 64)
2077 num_components
*= 2;
2079 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2080 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2081 LLVMValueRef results
[num_components
];
2082 for (unsigned i
= 0; i
< num_components
; ++i
) {
2083 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2086 if (load_bytes
== 1) {
2087 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2094 assert(load_bytes
== 2);
2095 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2103 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2105 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2106 NULL
, 0, 0, true, true);
2108 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2111 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2112 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2114 return exit_waterfall(ctx
, &wctx
, ret
);
2118 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2119 bool vs_in
, unsigned *vertex_index_out
,
2120 LLVMValueRef
*vertex_index_ref
,
2121 unsigned *const_out
, LLVMValueRef
*indir_out
)
2123 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2124 nir_deref_path path
;
2125 unsigned idx_lvl
= 1;
2127 nir_deref_path_init(&path
, instr
, NULL
);
2129 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2130 if (vertex_index_ref
) {
2131 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2132 if (vertex_index_out
)
2133 *vertex_index_out
= 0;
2135 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2140 uint32_t const_offset
= 0;
2141 LLVMValueRef offset
= NULL
;
2143 if (var
->data
.compact
) {
2144 assert(instr
->deref_type
== nir_deref_type_array
);
2145 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2149 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2150 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2151 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2152 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2154 for (unsigned i
= 0; i
< index
; i
++) {
2155 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2156 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2158 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2159 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2160 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2161 const_offset
+= size
*
2162 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2164 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2165 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2167 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2172 unreachable("Uhandled deref type in get_deref_instr_offset");
2176 nir_deref_path_finish(&path
);
2178 if (const_offset
&& offset
)
2179 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2180 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2183 *const_out
= const_offset
;
2184 *indir_out
= offset
;
2187 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2188 nir_intrinsic_instr
*instr
,
2191 LLVMValueRef result
;
2192 LLVMValueRef vertex_index
= NULL
;
2193 LLVMValueRef indir_index
= NULL
;
2194 unsigned const_index
= 0;
2196 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2198 unsigned location
= var
->data
.location
;
2199 unsigned driver_location
= var
->data
.driver_location
;
2200 const bool is_patch
= var
->data
.patch
||
2201 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2202 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2203 const bool is_compact
= var
->data
.compact
;
2205 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2206 false, NULL
, is_patch
? NULL
: &vertex_index
,
2207 &const_index
, &indir_index
);
2209 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2211 LLVMTypeRef src_component_type
;
2212 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2213 src_component_type
= LLVMGetElementType(dest_type
);
2215 src_component_type
= dest_type
;
2217 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2218 vertex_index
, indir_index
,
2219 const_index
, location
, driver_location
,
2220 var
->data
.location_frac
,
2221 instr
->num_components
,
2222 is_patch
, is_compact
, load_inputs
);
2223 if (instr
->dest
.ssa
.bit_size
== 16) {
2224 result
= ac_to_integer(&ctx
->ac
, result
);
2225 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2227 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2231 type_scalar_size_bytes(const struct glsl_type
*type
)
2233 assert(glsl_type_is_vector_or_scalar(type
) ||
2234 glsl_type_is_matrix(type
));
2235 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2238 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2239 nir_intrinsic_instr
*instr
)
2241 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2242 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2244 LLVMValueRef values
[8];
2246 int ve
= instr
->dest
.ssa
.num_components
;
2248 LLVMValueRef indir_index
;
2250 unsigned const_index
;
2251 unsigned stride
= 4;
2252 int mode
= deref
->mode
;
2255 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2256 var
->data
.mode
== nir_var_shader_in
;
2257 idx
= var
->data
.driver_location
;
2258 comp
= var
->data
.location_frac
;
2259 mode
= var
->data
.mode
;
2261 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2262 &const_index
, &indir_index
);
2264 if (var
->data
.compact
) {
2266 const_index
+= comp
;
2271 if (instr
->dest
.ssa
.bit_size
== 64 &&
2272 (deref
->mode
== nir_var_shader_in
||
2273 deref
->mode
== nir_var_shader_out
||
2274 deref
->mode
== nir_var_function_temp
))
2278 case nir_var_shader_in
:
2279 /* TODO: remove this after RADV switches to lowered IO */
2280 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2281 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2282 return load_tess_varyings(ctx
, instr
, true);
2285 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2286 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2287 LLVMValueRef indir_index
;
2288 unsigned const_index
, vertex_index
;
2289 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2290 &const_index
, &indir_index
);
2291 assert(indir_index
== NULL
);
2293 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2294 var
->data
.driver_location
,
2295 var
->data
.location_frac
,
2296 instr
->num_components
, vertex_index
, const_index
, type
);
2299 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2301 unsigned count
= glsl_count_attribute_slots(
2303 ctx
->stage
== MESA_SHADER_VERTEX
);
2305 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2306 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2307 stride
, false, true);
2309 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2313 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2316 case nir_var_function_temp
:
2317 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2319 unsigned count
= glsl_count_attribute_slots(
2322 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2323 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2324 stride
, true, true);
2326 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2330 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2334 case nir_var_shader_out
:
2335 /* TODO: remove this after RADV switches to lowered IO */
2336 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2337 return load_tess_varyings(ctx
, instr
, false);
2340 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2341 var
->data
.fb_fetch_output
&&
2342 ctx
->abi
->emit_fbfetch
)
2343 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2345 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2347 unsigned count
= glsl_count_attribute_slots(
2350 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2351 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2352 stride
, true, true);
2354 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2358 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2359 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2364 case nir_var_mem_global
: {
2365 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2366 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2367 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2368 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2369 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2370 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2371 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2373 if (stride
!= natural_stride
|| split_loads
) {
2374 if (LLVMGetTypeKind(result_type
) == LLVMVectorTypeKind
)
2375 result_type
= LLVMGetElementType(result_type
);
2377 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2378 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2379 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2381 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2382 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2383 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2384 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2386 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2387 LLVMSetOrdering(values
[i
], LLVMAtomicOrderingMonotonic
);
2389 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2391 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2392 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2393 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2394 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2396 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2397 LLVMSetOrdering(val
, LLVMAtomicOrderingMonotonic
);
2402 unreachable("unhandle variable mode");
2404 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2405 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2409 visit_store_var(struct ac_nir_context
*ctx
,
2410 nir_intrinsic_instr
*instr
)
2412 if (ctx
->ac
.postponed_kill
) {
2413 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2414 ctx
->ac
.postponed_kill
, "");
2415 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2418 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2419 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2421 LLVMValueRef temp_ptr
, value
;
2424 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2425 int writemask
= instr
->const_index
[0];
2426 LLVMValueRef indir_index
;
2427 unsigned const_index
;
2430 get_deref_offset(ctx
, deref
, false,
2431 NULL
, NULL
, &const_index
, &indir_index
);
2432 idx
= var
->data
.driver_location
;
2433 comp
= var
->data
.location_frac
;
2435 if (var
->data
.compact
) {
2436 const_index
+= comp
;
2441 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2442 (deref
->mode
== nir_var_shader_out
||
2443 deref
->mode
== nir_var_function_temp
)) {
2445 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2446 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2449 writemask
= widen_mask(writemask
, 2);
2452 writemask
= writemask
<< comp
;
2454 switch (deref
->mode
) {
2455 case nir_var_shader_out
:
2456 /* TODO: remove this after RADV switches to lowered IO */
2457 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2458 LLVMValueRef vertex_index
= NULL
;
2459 LLVMValueRef indir_index
= NULL
;
2460 unsigned const_index
= 0;
2461 const bool is_patch
= var
->data
.patch
||
2462 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2463 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2465 get_deref_offset(ctx
, deref
, false, NULL
,
2466 is_patch
? NULL
: &vertex_index
,
2467 &const_index
, &indir_index
);
2469 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2470 vertex_index
, indir_index
,
2471 const_index
, src
, writemask
,
2472 var
->data
.location_frac
,
2473 var
->data
.driver_location
);
2477 for (unsigned chan
= 0; chan
< 8; chan
++) {
2479 if (!(writemask
& (1 << chan
)))
2482 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2484 if (var
->data
.compact
)
2487 unsigned count
= glsl_count_attribute_slots(
2490 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2491 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2492 stride
, true, true);
2494 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2495 value
, indir_index
, "");
2496 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2497 count
, stride
, tmp_vec
);
2500 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2502 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2506 case nir_var_function_temp
:
2507 for (unsigned chan
= 0; chan
< 8; chan
++) {
2508 if (!(writemask
& (1 << chan
)))
2511 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2513 unsigned count
= glsl_count_attribute_slots(
2516 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2517 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2520 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2521 value
, indir_index
, "");
2522 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2525 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2527 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2532 case nir_var_mem_global
: {
2533 int writemask
= instr
->const_index
[0];
2534 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2535 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2537 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2538 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2539 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2540 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2541 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2543 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2544 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2545 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2547 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2548 stride
== natural_stride
&& !split_stores
) {
2549 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2550 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2551 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2553 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2554 LLVMGetElementType(LLVMTypeOf(address
)), "");
2555 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2557 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2558 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2560 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2561 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMVectorTypeKind
)
2562 val_type
= LLVMGetElementType(val_type
);
2564 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2565 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2566 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2567 for (unsigned chan
= 0; chan
< 4; chan
++) {
2568 if (!(writemask
& (1 << chan
)))
2571 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2573 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2574 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2576 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2577 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2578 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2580 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2581 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2591 if (ctx
->ac
.postponed_kill
)
2592 ac_build_endif(&ctx
->ac
, 7002);
2596 visit_store_output(struct ac_nir_context
*ctx
, nir_intrinsic_instr
*instr
)
2598 if (ctx
->ac
.postponed_kill
) {
2599 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2600 ctx
->ac
.postponed_kill
, "");
2601 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2604 unsigned base
= nir_intrinsic_base(instr
);
2605 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2606 unsigned component
= nir_intrinsic_component(instr
);
2607 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2608 nir_src offset
= *nir_get_io_offset_src(instr
);
2609 LLVMValueRef indir_index
= NULL
;
2611 if (nir_src_is_const(offset
))
2612 assert(nir_src_as_uint(offset
) == 0);
2614 indir_index
= get_src(ctx
, offset
);
2616 switch (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
))) {
2620 writemask
= widen_mask(writemask
, 2);
2621 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2622 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2626 unreachable("unhandled store_output bit size");
2630 writemask
<<= component
;
2632 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2633 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
2634 LLVMValueRef vertex_index
=
2635 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
2637 ctx
->abi
->store_tcs_outputs(ctx
->abi
, NULL
,
2638 vertex_index
, indir_index
,
2640 component
, base
* 4);
2644 /* No indirect indexing is allowed after this point. */
2645 assert(!indir_index
);
2647 for (unsigned chan
= 0; chan
< 8; chan
++) {
2648 if (!(writemask
& (1 << chan
)))
2651 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2652 LLVMBuildStore(ctx
->ac
.builder
, value
,
2653 ctx
->abi
->outputs
[base
* 4 + chan
]);
2656 if (ctx
->ac
.postponed_kill
)
2657 ac_build_endif(&ctx
->ac
, 7002);
2660 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2663 case GLSL_SAMPLER_DIM_BUF
:
2665 case GLSL_SAMPLER_DIM_1D
:
2666 return array
? 2 : 1;
2667 case GLSL_SAMPLER_DIM_2D
:
2668 return array
? 3 : 2;
2669 case GLSL_SAMPLER_DIM_MS
:
2670 return array
? 4 : 3;
2671 case GLSL_SAMPLER_DIM_3D
:
2672 case GLSL_SAMPLER_DIM_CUBE
:
2674 case GLSL_SAMPLER_DIM_RECT
:
2675 case GLSL_SAMPLER_DIM_SUBPASS
:
2677 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2685 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2686 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2687 LLVMValueRef coord_z
,
2688 LLVMValueRef sample_index
,
2689 LLVMValueRef fmask_desc_ptr
)
2691 unsigned sample_chan
= coord_z
? 3 : 2;
2692 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2693 addr
[sample_chan
] = sample_index
;
2695 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2696 return addr
[sample_chan
];
2699 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2701 assert(instr
->src
[0].is_ssa
);
2702 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2705 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2706 const nir_intrinsic_instr
*instr
,
2707 LLVMValueRef dynamic_index
,
2708 enum ac_descriptor_type desc_type
,
2711 nir_deref_instr
*deref_instr
=
2712 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2713 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2715 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2718 static void get_image_coords(struct ac_nir_context
*ctx
,
2719 const nir_intrinsic_instr
*instr
,
2720 LLVMValueRef dynamic_desc_index
,
2721 struct ac_image_args
*args
,
2722 enum glsl_sampler_dim dim
,
2725 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2726 LLVMValueRef masks
[] = {
2727 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2728 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2730 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2733 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2734 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2735 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2736 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2737 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2738 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2739 count
= image_type_to_components_count(dim
, is_array
);
2741 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2742 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2743 LLVMValueRef fmask_load_address
[3];
2745 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2746 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2748 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2750 fmask_load_address
[2] = NULL
;
2752 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2753 fmask_load_address
[0],
2754 fmask_load_address
[1],
2755 fmask_load_address
[2],
2757 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2758 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2760 if (count
== 1 && !gfx9_1d
) {
2761 if (instr
->src
[1].ssa
->num_components
)
2762 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2764 args
->coords
[0] = src0
;
2769 for (chan
= 0; chan
< count
; ++chan
) {
2770 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2775 args
->coords
[2] = args
->coords
[1];
2776 args
->coords
[1] = ctx
->ac
.i32_0
;
2778 args
->coords
[1] = ctx
->ac
.i32_0
;
2781 if (ctx
->ac
.chip_class
== GFX9
&&
2782 dim
== GLSL_SAMPLER_DIM_2D
&&
2784 /* The hw can't bind a slice of a 3D image as a 2D
2785 * image, because it ignores BASE_ARRAY if the target
2786 * is 3D. The workaround is to read BASE_ARRAY and set
2787 * it as the 3rd address operand for all 2D images.
2789 LLVMValueRef first_layer
, const5
, mask
;
2791 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2792 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2793 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2794 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2796 args
->coords
[count
] = first_layer
;
2802 args
->coords
[count
] = sample_index
;
2808 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2809 const nir_intrinsic_instr
*instr
,
2810 LLVMValueRef dynamic_index
,
2811 bool write
, bool atomic
)
2813 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2814 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2815 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2816 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2817 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2819 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2820 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2821 elem_count
, stride
, "");
2823 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2824 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2829 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2830 struct waterfall_context
*wctx
,
2831 const nir_intrinsic_instr
*instr
)
2833 nir_deref_instr
*deref_instr
= NULL
;
2835 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2836 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2838 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2839 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2842 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2843 const nir_intrinsic_instr
*instr
,
2848 enum glsl_sampler_dim dim
;
2849 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2852 dim
= nir_intrinsic_image_dim(instr
);
2853 is_array
= nir_intrinsic_image_array(instr
);
2855 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2856 const struct glsl_type
*type
= image_deref
->type
;
2857 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2858 dim
= glsl_get_sampler_dim(type
);
2859 access
|= var
->data
.access
;
2860 is_array
= glsl_sampler_type_is_array(type
);
2863 struct waterfall_context wctx
;
2864 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2866 struct ac_image_args args
= {};
2868 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2870 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2871 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2872 unsigned num_channels
= util_last_bit(mask
);
2873 LLVMValueRef rsrc
, vindex
;
2875 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2876 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2879 assert(instr
->dest
.is_ssa
);
2880 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2881 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2882 ctx
->ac
.i32_0
, num_channels
,
2885 instr
->dest
.ssa
.bit_size
== 16);
2886 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2888 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2889 res
= ac_to_integer(&ctx
->ac
, res
);
2891 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2893 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2894 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2895 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2896 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2898 args
.lod
= get_src(ctx
, instr
->src
[3]);
2900 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2902 assert(instr
->dest
.is_ssa
);
2903 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
2905 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2907 return exit_waterfall(ctx
, &wctx
, res
);
2910 static void visit_image_store(struct ac_nir_context
*ctx
,
2911 const nir_intrinsic_instr
*instr
,
2914 if (ctx
->ac
.postponed_kill
) {
2915 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2916 ctx
->ac
.postponed_kill
, "");
2917 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2920 enum glsl_sampler_dim dim
;
2921 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2925 dim
= nir_intrinsic_image_dim(instr
);
2926 is_array
= nir_intrinsic_image_array(instr
);
2928 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2929 const struct glsl_type
*type
= image_deref
->type
;
2930 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2931 dim
= glsl_get_sampler_dim(type
);
2932 access
|= var
->data
.access
;
2933 is_array
= glsl_sampler_type_is_array(type
);
2936 struct waterfall_context wctx
;
2937 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2939 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2940 struct ac_image_args args
= {};
2942 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2944 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2945 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2946 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2947 unsigned src_channels
= ac_get_llvm_num_components(src
);
2948 LLVMValueRef vindex
;
2950 if (src_channels
== 3)
2951 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2953 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2954 get_src(ctx
, instr
->src
[1]),
2957 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2958 ctx
->ac
.i32_0
, args
.cache_policy
);
2960 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2962 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2963 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2964 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2965 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2966 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2968 args
.lod
= get_src(ctx
, instr
->src
[4]);
2970 args
.d16
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(args
.data
[0])) == 16;
2972 ac_build_image_opcode(&ctx
->ac
, &args
);
2975 exit_waterfall(ctx
, &wctx
, NULL
);
2976 if (ctx
->ac
.postponed_kill
)
2977 ac_build_endif(&ctx
->ac
, 7003);
2980 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2981 const nir_intrinsic_instr
*instr
,
2984 if (ctx
->ac
.postponed_kill
) {
2985 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2986 ctx
->ac
.postponed_kill
, "");
2987 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
2990 LLVMValueRef params
[7];
2991 int param_count
= 0;
2993 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2994 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2995 const char *atomic_name
;
2996 char intrinsic_name
[64];
2997 enum ac_atomic_op atomic_subop
;
2998 ASSERTED
int length
;
3000 enum glsl_sampler_dim dim
;
3003 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
3004 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
3005 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
3006 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
3007 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
3008 assert(format
== GL_R32UI
|| format
== GL_R32I
);
3010 dim
= nir_intrinsic_image_dim(instr
);
3011 is_array
= nir_intrinsic_image_array(instr
);
3013 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3014 dim
= glsl_get_sampler_dim(type
);
3015 is_array
= glsl_sampler_type_is_array(type
);
3018 struct waterfall_context wctx
;
3019 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3021 switch (instr
->intrinsic
) {
3022 case nir_intrinsic_bindless_image_atomic_add
:
3023 case nir_intrinsic_image_deref_atomic_add
:
3024 atomic_name
= "add";
3025 atomic_subop
= ac_atomic_add
;
3027 case nir_intrinsic_bindless_image_atomic_imin
:
3028 case nir_intrinsic_image_deref_atomic_imin
:
3029 atomic_name
= "smin";
3030 atomic_subop
= ac_atomic_smin
;
3032 case nir_intrinsic_bindless_image_atomic_umin
:
3033 case nir_intrinsic_image_deref_atomic_umin
:
3034 atomic_name
= "umin";
3035 atomic_subop
= ac_atomic_umin
;
3037 case nir_intrinsic_bindless_image_atomic_imax
:
3038 case nir_intrinsic_image_deref_atomic_imax
:
3039 atomic_name
= "smax";
3040 atomic_subop
= ac_atomic_smax
;
3042 case nir_intrinsic_bindless_image_atomic_umax
:
3043 case nir_intrinsic_image_deref_atomic_umax
:
3044 atomic_name
= "umax";
3045 atomic_subop
= ac_atomic_umax
;
3047 case nir_intrinsic_bindless_image_atomic_and
:
3048 case nir_intrinsic_image_deref_atomic_and
:
3049 atomic_name
= "and";
3050 atomic_subop
= ac_atomic_and
;
3052 case nir_intrinsic_bindless_image_atomic_or
:
3053 case nir_intrinsic_image_deref_atomic_or
:
3055 atomic_subop
= ac_atomic_or
;
3057 case nir_intrinsic_bindless_image_atomic_xor
:
3058 case nir_intrinsic_image_deref_atomic_xor
:
3059 atomic_name
= "xor";
3060 atomic_subop
= ac_atomic_xor
;
3062 case nir_intrinsic_bindless_image_atomic_exchange
:
3063 case nir_intrinsic_image_deref_atomic_exchange
:
3064 atomic_name
= "swap";
3065 atomic_subop
= ac_atomic_swap
;
3067 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3068 case nir_intrinsic_image_deref_atomic_comp_swap
:
3069 atomic_name
= "cmpswap";
3070 atomic_subop
= 0; /* not used */
3072 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3073 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
3074 atomic_name
= "inc";
3075 atomic_subop
= ac_atomic_inc_wrap
;
3078 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3079 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3080 atomic_name
= "dec";
3081 atomic_subop
= ac_atomic_dec_wrap
;
3088 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
3089 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3091 LLVMValueRef result
;
3092 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3093 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
3094 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
3095 ctx
->ac
.i32_0
, ""); /* vindex */
3096 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3097 if (LLVM_VERSION_MAJOR
>= 9) {
3098 /* XXX: The new raw/struct atomic intrinsics are buggy
3099 * with LLVM 8, see r358579.
3101 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3102 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3104 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3105 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3107 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3109 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3110 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3113 assert(length
< sizeof(intrinsic_name
));
3114 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3115 params
, param_count
, 0);
3117 struct ac_image_args args
= {};
3118 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3119 args
.atomic
= atomic_subop
;
3120 args
.data
[0] = params
[0];
3122 args
.data
[1] = params
[1];
3123 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3124 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3125 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3127 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3130 result
= exit_waterfall(ctx
, &wctx
, result
);
3131 if (ctx
->ac
.postponed_kill
)
3132 ac_build_endif(&ctx
->ac
, 7004);
3136 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3137 nir_intrinsic_instr
*instr
)
3139 struct waterfall_context wctx
;
3140 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3141 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3143 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3145 return exit_waterfall(ctx
, &wctx
, ret
);
3148 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3149 const nir_intrinsic_instr
*instr
,
3154 enum glsl_sampler_dim dim
;
3157 dim
= nir_intrinsic_image_dim(instr
);
3158 is_array
= nir_intrinsic_image_array(instr
);
3160 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3161 dim
= glsl_get_sampler_dim(type
);
3162 is_array
= glsl_sampler_type_is_array(type
);
3165 struct waterfall_context wctx
;
3166 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3168 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3169 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3172 struct ac_image_args args
= { 0 };
3174 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3176 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3177 args
.opcode
= ac_image_get_resinfo
;
3178 assert(nir_src_as_uint(instr
->src
[1]) == 0);
3179 args
.lod
= ctx
->ac
.i32_0
;
3180 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3182 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3184 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3186 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3187 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3188 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3189 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3190 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3193 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3194 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3195 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3199 return exit_waterfall(ctx
, &wctx
, res
);
3202 static void emit_membar(struct ac_llvm_context
*ac
,
3203 const nir_intrinsic_instr
*instr
)
3205 unsigned wait_flags
= 0;
3207 switch (instr
->intrinsic
) {
3208 case nir_intrinsic_memory_barrier
:
3209 case nir_intrinsic_group_memory_barrier
:
3210 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3212 case nir_intrinsic_memory_barrier_buffer
:
3213 case nir_intrinsic_memory_barrier_image
:
3214 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3216 case nir_intrinsic_memory_barrier_shared
:
3217 wait_flags
= AC_WAIT_LGKM
;
3223 ac_build_waitcnt(ac
, wait_flags
);
3226 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3228 /* GFX6 only (thanks to a hw bug workaround):
3229 * The real barrier instruction isn’t needed, because an entire patch
3230 * always fits into a single wave.
3232 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3233 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3236 ac_build_s_barrier(ac
);
3239 static void emit_discard(struct ac_nir_context
*ctx
,
3240 const nir_intrinsic_instr
*instr
)
3244 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3245 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3246 get_src(ctx
, instr
->src
[0]),
3249 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3250 cond
= ctx
->ac
.i1false
;
3253 ac_build_kill_if_false(&ctx
->ac
, cond
);
3256 static void emit_demote(struct ac_nir_context
*ctx
,
3257 const nir_intrinsic_instr
*instr
)
3261 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3262 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3263 get_src(ctx
, instr
->src
[0]),
3266 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3267 cond
= ctx
->ac
.i1false
;
3270 /* Kill immediately while maintaining WQM. */
3271 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3273 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3274 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3275 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3280 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3282 LLVMValueRef result
;
3283 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3284 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3285 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3286 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3288 if (ctx
->ac
.wave_size
== 32)
3289 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3290 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3292 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3296 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3298 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3299 LLVMValueRef result
;
3300 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3301 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3302 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3303 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3305 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3310 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3312 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3313 return LLVMBuildAnd(ctx
->ac
.builder
,
3314 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3315 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3317 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3322 visit_first_invocation(struct ac_nir_context
*ctx
)
3324 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3325 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3327 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3328 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3329 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3330 ctx
->ac
.iN_wavemask
, args
, 2,
3331 AC_FUNC_ATTR_NOUNWIND
|
3332 AC_FUNC_ATTR_READNONE
);
3334 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3338 visit_load_shared(struct ac_nir_context
*ctx
,
3339 const nir_intrinsic_instr
*instr
)
3341 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3343 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3344 instr
->dest
.ssa
.bit_size
);
3346 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3347 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3348 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3349 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3352 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3353 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3357 visit_store_shared(struct ac_nir_context
*ctx
,
3358 const nir_intrinsic_instr
*instr
)
3360 LLVMValueRef derived_ptr
, data
,index
;
3361 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3363 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3364 instr
->src
[0].ssa
->bit_size
);
3365 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3367 int writemask
= nir_intrinsic_write_mask(instr
);
3368 for (int chan
= 0; chan
< 4; chan
++) {
3369 if (!(writemask
& (1 << chan
))) {
3372 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3373 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3374 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3375 LLVMBuildStore(builder
, data
, derived_ptr
);
3379 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3380 const nir_intrinsic_instr
*instr
,
3381 LLVMValueRef ptr
, int src_idx
)
3383 if (ctx
->ac
.postponed_kill
) {
3384 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3385 ctx
->ac
.postponed_kill
, "");
3386 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3389 LLVMValueRef result
;
3390 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3392 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3394 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3395 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3396 if (deref
->mode
== nir_var_mem_global
) {
3397 /* use "singlethread" sync scope to implement relaxed ordering */
3398 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3400 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3401 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3405 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3406 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3407 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3408 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3409 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3411 LLVMAtomicRMWBinOp op
;
3412 switch (instr
->intrinsic
) {
3413 case nir_intrinsic_shared_atomic_add
:
3414 case nir_intrinsic_deref_atomic_add
:
3415 op
= LLVMAtomicRMWBinOpAdd
;
3417 case nir_intrinsic_shared_atomic_umin
:
3418 case nir_intrinsic_deref_atomic_umin
:
3419 op
= LLVMAtomicRMWBinOpUMin
;
3421 case nir_intrinsic_shared_atomic_umax
:
3422 case nir_intrinsic_deref_atomic_umax
:
3423 op
= LLVMAtomicRMWBinOpUMax
;
3425 case nir_intrinsic_shared_atomic_imin
:
3426 case nir_intrinsic_deref_atomic_imin
:
3427 op
= LLVMAtomicRMWBinOpMin
;
3429 case nir_intrinsic_shared_atomic_imax
:
3430 case nir_intrinsic_deref_atomic_imax
:
3431 op
= LLVMAtomicRMWBinOpMax
;
3433 case nir_intrinsic_shared_atomic_and
:
3434 case nir_intrinsic_deref_atomic_and
:
3435 op
= LLVMAtomicRMWBinOpAnd
;
3437 case nir_intrinsic_shared_atomic_or
:
3438 case nir_intrinsic_deref_atomic_or
:
3439 op
= LLVMAtomicRMWBinOpOr
;
3441 case nir_intrinsic_shared_atomic_xor
:
3442 case nir_intrinsic_deref_atomic_xor
:
3443 op
= LLVMAtomicRMWBinOpXor
;
3445 case nir_intrinsic_shared_atomic_exchange
:
3446 case nir_intrinsic_deref_atomic_exchange
:
3447 op
= LLVMAtomicRMWBinOpXchg
;
3449 #if LLVM_VERSION_MAJOR >= 10
3450 case nir_intrinsic_shared_atomic_fadd
:
3451 case nir_intrinsic_deref_atomic_fadd
:
3452 op
= LLVMAtomicRMWBinOpFAdd
;
3461 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_fadd
||
3462 instr
->intrinsic
== nir_intrinsic_deref_atomic_fadd
) {
3463 val
= ac_to_float(&ctx
->ac
, src
);
3465 val
= ac_to_integer(&ctx
->ac
, src
);
3468 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, val
, sync_scope
);
3471 if (ctx
->ac
.postponed_kill
)
3472 ac_build_endif(&ctx
->ac
, 7005);
3476 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3478 LLVMValueRef values
[2];
3479 LLVMValueRef pos
[2];
3481 pos
[0] = ac_to_float(&ctx
->ac
,
3482 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3483 pos
[1] = ac_to_float(&ctx
->ac
,
3484 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3486 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3487 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3488 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3491 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3492 enum glsl_interp_mode interp
, unsigned location
)
3495 case INTERP_MODE_FLAT
:
3498 case INTERP_MODE_SMOOTH
:
3499 case INTERP_MODE_NONE
:
3500 if (location
== INTERP_CENTER
)
3501 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3502 else if (location
== INTERP_CENTROID
)
3503 return ctx
->abi
->persp_centroid
;
3504 else if (location
== INTERP_SAMPLE
)
3505 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3507 case INTERP_MODE_NOPERSPECTIVE
:
3508 if (location
== INTERP_CENTER
)
3509 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3510 else if (location
== INTERP_CENTROID
)
3511 return ctx
->abi
->linear_centroid
;
3512 else if (location
== INTERP_SAMPLE
)
3513 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3519 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3522 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3523 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3526 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3528 LLVMValueRef offset
)
3530 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3531 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3532 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3534 LLVMValueRef ij_out
[2];
3535 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3538 * take the I then J parameters, and the DDX/Y for it, and
3539 * calculate the IJ inputs for the interpolator.
3540 * temp1 = ddx * offset/sample.x + I;
3541 * interp_param.I = ddy * offset/sample.y + temp1;
3542 * temp1 = ddx * offset/sample.x + J;
3543 * interp_param.J = ddy * offset/sample.y + temp1;
3545 for (unsigned i
= 0; i
< 2; i
++) {
3546 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3547 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3548 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3549 ddxy_out
, ix_ll
, "");
3550 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3551 ddxy_out
, iy_ll
, "");
3552 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3553 interp_param
, ix_ll
, "");
3554 LLVMValueRef temp1
, temp2
;
3556 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3559 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3560 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3562 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3563 temp2
, ctx
->ac
.i32
, "");
3565 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3566 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3569 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3572 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3573 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3576 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3578 LLVMValueRef sample_id
)
3580 if (ctx
->abi
->interp_at_sample_force_center
)
3581 return barycentric_center(ctx
, mode
);
3583 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3585 /* fetch sample ID */
3586 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3588 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3589 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3590 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3591 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3592 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3593 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3595 return barycentric_offset(ctx
, mode
, offset
);
3599 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3602 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3603 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3606 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3608 return LLVMBuildBitCast(ctx
->ac
.builder
,
3609 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3613 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3614 LLVMValueRef interp_param
,
3615 unsigned index
, unsigned comp_start
,
3616 unsigned num_components
,
3619 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3620 LLVMValueRef interp_param_f
;
3622 interp_param_f
= LLVMBuildBitCast(ctx
->ac
.builder
,
3623 interp_param
, ctx
->ac
.v2f32
, "");
3624 LLVMValueRef i
= LLVMBuildExtractElement(
3625 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_0
, "");
3626 LLVMValueRef j
= LLVMBuildExtractElement(
3627 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_1
, "");
3629 /* Workaround for issue 2647: kill threads with infinite interpolation coeffs */
3630 if (ctx
->verified_interp
&&
3631 !_mesa_hash_table_search(ctx
->verified_interp
, interp_param
)) {
3632 LLVMValueRef args
[2];
3634 args
[1] = LLVMConstInt(ctx
->ac
.i32
, S_NAN
| Q_NAN
| N_INFINITY
| P_INFINITY
, false);
3635 LLVMValueRef cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f32", ctx
->ac
.i1
,
3636 args
, 2, AC_FUNC_ATTR_READNONE
);
3637 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildNot(ctx
->ac
.builder
, cond
, ""));
3638 _mesa_hash_table_insert(ctx
->verified_interp
, interp_param
, interp_param
);
3641 LLVMValueRef values
[4];
3642 assert(bitsize
== 16 || bitsize
== 32);
3643 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3644 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3645 if (bitsize
== 16) {
3646 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3647 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3649 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3650 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3654 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3657 static LLVMValueRef
visit_load(struct ac_nir_context
*ctx
,
3658 nir_intrinsic_instr
*instr
, bool is_output
)
3660 LLVMValueRef values
[8];
3661 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3662 LLVMTypeRef component_type
;
3663 unsigned base
= nir_intrinsic_base(instr
);
3664 unsigned component
= nir_intrinsic_component(instr
);
3665 unsigned count
= instr
->dest
.ssa
.num_components
*
3666 (instr
->dest
.ssa
.bit_size
== 64 ? 2 : 1);
3667 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3668 LLVMValueRef vertex_index
=
3669 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
3670 nir_src offset
= *nir_get_io_offset_src(instr
);
3671 LLVMValueRef indir_index
= NULL
;
3673 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
3674 component_type
= LLVMGetElementType(dest_type
);
3676 component_type
= dest_type
;
3678 if (nir_src_is_const(offset
))
3679 assert(nir_src_as_uint(offset
) == 0);
3681 indir_index
= get_src(ctx
, offset
);
3683 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3684 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& !is_output
)) {
3685 LLVMValueRef result
=
3686 ctx
->abi
->load_tess_varyings(ctx
->abi
, component_type
,
3687 vertex_index
, indir_index
,
3690 instr
->num_components
,
3691 false, false, !is_output
);
3692 if (instr
->dest
.ssa
.bit_size
== 16) {
3693 result
= ac_to_integer(&ctx
->ac
, result
);
3694 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
3696 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3699 /* No indirect indexing is allowed after this point. */
3700 assert(!indir_index
);
3702 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3703 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3704 assert(nir_src_is_const(*vertex_index_src
));
3706 return ctx
->abi
->load_inputs(ctx
->abi
, 0, base
* 4, component
,
3707 instr
->num_components
,
3708 nir_src_as_uint(*vertex_index_src
),
3712 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& is_output
&&
3713 nir_intrinsic_io_semantics(instr
).fb_fetch_output
)
3714 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
3716 /* Other non-fragment cases have inputs and outputs in temporaries. */
3717 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
3718 for (unsigned chan
= component
; chan
< count
+ component
; chan
++) {
3720 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3721 ctx
->abi
->outputs
[base
* 4 + chan
], "");
3723 values
[chan
] = ctx
->abi
->inputs
[base
* 4 + chan
];
3725 values
[chan
] = LLVMGetUndef(ctx
->ac
.i32
);
3728 LLVMValueRef result
= ac_build_varying_gather_values(&ctx
->ac
, values
, count
, component
);
3729 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3732 /* Fragment shader inputs. */
3733 unsigned vertex_id
= 2; /* P0 */
3735 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3736 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3738 switch (src0
[0].i32
) {
3749 unreachable("Invalid vertex index");
3753 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
, false);
3755 for (unsigned chan
= 0; chan
< count
; chan
++) {
3756 if (component
+ chan
> 4)
3757 attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
+ 1, false);
3758 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3759 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3760 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3763 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3764 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3765 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3766 instr
->dest
.ssa
.bit_size
== 16 ? ctx
->ac
.i16
3770 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, count
);
3771 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3774 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3775 nir_intrinsic_instr
*instr
)
3777 LLVMValueRef result
= NULL
;
3779 switch (instr
->intrinsic
) {
3780 case nir_intrinsic_ballot
:
3781 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3782 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3783 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3785 case nir_intrinsic_read_invocation
:
3786 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3787 get_src(ctx
, instr
->src
[1]));
3789 case nir_intrinsic_read_first_invocation
:
3790 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3792 case nir_intrinsic_load_subgroup_invocation
:
3793 result
= ac_get_thread_id(&ctx
->ac
);
3795 case nir_intrinsic_load_work_group_id
: {
3796 LLVMValueRef values
[3];
3798 for (int i
= 0; i
< 3; i
++) {
3799 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3800 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3803 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3806 case nir_intrinsic_load_base_vertex
:
3807 case nir_intrinsic_load_first_vertex
:
3808 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3810 case nir_intrinsic_load_local_group_size
:
3811 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3813 case nir_intrinsic_load_vertex_id
:
3814 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3815 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3816 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3818 case nir_intrinsic_load_vertex_id_zero_base
: {
3819 result
= ctx
->abi
->vertex_id
;
3822 case nir_intrinsic_load_local_invocation_id
: {
3823 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3826 case nir_intrinsic_load_base_instance
:
3827 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3829 case nir_intrinsic_load_draw_id
:
3830 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3832 case nir_intrinsic_load_view_index
:
3833 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3835 case nir_intrinsic_load_invocation_id
:
3836 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3837 result
= ac_unpack_param(&ctx
->ac
,
3838 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3841 if (ctx
->ac
.chip_class
>= GFX10
) {
3842 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3843 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3844 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3846 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3850 case nir_intrinsic_load_primitive_id
:
3851 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3852 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3853 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3854 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3855 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3856 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3858 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3860 case nir_intrinsic_load_sample_id
:
3861 result
= ac_unpack_param(&ctx
->ac
,
3862 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3865 case nir_intrinsic_load_sample_pos
:
3866 result
= load_sample_pos(ctx
);
3868 case nir_intrinsic_load_sample_mask_in
:
3869 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3871 case nir_intrinsic_load_frag_coord
: {
3872 LLVMValueRef values
[4] = {
3873 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3874 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3875 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3876 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3877 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3879 result
= ac_to_integer(&ctx
->ac
,
3880 ac_build_gather_values(&ctx
->ac
, values
, 4));
3883 case nir_intrinsic_load_layer_id
:
3884 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3886 case nir_intrinsic_load_front_face
:
3887 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3889 case nir_intrinsic_load_helper_invocation
:
3890 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3892 case nir_intrinsic_is_helper_invocation
:
3893 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3895 case nir_intrinsic_load_color0
:
3896 result
= ctx
->abi
->color0
;
3898 case nir_intrinsic_load_color1
:
3899 result
= ctx
->abi
->color1
;
3901 case nir_intrinsic_load_user_data_amd
:
3902 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3903 result
= ctx
->abi
->user_data
;
3905 case nir_intrinsic_load_instance_id
:
3906 result
= ctx
->abi
->instance_id
;
3908 case nir_intrinsic_load_num_work_groups
:
3909 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3911 case nir_intrinsic_load_local_invocation_index
:
3912 result
= visit_load_local_invocation_index(ctx
);
3914 case nir_intrinsic_load_subgroup_id
:
3915 result
= visit_load_subgroup_id(ctx
);
3917 case nir_intrinsic_load_num_subgroups
:
3918 result
= visit_load_num_subgroups(ctx
);
3920 case nir_intrinsic_first_invocation
:
3921 result
= visit_first_invocation(ctx
);
3923 case nir_intrinsic_load_push_constant
:
3924 result
= visit_load_push_constant(ctx
, instr
);
3926 case nir_intrinsic_vulkan_resource_index
: {
3927 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3928 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3929 unsigned binding
= nir_intrinsic_binding(instr
);
3931 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3935 case nir_intrinsic_vulkan_resource_reindex
:
3936 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3938 case nir_intrinsic_store_ssbo
:
3939 visit_store_ssbo(ctx
, instr
);
3941 case nir_intrinsic_load_ssbo
:
3942 result
= visit_load_buffer(ctx
, instr
);
3944 case nir_intrinsic_ssbo_atomic_add
:
3945 case nir_intrinsic_ssbo_atomic_imin
:
3946 case nir_intrinsic_ssbo_atomic_umin
:
3947 case nir_intrinsic_ssbo_atomic_imax
:
3948 case nir_intrinsic_ssbo_atomic_umax
:
3949 case nir_intrinsic_ssbo_atomic_and
:
3950 case nir_intrinsic_ssbo_atomic_or
:
3951 case nir_intrinsic_ssbo_atomic_xor
:
3952 case nir_intrinsic_ssbo_atomic_exchange
:
3953 case nir_intrinsic_ssbo_atomic_comp_swap
:
3954 result
= visit_atomic_ssbo(ctx
, instr
);
3956 case nir_intrinsic_load_ubo
:
3957 result
= visit_load_ubo_buffer(ctx
, instr
);
3959 case nir_intrinsic_get_buffer_size
:
3960 result
= visit_get_buffer_size(ctx
, instr
);
3962 case nir_intrinsic_load_deref
:
3963 result
= visit_load_var(ctx
, instr
);
3965 case nir_intrinsic_store_deref
:
3966 visit_store_var(ctx
, instr
);
3968 case nir_intrinsic_load_input
:
3969 case nir_intrinsic_load_input_vertex
:
3970 case nir_intrinsic_load_per_vertex_input
:
3971 result
= visit_load(ctx
, instr
, false);
3973 case nir_intrinsic_load_output
:
3974 case nir_intrinsic_load_per_vertex_output
:
3975 result
= visit_load(ctx
, instr
, true);
3977 case nir_intrinsic_store_output
:
3978 case nir_intrinsic_store_per_vertex_output
:
3979 visit_store_output(ctx
, instr
);
3981 case nir_intrinsic_load_shared
:
3982 result
= visit_load_shared(ctx
, instr
);
3984 case nir_intrinsic_store_shared
:
3985 visit_store_shared(ctx
, instr
);
3987 case nir_intrinsic_bindless_image_samples
:
3988 case nir_intrinsic_image_deref_samples
:
3989 result
= visit_image_samples(ctx
, instr
);
3991 case nir_intrinsic_bindless_image_load
:
3992 result
= visit_image_load(ctx
, instr
, true);
3994 case nir_intrinsic_image_deref_load
:
3995 result
= visit_image_load(ctx
, instr
, false);
3997 case nir_intrinsic_bindless_image_store
:
3998 visit_image_store(ctx
, instr
, true);
4000 case nir_intrinsic_image_deref_store
:
4001 visit_image_store(ctx
, instr
, false);
4003 case nir_intrinsic_bindless_image_atomic_add
:
4004 case nir_intrinsic_bindless_image_atomic_imin
:
4005 case nir_intrinsic_bindless_image_atomic_umin
:
4006 case nir_intrinsic_bindless_image_atomic_imax
:
4007 case nir_intrinsic_bindless_image_atomic_umax
:
4008 case nir_intrinsic_bindless_image_atomic_and
:
4009 case nir_intrinsic_bindless_image_atomic_or
:
4010 case nir_intrinsic_bindless_image_atomic_xor
:
4011 case nir_intrinsic_bindless_image_atomic_exchange
:
4012 case nir_intrinsic_bindless_image_atomic_comp_swap
:
4013 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
4014 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
4015 result
= visit_image_atomic(ctx
, instr
, true);
4017 case nir_intrinsic_image_deref_atomic_add
:
4018 case nir_intrinsic_image_deref_atomic_imin
:
4019 case nir_intrinsic_image_deref_atomic_umin
:
4020 case nir_intrinsic_image_deref_atomic_imax
:
4021 case nir_intrinsic_image_deref_atomic_umax
:
4022 case nir_intrinsic_image_deref_atomic_and
:
4023 case nir_intrinsic_image_deref_atomic_or
:
4024 case nir_intrinsic_image_deref_atomic_xor
:
4025 case nir_intrinsic_image_deref_atomic_exchange
:
4026 case nir_intrinsic_image_deref_atomic_comp_swap
:
4027 case nir_intrinsic_image_deref_atomic_inc_wrap
:
4028 case nir_intrinsic_image_deref_atomic_dec_wrap
:
4029 result
= visit_image_atomic(ctx
, instr
, false);
4031 case nir_intrinsic_bindless_image_size
:
4032 result
= visit_image_size(ctx
, instr
, true);
4034 case nir_intrinsic_image_deref_size
:
4035 result
= visit_image_size(ctx
, instr
, false);
4037 case nir_intrinsic_shader_clock
:
4038 result
= ac_build_shader_clock(&ctx
->ac
,
4039 nir_intrinsic_memory_scope(instr
));
4041 case nir_intrinsic_discard
:
4042 case nir_intrinsic_discard_if
:
4043 emit_discard(ctx
, instr
);
4045 case nir_intrinsic_demote
:
4046 case nir_intrinsic_demote_if
:
4047 emit_demote(ctx
, instr
);
4049 case nir_intrinsic_memory_barrier
:
4050 case nir_intrinsic_group_memory_barrier
:
4051 case nir_intrinsic_memory_barrier_buffer
:
4052 case nir_intrinsic_memory_barrier_image
:
4053 case nir_intrinsic_memory_barrier_shared
:
4054 emit_membar(&ctx
->ac
, instr
);
4056 case nir_intrinsic_scoped_barrier
: {
4057 assert(!(nir_intrinsic_memory_semantics(instr
) &
4058 (NIR_MEMORY_MAKE_AVAILABLE
| NIR_MEMORY_MAKE_VISIBLE
)));
4060 nir_variable_mode modes
= nir_intrinsic_memory_modes(instr
);
4062 unsigned wait_flags
= 0;
4063 if (modes
& (nir_var_mem_global
| nir_var_mem_ssbo
))
4064 wait_flags
|= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
4065 if (modes
& nir_var_mem_shared
)
4066 wait_flags
|= AC_WAIT_LGKM
;
4069 ac_build_waitcnt(&ctx
->ac
, wait_flags
);
4071 if (nir_intrinsic_execution_scope(instr
) == NIR_SCOPE_WORKGROUP
)
4072 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4075 case nir_intrinsic_memory_barrier_tcs_patch
:
4077 case nir_intrinsic_control_barrier
:
4078 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4080 case nir_intrinsic_shared_atomic_add
:
4081 case nir_intrinsic_shared_atomic_imin
:
4082 case nir_intrinsic_shared_atomic_umin
:
4083 case nir_intrinsic_shared_atomic_imax
:
4084 case nir_intrinsic_shared_atomic_umax
:
4085 case nir_intrinsic_shared_atomic_and
:
4086 case nir_intrinsic_shared_atomic_or
:
4087 case nir_intrinsic_shared_atomic_xor
:
4088 case nir_intrinsic_shared_atomic_exchange
:
4089 case nir_intrinsic_shared_atomic_comp_swap
:
4090 case nir_intrinsic_shared_atomic_fadd
: {
4091 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
4092 instr
->src
[1].ssa
->bit_size
);
4093 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4096 case nir_intrinsic_deref_atomic_add
:
4097 case nir_intrinsic_deref_atomic_imin
:
4098 case nir_intrinsic_deref_atomic_umin
:
4099 case nir_intrinsic_deref_atomic_imax
:
4100 case nir_intrinsic_deref_atomic_umax
:
4101 case nir_intrinsic_deref_atomic_and
:
4102 case nir_intrinsic_deref_atomic_or
:
4103 case nir_intrinsic_deref_atomic_xor
:
4104 case nir_intrinsic_deref_atomic_exchange
:
4105 case nir_intrinsic_deref_atomic_comp_swap
:
4106 case nir_intrinsic_deref_atomic_fadd
: {
4107 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
4108 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4111 case nir_intrinsic_load_barycentric_pixel
:
4112 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
4114 case nir_intrinsic_load_barycentric_centroid
:
4115 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
4117 case nir_intrinsic_load_barycentric_sample
:
4118 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
4120 case nir_intrinsic_load_barycentric_model
:
4121 result
= barycentric_model(ctx
);
4123 case nir_intrinsic_load_barycentric_at_offset
: {
4124 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4125 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
4128 case nir_intrinsic_load_barycentric_at_sample
: {
4129 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
4130 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
4133 case nir_intrinsic_load_interpolated_input
: {
4134 /* We assume any indirect loads have been lowered away */
4135 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
4137 assert(offset
[0].i32
== 0);
4139 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
4140 unsigned index
= nir_intrinsic_base(instr
);
4141 unsigned component
= nir_intrinsic_component(instr
);
4142 result
= load_interpolated_input(ctx
, interp_param
, index
,
4144 instr
->dest
.ssa
.num_components
,
4145 instr
->dest
.ssa
.bit_size
);
4148 case nir_intrinsic_emit_vertex
:
4149 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
4151 case nir_intrinsic_emit_vertex_with_counter
: {
4152 unsigned stream
= nir_intrinsic_stream_id(instr
);
4153 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
4154 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
4159 case nir_intrinsic_end_primitive
:
4160 case nir_intrinsic_end_primitive_with_counter
:
4161 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4163 case nir_intrinsic_load_tess_coord
:
4164 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
4166 case nir_intrinsic_load_tess_level_outer
:
4167 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
4169 case nir_intrinsic_load_tess_level_inner
:
4170 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
4172 case nir_intrinsic_load_tess_level_outer_default
:
4173 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
4175 case nir_intrinsic_load_tess_level_inner_default
:
4176 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
4178 case nir_intrinsic_load_patch_vertices_in
:
4179 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4181 case nir_intrinsic_vote_all
: {
4182 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4183 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4186 case nir_intrinsic_vote_any
: {
4187 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4188 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4191 case nir_intrinsic_shuffle
:
4192 if (ctx
->ac
.chip_class
== GFX8
||
4193 ctx
->ac
.chip_class
== GFX9
||
4194 (ctx
->ac
.chip_class
>= GFX10
&& ctx
->ac
.wave_size
== 32)) {
4195 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4196 get_src(ctx
, instr
->src
[1]));
4198 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4199 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
4200 LLVMTypeRef type
= LLVMTypeOf(src
);
4201 struct waterfall_context wctx
;
4202 LLVMValueRef index_val
;
4204 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
4206 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
4209 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4211 (LLVMValueRef
[]) { src
, index_val
}, 2,
4212 AC_FUNC_ATTR_READNONE
|
4213 AC_FUNC_ATTR_CONVERGENT
);
4215 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4217 result
= exit_waterfall(ctx
, &wctx
, result
);
4220 case nir_intrinsic_reduce
:
4221 result
= ac_build_reduce(&ctx
->ac
,
4222 get_src(ctx
, instr
->src
[0]),
4223 instr
->const_index
[0],
4224 instr
->const_index
[1]);
4226 case nir_intrinsic_inclusive_scan
:
4227 result
= ac_build_inclusive_scan(&ctx
->ac
,
4228 get_src(ctx
, instr
->src
[0]),
4229 instr
->const_index
[0]);
4231 case nir_intrinsic_exclusive_scan
:
4232 result
= ac_build_exclusive_scan(&ctx
->ac
,
4233 get_src(ctx
, instr
->src
[0]),
4234 instr
->const_index
[0]);
4236 case nir_intrinsic_quad_broadcast
: {
4237 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4238 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4239 lane
, lane
, lane
, lane
);
4242 case nir_intrinsic_quad_swap_horizontal
:
4243 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4245 case nir_intrinsic_quad_swap_vertical
:
4246 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4248 case nir_intrinsic_quad_swap_diagonal
:
4249 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4251 case nir_intrinsic_quad_swizzle_amd
: {
4252 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4253 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4254 mask
& 0x3, (mask
>> 2) & 0x3,
4255 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4258 case nir_intrinsic_masked_swizzle_amd
: {
4259 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4260 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4263 case nir_intrinsic_write_invocation_amd
:
4264 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4265 get_src(ctx
, instr
->src
[1]),
4266 get_src(ctx
, instr
->src
[2]));
4268 case nir_intrinsic_mbcnt_amd
:
4269 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4271 case nir_intrinsic_load_scratch
: {
4272 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4273 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4275 LLVMTypeRef comp_type
=
4276 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4277 LLVMTypeRef vec_type
=
4278 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4279 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4280 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4281 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4282 LLVMPointerType(vec_type
, addr_space
), "");
4283 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4286 case nir_intrinsic_store_scratch
: {
4287 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4288 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4290 LLVMTypeRef comp_type
=
4291 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4292 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4293 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4294 LLVMPointerType(comp_type
, addr_space
), "");
4295 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4296 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4299 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4301 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4302 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4303 LLVMTypeRef vec_type
=
4304 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4305 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4307 LLVMPointerType(vec_type
, addr_space
),
4309 LLVMValueRef offset_src
=
4310 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4311 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4315 case nir_intrinsic_load_constant
: {
4316 unsigned base
= nir_intrinsic_base(instr
);
4317 unsigned range
= nir_intrinsic_range(instr
);
4319 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4320 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4321 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4323 /* Clamp the offset to avoid out-of-bound access because global
4324 * instructions can't handle them.
4326 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4327 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4329 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4331 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4333 LLVMTypeRef comp_type
=
4334 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4335 LLVMTypeRef vec_type
=
4336 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4337 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4338 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4339 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4340 LLVMPointerType(vec_type
, addr_space
), "");
4341 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4345 fprintf(stderr
, "Unknown intrinsic: ");
4346 nir_print_instr(&instr
->instr
, stderr
);
4347 fprintf(stderr
, "\n");
4351 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4355 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4356 unsigned base_index
,
4357 unsigned constant_index
,
4358 LLVMValueRef dynamic_index
)
4360 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4361 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4362 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4364 /* Bindless uniforms are 64bit so multiple index by 8 */
4365 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4366 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4368 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4370 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4371 NULL
, 0, 0, true, true);
4373 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4376 struct sampler_desc_address
{
4377 unsigned descriptor_set
;
4378 unsigned base_index
; /* binding in vulkan */
4379 unsigned constant_index
;
4380 LLVMValueRef dynamic_index
;
4385 static struct sampler_desc_address
4386 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4387 nir_deref_instr
*deref_instr
,
4388 const nir_instr
*instr
,
4391 LLVMValueRef index
= NULL
;
4392 unsigned constant_index
= 0;
4393 unsigned descriptor_set
;
4394 unsigned base_index
;
4395 bool bindless
= false;
4400 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4403 index
= get_src(ctx
, img_instr
->src
[0]);
4405 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4406 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4407 nir_tex_src_sampler_handle
);
4408 if (sampSrcIdx
!= -1) {
4411 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4413 assert(tex_instr
&& !image
);
4414 base_index
= tex_instr
->sampler_index
;
4418 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4419 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4420 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4424 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4425 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4427 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4429 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4430 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4435 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4438 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4439 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4440 unsigned sidx
= deref_instr
->strct
.index
;
4441 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4442 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4444 unreachable("Unsupported deref type");
4447 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4449 if (deref_instr
->var
->data
.bindless
) {
4450 /* For now just assert on unhandled variable types */
4451 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4453 base_index
= deref_instr
->var
->data
.driver_location
;
4456 index
= index
? index
: ctx
->ac
.i32_0
;
4457 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4458 constant_index
, index
);
4460 base_index
= deref_instr
->var
->data
.binding
;
4462 return (struct sampler_desc_address
) {
4463 .descriptor_set
= descriptor_set
,
4464 .base_index
= base_index
,
4465 .constant_index
= constant_index
,
4466 .dynamic_index
= index
,
4468 .bindless
= bindless
,
4472 /* Extract any possibly divergent index into a separate value that can be fed
4473 * into get_sampler_desc with the same arguments. */
4474 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4475 nir_deref_instr
*deref_instr
,
4476 const nir_instr
*instr
,
4479 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4480 return addr
.dynamic_index
;
4483 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4484 nir_deref_instr
*deref_instr
,
4485 enum ac_descriptor_type desc_type
,
4486 const nir_instr
*instr
,
4488 bool image
, bool write
)
4490 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4491 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4492 addr
.descriptor_set
,
4494 addr
.constant_index
, index
,
4495 desc_type
, addr
.image
, write
, addr
.bindless
);
4498 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4501 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4502 * filtering manually. The driver sets img7 to a mask clearing
4503 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4504 * s_and_b32 samp0, samp0, img7
4507 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4509 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4510 LLVMValueRef res
, LLVMValueRef samp
)
4512 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4513 LLVMValueRef img7
, samp0
;
4515 if (ctx
->ac
.chip_class
>= GFX8
)
4518 img7
= LLVMBuildExtractElement(builder
, res
,
4519 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4520 samp0
= LLVMBuildExtractElement(builder
, samp
,
4521 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4522 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4523 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4524 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4527 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4528 nir_tex_instr
*instr
,
4529 struct waterfall_context
*wctx
,
4530 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4531 LLVMValueRef
*fmask_ptr
)
4533 nir_deref_instr
*texture_deref_instr
= NULL
;
4534 nir_deref_instr
*sampler_deref_instr
= NULL
;
4537 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4538 switch (instr
->src
[i
].src_type
) {
4539 case nir_tex_src_texture_deref
:
4540 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4542 case nir_tex_src_sampler_deref
:
4543 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4545 case nir_tex_src_plane
:
4546 plane
= nir_src_as_int(instr
->src
[i
].src
);
4553 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4554 &instr
->instr
, false);
4555 if (!sampler_deref_instr
)
4556 sampler_deref_instr
= texture_deref_instr
;
4558 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4559 &instr
->instr
, false);
4560 if (instr
->texture_non_uniform
)
4561 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4563 if (instr
->sampler_non_uniform
)
4564 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4566 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4569 assert(instr
->op
!= nir_texop_txf_ms
&&
4570 instr
->op
!= nir_texop_samples_identical
);
4571 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4573 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4576 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4577 /* The fragment mask is fetched from the compressed
4578 * multisampled surface.
4580 main_descriptor
= AC_DESC_FMASK
;
4583 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4584 texture_dynamic_index
, false, false);
4587 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4588 sampler_dynamic_index
, false, false);
4589 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4590 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4592 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4593 instr
->op
== nir_texop_samples_identical
))
4594 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4595 &instr
->instr
, texture_dynamic_index
, false, false);
4598 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4601 coord
= ac_to_float(ctx
, coord
);
4602 coord
= ac_build_round(ctx
, coord
);
4603 coord
= ac_to_integer(ctx
, coord
);
4607 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4609 LLVMValueRef result
= NULL
;
4610 struct ac_image_args args
= { 0 };
4611 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4612 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4613 unsigned offset_src
= 0;
4614 struct waterfall_context wctx
[2] = {{{0}}};
4616 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4618 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4619 switch (instr
->src
[i
].src_type
) {
4620 case nir_tex_src_coord
: {
4621 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4622 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4623 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4626 case nir_tex_src_projector
:
4628 case nir_tex_src_comparator
:
4629 if (instr
->is_shadow
) {
4630 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4631 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4634 case nir_tex_src_offset
:
4635 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4638 case nir_tex_src_bias
:
4639 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4641 case nir_tex_src_lod
: {
4642 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4643 args
.level_zero
= true;
4645 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4648 case nir_tex_src_ms_index
:
4649 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4651 case nir_tex_src_ms_mcs
:
4653 case nir_tex_src_ddx
:
4654 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4656 case nir_tex_src_ddy
:
4657 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4659 case nir_tex_src_min_lod
:
4660 args
.min_lod
= get_src(ctx
, instr
->src
[i
].src
);
4662 case nir_tex_src_texture_offset
:
4663 case nir_tex_src_sampler_offset
:
4664 case nir_tex_src_plane
:
4670 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4671 result
= get_buffer_size(ctx
, args
.resource
, true);
4675 if (instr
->op
== nir_texop_texture_samples
) {
4676 LLVMValueRef res
, samples
, is_msaa
;
4677 LLVMValueRef default_sample
;
4679 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4680 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4681 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4682 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4683 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4684 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4685 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4686 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4687 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4689 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4690 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4691 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4692 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4693 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4696 if (ctx
->abi
->robust_buffer_access
) {
4697 LLVMValueRef dword1
, is_null_descriptor
;
4699 /* Extract the second dword of the descriptor, if it's
4700 * all zero, then it's a null descriptor.
4702 dword1
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4703 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
4704 is_null_descriptor
=
4705 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, dword1
,
4706 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4708 LLVMBuildSelect(ctx
->ac
.builder
, is_null_descriptor
,
4709 ctx
->ac
.i32_0
, ctx
->ac
.i32_1
, "");
4711 default_sample
= ctx
->ac
.i32_1
;
4714 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4715 default_sample
, "");
4720 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4721 LLVMValueRef offset
[3], pack
;
4722 for (unsigned chan
= 0; chan
< 3; ++chan
)
4723 offset
[chan
] = ctx
->ac
.i32_0
;
4725 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4726 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4727 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4728 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4729 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4731 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4732 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4734 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4735 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4739 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4740 * OpenGL 4.5 spec says:
4742 * "If the texture’s internal format indicates a fixed-point
4743 * depth texture, then D_t and D_ref are clamped to the
4744 * range [0, 1]; otherwise no clamping is performed."
4746 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4747 * so the depth comparison value isn't clamped for Z16 and
4748 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4749 * an explicitly clamped 32-bit float format.
4752 ctx
->ac
.chip_class
>= GFX8
&&
4753 ctx
->ac
.chip_class
<= GFX9
&&
4754 ctx
->abi
->clamp_shadow_reference
) {
4755 LLVMValueRef upgraded
, clamped
;
4757 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4758 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4759 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4760 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4761 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4762 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4763 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4767 /* pack derivatives */
4769 int num_src_deriv_channels
, num_dest_deriv_channels
;
4770 switch (instr
->sampler_dim
) {
4771 case GLSL_SAMPLER_DIM_3D
:
4772 case GLSL_SAMPLER_DIM_CUBE
:
4773 num_src_deriv_channels
= 3;
4774 num_dest_deriv_channels
= 3;
4776 case GLSL_SAMPLER_DIM_2D
:
4778 num_src_deriv_channels
= 2;
4779 num_dest_deriv_channels
= 2;
4781 case GLSL_SAMPLER_DIM_1D
:
4782 num_src_deriv_channels
= 1;
4783 if (ctx
->ac
.chip_class
== GFX9
) {
4784 num_dest_deriv_channels
= 2;
4786 num_dest_deriv_channels
= 1;
4791 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4792 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4793 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4794 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4795 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4797 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4798 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4799 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4803 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4804 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4805 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4806 if (instr
->coord_components
== 3)
4807 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4808 ac_prepare_cube_coords(&ctx
->ac
,
4809 instr
->op
== nir_texop_txd
, instr
->is_array
,
4810 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4813 /* Texture coordinates fixups */
4814 if (instr
->coord_components
> 1 &&
4815 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4817 instr
->op
!= nir_texop_txf
) {
4818 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4821 if (instr
->coord_components
> 2 &&
4822 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4823 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4824 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4825 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4827 instr
->op
!= nir_texop_txf
&&
4828 instr
->op
!= nir_texop_txf_ms
&&
4829 instr
->op
!= nir_texop_fragment_fetch
&&
4830 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4831 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4834 if (ctx
->ac
.chip_class
== GFX9
&&
4835 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4836 instr
->op
!= nir_texop_lod
) {
4837 LLVMValueRef filler
;
4838 if (instr
->op
== nir_texop_txf
)
4839 filler
= ctx
->ac
.i32_0
;
4841 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4843 if (instr
->is_array
)
4844 args
.coords
[2] = args
.coords
[1];
4845 args
.coords
[1] = filler
;
4848 /* Pack sample index */
4849 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4850 instr
->op
== nir_texop_fragment_fetch
))
4851 args
.coords
[instr
->coord_components
] = sample_index
;
4853 if (instr
->op
== nir_texop_samples_identical
) {
4854 struct ac_image_args txf_args
= { 0 };
4855 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4857 txf_args
.dmask
= 0xf;
4858 txf_args
.resource
= fmask_ptr
;
4859 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4860 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4862 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4863 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4867 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4868 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4869 instr
->op
!= nir_texop_txs
&&
4870 instr
->op
!= nir_texop_fragment_fetch
&&
4871 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4872 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4873 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4874 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4875 instr
->is_array
? args
.coords
[2] : NULL
,
4876 args
.coords
[sample_chan
], fmask_ptr
);
4879 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4880 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4881 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4882 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4883 args
.coords
[i
] = LLVMBuildAdd(
4884 ctx
->ac
.builder
, args
.coords
[i
],
4885 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4890 /* DMASK was repurposed for GATHER4. 4 components are always
4891 * returned and DMASK works like a swizzle - it selects
4892 * the component to fetch. The only valid DMASK values are
4893 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4894 * (red,red,red,red) etc.) The ISA document doesn't mention
4898 if (instr
->op
== nir_texop_tg4
) {
4899 if (instr
->is_shadow
)
4902 args
.dmask
= 1 << instr
->component
;
4905 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4906 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4907 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4910 /* Adjust the number of coordinates because we only need (x,y) for 2D
4911 * multisampled images and (x,y,layer) for 2D multisampled layered
4912 * images or for multisampled input attachments.
4914 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4915 if (args
.dim
== ac_image_2dmsaa
) {
4916 args
.dim
= ac_image_2d
;
4918 assert(args
.dim
== ac_image_2darraymsaa
);
4919 args
.dim
= ac_image_2darray
;
4923 assert(instr
->dest
.is_ssa
);
4924 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
4926 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4928 if (instr
->op
== nir_texop_query_levels
)
4929 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4930 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4931 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4932 instr
->op
!= nir_texop_tg4
)
4933 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4934 else if (instr
->op
== nir_texop_txs
&&
4935 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4937 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4938 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4939 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4940 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4941 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4942 } else if (ctx
->ac
.chip_class
== GFX9
&&
4943 instr
->op
== nir_texop_txs
&&
4944 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4946 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4947 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4948 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4950 } else if (instr
->dest
.ssa
.num_components
!= 4)
4951 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4955 assert(instr
->dest
.is_ssa
);
4956 result
= ac_to_integer(&ctx
->ac
, result
);
4958 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4959 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4962 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4966 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4968 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4969 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4971 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4972 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4975 static void visit_post_phi(struct ac_nir_context
*ctx
,
4976 nir_phi_instr
*instr
,
4977 LLVMValueRef llvm_phi
)
4979 nir_foreach_phi_src(src
, instr
) {
4980 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4981 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4983 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4987 static void phi_post_pass(struct ac_nir_context
*ctx
)
4989 hash_table_foreach(ctx
->phis
, entry
) {
4990 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4991 (LLVMValueRef
)entry
->data
);
4996 static bool is_def_used_in_an_export(const nir_ssa_def
* def
) {
4997 nir_foreach_use(use_src
, def
) {
4998 if (use_src
->parent_instr
->type
== nir_instr_type_intrinsic
) {
4999 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(use_src
->parent_instr
);
5000 if (instr
->intrinsic
== nir_intrinsic_store_deref
)
5002 } else if (use_src
->parent_instr
->type
== nir_instr_type_alu
) {
5003 nir_alu_instr
*instr
= nir_instr_as_alu(use_src
->parent_instr
);
5004 if (instr
->op
== nir_op_vec4
&&
5005 is_def_used_in_an_export(&instr
->dest
.dest
.ssa
)) {
5013 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5014 const nir_ssa_undef_instr
*instr
)
5016 unsigned num_components
= instr
->def
.num_components
;
5017 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5019 if (!ctx
->abi
->convert_undef_to_zero
|| is_def_used_in_an_export(&instr
->def
)) {
5022 if (num_components
== 1)
5023 undef
= LLVMGetUndef(type
);
5025 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5027 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
5029 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
5030 if (num_components
> 1) {
5031 zero
= ac_build_gather_values_extended(
5032 &ctx
->ac
, &zero
, 4, 0, false, false);
5034 ctx
->ssa_defs
[instr
->def
.index
] = zero
;
5038 static void visit_jump(struct ac_llvm_context
*ctx
,
5039 const nir_jump_instr
*instr
)
5041 switch (instr
->type
) {
5042 case nir_jump_break
:
5043 ac_build_break(ctx
);
5045 case nir_jump_continue
:
5046 ac_build_continue(ctx
);
5049 fprintf(stderr
, "Unknown NIR jump instr: ");
5050 nir_print_instr(&instr
->instr
, stderr
);
5051 fprintf(stderr
, "\n");
5057 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
5058 enum glsl_base_type type
)
5062 case GLSL_TYPE_UINT
:
5063 case GLSL_TYPE_BOOL
:
5064 case GLSL_TYPE_SUBROUTINE
:
5066 case GLSL_TYPE_INT8
:
5067 case GLSL_TYPE_UINT8
:
5069 case GLSL_TYPE_INT16
:
5070 case GLSL_TYPE_UINT16
:
5072 case GLSL_TYPE_FLOAT
:
5074 case GLSL_TYPE_FLOAT16
:
5076 case GLSL_TYPE_INT64
:
5077 case GLSL_TYPE_UINT64
:
5079 case GLSL_TYPE_DOUBLE
:
5082 unreachable("unknown GLSL type");
5087 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
5088 const struct glsl_type
*type
)
5090 if (glsl_type_is_scalar(type
)) {
5091 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
5094 if (glsl_type_is_vector(type
)) {
5095 return LLVMVectorType(
5096 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
5097 glsl_get_vector_elements(type
));
5100 if (glsl_type_is_matrix(type
)) {
5101 return LLVMArrayType(
5102 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
5103 glsl_get_matrix_columns(type
));
5106 if (glsl_type_is_array(type
)) {
5107 return LLVMArrayType(
5108 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
5109 glsl_get_length(type
));
5112 assert(glsl_type_is_struct_or_ifc(type
));
5114 LLVMTypeRef member_types
[glsl_get_length(type
)];
5116 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5118 glsl_to_llvm_type(ac
,
5119 glsl_get_struct_field(type
, i
));
5122 return LLVMStructTypeInContext(ac
->context
, member_types
,
5123 glsl_get_length(type
), false);
5126 static void visit_deref(struct ac_nir_context
*ctx
,
5127 nir_deref_instr
*instr
)
5129 if (instr
->mode
!= nir_var_mem_shared
&&
5130 instr
->mode
!= nir_var_mem_global
)
5133 LLVMValueRef result
= NULL
;
5134 switch(instr
->deref_type
) {
5135 case nir_deref_type_var
: {
5136 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
5137 result
= entry
->data
;
5140 case nir_deref_type_struct
:
5141 if (instr
->mode
== nir_var_mem_global
) {
5142 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5143 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
5144 instr
->strct
.index
);
5145 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5146 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
5148 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5149 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
5152 case nir_deref_type_array
:
5153 if (instr
->mode
== nir_var_mem_global
) {
5154 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5155 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
5157 if ((glsl_type_is_matrix(parent
->type
) &&
5158 glsl_matrix_type_is_row_major(parent
->type
)) ||
5159 (glsl_type_is_vector(parent
->type
) && stride
== 0))
5160 stride
= type_scalar_size_bytes(parent
->type
);
5163 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5164 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5165 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5167 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5169 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5171 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5172 get_src(ctx
, instr
->arr
.index
));
5175 case nir_deref_type_ptr_as_array
:
5176 if (instr
->mode
== nir_var_mem_global
) {
5177 unsigned stride
= nir_deref_instr_array_stride(instr
);
5179 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5180 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5181 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5183 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5185 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5187 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5188 get_src(ctx
, instr
->arr
.index
));
5191 case nir_deref_type_cast
: {
5192 result
= get_src(ctx
, instr
->parent
);
5194 /* We can't use the structs from LLVM because the shader
5195 * specifies its own offsets. */
5196 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
5197 if (instr
->mode
== nir_var_mem_shared
)
5198 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
5200 unsigned address_space
;
5202 switch(instr
->mode
) {
5203 case nir_var_mem_shared
:
5204 address_space
= AC_ADDR_SPACE_LDS
;
5206 case nir_var_mem_global
:
5207 address_space
= AC_ADDR_SPACE_GLOBAL
;
5210 unreachable("Unhandled address space");
5213 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
5215 if (LLVMTypeOf(result
) != type
) {
5216 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
5217 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
5220 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
5227 unreachable("Unhandled deref_instr deref type");
5230 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
5233 static void visit_cf_list(struct ac_nir_context
*ctx
,
5234 struct exec_list
*list
);
5236 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5238 nir_foreach_instr(instr
, block
)
5240 switch (instr
->type
) {
5241 case nir_instr_type_alu
:
5242 visit_alu(ctx
, nir_instr_as_alu(instr
));
5244 case nir_instr_type_load_const
:
5245 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5247 case nir_instr_type_intrinsic
:
5248 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5250 case nir_instr_type_tex
:
5251 visit_tex(ctx
, nir_instr_as_tex(instr
));
5253 case nir_instr_type_phi
:
5254 visit_phi(ctx
, nir_instr_as_phi(instr
));
5256 case nir_instr_type_ssa_undef
:
5257 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5259 case nir_instr_type_jump
:
5260 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
5262 case nir_instr_type_deref
:
5263 visit_deref(ctx
, nir_instr_as_deref(instr
));
5266 fprintf(stderr
, "Unknown NIR instr type: ");
5267 nir_print_instr(instr
, stderr
);
5268 fprintf(stderr
, "\n");
5273 _mesa_hash_table_insert(ctx
->defs
, block
,
5274 LLVMGetInsertBlock(ctx
->ac
.builder
));
5277 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5279 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5281 nir_block
*then_block
=
5282 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5284 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5286 visit_cf_list(ctx
, &if_stmt
->then_list
);
5288 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5289 nir_block
*else_block
=
5290 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5292 ac_build_else(&ctx
->ac
, else_block
->index
);
5293 visit_cf_list(ctx
, &if_stmt
->else_list
);
5296 ac_build_endif(&ctx
->ac
, then_block
->index
);
5299 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5301 nir_block
*first_loop_block
=
5302 (nir_block
*) exec_list_get_head(&loop
->body
);
5304 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5306 visit_cf_list(ctx
, &loop
->body
);
5308 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5311 static void visit_cf_list(struct ac_nir_context
*ctx
,
5312 struct exec_list
*list
)
5314 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5316 switch (node
->type
) {
5317 case nir_cf_node_block
:
5318 visit_block(ctx
, nir_cf_node_as_block(node
));
5321 case nir_cf_node_if
:
5322 visit_if(ctx
, nir_cf_node_as_if(node
));
5325 case nir_cf_node_loop
:
5326 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5336 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5337 struct ac_shader_abi
*abi
,
5338 struct nir_shader
*nir
,
5339 struct nir_variable
*variable
,
5340 gl_shader_stage stage
)
5342 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5343 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5345 /* tess ctrl has it's own load/store paths for outputs */
5346 if (stage
== MESA_SHADER_TESS_CTRL
)
5349 if (stage
== MESA_SHADER_VERTEX
||
5350 stage
== MESA_SHADER_TESS_EVAL
||
5351 stage
== MESA_SHADER_GEOMETRY
) {
5352 int idx
= variable
->data
.location
+ variable
->data
.index
;
5353 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5354 int length
= nir
->info
.clip_distance_array_size
+
5355 nir
->info
.cull_distance_array_size
;
5364 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5365 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5366 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5367 for (unsigned chan
= 0; chan
< 4; chan
++) {
5368 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5369 ac_build_alloca_undef(ctx
, type
, "");
5375 setup_locals(struct ac_nir_context
*ctx
,
5376 struct nir_function
*func
)
5379 ctx
->num_locals
= 0;
5380 nir_foreach_function_temp_variable(variable
, func
->impl
) {
5381 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5382 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5383 variable
->data
.location_frac
= 0;
5384 ctx
->num_locals
+= attrib_count
;
5386 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5390 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5391 for (j
= 0; j
< 4; j
++) {
5392 ctx
->locals
[i
* 4 + j
] =
5393 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5399 setup_scratch(struct ac_nir_context
*ctx
,
5400 struct nir_shader
*shader
)
5402 if (shader
->scratch_size
== 0)
5405 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5406 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5411 setup_constant_data(struct ac_nir_context
*ctx
,
5412 struct nir_shader
*shader
)
5414 if (!shader
->constant_data
)
5418 LLVMConstStringInContext(ctx
->ac
.context
,
5419 shader
->constant_data
,
5420 shader
->constant_data_size
,
5422 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5424 /* We want to put the constant data in the CONST address space so that
5425 * we can use scalar loads. However, LLVM versions before 10 put these
5426 * variables in the same section as the code, which is unacceptable
5427 * for RadeonSI as it needs to relocate all the data sections after
5428 * the code sections. See https://reviews.llvm.org/D65813.
5430 unsigned address_space
=
5431 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5433 LLVMValueRef global
=
5434 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5438 LLVMSetInitializer(global
, data
);
5439 LLVMSetGlobalConstant(global
, true);
5440 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5441 ctx
->constant_data
= global
;
5445 setup_shared(struct ac_nir_context
*ctx
,
5446 struct nir_shader
*nir
)
5451 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5452 nir
->info
.cs
.shared_size
);
5455 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5458 LLVMSetAlignment(lds
, 64 * 1024);
5460 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5461 LLVMPointerType(ctx
->ac
.i8
,
5462 AC_ADDR_SPACE_LDS
), "");
5465 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5466 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5468 struct ac_nir_context ctx
= {};
5469 struct nir_function
*func
;
5475 ctx
.stage
= nir
->info
.stage
;
5476 ctx
.info
= &nir
->info
;
5478 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5480 /* TODO: remove this after RADV switches to lowered IO */
5481 if (!nir
->info
.io_lowered
) {
5482 nir_foreach_shader_out_variable(variable
, nir
) {
5483 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5488 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5489 _mesa_key_pointer_equal
);
5490 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5491 _mesa_key_pointer_equal
);
5492 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5493 _mesa_key_pointer_equal
);
5495 if (ctx
.abi
->kill_ps_if_inf_interp
)
5496 ctx
.verified_interp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5497 _mesa_key_pointer_equal
);
5499 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5501 nir_index_ssa_defs(func
->impl
);
5502 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5504 setup_locals(&ctx
, func
);
5505 setup_scratch(&ctx
, nir
);
5506 setup_constant_data(&ctx
, nir
);
5508 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5509 setup_shared(&ctx
, nir
);
5511 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5512 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5513 /* true = don't kill. */
5514 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5517 visit_cf_list(&ctx
, &func
->impl
->body
);
5518 phi_post_pass(&ctx
);
5520 if (ctx
.ac
.postponed_kill
)
5521 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5522 ctx
.ac
.postponed_kill
, ""));
5524 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5525 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5530 ralloc_free(ctx
.defs
);
5531 ralloc_free(ctx
.phis
);
5532 ralloc_free(ctx
.vars
);
5533 if (ctx
.abi
->kill_ps_if_inf_interp
)
5534 ralloc_free(ctx
.verified_interp
);
5538 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5540 bool progress
= false;
5542 /* Lower large variables to scratch first so that we won't bloat the
5543 * shader by generating large if ladders for them. We later lower
5544 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5546 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5547 nir_var_function_temp
,
5549 glsl_get_natural_size_align_bytes
);
5551 /* While it would be nice not to have this flag, we are constrained
5552 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5554 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5556 /* TODO: Indirect indexing of GS inputs is unimplemented.
5558 * TCS and TES load inputs directly from LDS or offchip memory, so
5559 * indirect indexing is trivial.
5561 nir_variable_mode indirect_mask
= 0;
5562 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5563 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5564 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5565 !llvm_has_working_vgpr_indexing
)) {
5566 indirect_mask
|= nir_var_shader_in
;
5568 if (!llvm_has_working_vgpr_indexing
&&
5569 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5570 indirect_mask
|= nir_var_shader_out
;
5572 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5573 * smart enough to handle indirects without causing excess spilling
5574 * causing the gpu to hang.
5576 * See the following thread for more details of the problem:
5577 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5579 indirect_mask
|= nir_var_function_temp
;
5581 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
, UINT32_MAX
);
5586 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5588 if (intrin
->intrinsic
!= nir_intrinsic_store_output
)
5591 unsigned writemask
= nir_intrinsic_write_mask(intrin
) <<
5592 nir_intrinsic_component(intrin
);
5593 unsigned location
= nir_intrinsic_io_semantics(intrin
).location
;
5595 if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5596 return writemask
<< 4;
5597 else if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5604 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5605 unsigned *cond_block_tf_writemask
,
5606 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5608 switch (cf_node
->type
) {
5609 case nir_cf_node_block
: {
5610 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5611 nir_foreach_instr(instr
, block
) {
5612 if (instr
->type
!= nir_instr_type_intrinsic
)
5615 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5616 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5618 /* If we find a barrier in nested control flow put this in the
5619 * too hard basket. In GLSL this is not possible but it is in
5623 *tessfactors_are_def_in_all_invocs
= false;
5627 /* The following case must be prevented:
5628 * gl_TessLevelInner = ...;
5630 * if (gl_InvocationID == 1)
5631 * gl_TessLevelInner = ...;
5633 * If you consider disjoint code segments separated by barriers, each
5634 * such segment that writes tess factor channels should write the same
5635 * channels in all codepaths within that segment.
5637 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5638 /* Accumulate the result: */
5639 *tessfactors_are_def_in_all_invocs
&=
5640 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5642 /* Analyze the next code segment from scratch. */
5643 *upper_block_tf_writemask
= 0;
5644 *cond_block_tf_writemask
= 0;
5647 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5652 case nir_cf_node_if
: {
5653 unsigned then_tessfactor_writemask
= 0;
5654 unsigned else_tessfactor_writemask
= 0;
5656 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5657 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5658 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5659 cond_block_tf_writemask
,
5660 tessfactors_are_def_in_all_invocs
, true);
5663 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5664 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5665 cond_block_tf_writemask
,
5666 tessfactors_are_def_in_all_invocs
, true);
5669 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5670 /* If both statements write the same tess factor channels,
5671 * we can say that the upper block writes them too.
5673 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5674 else_tessfactor_writemask
;
5675 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5676 else_tessfactor_writemask
;
5681 case nir_cf_node_loop
: {
5682 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5683 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5684 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5685 cond_block_tf_writemask
,
5686 tessfactors_are_def_in_all_invocs
, true);
5692 unreachable("unknown cf node type");
5697 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5699 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5701 /* The pass works as follows:
5702 * If all codepaths write tess factors, we can say that all
5703 * invocations define tess factors.
5705 * Each tess factor channel is tracked separately.
5707 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5708 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5710 /* Initial value = true. Here the pass will accumulate results from
5711 * multiple segments surrounded by barriers. If tess factors aren't
5712 * written at all, it's a shader bug and we don't care if this will be
5715 bool tessfactors_are_def_in_all_invocs
= true;
5717 nir_foreach_function(function
, nir
) {
5718 if (function
->impl
) {
5719 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5720 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5721 &cond_block_tf_writemask
,
5722 &tessfactors_are_def_in_all_invocs
,
5728 /* Accumulate the result for the last code segment separated by a
5731 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5732 tessfactors_are_def_in_all_invocs
&=
5733 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5736 return tessfactors_are_def_in_all_invocs
;