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 /* lower_fmod only lower 16-bit and 32-bit fmod */
706 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
707 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
708 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
709 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
710 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
711 ac_to_float_type(&ctx
->ac
, def_type
), result
);
712 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
713 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
716 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
719 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
722 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
725 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
726 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
727 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
730 /* For doubles, we need precise division to pass GLCTS. */
731 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
&&
732 ac_get_type_size(def_type
) == 8) {
733 result
= LLVMBuildFDiv(ctx
->ac
.builder
, ctx
->ac
.f64_1
,
734 ac_to_float(&ctx
->ac
, src
[0]), "");
736 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rcp",
737 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
739 if (ctx
->abi
->clamp_div_by_zero
)
740 result
= ac_build_fmin(&ctx
->ac
, result
,
741 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
744 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
747 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
750 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
753 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
754 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
755 LLVMTypeOf(src
[0]), "");
756 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
757 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
758 LLVMTypeOf(src
[0]), "");
759 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
762 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
763 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
764 LLVMTypeOf(src
[0]), "");
765 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
766 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
767 LLVMTypeOf(src
[0]), "");
768 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
771 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
772 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
773 LLVMTypeOf(src
[0]), "");
774 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
775 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
776 LLVMTypeOf(src
[0]), "");
777 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
780 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
783 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
786 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
789 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
792 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
795 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
798 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
801 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
804 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
807 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
810 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
811 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
812 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
813 /* fabs will be optimized by backend compiler with sign
814 * bit removed via AND.
816 result
= ac_build_canonicalize(&ctx
->ac
, result
,
817 instr
->dest
.dest
.ssa
.bit_size
);
821 result
= emit_iabs(&ctx
->ac
, src
[0]);
824 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
827 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
830 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
833 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
836 result
= ac_build_isign(&ctx
->ac
, src
[0],
837 instr
->dest
.dest
.ssa
.bit_size
);
840 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
841 result
= ac_build_fsign(&ctx
->ac
, src
[0],
842 instr
->dest
.dest
.ssa
.bit_size
);
845 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
846 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
849 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
850 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
853 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
854 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
856 case nir_op_fround_even
:
857 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
858 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
861 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.fract",
862 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
865 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
866 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
869 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
870 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
873 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
874 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
877 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
878 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
881 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
882 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
885 result
= emit_intrin_1f_param_scalar(&ctx
->ac
, "llvm.amdgcn.rsq",
886 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
887 if (ctx
->abi
->clamp_div_by_zero
)
888 result
= ac_build_fmin(&ctx
->ac
, result
,
889 LLVMConstReal(ac_to_float_type(&ctx
->ac
, def_type
), FLT_MAX
));
891 case nir_op_frexp_exp
:
892 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
893 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
894 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
895 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
896 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
899 case nir_op_frexp_sig
:
900 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
901 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
902 instr
->dest
.dest
.ssa
.bit_size
);
905 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
906 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
909 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
910 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
911 if (ctx
->ac
.chip_class
< GFX9
&&
912 instr
->dest
.dest
.ssa
.bit_size
== 32) {
913 /* Only pre-GFX9 chips do not flush denorms. */
914 result
= ac_build_canonicalize(&ctx
->ac
, result
,
915 instr
->dest
.dest
.ssa
.bit_size
);
919 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
920 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
921 if (ctx
->ac
.chip_class
< GFX9
&&
922 instr
->dest
.dest
.ssa
.bit_size
== 32) {
923 /* Only pre-GFX9 chips do not flush denorms. */
924 result
= ac_build_canonicalize(&ctx
->ac
, result
,
925 instr
->dest
.dest
.ssa
.bit_size
);
929 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
930 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
931 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
934 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
935 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
936 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
937 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
938 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
940 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
943 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
945 case nir_op_bitfield_select
:
946 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
949 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
952 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
954 case nir_op_bitfield_reverse
:
955 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
957 case nir_op_bit_count
:
958 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
963 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
964 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
965 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
971 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
972 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
978 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
979 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
984 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
989 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
991 case nir_op_f2f16_rtz
:
994 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
996 /* For OpenGL, we want fast packing with v_cvt_pkrtz_f16, but if we use it,
997 * all f32->f16 conversions have to round towards zero, because both scalar
998 * and vec2 down-conversions have to round equally.
1000 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DEFAULT_OPENGL
||
1001 instr
->op
== nir_op_f2f16_rtz
) {
1002 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1004 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
1005 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
1007 /* Fast path conversion. This only works if NIR is vectorized
1010 if (LLVMTypeOf(src
[0]) == ctx
->ac
.v2f32
) {
1011 LLVMValueRef args
[] = {
1012 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 0),
1013 ac_llvm_extract_elem(&ctx
->ac
, src
[0], 1),
1015 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, args
);
1019 assert(ac_get_llvm_num_components(src
[0]) == 1);
1020 LLVMValueRef param
[2] = { src
[0], LLVMGetUndef(ctx
->ac
.f32
) };
1021 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
1022 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
1024 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1025 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1027 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1030 case nir_op_f2f16_rtne
:
1033 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1034 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1035 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1037 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
1044 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1045 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1047 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1054 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
1055 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
1057 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
1059 case nir_op_b32csel
:
1060 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
1062 case nir_op_find_lsb
:
1063 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
1065 case nir_op_ufind_msb
:
1066 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1068 case nir_op_ifind_msb
:
1069 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
1071 case nir_op_uadd_carry
:
1072 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
1074 case nir_op_usub_borrow
:
1075 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1080 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1083 result
= emit_f2b(&ctx
->ac
, src
[0]);
1089 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1092 result
= emit_i2b(&ctx
->ac
, src
[0]);
1094 case nir_op_fquantize2f16
:
1095 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1097 case nir_op_umul_high
:
1098 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1100 case nir_op_imul_high
:
1101 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1103 case nir_op_pack_half_2x16
:
1104 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1106 case nir_op_pack_snorm_2x16
:
1107 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1109 case nir_op_pack_unorm_2x16
:
1110 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1112 case nir_op_unpack_half_2x16
:
1113 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1117 case nir_op_fddx_fine
:
1118 case nir_op_fddy_fine
:
1119 case nir_op_fddx_coarse
:
1120 case nir_op_fddy_coarse
:
1121 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1124 case nir_op_unpack_64_2x32_split_x
: {
1125 assert(ac_get_llvm_num_components(src
[0]) == 1);
1126 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1129 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1134 case nir_op_unpack_64_2x32_split_y
: {
1135 assert(ac_get_llvm_num_components(src
[0]) == 1);
1136 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1139 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1144 case nir_op_pack_64_2x32_split
: {
1145 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1146 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1150 case nir_op_pack_32_2x16_split
: {
1151 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1152 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1156 case nir_op_unpack_32_2x16_split_x
: {
1157 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1160 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1165 case nir_op_unpack_32_2x16_split_y
: {
1166 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1169 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1174 case nir_op_cube_face_coord
: {
1175 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1176 LLVMValueRef results
[2];
1178 for (unsigned chan
= 0; chan
< 3; chan
++)
1179 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1180 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1181 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1182 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1183 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1184 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1185 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1186 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1187 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1188 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1189 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1190 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1191 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1195 case nir_op_cube_face_index
: {
1196 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1198 for (unsigned chan
= 0; chan
< 3; chan
++)
1199 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1200 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1201 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1206 fprintf(stderr
, "Unknown NIR alu instr: ");
1207 nir_print_instr(&instr
->instr
, stderr
);
1208 fprintf(stderr
, "\n");
1213 assert(instr
->dest
.dest
.is_ssa
);
1214 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1215 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1219 static void visit_load_const(struct ac_nir_context
*ctx
,
1220 const nir_load_const_instr
*instr
)
1222 LLVMValueRef values
[4], value
= NULL
;
1223 LLVMTypeRef element_type
=
1224 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1226 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1227 switch (instr
->def
.bit_size
) {
1229 values
[i
] = LLVMConstInt(element_type
,
1230 instr
->value
[i
].u8
, false);
1233 values
[i
] = LLVMConstInt(element_type
,
1234 instr
->value
[i
].u16
, false);
1237 values
[i
] = LLVMConstInt(element_type
,
1238 instr
->value
[i
].u32
, false);
1241 values
[i
] = LLVMConstInt(element_type
,
1242 instr
->value
[i
].u64
, false);
1246 "unsupported nir load_const bit_size: %d\n",
1247 instr
->def
.bit_size
);
1251 if (instr
->def
.num_components
> 1) {
1252 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1256 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1260 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1263 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1264 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1267 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1268 /* On GFX8, the descriptor contains the size in bytes,
1269 * but TXQ must return the size in elements.
1270 * The stride is always non-zero for resources using TXQ.
1272 LLVMValueRef stride
=
1273 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1275 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1276 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1277 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1278 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1280 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1285 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1286 * incorrectly forces nearest filtering if the texture format is integer.
1287 * The only effect it has on Gather4, which always returns 4 texels for
1288 * bilinear filtering, is that the final coordinates are off by 0.5 of
1291 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1292 * or (0.5 / size) from the normalized coordinates.
1294 * However, cube textures with 8_8_8_8 data formats require a different
1295 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1296 * precision in 32-bit data formats, so it needs to be applied dynamically at
1297 * runtime. In this case, return an i1 value that indicates whether the
1298 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1300 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1302 struct ac_image_args
*args
,
1303 const nir_tex_instr
*instr
)
1305 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1306 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1307 LLVMValueRef wa_8888
= NULL
;
1308 LLVMValueRef half_texel
[2];
1309 LLVMValueRef result
;
1311 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1313 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1314 LLVMValueRef formats
;
1315 LLVMValueRef data_format
;
1316 LLVMValueRef wa_formats
;
1318 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1320 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1321 LLVMConstInt(ctx
->i32
, 20, false), "");
1322 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1323 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1324 wa_8888
= LLVMBuildICmp(
1325 ctx
->builder
, LLVMIntEQ
, data_format
,
1326 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1329 uint32_t wa_num_format
=
1330 stype
== GLSL_TYPE_UINT
?
1331 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1332 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1333 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1334 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1336 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1337 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1339 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1340 args
->resource
= LLVMBuildInsertElement(
1341 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1344 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1346 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1348 struct ac_image_args resinfo
= {};
1349 LLVMBasicBlockRef bbs
[2];
1351 LLVMValueRef unnorm
= NULL
;
1352 LLVMValueRef default_offset
= ctx
->f32_0
;
1353 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1355 /* In vulkan, whether the sampler uses unnormalized
1356 * coordinates or not is a dynamic property of the
1357 * sampler. Hence, to figure out whether or not we
1358 * need to divide by the texture size, we need to test
1359 * the sampler at runtime. This tests the bit set by
1360 * radv_init_sampler().
1362 LLVMValueRef sampler0
=
1363 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1364 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1365 LLVMConstInt(ctx
->i32
, 15, false), "");
1366 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1367 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1368 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1371 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1372 if (wa_8888
|| unnorm
) {
1373 assert(!(wa_8888
&& unnorm
));
1374 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1375 /* Skip the texture size query entirely if we don't need it. */
1376 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1377 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1380 /* Query the texture size. */
1381 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1382 resinfo
.opcode
= ac_image_get_resinfo
;
1383 resinfo
.dmask
= 0xf;
1384 resinfo
.lod
= ctx
->i32_0
;
1385 resinfo
.resource
= args
->resource
;
1386 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1387 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1389 /* Compute -0.5 / size. */
1390 for (unsigned c
= 0; c
< 2; c
++) {
1392 LLVMBuildExtractElement(ctx
->builder
, size
,
1393 LLVMConstInt(ctx
->i32
, c
, 0), "");
1394 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1395 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1396 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1397 LLVMConstReal(ctx
->f32
, -0.5), "");
1400 if (wa_8888
|| unnorm
) {
1401 ac_build_endif(ctx
, 2000);
1403 for (unsigned c
= 0; c
< 2; c
++) {
1404 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1405 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1411 for (unsigned c
= 0; c
< 2; c
++) {
1413 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1414 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1417 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1418 result
= ac_build_image_opcode(ctx
, args
);
1420 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1421 LLVMValueRef tmp
, tmp2
;
1423 /* if the cube workaround is in place, f2i the result. */
1424 for (unsigned c
= 0; c
< 4; c
++) {
1425 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1426 if (stype
== GLSL_TYPE_UINT
)
1427 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1429 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1430 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1431 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1432 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1433 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1434 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1440 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1442 nir_deref_instr
*texture_deref_instr
= NULL
;
1444 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1445 switch (instr
->src
[i
].src_type
) {
1446 case nir_tex_src_texture_deref
:
1447 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1453 return texture_deref_instr
;
1456 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1457 const nir_tex_instr
*instr
,
1458 struct ac_image_args
*args
)
1460 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1461 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1463 assert(instr
->dest
.is_ssa
);
1464 return ac_build_buffer_load_format(&ctx
->ac
,
1468 util_last_bit(mask
),
1470 instr
->dest
.ssa
.bit_size
== 16);
1473 args
->opcode
= ac_image_sample
;
1475 switch (instr
->op
) {
1477 case nir_texop_txf_ms
:
1478 case nir_texop_samples_identical
:
1479 args
->opcode
= args
->level_zero
||
1480 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1481 ac_image_load
: ac_image_load_mip
;
1482 args
->level_zero
= false;
1485 case nir_texop_query_levels
:
1486 args
->opcode
= ac_image_get_resinfo
;
1488 args
->lod
= ctx
->ac
.i32_0
;
1489 args
->level_zero
= false;
1492 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1494 args
->level_zero
= true;
1498 args
->opcode
= ac_image_gather4
;
1499 if (!args
->lod
&& !args
->bias
)
1500 args
->level_zero
= true;
1503 args
->opcode
= ac_image_get_lod
;
1505 case nir_texop_fragment_fetch
:
1506 case nir_texop_fragment_mask_fetch
:
1507 args
->opcode
= ac_image_load
;
1508 args
->level_zero
= false;
1514 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1515 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1516 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1517 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1518 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1519 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1520 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1524 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1525 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1526 if ((args
->dim
== ac_image_2darray
||
1527 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1528 args
->coords
[1] = ctx
->ac
.i32_0
;
1532 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1533 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1534 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1535 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1536 /* Prevent texture instructions with implicit derivatives from being
1537 * sinked into branches. */
1538 switch (instr
->op
) {
1542 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1549 return ac_build_image_opcode(&ctx
->ac
, args
);
1552 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1553 nir_intrinsic_instr
*instr
)
1555 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1556 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1558 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1559 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1563 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1564 nir_intrinsic_instr
*instr
)
1566 LLVMValueRef ptr
, addr
;
1567 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1568 unsigned index
= nir_intrinsic_base(instr
);
1570 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1571 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1573 /* Load constant values from user SGPRS when possible, otherwise
1574 * fallback to the default path that loads directly from memory.
1576 if (LLVMIsConstant(src0
) &&
1577 instr
->dest
.ssa
.bit_size
== 32) {
1578 unsigned count
= instr
->dest
.ssa
.num_components
;
1579 unsigned offset
= index
;
1581 offset
+= LLVMConstIntGetZExtValue(src0
);
1584 offset
-= ctx
->args
->base_inline_push_consts
;
1586 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1587 if (offset
+ count
<= num_inline_push_consts
) {
1588 LLVMValueRef push_constants
[num_inline_push_consts
];
1589 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1590 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1591 ctx
->args
->inline_push_consts
[i
]);
1592 return ac_build_gather_values(&ctx
->ac
,
1593 push_constants
+ offset
,
1598 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1599 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1601 if (instr
->dest
.ssa
.bit_size
== 8) {
1602 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1603 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i8
, 4 * load_dwords
);
1604 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1605 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1607 LLVMValueRef params
[3];
1608 if (load_dwords
> 1) {
1609 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.v2i32
, "");
1610 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1611 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1613 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1614 params
[0] = ctx
->ac
.i32_0
;
1618 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1620 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1621 if (instr
->dest
.ssa
.num_components
> 1)
1622 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i8
, instr
->dest
.ssa
.num_components
), "");
1624 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1625 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1626 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->ac
.i16
, 2 * load_dwords
);
1627 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1628 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1629 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1630 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1631 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1632 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1633 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1634 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1635 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1636 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1637 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1638 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1639 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1640 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1643 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1645 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1648 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1649 const nir_intrinsic_instr
*instr
)
1651 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1653 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1656 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1658 uint32_t new_mask
= 0;
1659 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1660 if (mask
& (1u << i
))
1661 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1665 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1666 unsigned start
, unsigned count
)
1668 LLVMValueRef mask
[] = {
1669 ctx
->i32_0
, ctx
->i32_1
,
1670 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1672 unsigned src_elements
= ac_get_llvm_num_components(src
);
1674 if (count
== src_elements
) {
1677 } else if (count
== 1) {
1678 assert(start
< src_elements
);
1679 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1681 assert(start
+ count
<= src_elements
);
1683 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1684 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1688 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1689 enum gl_access_qualifier access
,
1690 bool may_store_unaligned
,
1691 bool writeonly_memory
)
1693 unsigned cache_policy
= 0;
1695 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1696 * store opcodes not aligned to a dword are affected. The only way to
1697 * get unaligned stores is through shader images.
1699 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1700 /* If this is write-only, don't keep data in L1 to prevent
1701 * evicting L1 cache lines that may be needed by other
1705 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1706 cache_policy
|= ac_glc
;
1709 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1710 cache_policy
|= ac_slc
| ac_glc
;
1712 return cache_policy
;
1715 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1716 struct waterfall_context
*wctx
,
1717 const nir_intrinsic_instr
*instr
,
1720 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1721 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1724 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1725 nir_intrinsic_instr
*instr
)
1727 if (ctx
->ac
.postponed_kill
) {
1728 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1729 ctx
->ac
.postponed_kill
, "");
1730 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1733 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1734 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1735 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1736 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1737 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1738 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1740 struct waterfall_context wctx
;
1741 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1743 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1744 LLVMValueRef base_data
= src_data
;
1745 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1746 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1750 LLVMValueRef data
, offset
;
1751 LLVMTypeRef data_type
;
1753 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1755 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1756 * writes into a 2-element and a 1-element write. */
1758 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1759 writemask
|= 1 << (start
+ 2);
1762 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1764 /* we can only store 4 DWords at the same time.
1765 * can only happen for 64 Bit vectors. */
1766 if (num_bytes
> 16) {
1767 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1772 /* check alignment of 16 Bit stores */
1773 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1774 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1779 /* Due to alignment issues, split stores of 8-bit/16-bit
1782 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1783 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1785 num_bytes
= elem_size_bytes
;
1788 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1790 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1791 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1793 if (num_bytes
== 1) {
1794 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1795 offset
, ctx
->ac
.i32_0
,
1797 } else if (num_bytes
== 2) {
1798 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1799 offset
, ctx
->ac
.i32_0
,
1802 int num_channels
= num_bytes
/ 4;
1804 switch (num_bytes
) {
1805 case 16: /* v4f32 */
1806 data_type
= ctx
->ac
.v4f32
;
1808 case 12: /* v3f32 */
1809 data_type
= ctx
->ac
.v3f32
;
1812 data_type
= ctx
->ac
.v2f32
;
1815 data_type
= ctx
->ac
.f32
;
1818 unreachable("Malformed vector store.");
1820 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1822 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1823 num_channels
, offset
,
1829 exit_waterfall(ctx
, &wctx
, NULL
);
1831 if (ctx
->ac
.postponed_kill
)
1832 ac_build_endif(&ctx
->ac
, 7000);
1835 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1836 LLVMValueRef descriptor
,
1837 LLVMValueRef offset
,
1838 LLVMValueRef compare
,
1839 LLVMValueRef exchange
)
1841 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1842 if (ctx
->abi
->robust_buffer_access
) {
1843 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1845 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1846 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1848 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1850 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1853 LLVMValueRef ptr_parts
[2] = {
1854 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1855 LLVMBuildAnd(ctx
->ac
.builder
,
1856 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1857 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1860 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1861 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1863 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1865 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1866 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1867 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1868 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1870 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1871 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1873 if (ctx
->abi
->robust_buffer_access
) {
1874 ac_build_endif(&ctx
->ac
, -1);
1876 LLVMBasicBlockRef incoming_blocks
[2] = {
1881 LLVMValueRef incoming_values
[2] = {
1882 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1885 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1886 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1893 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1894 nir_intrinsic_instr
*instr
)
1896 if (ctx
->ac
.postponed_kill
) {
1897 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1898 ctx
->ac
.postponed_kill
, "");
1899 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1902 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1904 char name
[64], type
[8];
1905 LLVMValueRef params
[6], descriptor
;
1906 LLVMValueRef result
;
1909 struct waterfall_context wctx
;
1910 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1912 switch (instr
->intrinsic
) {
1913 case nir_intrinsic_ssbo_atomic_add
:
1916 case nir_intrinsic_ssbo_atomic_imin
:
1919 case nir_intrinsic_ssbo_atomic_umin
:
1922 case nir_intrinsic_ssbo_atomic_imax
:
1925 case nir_intrinsic_ssbo_atomic_umax
:
1928 case nir_intrinsic_ssbo_atomic_and
:
1931 case nir_intrinsic_ssbo_atomic_or
:
1934 case nir_intrinsic_ssbo_atomic_xor
:
1937 case nir_intrinsic_ssbo_atomic_exchange
:
1940 case nir_intrinsic_ssbo_atomic_comp_swap
:
1947 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1951 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1952 return_type
== ctx
->ac
.i64
) {
1953 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1954 get_src(ctx
, instr
->src
[1]),
1955 get_src(ctx
, instr
->src
[2]),
1956 get_src(ctx
, instr
->src
[3]));
1958 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1959 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1961 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1962 params
[arg_count
++] = descriptor
;
1964 if (LLVM_VERSION_MAJOR
>= 9) {
1965 /* XXX: The new raw/struct atomic intrinsics are buggy with
1966 * LLVM 8, see r358579.
1968 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1969 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1970 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1972 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1973 snprintf(name
, sizeof(name
),
1974 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1976 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1977 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1978 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1980 assert(return_type
== ctx
->ac
.i32
);
1981 snprintf(name
, sizeof(name
),
1982 "llvm.amdgcn.buffer.atomic.%s", op
);
1985 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1989 result
= exit_waterfall(ctx
, &wctx
, result
);
1990 if (ctx
->ac
.postponed_kill
)
1991 ac_build_endif(&ctx
->ac
, 7001);
1995 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1996 nir_intrinsic_instr
*instr
)
1998 struct waterfall_context wctx
;
1999 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
2001 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2002 int num_components
= instr
->num_components
;
2003 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2004 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
2006 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2007 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
2008 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
2010 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2011 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
2013 LLVMValueRef results
[4];
2014 for (int i
= 0; i
< num_components
;) {
2015 int num_elems
= num_components
- i
;
2016 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
2018 if (num_elems
* elem_size_bytes
> 16)
2019 num_elems
= 16 / elem_size_bytes
;
2020 int load_bytes
= num_elems
* elem_size_bytes
;
2022 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
2026 if (load_bytes
== 1) {
2027 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
2033 } else if (load_bytes
== 2) {
2034 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2041 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2042 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2044 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2045 vindex
, offset
, immoffset
, 0,
2046 cache_policy
, can_speculate
, false);
2049 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2050 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2051 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2053 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2054 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2056 for (unsigned j
= 0; j
< num_elems
; j
++) {
2057 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2062 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2063 return exit_waterfall(ctx
, &wctx
, ret
);
2066 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2067 struct waterfall_context
*wctx
,
2068 const nir_intrinsic_instr
*instr
)
2070 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2071 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2074 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2075 nir_intrinsic_instr
*instr
)
2077 struct waterfall_context wctx
;
2078 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2081 LLVMValueRef rsrc
= rsrc_base
;
2082 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2083 int num_components
= instr
->num_components
;
2085 if (ctx
->abi
->load_ubo
)
2086 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2088 if (instr
->dest
.ssa
.bit_size
== 64)
2089 num_components
*= 2;
2091 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2092 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2093 LLVMValueRef results
[num_components
];
2094 for (unsigned i
= 0; i
< num_components
; ++i
) {
2095 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2098 if (load_bytes
== 1) {
2099 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2106 assert(load_bytes
== 2);
2107 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2115 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2117 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2118 NULL
, 0, 0, true, true);
2120 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2123 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2124 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2126 return exit_waterfall(ctx
, &wctx
, ret
);
2130 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2131 bool vs_in
, unsigned *vertex_index_out
,
2132 LLVMValueRef
*vertex_index_ref
,
2133 unsigned *const_out
, LLVMValueRef
*indir_out
)
2135 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2136 nir_deref_path path
;
2137 unsigned idx_lvl
= 1;
2139 nir_deref_path_init(&path
, instr
, NULL
);
2141 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2142 if (vertex_index_ref
) {
2143 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2144 if (vertex_index_out
)
2145 *vertex_index_out
= 0;
2147 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2152 uint32_t const_offset
= 0;
2153 LLVMValueRef offset
= NULL
;
2155 if (var
->data
.compact
) {
2156 assert(instr
->deref_type
== nir_deref_type_array
);
2157 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2161 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2162 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2163 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2164 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2166 for (unsigned i
= 0; i
< index
; i
++) {
2167 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2168 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2170 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2171 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2172 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2173 const_offset
+= size
*
2174 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2176 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2177 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2179 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2184 unreachable("Uhandled deref type in get_deref_instr_offset");
2188 nir_deref_path_finish(&path
);
2190 if (const_offset
&& offset
)
2191 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2192 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2195 *const_out
= const_offset
;
2196 *indir_out
= offset
;
2199 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2200 nir_intrinsic_instr
*instr
,
2203 LLVMValueRef result
;
2204 LLVMValueRef vertex_index
= NULL
;
2205 LLVMValueRef indir_index
= NULL
;
2206 unsigned const_index
= 0;
2208 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2210 unsigned location
= var
->data
.location
;
2211 unsigned driver_location
= var
->data
.driver_location
;
2212 const bool is_patch
= var
->data
.patch
||
2213 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2214 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2215 const bool is_compact
= var
->data
.compact
;
2217 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2218 false, NULL
, is_patch
? NULL
: &vertex_index
,
2219 &const_index
, &indir_index
);
2221 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2223 LLVMTypeRef src_component_type
;
2224 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2225 src_component_type
= LLVMGetElementType(dest_type
);
2227 src_component_type
= dest_type
;
2229 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2230 vertex_index
, indir_index
,
2231 const_index
, location
, driver_location
,
2232 var
->data
.location_frac
,
2233 instr
->num_components
,
2234 is_patch
, is_compact
, load_inputs
);
2235 if (instr
->dest
.ssa
.bit_size
== 16) {
2236 result
= ac_to_integer(&ctx
->ac
, result
);
2237 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2239 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2243 type_scalar_size_bytes(const struct glsl_type
*type
)
2245 assert(glsl_type_is_vector_or_scalar(type
) ||
2246 glsl_type_is_matrix(type
));
2247 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2250 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2251 nir_intrinsic_instr
*instr
)
2253 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2254 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2256 LLVMValueRef values
[8];
2258 int ve
= instr
->dest
.ssa
.num_components
;
2260 LLVMValueRef indir_index
;
2262 unsigned const_index
;
2263 unsigned stride
= 4;
2264 int mode
= deref
->mode
;
2267 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2268 var
->data
.mode
== nir_var_shader_in
;
2269 idx
= var
->data
.driver_location
;
2270 comp
= var
->data
.location_frac
;
2271 mode
= var
->data
.mode
;
2273 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2274 &const_index
, &indir_index
);
2276 if (var
->data
.compact
) {
2278 const_index
+= comp
;
2283 if (instr
->dest
.ssa
.bit_size
== 64 &&
2284 (deref
->mode
== nir_var_shader_in
||
2285 deref
->mode
== nir_var_shader_out
||
2286 deref
->mode
== nir_var_function_temp
))
2290 case nir_var_shader_in
:
2291 /* TODO: remove this after RADV switches to lowered IO */
2292 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2293 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2294 return load_tess_varyings(ctx
, instr
, true);
2297 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2298 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2299 LLVMValueRef indir_index
;
2300 unsigned const_index
, vertex_index
;
2301 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2302 &const_index
, &indir_index
);
2303 assert(indir_index
== NULL
);
2305 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2306 var
->data
.driver_location
,
2307 var
->data
.location_frac
,
2308 instr
->num_components
, vertex_index
, const_index
, type
);
2311 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2313 unsigned count
= glsl_count_attribute_slots(
2315 ctx
->stage
== MESA_SHADER_VERTEX
);
2317 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2318 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2319 stride
, false, true);
2321 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2325 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2328 case nir_var_function_temp
:
2329 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2331 unsigned count
= glsl_count_attribute_slots(
2334 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2335 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2336 stride
, true, true);
2338 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2342 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2346 case nir_var_shader_out
:
2347 /* TODO: remove this after RADV switches to lowered IO */
2348 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2349 return load_tess_varyings(ctx
, instr
, false);
2352 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2353 var
->data
.fb_fetch_output
&&
2354 ctx
->abi
->emit_fbfetch
)
2355 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2357 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2359 unsigned count
= glsl_count_attribute_slots(
2362 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2363 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2364 stride
, true, true);
2366 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2370 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2371 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2376 case nir_var_mem_global
: {
2377 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2378 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2379 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2380 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2381 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2382 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2383 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2385 if (stride
!= natural_stride
|| split_loads
) {
2386 if (LLVMGetTypeKind(result_type
) == LLVMVectorTypeKind
)
2387 result_type
= LLVMGetElementType(result_type
);
2389 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2390 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2391 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2393 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2394 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2395 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2396 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2398 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2399 LLVMSetOrdering(values
[i
], LLVMAtomicOrderingMonotonic
);
2401 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2403 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2404 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2405 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2406 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2408 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2409 LLVMSetOrdering(val
, LLVMAtomicOrderingMonotonic
);
2414 unreachable("unhandle variable mode");
2416 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2417 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2421 visit_store_var(struct ac_nir_context
*ctx
,
2422 nir_intrinsic_instr
*instr
)
2424 if (ctx
->ac
.postponed_kill
) {
2425 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2426 ctx
->ac
.postponed_kill
, "");
2427 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2430 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2431 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2433 LLVMValueRef temp_ptr
, value
;
2436 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2437 int writemask
= instr
->const_index
[0];
2438 LLVMValueRef indir_index
;
2439 unsigned const_index
;
2442 get_deref_offset(ctx
, deref
, false,
2443 NULL
, NULL
, &const_index
, &indir_index
);
2444 idx
= var
->data
.driver_location
;
2445 comp
= var
->data
.location_frac
;
2447 if (var
->data
.compact
) {
2448 const_index
+= comp
;
2453 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2454 (deref
->mode
== nir_var_shader_out
||
2455 deref
->mode
== nir_var_function_temp
)) {
2457 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2458 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2461 writemask
= widen_mask(writemask
, 2);
2464 writemask
= writemask
<< comp
;
2466 switch (deref
->mode
) {
2467 case nir_var_shader_out
:
2468 /* TODO: remove this after RADV switches to lowered IO */
2469 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2470 LLVMValueRef vertex_index
= NULL
;
2471 LLVMValueRef indir_index
= NULL
;
2472 unsigned const_index
= 0;
2473 const bool is_patch
= var
->data
.patch
||
2474 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2475 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2477 get_deref_offset(ctx
, deref
, false, NULL
,
2478 is_patch
? NULL
: &vertex_index
,
2479 &const_index
, &indir_index
);
2481 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2482 vertex_index
, indir_index
,
2483 const_index
, src
, writemask
,
2484 var
->data
.location_frac
,
2485 var
->data
.driver_location
);
2489 for (unsigned chan
= 0; chan
< 8; chan
++) {
2491 if (!(writemask
& (1 << chan
)))
2494 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2496 if (var
->data
.compact
)
2499 unsigned count
= glsl_count_attribute_slots(
2502 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2503 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2504 stride
, true, true);
2506 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2507 value
, indir_index
, "");
2508 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2509 count
, stride
, tmp_vec
);
2512 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2514 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2518 case nir_var_function_temp
:
2519 for (unsigned chan
= 0; chan
< 8; chan
++) {
2520 if (!(writemask
& (1 << chan
)))
2523 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2525 unsigned count
= glsl_count_attribute_slots(
2528 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2529 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2532 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2533 value
, indir_index
, "");
2534 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2537 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2539 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2544 case nir_var_mem_global
: {
2545 int writemask
= instr
->const_index
[0];
2546 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2547 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2549 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2550 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2551 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2552 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2553 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2555 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2556 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2557 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2559 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2560 stride
== natural_stride
&& !split_stores
) {
2561 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2562 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2563 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2565 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2566 LLVMGetElementType(LLVMTypeOf(address
)), "");
2567 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2569 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2570 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2572 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2573 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMVectorTypeKind
)
2574 val_type
= LLVMGetElementType(val_type
);
2576 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2577 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2578 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2579 for (unsigned chan
= 0; chan
< 4; chan
++) {
2580 if (!(writemask
& (1 << chan
)))
2583 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2585 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2586 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2588 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2589 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2590 LLVMValueRef store
= LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2592 if (nir_intrinsic_access(instr
) & (ACCESS_COHERENT
| ACCESS_VOLATILE
))
2593 LLVMSetOrdering(store
, LLVMAtomicOrderingMonotonic
);
2603 if (ctx
->ac
.postponed_kill
)
2604 ac_build_endif(&ctx
->ac
, 7002);
2608 visit_store_output(struct ac_nir_context
*ctx
, nir_intrinsic_instr
*instr
)
2610 if (ctx
->ac
.postponed_kill
) {
2611 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2612 ctx
->ac
.postponed_kill
, "");
2613 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2616 unsigned base
= nir_intrinsic_base(instr
);
2617 unsigned writemask
= nir_intrinsic_write_mask(instr
);
2618 unsigned component
= nir_intrinsic_component(instr
);
2619 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2620 nir_src offset
= *nir_get_io_offset_src(instr
);
2621 LLVMValueRef indir_index
= NULL
;
2623 if (nir_src_is_const(offset
))
2624 assert(nir_src_as_uint(offset
) == 0);
2626 indir_index
= get_src(ctx
, offset
);
2628 switch (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
))) {
2632 writemask
= widen_mask(writemask
, 2);
2633 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2634 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2638 unreachable("unhandled store_output bit size");
2642 writemask
<<= component
;
2644 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2645 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
2646 LLVMValueRef vertex_index
=
2647 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
2649 ctx
->abi
->store_tcs_outputs(ctx
->abi
, NULL
,
2650 vertex_index
, indir_index
,
2652 component
, base
* 4);
2656 /* No indirect indexing is allowed after this point. */
2657 assert(!indir_index
);
2659 for (unsigned chan
= 0; chan
< 8; chan
++) {
2660 if (!(writemask
& (1 << chan
)))
2663 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
2664 LLVMBuildStore(ctx
->ac
.builder
, value
,
2665 ctx
->abi
->outputs
[base
* 4 + chan
]);
2668 if (ctx
->ac
.postponed_kill
)
2669 ac_build_endif(&ctx
->ac
, 7002);
2672 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2675 case GLSL_SAMPLER_DIM_BUF
:
2677 case GLSL_SAMPLER_DIM_1D
:
2678 return array
? 2 : 1;
2679 case GLSL_SAMPLER_DIM_2D
:
2680 return array
? 3 : 2;
2681 case GLSL_SAMPLER_DIM_MS
:
2682 return array
? 4 : 3;
2683 case GLSL_SAMPLER_DIM_3D
:
2684 case GLSL_SAMPLER_DIM_CUBE
:
2686 case GLSL_SAMPLER_DIM_RECT
:
2687 case GLSL_SAMPLER_DIM_SUBPASS
:
2689 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2697 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2698 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2699 LLVMValueRef coord_z
,
2700 LLVMValueRef sample_index
,
2701 LLVMValueRef fmask_desc_ptr
)
2703 unsigned sample_chan
= coord_z
? 3 : 2;
2704 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2705 addr
[sample_chan
] = sample_index
;
2707 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2708 return addr
[sample_chan
];
2711 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2713 assert(instr
->src
[0].is_ssa
);
2714 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2717 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2718 const nir_intrinsic_instr
*instr
,
2719 LLVMValueRef dynamic_index
,
2720 enum ac_descriptor_type desc_type
,
2723 nir_deref_instr
*deref_instr
=
2724 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2725 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2727 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2730 static void get_image_coords(struct ac_nir_context
*ctx
,
2731 const nir_intrinsic_instr
*instr
,
2732 LLVMValueRef dynamic_desc_index
,
2733 struct ac_image_args
*args
,
2734 enum glsl_sampler_dim dim
,
2737 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2738 LLVMValueRef masks
[] = {
2739 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2740 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2742 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2745 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2746 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2747 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2748 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2749 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2750 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2751 count
= image_type_to_components_count(dim
, is_array
);
2753 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2754 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2755 LLVMValueRef fmask_load_address
[3];
2757 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2758 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2760 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2762 fmask_load_address
[2] = NULL
;
2764 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2765 fmask_load_address
[0],
2766 fmask_load_address
[1],
2767 fmask_load_address
[2],
2769 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2770 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2772 if (count
== 1 && !gfx9_1d
) {
2773 if (instr
->src
[1].ssa
->num_components
)
2774 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2776 args
->coords
[0] = src0
;
2781 for (chan
= 0; chan
< count
; ++chan
) {
2782 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2787 args
->coords
[2] = args
->coords
[1];
2788 args
->coords
[1] = ctx
->ac
.i32_0
;
2790 args
->coords
[1] = ctx
->ac
.i32_0
;
2793 if (ctx
->ac
.chip_class
== GFX9
&&
2794 dim
== GLSL_SAMPLER_DIM_2D
&&
2796 /* The hw can't bind a slice of a 3D image as a 2D
2797 * image, because it ignores BASE_ARRAY if the target
2798 * is 3D. The workaround is to read BASE_ARRAY and set
2799 * it as the 3rd address operand for all 2D images.
2801 LLVMValueRef first_layer
, const5
, mask
;
2803 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2804 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2805 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2806 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2808 args
->coords
[count
] = first_layer
;
2814 args
->coords
[count
] = sample_index
;
2820 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2821 const nir_intrinsic_instr
*instr
,
2822 LLVMValueRef dynamic_index
,
2823 bool write
, bool atomic
)
2825 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2826 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2827 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2828 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2829 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2831 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2832 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2833 elem_count
, stride
, "");
2835 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2836 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2841 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2842 struct waterfall_context
*wctx
,
2843 const nir_intrinsic_instr
*instr
)
2845 nir_deref_instr
*deref_instr
= NULL
;
2847 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2848 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2850 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2851 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2854 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2855 const nir_intrinsic_instr
*instr
,
2860 enum glsl_sampler_dim dim
;
2861 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2864 dim
= nir_intrinsic_image_dim(instr
);
2865 is_array
= nir_intrinsic_image_array(instr
);
2867 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2868 const struct glsl_type
*type
= image_deref
->type
;
2869 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2870 dim
= glsl_get_sampler_dim(type
);
2871 access
|= var
->data
.access
;
2872 is_array
= glsl_sampler_type_is_array(type
);
2875 struct waterfall_context wctx
;
2876 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2878 struct ac_image_args args
= {};
2880 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2882 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2883 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2884 unsigned num_channels
= util_last_bit(mask
);
2885 LLVMValueRef rsrc
, vindex
;
2887 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2888 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2891 assert(instr
->dest
.is_ssa
);
2892 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2893 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2894 ctx
->ac
.i32_0
, num_channels
,
2897 instr
->dest
.ssa
.bit_size
== 16);
2898 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2900 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2901 res
= ac_to_integer(&ctx
->ac
, res
);
2903 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2905 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2906 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2907 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2908 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2910 args
.lod
= get_src(ctx
, instr
->src
[3]);
2912 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2914 assert(instr
->dest
.is_ssa
);
2915 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
2917 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2919 return exit_waterfall(ctx
, &wctx
, res
);
2922 static void visit_image_store(struct ac_nir_context
*ctx
,
2923 const nir_intrinsic_instr
*instr
,
2926 if (ctx
->ac
.postponed_kill
) {
2927 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2928 ctx
->ac
.postponed_kill
, "");
2929 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2932 enum glsl_sampler_dim dim
;
2933 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
2937 dim
= nir_intrinsic_image_dim(instr
);
2938 is_array
= nir_intrinsic_image_array(instr
);
2940 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2941 const struct glsl_type
*type
= image_deref
->type
;
2942 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2943 dim
= glsl_get_sampler_dim(type
);
2944 access
|= var
->data
.access
;
2945 is_array
= glsl_sampler_type_is_array(type
);
2948 struct waterfall_context wctx
;
2949 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2951 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2952 struct ac_image_args args
= {};
2954 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2956 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2957 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2958 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2959 unsigned src_channels
= ac_get_llvm_num_components(src
);
2960 LLVMValueRef vindex
;
2962 if (src_channels
== 3)
2963 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2965 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2966 get_src(ctx
, instr
->src
[1]),
2969 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2970 ctx
->ac
.i32_0
, args
.cache_policy
);
2972 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2974 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2975 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2976 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2977 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2978 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2980 args
.lod
= get_src(ctx
, instr
->src
[4]);
2982 args
.d16
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(args
.data
[0])) == 16;
2984 ac_build_image_opcode(&ctx
->ac
, &args
);
2987 exit_waterfall(ctx
, &wctx
, NULL
);
2988 if (ctx
->ac
.postponed_kill
)
2989 ac_build_endif(&ctx
->ac
, 7003);
2992 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2993 const nir_intrinsic_instr
*instr
,
2996 if (ctx
->ac
.postponed_kill
) {
2997 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2998 ctx
->ac
.postponed_kill
, "");
2999 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
3002 LLVMValueRef params
[7];
3003 int param_count
= 0;
3005 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
3006 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
3007 const char *atomic_name
;
3008 char intrinsic_name
[64];
3009 enum ac_atomic_op atomic_subop
;
3010 ASSERTED
int length
;
3012 enum glsl_sampler_dim dim
;
3015 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
3016 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
3017 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
3018 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
3019 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
3020 assert(format
== GL_R32UI
|| format
== GL_R32I
);
3022 dim
= nir_intrinsic_image_dim(instr
);
3023 is_array
= nir_intrinsic_image_array(instr
);
3025 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3026 dim
= glsl_get_sampler_dim(type
);
3027 is_array
= glsl_sampler_type_is_array(type
);
3030 struct waterfall_context wctx
;
3031 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3033 switch (instr
->intrinsic
) {
3034 case nir_intrinsic_bindless_image_atomic_add
:
3035 case nir_intrinsic_image_deref_atomic_add
:
3036 atomic_name
= "add";
3037 atomic_subop
= ac_atomic_add
;
3039 case nir_intrinsic_bindless_image_atomic_imin
:
3040 case nir_intrinsic_image_deref_atomic_imin
:
3041 atomic_name
= "smin";
3042 atomic_subop
= ac_atomic_smin
;
3044 case nir_intrinsic_bindless_image_atomic_umin
:
3045 case nir_intrinsic_image_deref_atomic_umin
:
3046 atomic_name
= "umin";
3047 atomic_subop
= ac_atomic_umin
;
3049 case nir_intrinsic_bindless_image_atomic_imax
:
3050 case nir_intrinsic_image_deref_atomic_imax
:
3051 atomic_name
= "smax";
3052 atomic_subop
= ac_atomic_smax
;
3054 case nir_intrinsic_bindless_image_atomic_umax
:
3055 case nir_intrinsic_image_deref_atomic_umax
:
3056 atomic_name
= "umax";
3057 atomic_subop
= ac_atomic_umax
;
3059 case nir_intrinsic_bindless_image_atomic_and
:
3060 case nir_intrinsic_image_deref_atomic_and
:
3061 atomic_name
= "and";
3062 atomic_subop
= ac_atomic_and
;
3064 case nir_intrinsic_bindless_image_atomic_or
:
3065 case nir_intrinsic_image_deref_atomic_or
:
3067 atomic_subop
= ac_atomic_or
;
3069 case nir_intrinsic_bindless_image_atomic_xor
:
3070 case nir_intrinsic_image_deref_atomic_xor
:
3071 atomic_name
= "xor";
3072 atomic_subop
= ac_atomic_xor
;
3074 case nir_intrinsic_bindless_image_atomic_exchange
:
3075 case nir_intrinsic_image_deref_atomic_exchange
:
3076 atomic_name
= "swap";
3077 atomic_subop
= ac_atomic_swap
;
3079 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3080 case nir_intrinsic_image_deref_atomic_comp_swap
:
3081 atomic_name
= "cmpswap";
3082 atomic_subop
= 0; /* not used */
3084 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3085 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
3086 atomic_name
= "inc";
3087 atomic_subop
= ac_atomic_inc_wrap
;
3090 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3091 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3092 atomic_name
= "dec";
3093 atomic_subop
= ac_atomic_dec_wrap
;
3100 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
3101 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3103 LLVMValueRef result
;
3104 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3105 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
3106 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
3107 ctx
->ac
.i32_0
, ""); /* vindex */
3108 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3109 if (LLVM_VERSION_MAJOR
>= 9) {
3110 /* XXX: The new raw/struct atomic intrinsics are buggy
3111 * with LLVM 8, see r358579.
3113 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3114 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3116 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3117 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3119 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3121 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3122 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3125 assert(length
< sizeof(intrinsic_name
));
3126 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3127 params
, param_count
, 0);
3129 struct ac_image_args args
= {};
3130 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3131 args
.atomic
= atomic_subop
;
3132 args
.data
[0] = params
[0];
3134 args
.data
[1] = params
[1];
3135 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3136 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3137 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3139 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3142 result
= exit_waterfall(ctx
, &wctx
, result
);
3143 if (ctx
->ac
.postponed_kill
)
3144 ac_build_endif(&ctx
->ac
, 7004);
3148 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3149 nir_intrinsic_instr
*instr
)
3151 struct waterfall_context wctx
;
3152 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3153 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3155 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3157 return exit_waterfall(ctx
, &wctx
, ret
);
3160 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3161 const nir_intrinsic_instr
*instr
,
3166 enum glsl_sampler_dim dim
;
3169 dim
= nir_intrinsic_image_dim(instr
);
3170 is_array
= nir_intrinsic_image_array(instr
);
3172 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3173 dim
= glsl_get_sampler_dim(type
);
3174 is_array
= glsl_sampler_type_is_array(type
);
3177 struct waterfall_context wctx
;
3178 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3180 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3181 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3184 struct ac_image_args args
= { 0 };
3186 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3188 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3189 args
.opcode
= ac_image_get_resinfo
;
3190 assert(nir_src_as_uint(instr
->src
[1]) == 0);
3191 args
.lod
= ctx
->ac
.i32_0
;
3192 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3194 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3196 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3198 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3199 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3200 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3201 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3202 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3205 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3206 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3207 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3211 return exit_waterfall(ctx
, &wctx
, res
);
3214 static void emit_membar(struct ac_llvm_context
*ac
,
3215 const nir_intrinsic_instr
*instr
)
3217 unsigned wait_flags
= 0;
3219 switch (instr
->intrinsic
) {
3220 case nir_intrinsic_memory_barrier
:
3221 case nir_intrinsic_group_memory_barrier
:
3222 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3224 case nir_intrinsic_memory_barrier_buffer
:
3225 case nir_intrinsic_memory_barrier_image
:
3226 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3228 case nir_intrinsic_memory_barrier_shared
:
3229 wait_flags
= AC_WAIT_LGKM
;
3235 ac_build_waitcnt(ac
, wait_flags
);
3238 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3240 /* GFX6 only (thanks to a hw bug workaround):
3241 * The real barrier instruction isn’t needed, because an entire patch
3242 * always fits into a single wave.
3244 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3245 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3248 ac_build_s_barrier(ac
);
3251 static void emit_discard(struct ac_nir_context
*ctx
,
3252 const nir_intrinsic_instr
*instr
)
3256 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3257 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3258 get_src(ctx
, instr
->src
[0]),
3261 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3262 cond
= ctx
->ac
.i1false
;
3265 ac_build_kill_if_false(&ctx
->ac
, cond
);
3268 static void emit_demote(struct ac_nir_context
*ctx
,
3269 const nir_intrinsic_instr
*instr
)
3273 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3274 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3275 get_src(ctx
, instr
->src
[0]),
3278 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3279 cond
= ctx
->ac
.i1false
;
3282 /* Kill immediately while maintaining WQM. */
3283 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3285 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3286 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3287 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3292 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3294 LLVMValueRef result
;
3295 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3296 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3297 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3298 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3300 if (ctx
->ac
.wave_size
== 32)
3301 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3302 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3304 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3308 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3310 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3311 LLVMValueRef result
;
3312 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3313 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3314 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3315 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3317 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3322 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3324 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3325 return LLVMBuildAnd(ctx
->ac
.builder
,
3326 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3327 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3329 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3334 visit_first_invocation(struct ac_nir_context
*ctx
)
3336 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3337 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3339 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3340 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3341 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3342 ctx
->ac
.iN_wavemask
, args
, 2,
3343 AC_FUNC_ATTR_NOUNWIND
|
3344 AC_FUNC_ATTR_READNONE
);
3346 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3350 visit_load_shared(struct ac_nir_context
*ctx
,
3351 const nir_intrinsic_instr
*instr
)
3353 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3355 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3356 instr
->dest
.ssa
.bit_size
);
3358 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3359 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3360 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3361 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3364 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3365 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3369 visit_store_shared(struct ac_nir_context
*ctx
,
3370 const nir_intrinsic_instr
*instr
)
3372 LLVMValueRef derived_ptr
, data
,index
;
3373 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3375 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3376 instr
->src
[0].ssa
->bit_size
);
3377 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3379 int writemask
= nir_intrinsic_write_mask(instr
);
3380 for (int chan
= 0; chan
< 4; chan
++) {
3381 if (!(writemask
& (1 << chan
))) {
3384 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3385 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3386 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3387 LLVMBuildStore(builder
, data
, derived_ptr
);
3391 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3392 const nir_intrinsic_instr
*instr
,
3393 LLVMValueRef ptr
, int src_idx
)
3395 if (ctx
->ac
.postponed_kill
) {
3396 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3397 ctx
->ac
.postponed_kill
, "");
3398 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3401 LLVMValueRef result
;
3402 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3404 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3406 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3407 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3408 if (deref
->mode
== nir_var_mem_global
) {
3409 /* use "singlethread" sync scope to implement relaxed ordering */
3410 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3412 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3413 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3417 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3418 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3419 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3420 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3421 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3423 LLVMAtomicRMWBinOp op
;
3424 switch (instr
->intrinsic
) {
3425 case nir_intrinsic_shared_atomic_add
:
3426 case nir_intrinsic_deref_atomic_add
:
3427 op
= LLVMAtomicRMWBinOpAdd
;
3429 case nir_intrinsic_shared_atomic_umin
:
3430 case nir_intrinsic_deref_atomic_umin
:
3431 op
= LLVMAtomicRMWBinOpUMin
;
3433 case nir_intrinsic_shared_atomic_umax
:
3434 case nir_intrinsic_deref_atomic_umax
:
3435 op
= LLVMAtomicRMWBinOpUMax
;
3437 case nir_intrinsic_shared_atomic_imin
:
3438 case nir_intrinsic_deref_atomic_imin
:
3439 op
= LLVMAtomicRMWBinOpMin
;
3441 case nir_intrinsic_shared_atomic_imax
:
3442 case nir_intrinsic_deref_atomic_imax
:
3443 op
= LLVMAtomicRMWBinOpMax
;
3445 case nir_intrinsic_shared_atomic_and
:
3446 case nir_intrinsic_deref_atomic_and
:
3447 op
= LLVMAtomicRMWBinOpAnd
;
3449 case nir_intrinsic_shared_atomic_or
:
3450 case nir_intrinsic_deref_atomic_or
:
3451 op
= LLVMAtomicRMWBinOpOr
;
3453 case nir_intrinsic_shared_atomic_xor
:
3454 case nir_intrinsic_deref_atomic_xor
:
3455 op
= LLVMAtomicRMWBinOpXor
;
3457 case nir_intrinsic_shared_atomic_exchange
:
3458 case nir_intrinsic_deref_atomic_exchange
:
3459 op
= LLVMAtomicRMWBinOpXchg
;
3461 #if LLVM_VERSION_MAJOR >= 10
3462 case nir_intrinsic_shared_atomic_fadd
:
3463 case nir_intrinsic_deref_atomic_fadd
:
3464 op
= LLVMAtomicRMWBinOpFAdd
;
3473 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_fadd
||
3474 instr
->intrinsic
== nir_intrinsic_deref_atomic_fadd
) {
3475 val
= ac_to_float(&ctx
->ac
, src
);
3477 val
= ac_to_integer(&ctx
->ac
, src
);
3480 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, val
, sync_scope
);
3483 if (ctx
->ac
.postponed_kill
)
3484 ac_build_endif(&ctx
->ac
, 7005);
3488 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3490 LLVMValueRef values
[2];
3491 LLVMValueRef pos
[2];
3493 pos
[0] = ac_to_float(&ctx
->ac
,
3494 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3495 pos
[1] = ac_to_float(&ctx
->ac
,
3496 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3498 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3499 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3500 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3503 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3504 enum glsl_interp_mode interp
, unsigned location
)
3507 case INTERP_MODE_FLAT
:
3510 case INTERP_MODE_SMOOTH
:
3511 case INTERP_MODE_NONE
:
3512 if (location
== INTERP_CENTER
)
3513 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3514 else if (location
== INTERP_CENTROID
)
3515 return ctx
->abi
->persp_centroid
;
3516 else if (location
== INTERP_SAMPLE
)
3517 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3519 case INTERP_MODE_NOPERSPECTIVE
:
3520 if (location
== INTERP_CENTER
)
3521 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3522 else if (location
== INTERP_CENTROID
)
3523 return ctx
->abi
->linear_centroid
;
3524 else if (location
== INTERP_SAMPLE
)
3525 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3531 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3534 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3535 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3538 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3540 LLVMValueRef offset
)
3542 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3543 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3544 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3546 LLVMValueRef ij_out
[2];
3547 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3550 * take the I then J parameters, and the DDX/Y for it, and
3551 * calculate the IJ inputs for the interpolator.
3552 * temp1 = ddx * offset/sample.x + I;
3553 * interp_param.I = ddy * offset/sample.y + temp1;
3554 * temp1 = ddx * offset/sample.x + J;
3555 * interp_param.J = ddy * offset/sample.y + temp1;
3557 for (unsigned i
= 0; i
< 2; i
++) {
3558 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3559 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3560 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3561 ddxy_out
, ix_ll
, "");
3562 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3563 ddxy_out
, iy_ll
, "");
3564 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3565 interp_param
, ix_ll
, "");
3566 LLVMValueRef temp1
, temp2
;
3568 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3571 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3572 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3574 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3575 temp2
, ctx
->ac
.i32
, "");
3577 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3578 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3581 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3584 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3585 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3588 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3590 LLVMValueRef sample_id
)
3592 if (ctx
->abi
->interp_at_sample_force_center
)
3593 return barycentric_center(ctx
, mode
);
3595 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3597 /* fetch sample ID */
3598 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3600 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3601 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3602 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3603 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3604 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3605 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3607 return barycentric_offset(ctx
, mode
, offset
);
3611 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3614 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3615 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3618 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3620 return LLVMBuildBitCast(ctx
->ac
.builder
,
3621 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3625 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3626 LLVMValueRef interp_param
,
3627 unsigned index
, unsigned comp_start
,
3628 unsigned num_components
,
3631 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3632 LLVMValueRef interp_param_f
;
3634 interp_param_f
= LLVMBuildBitCast(ctx
->ac
.builder
,
3635 interp_param
, ctx
->ac
.v2f32
, "");
3636 LLVMValueRef i
= LLVMBuildExtractElement(
3637 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_0
, "");
3638 LLVMValueRef j
= LLVMBuildExtractElement(
3639 ctx
->ac
.builder
, interp_param_f
, ctx
->ac
.i32_1
, "");
3641 /* Workaround for issue 2647: kill threads with infinite interpolation coeffs */
3642 if (ctx
->verified_interp
&&
3643 !_mesa_hash_table_search(ctx
->verified_interp
, interp_param
)) {
3644 LLVMValueRef args
[2];
3646 args
[1] = LLVMConstInt(ctx
->ac
.i32
, S_NAN
| Q_NAN
| N_INFINITY
| P_INFINITY
, false);
3647 LLVMValueRef cond
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.class.f32", ctx
->ac
.i1
,
3648 args
, 2, AC_FUNC_ATTR_READNONE
);
3649 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildNot(ctx
->ac
.builder
, cond
, ""));
3650 _mesa_hash_table_insert(ctx
->verified_interp
, interp_param
, interp_param
);
3653 LLVMValueRef values
[4];
3654 assert(bitsize
== 16 || bitsize
== 32);
3655 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3656 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3657 if (bitsize
== 16) {
3658 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3659 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3661 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3662 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3666 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3669 static LLVMValueRef
visit_load(struct ac_nir_context
*ctx
,
3670 nir_intrinsic_instr
*instr
, bool is_output
)
3672 LLVMValueRef values
[8];
3673 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3674 LLVMTypeRef component_type
;
3675 unsigned base
= nir_intrinsic_base(instr
);
3676 unsigned component
= nir_intrinsic_component(instr
);
3677 unsigned count
= instr
->dest
.ssa
.num_components
*
3678 (instr
->dest
.ssa
.bit_size
== 64 ? 2 : 1);
3679 nir_src
*vertex_index_src
= nir_get_io_vertex_index_src(instr
);
3680 LLVMValueRef vertex_index
=
3681 vertex_index_src
? get_src(ctx
, *vertex_index_src
) : NULL
;
3682 nir_src offset
= *nir_get_io_offset_src(instr
);
3683 LLVMValueRef indir_index
= NULL
;
3685 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
3686 component_type
= LLVMGetElementType(dest_type
);
3688 component_type
= dest_type
;
3690 if (nir_src_is_const(offset
))
3691 assert(nir_src_as_uint(offset
) == 0);
3693 indir_index
= get_src(ctx
, offset
);
3695 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3696 (ctx
->stage
== MESA_SHADER_TESS_EVAL
&& !is_output
)) {
3697 LLVMValueRef result
=
3698 ctx
->abi
->load_tess_varyings(ctx
->abi
, component_type
,
3699 vertex_index
, indir_index
,
3702 instr
->num_components
,
3703 false, false, !is_output
);
3704 if (instr
->dest
.ssa
.bit_size
== 16) {
3705 result
= ac_to_integer(&ctx
->ac
, result
);
3706 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
3708 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3711 /* No indirect indexing is allowed after this point. */
3712 assert(!indir_index
);
3714 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3715 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3716 assert(nir_src_is_const(*vertex_index_src
));
3718 return ctx
->abi
->load_inputs(ctx
->abi
, 0, base
* 4, component
,
3719 instr
->num_components
,
3720 nir_src_as_uint(*vertex_index_src
),
3724 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& is_output
&&
3725 nir_intrinsic_io_semantics(instr
).fb_fetch_output
)
3726 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
3728 /* Other non-fragment cases have inputs and outputs in temporaries. */
3729 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
3730 for (unsigned chan
= component
; chan
< count
+ component
; chan
++) {
3732 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
3733 ctx
->abi
->outputs
[base
* 4 + chan
], "");
3735 values
[chan
] = ctx
->abi
->inputs
[base
* 4 + chan
];
3737 values
[chan
] = LLVMGetUndef(ctx
->ac
.i32
);
3740 LLVMValueRef result
= ac_build_varying_gather_values(&ctx
->ac
, values
, count
, component
);
3741 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3744 /* Fragment shader inputs. */
3745 unsigned vertex_id
= 2; /* P0 */
3747 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3748 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3750 switch (src0
[0].i32
) {
3761 unreachable("Invalid vertex index");
3765 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
, false);
3767 for (unsigned chan
= 0; chan
< count
; chan
++) {
3768 if (component
+ chan
> 4)
3769 attr_number
= LLVMConstInt(ctx
->ac
.i32
, base
+ 1, false);
3770 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3771 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3772 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3775 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3776 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3777 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3778 instr
->dest
.ssa
.bit_size
== 16 ? ctx
->ac
.i16
3782 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, count
);
3783 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
3786 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3787 nir_intrinsic_instr
*instr
)
3789 LLVMValueRef result
= NULL
;
3791 switch (instr
->intrinsic
) {
3792 case nir_intrinsic_ballot
:
3793 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3794 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3795 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3797 case nir_intrinsic_read_invocation
:
3798 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3799 get_src(ctx
, instr
->src
[1]));
3801 case nir_intrinsic_read_first_invocation
:
3802 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3804 case nir_intrinsic_load_subgroup_invocation
:
3805 result
= ac_get_thread_id(&ctx
->ac
);
3807 case nir_intrinsic_load_work_group_id
: {
3808 LLVMValueRef values
[3];
3810 for (int i
= 0; i
< 3; i
++) {
3811 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3812 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3815 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3818 case nir_intrinsic_load_base_vertex
:
3819 case nir_intrinsic_load_first_vertex
:
3820 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3822 case nir_intrinsic_load_local_group_size
:
3823 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3825 case nir_intrinsic_load_vertex_id
:
3826 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3827 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3828 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3830 case nir_intrinsic_load_vertex_id_zero_base
: {
3831 result
= ctx
->abi
->vertex_id
;
3834 case nir_intrinsic_load_local_invocation_id
: {
3835 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3838 case nir_intrinsic_load_base_instance
:
3839 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3841 case nir_intrinsic_load_draw_id
:
3842 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3844 case nir_intrinsic_load_view_index
:
3845 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3847 case nir_intrinsic_load_invocation_id
:
3848 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3849 result
= ac_unpack_param(&ctx
->ac
,
3850 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3853 if (ctx
->ac
.chip_class
>= GFX10
) {
3854 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3855 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3856 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3858 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3862 case nir_intrinsic_load_primitive_id
:
3863 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3864 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3865 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3866 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3867 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3868 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3870 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3872 case nir_intrinsic_load_sample_id
:
3873 result
= ac_unpack_param(&ctx
->ac
,
3874 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3877 case nir_intrinsic_load_sample_pos
:
3878 result
= load_sample_pos(ctx
);
3880 case nir_intrinsic_load_sample_mask_in
:
3881 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3883 case nir_intrinsic_load_frag_coord
: {
3884 LLVMValueRef values
[4] = {
3885 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3886 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3887 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3888 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3889 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3891 result
= ac_to_integer(&ctx
->ac
,
3892 ac_build_gather_values(&ctx
->ac
, values
, 4));
3895 case nir_intrinsic_load_layer_id
:
3896 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3898 case nir_intrinsic_load_front_face
:
3899 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3901 case nir_intrinsic_load_helper_invocation
:
3902 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3904 case nir_intrinsic_is_helper_invocation
:
3905 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3907 case nir_intrinsic_load_color0
:
3908 result
= ctx
->abi
->color0
;
3910 case nir_intrinsic_load_color1
:
3911 result
= ctx
->abi
->color1
;
3913 case nir_intrinsic_load_user_data_amd
:
3914 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3915 result
= ctx
->abi
->user_data
;
3917 case nir_intrinsic_load_instance_id
:
3918 result
= ctx
->abi
->instance_id
;
3920 case nir_intrinsic_load_num_work_groups
:
3921 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3923 case nir_intrinsic_load_local_invocation_index
:
3924 result
= visit_load_local_invocation_index(ctx
);
3926 case nir_intrinsic_load_subgroup_id
:
3927 result
= visit_load_subgroup_id(ctx
);
3929 case nir_intrinsic_load_num_subgroups
:
3930 result
= visit_load_num_subgroups(ctx
);
3932 case nir_intrinsic_first_invocation
:
3933 result
= visit_first_invocation(ctx
);
3935 case nir_intrinsic_load_push_constant
:
3936 result
= visit_load_push_constant(ctx
, instr
);
3938 case nir_intrinsic_vulkan_resource_index
: {
3939 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3940 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3941 unsigned binding
= nir_intrinsic_binding(instr
);
3943 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3947 case nir_intrinsic_vulkan_resource_reindex
:
3948 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3950 case nir_intrinsic_store_ssbo
:
3951 visit_store_ssbo(ctx
, instr
);
3953 case nir_intrinsic_load_ssbo
:
3954 result
= visit_load_buffer(ctx
, instr
);
3956 case nir_intrinsic_ssbo_atomic_add
:
3957 case nir_intrinsic_ssbo_atomic_imin
:
3958 case nir_intrinsic_ssbo_atomic_umin
:
3959 case nir_intrinsic_ssbo_atomic_imax
:
3960 case nir_intrinsic_ssbo_atomic_umax
:
3961 case nir_intrinsic_ssbo_atomic_and
:
3962 case nir_intrinsic_ssbo_atomic_or
:
3963 case nir_intrinsic_ssbo_atomic_xor
:
3964 case nir_intrinsic_ssbo_atomic_exchange
:
3965 case nir_intrinsic_ssbo_atomic_comp_swap
:
3966 result
= visit_atomic_ssbo(ctx
, instr
);
3968 case nir_intrinsic_load_ubo
:
3969 result
= visit_load_ubo_buffer(ctx
, instr
);
3971 case nir_intrinsic_get_buffer_size
:
3972 result
= visit_get_buffer_size(ctx
, instr
);
3974 case nir_intrinsic_load_deref
:
3975 result
= visit_load_var(ctx
, instr
);
3977 case nir_intrinsic_store_deref
:
3978 visit_store_var(ctx
, instr
);
3980 case nir_intrinsic_load_input
:
3981 case nir_intrinsic_load_input_vertex
:
3982 case nir_intrinsic_load_per_vertex_input
:
3983 result
= visit_load(ctx
, instr
, false);
3985 case nir_intrinsic_load_output
:
3986 case nir_intrinsic_load_per_vertex_output
:
3987 result
= visit_load(ctx
, instr
, true);
3989 case nir_intrinsic_store_output
:
3990 case nir_intrinsic_store_per_vertex_output
:
3991 visit_store_output(ctx
, instr
);
3993 case nir_intrinsic_load_shared
:
3994 result
= visit_load_shared(ctx
, instr
);
3996 case nir_intrinsic_store_shared
:
3997 visit_store_shared(ctx
, instr
);
3999 case nir_intrinsic_bindless_image_samples
:
4000 case nir_intrinsic_image_deref_samples
:
4001 result
= visit_image_samples(ctx
, instr
);
4003 case nir_intrinsic_bindless_image_load
:
4004 result
= visit_image_load(ctx
, instr
, true);
4006 case nir_intrinsic_image_deref_load
:
4007 result
= visit_image_load(ctx
, instr
, false);
4009 case nir_intrinsic_bindless_image_store
:
4010 visit_image_store(ctx
, instr
, true);
4012 case nir_intrinsic_image_deref_store
:
4013 visit_image_store(ctx
, instr
, false);
4015 case nir_intrinsic_bindless_image_atomic_add
:
4016 case nir_intrinsic_bindless_image_atomic_imin
:
4017 case nir_intrinsic_bindless_image_atomic_umin
:
4018 case nir_intrinsic_bindless_image_atomic_imax
:
4019 case nir_intrinsic_bindless_image_atomic_umax
:
4020 case nir_intrinsic_bindless_image_atomic_and
:
4021 case nir_intrinsic_bindless_image_atomic_or
:
4022 case nir_intrinsic_bindless_image_atomic_xor
:
4023 case nir_intrinsic_bindless_image_atomic_exchange
:
4024 case nir_intrinsic_bindless_image_atomic_comp_swap
:
4025 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
4026 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
4027 result
= visit_image_atomic(ctx
, instr
, true);
4029 case nir_intrinsic_image_deref_atomic_add
:
4030 case nir_intrinsic_image_deref_atomic_imin
:
4031 case nir_intrinsic_image_deref_atomic_umin
:
4032 case nir_intrinsic_image_deref_atomic_imax
:
4033 case nir_intrinsic_image_deref_atomic_umax
:
4034 case nir_intrinsic_image_deref_atomic_and
:
4035 case nir_intrinsic_image_deref_atomic_or
:
4036 case nir_intrinsic_image_deref_atomic_xor
:
4037 case nir_intrinsic_image_deref_atomic_exchange
:
4038 case nir_intrinsic_image_deref_atomic_comp_swap
:
4039 case nir_intrinsic_image_deref_atomic_inc_wrap
:
4040 case nir_intrinsic_image_deref_atomic_dec_wrap
:
4041 result
= visit_image_atomic(ctx
, instr
, false);
4043 case nir_intrinsic_bindless_image_size
:
4044 result
= visit_image_size(ctx
, instr
, true);
4046 case nir_intrinsic_image_deref_size
:
4047 result
= visit_image_size(ctx
, instr
, false);
4049 case nir_intrinsic_shader_clock
:
4050 result
= ac_build_shader_clock(&ctx
->ac
,
4051 nir_intrinsic_memory_scope(instr
));
4053 case nir_intrinsic_discard
:
4054 case nir_intrinsic_discard_if
:
4055 emit_discard(ctx
, instr
);
4057 case nir_intrinsic_demote
:
4058 case nir_intrinsic_demote_if
:
4059 emit_demote(ctx
, instr
);
4061 case nir_intrinsic_memory_barrier
:
4062 case nir_intrinsic_group_memory_barrier
:
4063 case nir_intrinsic_memory_barrier_buffer
:
4064 case nir_intrinsic_memory_barrier_image
:
4065 case nir_intrinsic_memory_barrier_shared
:
4066 emit_membar(&ctx
->ac
, instr
);
4068 case nir_intrinsic_scoped_barrier
: {
4069 assert(!(nir_intrinsic_memory_semantics(instr
) &
4070 (NIR_MEMORY_MAKE_AVAILABLE
| NIR_MEMORY_MAKE_VISIBLE
)));
4072 nir_variable_mode modes
= nir_intrinsic_memory_modes(instr
);
4074 unsigned wait_flags
= 0;
4075 if (modes
& (nir_var_mem_global
| nir_var_mem_ssbo
))
4076 wait_flags
|= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
4077 if (modes
& nir_var_mem_shared
)
4078 wait_flags
|= AC_WAIT_LGKM
;
4081 ac_build_waitcnt(&ctx
->ac
, wait_flags
);
4083 if (nir_intrinsic_execution_scope(instr
) == NIR_SCOPE_WORKGROUP
)
4084 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4087 case nir_intrinsic_memory_barrier_tcs_patch
:
4089 case nir_intrinsic_control_barrier
:
4090 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
4092 case nir_intrinsic_shared_atomic_add
:
4093 case nir_intrinsic_shared_atomic_imin
:
4094 case nir_intrinsic_shared_atomic_umin
:
4095 case nir_intrinsic_shared_atomic_imax
:
4096 case nir_intrinsic_shared_atomic_umax
:
4097 case nir_intrinsic_shared_atomic_and
:
4098 case nir_intrinsic_shared_atomic_or
:
4099 case nir_intrinsic_shared_atomic_xor
:
4100 case nir_intrinsic_shared_atomic_exchange
:
4101 case nir_intrinsic_shared_atomic_comp_swap
:
4102 case nir_intrinsic_shared_atomic_fadd
: {
4103 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
4104 instr
->src
[1].ssa
->bit_size
);
4105 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4108 case nir_intrinsic_deref_atomic_add
:
4109 case nir_intrinsic_deref_atomic_imin
:
4110 case nir_intrinsic_deref_atomic_umin
:
4111 case nir_intrinsic_deref_atomic_imax
:
4112 case nir_intrinsic_deref_atomic_umax
:
4113 case nir_intrinsic_deref_atomic_and
:
4114 case nir_intrinsic_deref_atomic_or
:
4115 case nir_intrinsic_deref_atomic_xor
:
4116 case nir_intrinsic_deref_atomic_exchange
:
4117 case nir_intrinsic_deref_atomic_comp_swap
:
4118 case nir_intrinsic_deref_atomic_fadd
: {
4119 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
4120 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
4123 case nir_intrinsic_load_barycentric_pixel
:
4124 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
4126 case nir_intrinsic_load_barycentric_centroid
:
4127 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
4129 case nir_intrinsic_load_barycentric_sample
:
4130 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
4132 case nir_intrinsic_load_barycentric_model
:
4133 result
= barycentric_model(ctx
);
4135 case nir_intrinsic_load_barycentric_at_offset
: {
4136 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4137 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
4140 case nir_intrinsic_load_barycentric_at_sample
: {
4141 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
4142 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
4145 case nir_intrinsic_load_interpolated_input
: {
4146 /* We assume any indirect loads have been lowered away */
4147 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
4149 assert(offset
[0].i32
== 0);
4151 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
4152 unsigned index
= nir_intrinsic_base(instr
);
4153 unsigned component
= nir_intrinsic_component(instr
);
4154 result
= load_interpolated_input(ctx
, interp_param
, index
,
4156 instr
->dest
.ssa
.num_components
,
4157 instr
->dest
.ssa
.bit_size
);
4160 case nir_intrinsic_emit_vertex
:
4161 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
4163 case nir_intrinsic_emit_vertex_with_counter
: {
4164 unsigned stream
= nir_intrinsic_stream_id(instr
);
4165 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
4166 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
4171 case nir_intrinsic_end_primitive
:
4172 case nir_intrinsic_end_primitive_with_counter
:
4173 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
4175 case nir_intrinsic_load_tess_coord
:
4176 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
4178 case nir_intrinsic_load_tess_level_outer
:
4179 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
4181 case nir_intrinsic_load_tess_level_inner
:
4182 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
4184 case nir_intrinsic_load_tess_level_outer_default
:
4185 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
4187 case nir_intrinsic_load_tess_level_inner_default
:
4188 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
4190 case nir_intrinsic_load_patch_vertices_in
:
4191 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
4193 case nir_intrinsic_vote_all
: {
4194 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4195 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4198 case nir_intrinsic_vote_any
: {
4199 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4200 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
4203 case nir_intrinsic_shuffle
:
4204 if (ctx
->ac
.chip_class
== GFX8
||
4205 ctx
->ac
.chip_class
== GFX9
||
4206 (ctx
->ac
.chip_class
>= GFX10
&& ctx
->ac
.wave_size
== 32)) {
4207 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4208 get_src(ctx
, instr
->src
[1]));
4210 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4211 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
4212 LLVMTypeRef type
= LLVMTypeOf(src
);
4213 struct waterfall_context wctx
;
4214 LLVMValueRef index_val
;
4216 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
4218 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
4221 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4223 (LLVMValueRef
[]) { src
, index_val
}, 2,
4224 AC_FUNC_ATTR_READNONE
|
4225 AC_FUNC_ATTR_CONVERGENT
);
4227 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4229 result
= exit_waterfall(ctx
, &wctx
, result
);
4232 case nir_intrinsic_reduce
:
4233 result
= ac_build_reduce(&ctx
->ac
,
4234 get_src(ctx
, instr
->src
[0]),
4235 instr
->const_index
[0],
4236 instr
->const_index
[1]);
4238 case nir_intrinsic_inclusive_scan
:
4239 result
= ac_build_inclusive_scan(&ctx
->ac
,
4240 get_src(ctx
, instr
->src
[0]),
4241 instr
->const_index
[0]);
4243 case nir_intrinsic_exclusive_scan
:
4244 result
= ac_build_exclusive_scan(&ctx
->ac
,
4245 get_src(ctx
, instr
->src
[0]),
4246 instr
->const_index
[0]);
4248 case nir_intrinsic_quad_broadcast
: {
4249 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4250 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4251 lane
, lane
, lane
, lane
);
4254 case nir_intrinsic_quad_swap_horizontal
:
4255 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4257 case nir_intrinsic_quad_swap_vertical
:
4258 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4260 case nir_intrinsic_quad_swap_diagonal
:
4261 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4263 case nir_intrinsic_quad_swizzle_amd
: {
4264 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4265 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4266 mask
& 0x3, (mask
>> 2) & 0x3,
4267 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4270 case nir_intrinsic_masked_swizzle_amd
: {
4271 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4272 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4275 case nir_intrinsic_write_invocation_amd
:
4276 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4277 get_src(ctx
, instr
->src
[1]),
4278 get_src(ctx
, instr
->src
[2]));
4280 case nir_intrinsic_mbcnt_amd
:
4281 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4283 case nir_intrinsic_load_scratch
: {
4284 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4285 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4287 LLVMTypeRef comp_type
=
4288 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4289 LLVMTypeRef vec_type
=
4290 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4291 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4292 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4293 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4294 LLVMPointerType(vec_type
, addr_space
), "");
4295 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4298 case nir_intrinsic_store_scratch
: {
4299 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4300 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4302 LLVMTypeRef comp_type
=
4303 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4304 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4305 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4306 LLVMPointerType(comp_type
, addr_space
), "");
4307 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4308 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4311 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4313 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4314 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4315 LLVMTypeRef vec_type
=
4316 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4317 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4319 LLVMPointerType(vec_type
, addr_space
),
4321 LLVMValueRef offset_src
=
4322 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4323 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4327 case nir_intrinsic_load_constant
: {
4328 unsigned base
= nir_intrinsic_base(instr
);
4329 unsigned range
= nir_intrinsic_range(instr
);
4331 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4332 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4333 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4335 /* Clamp the offset to avoid out-of-bound access because global
4336 * instructions can't handle them.
4338 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4339 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4341 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4343 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4345 LLVMTypeRef comp_type
=
4346 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4347 LLVMTypeRef vec_type
=
4348 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4349 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4350 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4351 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4352 LLVMPointerType(vec_type
, addr_space
), "");
4353 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4357 fprintf(stderr
, "Unknown intrinsic: ");
4358 nir_print_instr(&instr
->instr
, stderr
);
4359 fprintf(stderr
, "\n");
4363 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4367 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4368 unsigned base_index
,
4369 unsigned constant_index
,
4370 LLVMValueRef dynamic_index
)
4372 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4373 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4374 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4376 /* Bindless uniforms are 64bit so multiple index by 8 */
4377 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4378 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4380 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4382 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4383 NULL
, 0, 0, true, true);
4385 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4388 struct sampler_desc_address
{
4389 unsigned descriptor_set
;
4390 unsigned base_index
; /* binding in vulkan */
4391 unsigned constant_index
;
4392 LLVMValueRef dynamic_index
;
4397 static struct sampler_desc_address
4398 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4399 nir_deref_instr
*deref_instr
,
4400 const nir_instr
*instr
,
4403 LLVMValueRef index
= NULL
;
4404 unsigned constant_index
= 0;
4405 unsigned descriptor_set
;
4406 unsigned base_index
;
4407 bool bindless
= false;
4412 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4415 index
= get_src(ctx
, img_instr
->src
[0]);
4417 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4418 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4419 nir_tex_src_sampler_handle
);
4420 if (sampSrcIdx
!= -1) {
4423 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4425 assert(tex_instr
&& !image
);
4426 base_index
= tex_instr
->sampler_index
;
4430 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4431 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4432 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4436 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4437 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4439 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4441 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4442 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4447 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4450 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4451 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4452 unsigned sidx
= deref_instr
->strct
.index
;
4453 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4454 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4456 unreachable("Unsupported deref type");
4459 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4461 if (deref_instr
->var
->data
.bindless
) {
4462 /* For now just assert on unhandled variable types */
4463 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4465 base_index
= deref_instr
->var
->data
.driver_location
;
4468 index
= index
? index
: ctx
->ac
.i32_0
;
4469 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4470 constant_index
, index
);
4472 base_index
= deref_instr
->var
->data
.binding
;
4474 return (struct sampler_desc_address
) {
4475 .descriptor_set
= descriptor_set
,
4476 .base_index
= base_index
,
4477 .constant_index
= constant_index
,
4478 .dynamic_index
= index
,
4480 .bindless
= bindless
,
4484 /* Extract any possibly divergent index into a separate value that can be fed
4485 * into get_sampler_desc with the same arguments. */
4486 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4487 nir_deref_instr
*deref_instr
,
4488 const nir_instr
*instr
,
4491 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4492 return addr
.dynamic_index
;
4495 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4496 nir_deref_instr
*deref_instr
,
4497 enum ac_descriptor_type desc_type
,
4498 const nir_instr
*instr
,
4500 bool image
, bool write
)
4502 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4503 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4504 addr
.descriptor_set
,
4506 addr
.constant_index
, index
,
4507 desc_type
, addr
.image
, write
, addr
.bindless
);
4510 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4513 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4514 * filtering manually. The driver sets img7 to a mask clearing
4515 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4516 * s_and_b32 samp0, samp0, img7
4519 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4521 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4522 LLVMValueRef res
, LLVMValueRef samp
)
4524 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4525 LLVMValueRef img7
, samp0
;
4527 if (ctx
->ac
.chip_class
>= GFX8
)
4530 img7
= LLVMBuildExtractElement(builder
, res
,
4531 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4532 samp0
= LLVMBuildExtractElement(builder
, samp
,
4533 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4534 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4535 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4536 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4539 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4540 nir_tex_instr
*instr
,
4541 struct waterfall_context
*wctx
,
4542 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4543 LLVMValueRef
*fmask_ptr
)
4545 nir_deref_instr
*texture_deref_instr
= NULL
;
4546 nir_deref_instr
*sampler_deref_instr
= NULL
;
4549 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4550 switch (instr
->src
[i
].src_type
) {
4551 case nir_tex_src_texture_deref
:
4552 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4554 case nir_tex_src_sampler_deref
:
4555 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4557 case nir_tex_src_plane
:
4558 plane
= nir_src_as_int(instr
->src
[i
].src
);
4565 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4566 &instr
->instr
, false);
4567 if (!sampler_deref_instr
)
4568 sampler_deref_instr
= texture_deref_instr
;
4570 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4571 &instr
->instr
, false);
4572 if (instr
->texture_non_uniform
)
4573 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4575 if (instr
->sampler_non_uniform
)
4576 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4578 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4581 assert(instr
->op
!= nir_texop_txf_ms
&&
4582 instr
->op
!= nir_texop_samples_identical
);
4583 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4585 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4588 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4589 /* The fragment mask is fetched from the compressed
4590 * multisampled surface.
4592 main_descriptor
= AC_DESC_FMASK
;
4595 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4596 texture_dynamic_index
, false, false);
4599 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4600 sampler_dynamic_index
, false, false);
4601 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4602 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4604 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4605 instr
->op
== nir_texop_samples_identical
))
4606 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4607 &instr
->instr
, texture_dynamic_index
, false, false);
4610 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4613 coord
= ac_to_float(ctx
, coord
);
4614 coord
= ac_build_round(ctx
, coord
);
4615 coord
= ac_to_integer(ctx
, coord
);
4619 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4621 LLVMValueRef result
= NULL
;
4622 struct ac_image_args args
= { 0 };
4623 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4624 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4625 unsigned offset_src
= 0;
4626 struct waterfall_context wctx
[2] = {{{0}}};
4628 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4630 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4631 switch (instr
->src
[i
].src_type
) {
4632 case nir_tex_src_coord
: {
4633 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4634 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4635 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4638 case nir_tex_src_projector
:
4640 case nir_tex_src_comparator
:
4641 if (instr
->is_shadow
) {
4642 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4643 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4646 case nir_tex_src_offset
:
4647 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4650 case nir_tex_src_bias
:
4651 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4653 case nir_tex_src_lod
: {
4654 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4655 args
.level_zero
= true;
4657 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4660 case nir_tex_src_ms_index
:
4661 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4663 case nir_tex_src_ms_mcs
:
4665 case nir_tex_src_ddx
:
4666 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4668 case nir_tex_src_ddy
:
4669 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4671 case nir_tex_src_min_lod
:
4672 args
.min_lod
= get_src(ctx
, instr
->src
[i
].src
);
4674 case nir_tex_src_texture_offset
:
4675 case nir_tex_src_sampler_offset
:
4676 case nir_tex_src_plane
:
4682 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4683 result
= get_buffer_size(ctx
, args
.resource
, true);
4687 if (instr
->op
== nir_texop_texture_samples
) {
4688 LLVMValueRef res
, samples
, is_msaa
;
4689 LLVMValueRef default_sample
;
4691 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4692 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4693 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4694 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4695 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4696 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4697 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4698 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4699 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4701 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4702 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4703 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4704 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4705 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4708 if (ctx
->abi
->robust_buffer_access
) {
4709 LLVMValueRef dword1
, is_null_descriptor
;
4711 /* Extract the second dword of the descriptor, if it's
4712 * all zero, then it's a null descriptor.
4714 dword1
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4715 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
4716 is_null_descriptor
=
4717 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, dword1
,
4718 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4720 LLVMBuildSelect(ctx
->ac
.builder
, is_null_descriptor
,
4721 ctx
->ac
.i32_0
, ctx
->ac
.i32_1
, "");
4723 default_sample
= ctx
->ac
.i32_1
;
4726 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4727 default_sample
, "");
4732 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4733 LLVMValueRef offset
[3], pack
;
4734 for (unsigned chan
= 0; chan
< 3; ++chan
)
4735 offset
[chan
] = ctx
->ac
.i32_0
;
4737 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4738 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4739 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4740 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4741 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4743 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4744 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4746 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4747 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4751 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4752 * OpenGL 4.5 spec says:
4754 * "If the texture’s internal format indicates a fixed-point
4755 * depth texture, then D_t and D_ref are clamped to the
4756 * range [0, 1]; otherwise no clamping is performed."
4758 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4759 * so the depth comparison value isn't clamped for Z16 and
4760 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4761 * an explicitly clamped 32-bit float format.
4764 ctx
->ac
.chip_class
>= GFX8
&&
4765 ctx
->ac
.chip_class
<= GFX9
&&
4766 ctx
->abi
->clamp_shadow_reference
) {
4767 LLVMValueRef upgraded
, clamped
;
4769 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4770 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4771 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4772 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4773 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4774 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4775 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4779 /* pack derivatives */
4781 int num_src_deriv_channels
, num_dest_deriv_channels
;
4782 switch (instr
->sampler_dim
) {
4783 case GLSL_SAMPLER_DIM_3D
:
4784 case GLSL_SAMPLER_DIM_CUBE
:
4785 num_src_deriv_channels
= 3;
4786 num_dest_deriv_channels
= 3;
4788 case GLSL_SAMPLER_DIM_2D
:
4790 num_src_deriv_channels
= 2;
4791 num_dest_deriv_channels
= 2;
4793 case GLSL_SAMPLER_DIM_1D
:
4794 num_src_deriv_channels
= 1;
4795 if (ctx
->ac
.chip_class
== GFX9
) {
4796 num_dest_deriv_channels
= 2;
4798 num_dest_deriv_channels
= 1;
4803 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4804 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4805 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4806 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4807 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4809 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4810 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4811 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4815 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4816 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4817 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4818 if (instr
->coord_components
== 3)
4819 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4820 ac_prepare_cube_coords(&ctx
->ac
,
4821 instr
->op
== nir_texop_txd
, instr
->is_array
,
4822 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4825 /* Texture coordinates fixups */
4826 if (instr
->coord_components
> 1 &&
4827 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4829 instr
->op
!= nir_texop_txf
) {
4830 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4833 if (instr
->coord_components
> 2 &&
4834 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4835 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4836 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4837 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4839 instr
->op
!= nir_texop_txf
&&
4840 instr
->op
!= nir_texop_txf_ms
&&
4841 instr
->op
!= nir_texop_fragment_fetch
&&
4842 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4843 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4846 if (ctx
->ac
.chip_class
== GFX9
&&
4847 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4848 instr
->op
!= nir_texop_lod
) {
4849 LLVMValueRef filler
;
4850 if (instr
->op
== nir_texop_txf
)
4851 filler
= ctx
->ac
.i32_0
;
4853 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4855 if (instr
->is_array
)
4856 args
.coords
[2] = args
.coords
[1];
4857 args
.coords
[1] = filler
;
4860 /* Pack sample index */
4861 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4862 instr
->op
== nir_texop_fragment_fetch
))
4863 args
.coords
[instr
->coord_components
] = sample_index
;
4865 if (instr
->op
== nir_texop_samples_identical
) {
4866 struct ac_image_args txf_args
= { 0 };
4867 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4869 txf_args
.dmask
= 0xf;
4870 txf_args
.resource
= fmask_ptr
;
4871 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4872 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4874 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4875 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4879 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4880 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4881 instr
->op
!= nir_texop_txs
&&
4882 instr
->op
!= nir_texop_fragment_fetch
&&
4883 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4884 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4885 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4886 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4887 instr
->is_array
? args
.coords
[2] : NULL
,
4888 args
.coords
[sample_chan
], fmask_ptr
);
4891 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4892 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4893 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4894 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4895 args
.coords
[i
] = LLVMBuildAdd(
4896 ctx
->ac
.builder
, args
.coords
[i
],
4897 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4902 /* DMASK was repurposed for GATHER4. 4 components are always
4903 * returned and DMASK works like a swizzle - it selects
4904 * the component to fetch. The only valid DMASK values are
4905 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4906 * (red,red,red,red) etc.) The ISA document doesn't mention
4910 if (instr
->op
== nir_texop_tg4
) {
4911 if (instr
->is_shadow
)
4914 args
.dmask
= 1 << instr
->component
;
4917 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4918 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4919 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4922 /* Adjust the number of coordinates because we only need (x,y) for 2D
4923 * multisampled images and (x,y,layer) for 2D multisampled layered
4924 * images or for multisampled input attachments.
4926 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4927 if (args
.dim
== ac_image_2dmsaa
) {
4928 args
.dim
= ac_image_2d
;
4930 assert(args
.dim
== ac_image_2darraymsaa
);
4931 args
.dim
= ac_image_2darray
;
4935 assert(instr
->dest
.is_ssa
);
4936 args
.d16
= instr
->dest
.ssa
.bit_size
== 16;
4938 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4940 if (instr
->op
== nir_texop_query_levels
)
4941 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4942 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4943 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4944 instr
->op
!= nir_texop_tg4
)
4945 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4946 else if (instr
->op
== nir_texop_txs
&&
4947 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4949 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4950 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4951 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4952 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4953 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4954 } else if (ctx
->ac
.chip_class
== GFX9
&&
4955 instr
->op
== nir_texop_txs
&&
4956 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4958 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4959 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4960 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4962 } else if (instr
->dest
.ssa
.num_components
!= 4)
4963 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4967 assert(instr
->dest
.is_ssa
);
4968 result
= ac_to_integer(&ctx
->ac
, result
);
4970 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4971 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4974 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4978 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4980 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4981 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4983 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4984 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4987 static void visit_post_phi(struct ac_nir_context
*ctx
,
4988 nir_phi_instr
*instr
,
4989 LLVMValueRef llvm_phi
)
4991 nir_foreach_phi_src(src
, instr
) {
4992 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4993 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4995 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4999 static void phi_post_pass(struct ac_nir_context
*ctx
)
5001 hash_table_foreach(ctx
->phis
, entry
) {
5002 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
5003 (LLVMValueRef
)entry
->data
);
5008 static bool is_def_used_in_an_export(const nir_ssa_def
* def
) {
5009 nir_foreach_use(use_src
, def
) {
5010 if (use_src
->parent_instr
->type
== nir_instr_type_intrinsic
) {
5011 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(use_src
->parent_instr
);
5012 if (instr
->intrinsic
== nir_intrinsic_store_deref
)
5014 } else if (use_src
->parent_instr
->type
== nir_instr_type_alu
) {
5015 nir_alu_instr
*instr
= nir_instr_as_alu(use_src
->parent_instr
);
5016 if (instr
->op
== nir_op_vec4
&&
5017 is_def_used_in_an_export(&instr
->dest
.dest
.ssa
)) {
5025 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
5026 const nir_ssa_undef_instr
*instr
)
5028 unsigned num_components
= instr
->def
.num_components
;
5029 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
5031 if (!ctx
->abi
->convert_undef_to_zero
|| is_def_used_in_an_export(&instr
->def
)) {
5034 if (num_components
== 1)
5035 undef
= LLVMGetUndef(type
);
5037 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
5039 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
5041 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
5042 if (num_components
> 1) {
5043 zero
= ac_build_gather_values_extended(
5044 &ctx
->ac
, &zero
, 4, 0, false, false);
5046 ctx
->ssa_defs
[instr
->def
.index
] = zero
;
5050 static void visit_jump(struct ac_llvm_context
*ctx
,
5051 const nir_jump_instr
*instr
)
5053 switch (instr
->type
) {
5054 case nir_jump_break
:
5055 ac_build_break(ctx
);
5057 case nir_jump_continue
:
5058 ac_build_continue(ctx
);
5061 fprintf(stderr
, "Unknown NIR jump instr: ");
5062 nir_print_instr(&instr
->instr
, stderr
);
5063 fprintf(stderr
, "\n");
5069 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
5070 enum glsl_base_type type
)
5074 case GLSL_TYPE_UINT
:
5075 case GLSL_TYPE_BOOL
:
5076 case GLSL_TYPE_SUBROUTINE
:
5078 case GLSL_TYPE_INT8
:
5079 case GLSL_TYPE_UINT8
:
5081 case GLSL_TYPE_INT16
:
5082 case GLSL_TYPE_UINT16
:
5084 case GLSL_TYPE_FLOAT
:
5086 case GLSL_TYPE_FLOAT16
:
5088 case GLSL_TYPE_INT64
:
5089 case GLSL_TYPE_UINT64
:
5091 case GLSL_TYPE_DOUBLE
:
5094 unreachable("unknown GLSL type");
5099 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
5100 const struct glsl_type
*type
)
5102 if (glsl_type_is_scalar(type
)) {
5103 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
5106 if (glsl_type_is_vector(type
)) {
5107 return LLVMVectorType(
5108 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
5109 glsl_get_vector_elements(type
));
5112 if (glsl_type_is_matrix(type
)) {
5113 return LLVMArrayType(
5114 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
5115 glsl_get_matrix_columns(type
));
5118 if (glsl_type_is_array(type
)) {
5119 return LLVMArrayType(
5120 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
5121 glsl_get_length(type
));
5124 assert(glsl_type_is_struct_or_ifc(type
));
5126 LLVMTypeRef member_types
[glsl_get_length(type
)];
5128 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
5130 glsl_to_llvm_type(ac
,
5131 glsl_get_struct_field(type
, i
));
5134 return LLVMStructTypeInContext(ac
->context
, member_types
,
5135 glsl_get_length(type
), false);
5138 static void visit_deref(struct ac_nir_context
*ctx
,
5139 nir_deref_instr
*instr
)
5141 if (instr
->mode
!= nir_var_mem_shared
&&
5142 instr
->mode
!= nir_var_mem_global
)
5145 LLVMValueRef result
= NULL
;
5146 switch(instr
->deref_type
) {
5147 case nir_deref_type_var
: {
5148 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
5149 result
= entry
->data
;
5152 case nir_deref_type_struct
:
5153 if (instr
->mode
== nir_var_mem_global
) {
5154 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5155 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
5156 instr
->strct
.index
);
5157 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5158 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
5160 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5161 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
5164 case nir_deref_type_array
:
5165 if (instr
->mode
== nir_var_mem_global
) {
5166 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
5167 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
5169 if ((glsl_type_is_matrix(parent
->type
) &&
5170 glsl_matrix_type_is_row_major(parent
->type
)) ||
5171 (glsl_type_is_vector(parent
->type
) && stride
== 0))
5172 stride
= type_scalar_size_bytes(parent
->type
);
5175 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5176 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5177 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5179 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5181 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5183 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5184 get_src(ctx
, instr
->arr
.index
));
5187 case nir_deref_type_ptr_as_array
:
5188 if (instr
->mode
== nir_var_mem_global
) {
5189 unsigned stride
= nir_deref_instr_array_stride(instr
);
5191 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
5192 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
5193 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
5195 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
5197 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
5199 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
5200 get_src(ctx
, instr
->arr
.index
));
5203 case nir_deref_type_cast
: {
5204 result
= get_src(ctx
, instr
->parent
);
5206 /* We can't use the structs from LLVM because the shader
5207 * specifies its own offsets. */
5208 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
5209 if (instr
->mode
== nir_var_mem_shared
)
5210 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
5212 unsigned address_space
;
5214 switch(instr
->mode
) {
5215 case nir_var_mem_shared
:
5216 address_space
= AC_ADDR_SPACE_LDS
;
5218 case nir_var_mem_global
:
5219 address_space
= AC_ADDR_SPACE_GLOBAL
;
5222 unreachable("Unhandled address space");
5225 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
5227 if (LLVMTypeOf(result
) != type
) {
5228 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
5229 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
5232 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
5239 unreachable("Unhandled deref_instr deref type");
5242 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
5245 static void visit_cf_list(struct ac_nir_context
*ctx
,
5246 struct exec_list
*list
);
5248 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5250 nir_foreach_instr(instr
, block
)
5252 switch (instr
->type
) {
5253 case nir_instr_type_alu
:
5254 visit_alu(ctx
, nir_instr_as_alu(instr
));
5256 case nir_instr_type_load_const
:
5257 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5259 case nir_instr_type_intrinsic
:
5260 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5262 case nir_instr_type_tex
:
5263 visit_tex(ctx
, nir_instr_as_tex(instr
));
5265 case nir_instr_type_phi
:
5266 visit_phi(ctx
, nir_instr_as_phi(instr
));
5268 case nir_instr_type_ssa_undef
:
5269 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5271 case nir_instr_type_jump
:
5272 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
5274 case nir_instr_type_deref
:
5275 visit_deref(ctx
, nir_instr_as_deref(instr
));
5278 fprintf(stderr
, "Unknown NIR instr type: ");
5279 nir_print_instr(instr
, stderr
);
5280 fprintf(stderr
, "\n");
5285 _mesa_hash_table_insert(ctx
->defs
, block
,
5286 LLVMGetInsertBlock(ctx
->ac
.builder
));
5289 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5291 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5293 nir_block
*then_block
=
5294 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5296 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5298 visit_cf_list(ctx
, &if_stmt
->then_list
);
5300 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5301 nir_block
*else_block
=
5302 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5304 ac_build_else(&ctx
->ac
, else_block
->index
);
5305 visit_cf_list(ctx
, &if_stmt
->else_list
);
5308 ac_build_endif(&ctx
->ac
, then_block
->index
);
5311 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5313 nir_block
*first_loop_block
=
5314 (nir_block
*) exec_list_get_head(&loop
->body
);
5316 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5318 visit_cf_list(ctx
, &loop
->body
);
5320 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5323 static void visit_cf_list(struct ac_nir_context
*ctx
,
5324 struct exec_list
*list
)
5326 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5328 switch (node
->type
) {
5329 case nir_cf_node_block
:
5330 visit_block(ctx
, nir_cf_node_as_block(node
));
5333 case nir_cf_node_if
:
5334 visit_if(ctx
, nir_cf_node_as_if(node
));
5337 case nir_cf_node_loop
:
5338 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5348 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5349 struct ac_shader_abi
*abi
,
5350 struct nir_shader
*nir
,
5351 struct nir_variable
*variable
,
5352 gl_shader_stage stage
)
5354 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5355 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5357 /* tess ctrl has it's own load/store paths for outputs */
5358 if (stage
== MESA_SHADER_TESS_CTRL
)
5361 if (stage
== MESA_SHADER_VERTEX
||
5362 stage
== MESA_SHADER_TESS_EVAL
||
5363 stage
== MESA_SHADER_GEOMETRY
) {
5364 int idx
= variable
->data
.location
+ variable
->data
.index
;
5365 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5366 int length
= nir
->info
.clip_distance_array_size
+
5367 nir
->info
.cull_distance_array_size
;
5376 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5377 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5378 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5379 for (unsigned chan
= 0; chan
< 4; chan
++) {
5380 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5381 ac_build_alloca_undef(ctx
, type
, "");
5387 setup_locals(struct ac_nir_context
*ctx
,
5388 struct nir_function
*func
)
5391 ctx
->num_locals
= 0;
5392 nir_foreach_function_temp_variable(variable
, func
->impl
) {
5393 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5394 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5395 variable
->data
.location_frac
= 0;
5396 ctx
->num_locals
+= attrib_count
;
5398 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5402 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5403 for (j
= 0; j
< 4; j
++) {
5404 ctx
->locals
[i
* 4 + j
] =
5405 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5411 setup_scratch(struct ac_nir_context
*ctx
,
5412 struct nir_shader
*shader
)
5414 if (shader
->scratch_size
== 0)
5417 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5418 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5423 setup_constant_data(struct ac_nir_context
*ctx
,
5424 struct nir_shader
*shader
)
5426 if (!shader
->constant_data
)
5430 LLVMConstStringInContext(ctx
->ac
.context
,
5431 shader
->constant_data
,
5432 shader
->constant_data_size
,
5434 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5436 /* We want to put the constant data in the CONST address space so that
5437 * we can use scalar loads. However, LLVM versions before 10 put these
5438 * variables in the same section as the code, which is unacceptable
5439 * for RadeonSI as it needs to relocate all the data sections after
5440 * the code sections. See https://reviews.llvm.org/D65813.
5442 unsigned address_space
=
5443 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5445 LLVMValueRef global
=
5446 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5450 LLVMSetInitializer(global
, data
);
5451 LLVMSetGlobalConstant(global
, true);
5452 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5453 ctx
->constant_data
= global
;
5457 setup_shared(struct ac_nir_context
*ctx
,
5458 struct nir_shader
*nir
)
5463 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5464 nir
->info
.cs
.shared_size
);
5467 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5470 LLVMSetAlignment(lds
, 64 * 1024);
5472 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5473 LLVMPointerType(ctx
->ac
.i8
,
5474 AC_ADDR_SPACE_LDS
), "");
5477 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5478 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5480 struct ac_nir_context ctx
= {};
5481 struct nir_function
*func
;
5487 ctx
.stage
= nir
->info
.stage
;
5488 ctx
.info
= &nir
->info
;
5490 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5492 /* TODO: remove this after RADV switches to lowered IO */
5493 if (!nir
->info
.io_lowered
) {
5494 nir_foreach_shader_out_variable(variable
, nir
) {
5495 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5500 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5501 _mesa_key_pointer_equal
);
5502 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5503 _mesa_key_pointer_equal
);
5504 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5505 _mesa_key_pointer_equal
);
5507 if (ctx
.abi
->kill_ps_if_inf_interp
)
5508 ctx
.verified_interp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5509 _mesa_key_pointer_equal
);
5511 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5513 nir_index_ssa_defs(func
->impl
);
5514 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5516 setup_locals(&ctx
, func
);
5517 setup_scratch(&ctx
, nir
);
5518 setup_constant_data(&ctx
, nir
);
5520 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5521 setup_shared(&ctx
, nir
);
5523 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5524 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5525 /* true = don't kill. */
5526 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5529 visit_cf_list(&ctx
, &func
->impl
->body
);
5530 phi_post_pass(&ctx
);
5532 if (ctx
.ac
.postponed_kill
)
5533 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5534 ctx
.ac
.postponed_kill
, ""));
5536 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5537 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5542 ralloc_free(ctx
.defs
);
5543 ralloc_free(ctx
.phis
);
5544 ralloc_free(ctx
.vars
);
5545 if (ctx
.abi
->kill_ps_if_inf_interp
)
5546 ralloc_free(ctx
.verified_interp
);
5550 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5552 bool progress
= false;
5554 /* Lower large variables to scratch first so that we won't bloat the
5555 * shader by generating large if ladders for them. We later lower
5556 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5558 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5559 nir_var_function_temp
,
5561 glsl_get_natural_size_align_bytes
);
5563 /* While it would be nice not to have this flag, we are constrained
5564 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5566 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5568 /* TODO: Indirect indexing of GS inputs is unimplemented.
5570 * TCS and TES load inputs directly from LDS or offchip memory, so
5571 * indirect indexing is trivial.
5573 nir_variable_mode indirect_mask
= 0;
5574 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5575 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5576 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5577 !llvm_has_working_vgpr_indexing
)) {
5578 indirect_mask
|= nir_var_shader_in
;
5580 if (!llvm_has_working_vgpr_indexing
&&
5581 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5582 indirect_mask
|= nir_var_shader_out
;
5584 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5585 * smart enough to handle indirects without causing excess spilling
5586 * causing the gpu to hang.
5588 * See the following thread for more details of the problem:
5589 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5591 indirect_mask
|= nir_var_function_temp
;
5593 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
, UINT32_MAX
);
5598 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5600 if (intrin
->intrinsic
!= nir_intrinsic_store_output
)
5603 unsigned writemask
= nir_intrinsic_write_mask(intrin
) <<
5604 nir_intrinsic_component(intrin
);
5605 unsigned location
= nir_intrinsic_io_semantics(intrin
).location
;
5607 if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5608 return writemask
<< 4;
5609 else if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5616 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5617 unsigned *cond_block_tf_writemask
,
5618 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5620 switch (cf_node
->type
) {
5621 case nir_cf_node_block
: {
5622 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5623 nir_foreach_instr(instr
, block
) {
5624 if (instr
->type
!= nir_instr_type_intrinsic
)
5627 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5628 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5630 /* If we find a barrier in nested control flow put this in the
5631 * too hard basket. In GLSL this is not possible but it is in
5635 *tessfactors_are_def_in_all_invocs
= false;
5639 /* The following case must be prevented:
5640 * gl_TessLevelInner = ...;
5642 * if (gl_InvocationID == 1)
5643 * gl_TessLevelInner = ...;
5645 * If you consider disjoint code segments separated by barriers, each
5646 * such segment that writes tess factor channels should write the same
5647 * channels in all codepaths within that segment.
5649 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5650 /* Accumulate the result: */
5651 *tessfactors_are_def_in_all_invocs
&=
5652 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5654 /* Analyze the next code segment from scratch. */
5655 *upper_block_tf_writemask
= 0;
5656 *cond_block_tf_writemask
= 0;
5659 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5664 case nir_cf_node_if
: {
5665 unsigned then_tessfactor_writemask
= 0;
5666 unsigned else_tessfactor_writemask
= 0;
5668 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5669 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5670 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5671 cond_block_tf_writemask
,
5672 tessfactors_are_def_in_all_invocs
, true);
5675 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5676 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5677 cond_block_tf_writemask
,
5678 tessfactors_are_def_in_all_invocs
, true);
5681 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5682 /* If both statements write the same tess factor channels,
5683 * we can say that the upper block writes them too.
5685 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5686 else_tessfactor_writemask
;
5687 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5688 else_tessfactor_writemask
;
5693 case nir_cf_node_loop
: {
5694 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5695 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5696 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5697 cond_block_tf_writemask
,
5698 tessfactors_are_def_in_all_invocs
, true);
5704 unreachable("unknown cf node type");
5709 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5711 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5713 /* The pass works as follows:
5714 * If all codepaths write tess factors, we can say that all
5715 * invocations define tess factors.
5717 * Each tess factor channel is tracked separately.
5719 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5720 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5722 /* Initial value = true. Here the pass will accumulate results from
5723 * multiple segments surrounded by barriers. If tess factors aren't
5724 * written at all, it's a shader bug and we don't care if this will be
5727 bool tessfactors_are_def_in_all_invocs
= true;
5729 nir_foreach_function(function
, nir
) {
5730 if (function
->impl
) {
5731 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5732 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5733 &cond_block_tf_writemask
,
5734 &tessfactors_are_def_in_all_invocs
,
5740 /* Accumulate the result for the last code segment separated by a
5743 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5744 tessfactors_are_def_in_all_invocs
&=
5745 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5748 return tessfactors_are_def_in_all_invocs
;