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 "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "util/bitscan.h"
31 #include "util/u_math.h"
32 #include "ac_shader_abi.h"
33 #include "ac_shader_util.h"
35 struct ac_nir_context
{
36 struct ac_llvm_context ac
;
37 struct ac_shader_abi
*abi
;
39 gl_shader_stage stage
;
41 struct hash_table
*defs
;
42 struct hash_table
*phis
;
43 struct hash_table
*vars
;
45 LLVMValueRef main_function
;
46 LLVMBasicBlockRef continue_block
;
47 LLVMBasicBlockRef break_block
;
53 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
54 const nir_deref_var
*deref
,
55 enum ac_descriptor_type desc_type
,
56 const nir_tex_instr
*instr
,
57 bool image
, bool write
);
60 build_store_values_extended(struct ac_llvm_context
*ac
,
63 unsigned value_stride
,
66 LLVMBuilderRef builder
= ac
->builder
;
69 for (i
= 0; i
< value_count
; i
++) {
70 LLVMValueRef ptr
= values
[i
* value_stride
];
71 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
72 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
73 LLVMBuildStore(builder
, value
, ptr
);
77 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
78 const nir_ssa_def
*def
)
80 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
81 if (def
->num_components
> 1) {
82 type
= LLVMVectorType(type
, def
->num_components
);
87 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
90 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
91 return (LLVMValueRef
)entry
->data
;
95 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
97 LLVMValueRef ptr
= get_src(ctx
, src
);
98 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
99 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
101 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
102 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
105 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
106 const struct nir_block
*b
)
108 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
109 return (LLVMBasicBlockRef
)entry
->data
;
112 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
114 unsigned num_components
)
116 LLVMValueRef value
= get_src(ctx
, src
.src
);
117 bool need_swizzle
= false;
120 unsigned src_components
= ac_get_llvm_num_components(value
);
121 for (unsigned i
= 0; i
< num_components
; ++i
) {
122 assert(src
.swizzle
[i
] < src_components
);
123 if (src
.swizzle
[i
] != i
)
127 if (need_swizzle
|| num_components
!= src_components
) {
128 LLVMValueRef masks
[] = {
129 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
130 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
131 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
132 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
134 if (src_components
> 1 && num_components
== 1) {
135 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
137 } else if (src_components
== 1 && num_components
> 1) {
138 LLVMValueRef values
[] = {value
, value
, value
, value
};
139 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
141 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
142 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
151 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
152 LLVMIntPredicate pred
, LLVMValueRef src0
,
155 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
156 return LLVMBuildSelect(ctx
->builder
, result
,
157 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
161 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
162 LLVMRealPredicate pred
, LLVMValueRef src0
,
166 src0
= ac_to_float(ctx
, src0
);
167 src1
= ac_to_float(ctx
, src1
);
168 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
169 return LLVMBuildSelect(ctx
->builder
, result
,
170 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
174 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
176 LLVMTypeRef result_type
,
180 LLVMValueRef params
[] = {
181 ac_to_float(ctx
, src0
),
184 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
185 ac_get_elem_bits(ctx
, result_type
));
186 assert(length
< sizeof(name
));
187 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
190 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
192 LLVMTypeRef result_type
,
193 LLVMValueRef src0
, LLVMValueRef src1
)
196 LLVMValueRef params
[] = {
197 ac_to_float(ctx
, src0
),
198 ac_to_float(ctx
, src1
),
201 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
202 ac_get_elem_bits(ctx
, result_type
));
203 assert(length
< sizeof(name
));
204 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
207 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
209 LLVMTypeRef result_type
,
210 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
213 LLVMValueRef params
[] = {
214 ac_to_float(ctx
, src0
),
215 ac_to_float(ctx
, src1
),
216 ac_to_float(ctx
, src2
),
219 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
220 ac_get_elem_bits(ctx
, result_type
));
221 assert(length
< sizeof(name
));
222 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
225 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
226 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
228 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
230 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
231 ac_to_integer(ctx
, src2
), "");
234 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
235 LLVMIntPredicate pred
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
238 return LLVMBuildSelect(ctx
->builder
,
239 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
244 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
247 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
248 LLVMBuildNeg(ctx
->builder
, src0
, ""));
251 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
253 LLVMValueRef src0
, LLVMValueRef src1
)
255 LLVMTypeRef ret_type
;
256 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
258 LLVMValueRef params
[] = { src0
, src1
};
259 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
262 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
263 params
, 2, AC_FUNC_ATTR_READNONE
);
265 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
266 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
270 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
273 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
276 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
279 src0
= ac_to_float(ctx
, src0
);
280 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
281 return LLVMBuildSExt(ctx
->builder
,
282 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
286 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
290 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
295 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
298 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
301 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
302 return LLVMBuildSExt(ctx
->builder
,
303 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
307 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
311 LLVMValueRef cond
= NULL
;
313 src0
= ac_to_float(ctx
, src0
);
314 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
316 if (ctx
->chip_class
>= VI
) {
317 LLVMValueRef args
[2];
318 /* Check if the result is a denormal - and flush to 0 if so. */
320 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
321 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
324 /* need to convert back up to f32 */
325 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
327 if (ctx
->chip_class
>= VI
)
328 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
331 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
332 * so compare the result and flush to 0 if it's smaller.
334 LLVMValueRef temp
, cond2
;
335 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
336 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
337 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
339 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
340 temp
, ctx
->f32_0
, "");
341 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
342 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
347 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
348 LLVMValueRef src0
, LLVMValueRef src1
)
350 LLVMValueRef dst64
, result
;
351 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
352 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
354 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
355 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
356 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
360 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
361 LLVMValueRef src0
, LLVMValueRef src1
)
363 LLVMValueRef dst64
, result
;
364 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
365 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
367 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
368 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
369 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
373 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
375 const LLVMValueRef srcs
[3])
378 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
380 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
381 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
385 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
386 LLVMValueRef src0
, LLVMValueRef src1
,
387 LLVMValueRef src2
, LLVMValueRef src3
)
389 LLVMValueRef bfi_args
[3], result
;
391 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
392 LLVMBuildSub(ctx
->builder
,
393 LLVMBuildShl(ctx
->builder
,
398 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
401 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
404 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
405 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
407 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
408 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
409 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
411 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
415 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
418 LLVMValueRef comp
[2];
420 src0
= ac_to_float(ctx
, src0
);
421 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
422 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
424 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
427 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
430 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
431 LLVMValueRef temps
[2], result
, val
;
434 for (i
= 0; i
< 2; i
++) {
435 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
436 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
437 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
438 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
441 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
443 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
448 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
456 if (op
== nir_op_fddx_fine
)
457 mask
= AC_TID_MASK_LEFT
;
458 else if (op
== nir_op_fddy_fine
)
459 mask
= AC_TID_MASK_TOP
;
461 mask
= AC_TID_MASK_TOP_LEFT
;
463 /* for DDX we want to next X pixel, DDY next Y pixel. */
464 if (op
== nir_op_fddx_fine
||
465 op
== nir_op_fddx_coarse
||
471 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
476 * this takes an I,J coordinate pair,
477 * and works out the X and Y derivatives.
478 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
480 static LLVMValueRef
emit_ddxy_interp(
481 struct ac_nir_context
*ctx
,
482 LLVMValueRef interp_ij
)
484 LLVMValueRef result
[4], a
;
487 for (i
= 0; i
< 2; i
++) {
488 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
489 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
490 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
491 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
493 return ac_build_gather_values(&ctx
->ac
, result
, 4);
496 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
498 LLVMValueRef src
[4], result
= NULL
;
499 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
500 unsigned src_components
;
501 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
503 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
510 case nir_op_pack_half_2x16
:
513 case nir_op_unpack_half_2x16
:
516 case nir_op_cube_face_coord
:
517 case nir_op_cube_face_index
:
521 src_components
= num_components
;
524 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
525 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
533 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
534 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
537 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
540 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
543 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
546 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
547 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
548 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
551 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
552 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
553 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
556 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
559 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
562 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
565 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
568 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
569 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
570 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
571 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
572 ac_to_float_type(&ctx
->ac
, def_type
), result
);
573 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
574 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
577 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
578 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
579 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
582 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
585 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
588 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
591 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
592 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
593 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
597 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
601 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
604 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
607 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
610 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
611 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
612 LLVMTypeOf(src
[0]), ""),
616 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
617 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
618 LLVMTypeOf(src
[0]), ""),
622 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
623 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
624 LLVMTypeOf(src
[0]), ""),
628 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
631 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
634 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
637 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
640 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
643 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
646 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
649 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
652 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
655 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
658 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
659 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
662 result
= emit_iabs(&ctx
->ac
, src
[0]);
665 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
668 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
671 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
674 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
677 result
= ac_build_isign(&ctx
->ac
, src
[0],
678 instr
->dest
.dest
.ssa
.bit_size
);
681 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
682 result
= ac_build_fsign(&ctx
->ac
, src
[0],
683 instr
->dest
.dest
.ssa
.bit_size
);
686 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
687 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
690 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
691 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
694 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
695 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
697 case nir_op_fround_even
:
698 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
699 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
702 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
703 result
= ac_build_fract(&ctx
->ac
, src
[0],
704 instr
->dest
.dest
.ssa
.bit_size
);
707 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
708 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
711 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
712 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
715 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
716 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
719 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
720 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
723 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
724 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
727 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
728 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
729 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
732 case nir_op_frexp_exp
:
733 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
734 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
735 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
738 case nir_op_frexp_sig
:
739 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
740 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
741 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
744 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
746 if (ctx
->ac
.chip_class
< GFX9
&&
747 instr
->dest
.dest
.ssa
.bit_size
== 32) {
748 /* Only pre-GFX9 chips do not flush denorms. */
749 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
750 ac_to_float_type(&ctx
->ac
, def_type
),
755 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
756 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
757 if (ctx
->ac
.chip_class
< GFX9
&&
758 instr
->dest
.dest
.ssa
.bit_size
== 32) {
759 /* Only pre-GFX9 chips do not flush denorms. */
760 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
761 ac_to_float_type(&ctx
->ac
, def_type
),
766 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
767 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
770 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
771 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
772 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
774 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
776 case nir_op_ibitfield_extract
:
777 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
779 case nir_op_ubitfield_extract
:
780 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
782 case nir_op_bitfield_insert
:
783 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
785 case nir_op_bitfield_reverse
:
786 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
788 case nir_op_bit_count
:
789 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
790 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
792 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
793 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
799 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
800 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
801 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
805 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
806 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
810 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
811 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
815 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
816 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
820 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
821 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
824 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
825 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
828 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
829 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
833 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
834 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
835 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
837 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
841 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
842 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
843 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
845 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
848 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
850 case nir_op_find_lsb
:
851 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
852 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
854 case nir_op_ufind_msb
:
855 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
856 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
858 case nir_op_ifind_msb
:
859 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
860 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
862 case nir_op_uadd_carry
:
863 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
864 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
865 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
867 case nir_op_usub_borrow
:
868 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
869 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
870 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
873 result
= emit_b2f(&ctx
->ac
, src
[0]);
876 result
= emit_f2b(&ctx
->ac
, src
[0]);
879 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 result
= emit_i2b(&ctx
->ac
, src
[0]);
885 case nir_op_fquantize2f16
:
886 result
= emit_f2f16(&ctx
->ac
, src
[0]);
888 case nir_op_umul_high
:
889 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
890 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
891 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
893 case nir_op_imul_high
:
894 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
895 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
896 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
898 case nir_op_pack_half_2x16
:
899 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
901 case nir_op_unpack_half_2x16
:
902 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
906 case nir_op_fddx_fine
:
907 case nir_op_fddy_fine
:
908 case nir_op_fddx_coarse
:
909 case nir_op_fddy_coarse
:
910 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
913 case nir_op_unpack_64_2x32_split_x
: {
914 assert(ac_get_llvm_num_components(src
[0]) == 1);
915 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
918 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
923 case nir_op_unpack_64_2x32_split_y
: {
924 assert(ac_get_llvm_num_components(src
[0]) == 1);
925 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
928 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
933 case nir_op_pack_64_2x32_split
: {
934 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
935 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
936 src
[0], ctx
->ac
.i32_0
, "");
937 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
938 src
[1], ctx
->ac
.i32_1
, "");
939 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
943 case nir_op_cube_face_coord
: {
944 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
945 LLVMValueRef results
[2];
947 for (unsigned chan
= 0; chan
< 3; chan
++)
948 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
949 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
950 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
951 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
952 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
953 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
957 case nir_op_cube_face_index
: {
958 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
960 for (unsigned chan
= 0; chan
< 3; chan
++)
961 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
962 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
963 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
968 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
969 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
970 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
971 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
974 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
975 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
978 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
979 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
982 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
983 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
984 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
985 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
988 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
989 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
992 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
993 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
996 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
997 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
998 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
999 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1000 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1001 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1002 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1003 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1006 case nir_op_imed3
: {
1007 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1008 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1009 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1010 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1013 case nir_op_umed3
: {
1014 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1015 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1016 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1017 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1022 fprintf(stderr
, "Unknown NIR alu instr: ");
1023 nir_print_instr(&instr
->instr
, stderr
);
1024 fprintf(stderr
, "\n");
1029 assert(instr
->dest
.dest
.is_ssa
);
1030 result
= ac_to_integer(&ctx
->ac
, result
);
1031 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
1036 static void visit_load_const(struct ac_nir_context
*ctx
,
1037 const nir_load_const_instr
*instr
)
1039 LLVMValueRef values
[4], value
= NULL
;
1040 LLVMTypeRef element_type
=
1041 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1043 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1044 switch (instr
->def
.bit_size
) {
1046 values
[i
] = LLVMConstInt(element_type
,
1047 instr
->value
.u32
[i
], false);
1050 values
[i
] = LLVMConstInt(element_type
,
1051 instr
->value
.u64
[i
], false);
1055 "unsupported nir load_const bit_size: %d\n",
1056 instr
->def
.bit_size
);
1060 if (instr
->def
.num_components
> 1) {
1061 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1065 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1069 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1072 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1073 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1076 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1077 /* On VI, the descriptor contains the size in bytes,
1078 * but TXQ must return the size in elements.
1079 * The stride is always non-zero for resources using TXQ.
1081 LLVMValueRef stride
=
1082 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1084 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1085 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1086 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1087 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1089 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1095 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1098 static void build_int_type_name(
1100 char *buf
, unsigned bufsize
)
1102 assert(bufsize
>= 6);
1104 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1105 snprintf(buf
, bufsize
, "v%ui32",
1106 LLVMGetVectorSize(type
));
1111 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1112 struct ac_image_args
*args
,
1113 const nir_tex_instr
*instr
)
1115 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1116 LLVMValueRef coord
= args
->addr
;
1117 LLVMValueRef half_texel
[2];
1118 LLVMValueRef compare_cube_wa
= NULL
;
1119 LLVMValueRef result
;
1121 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1125 struct ac_image_args txq_args
= { 0 };
1127 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1128 txq_args
.opcode
= ac_image_get_resinfo
;
1129 txq_args
.dmask
= 0xf;
1130 txq_args
.addr
= ctx
->i32_0
;
1131 txq_args
.resource
= args
->resource
;
1132 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1134 for (c
= 0; c
< 2; c
++) {
1135 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1136 LLVMConstInt(ctx
->i32
, c
, false), "");
1137 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1138 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1139 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1140 LLVMConstReal(ctx
->f32
, -0.5), "");
1144 LLVMValueRef orig_coords
= args
->addr
;
1146 for (c
= 0; c
< 2; c
++) {
1148 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1149 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1150 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1151 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1152 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1153 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1158 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1159 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1160 * workaround by sampling using a scaled type and converting.
1161 * This is taken from amdgpu-pro shaders.
1163 /* NOTE this produces some ugly code compared to amdgpu-pro,
1164 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1165 * and then reads them back. -pro generates two selects,
1166 * one s_cmp for the descriptor rewriting
1167 * one v_cmp for the coordinate and result changes.
1169 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1170 LLVMValueRef tmp
, tmp2
;
1172 /* workaround 8/8/8/8 uint/sint cube gather bug */
1173 /* first detect it then change to a scaled read and f2i */
1174 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1177 /* extract the DATA_FORMAT */
1178 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1179 LLVMConstInt(ctx
->i32
, 6, false), false);
1181 /* is the DATA_FORMAT == 8_8_8_8 */
1182 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1184 if (stype
== GLSL_TYPE_UINT
)
1185 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1186 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1187 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1189 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1190 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1191 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1193 /* replace the NUM FORMAT in the descriptor */
1194 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1195 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1197 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1199 /* don't modify the coordinates for this case */
1200 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1203 result
= ac_build_image_opcode(ctx
, args
);
1205 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1206 LLVMValueRef tmp
, tmp2
;
1208 /* if the cube workaround is in place, f2i the result. */
1209 for (c
= 0; c
< 4; c
++) {
1210 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1211 if (stype
== GLSL_TYPE_UINT
)
1212 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1214 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1215 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1216 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1217 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1218 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1219 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1225 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1226 const nir_tex_instr
*instr
,
1228 struct ac_image_args
*args
)
1230 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1231 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1233 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1234 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1238 util_last_bit(mask
),
1241 return ac_build_buffer_load_format(&ctx
->ac
,
1245 util_last_bit(mask
),
1250 args
->opcode
= ac_image_sample
;
1251 args
->compare
= instr
->is_shadow
;
1253 switch (instr
->op
) {
1255 case nir_texop_txf_ms
:
1256 case nir_texop_samples_identical
:
1257 args
->opcode
= lod_is_zero
||
1258 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1259 ac_image_load
: ac_image_load_mip
;
1260 args
->compare
= false;
1261 args
->offset
= false;
1268 args
->level_zero
= true;
1273 case nir_texop_query_levels
:
1274 args
->opcode
= ac_image_get_resinfo
;
1277 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1278 args
->level_zero
= true;
1284 args
->opcode
= ac_image_gather4
;
1285 args
->level_zero
= true;
1288 args
->opcode
= ac_image_get_lod
;
1289 args
->compare
= false;
1290 args
->offset
= false;
1296 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1297 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1298 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1299 return lower_gather4_integer(&ctx
->ac
, args
, instr
);
1302 return ac_build_image_opcode(&ctx
->ac
, args
);
1305 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1306 nir_intrinsic_instr
*instr
)
1308 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1309 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1311 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1312 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1316 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1317 nir_intrinsic_instr
*instr
)
1319 LLVMValueRef ptr
, addr
;
1321 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1322 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1323 get_src(ctx
, instr
->src
[0]), "");
1325 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1326 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1328 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1331 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1332 const nir_intrinsic_instr
*instr
)
1334 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1336 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1339 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1341 uint32_t new_mask
= 0;
1342 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1343 if (mask
& (1u << i
))
1344 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1348 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1349 unsigned start
, unsigned count
)
1351 LLVMTypeRef type
= LLVMTypeOf(src
);
1353 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1359 unsigned src_elements
= LLVMGetVectorSize(type
);
1360 assert(start
< src_elements
);
1361 assert(start
+ count
<= src_elements
);
1363 if (start
== 0 && count
== src_elements
)
1367 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1370 LLVMValueRef indices
[8];
1371 for (unsigned i
= 0; i
< count
; ++i
)
1372 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1374 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1375 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1378 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1379 nir_intrinsic_instr
*instr
)
1381 const char *store_name
;
1382 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1383 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1384 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1385 int components_32bit
= elem_size_mult
* instr
->num_components
;
1386 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1387 LLVMValueRef base_data
, base_offset
;
1388 LLVMValueRef params
[6];
1390 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1391 get_src(ctx
, instr
->src
[1]), true);
1392 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1393 params
[4] = ctx
->ac
.i1false
; /* glc */
1394 params
[5] = ctx
->ac
.i1false
; /* slc */
1396 if (components_32bit
> 1)
1397 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1399 writemask
= widen_mask(writemask
, elem_size_mult
);
1401 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1402 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1403 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1405 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1409 LLVMValueRef offset
;
1411 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1413 /* Due to an LLVM limitation, split 3-element writes
1414 * into a 2-element and a 1-element write. */
1416 writemask
|= 1 << (start
+ 2);
1421 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1426 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1427 } else if (count
== 2) {
1428 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1432 store_name
= "llvm.amdgcn.buffer.store.f32";
1434 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1436 offset
= base_offset
;
1438 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1442 ac_build_intrinsic(&ctx
->ac
, store_name
,
1443 ctx
->ac
.voidt
, params
, 6, 0);
1447 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1448 const nir_intrinsic_instr
*instr
)
1451 LLVMValueRef params
[6];
1454 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1455 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1457 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1458 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1459 get_src(ctx
, instr
->src
[0]),
1461 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1462 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1463 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1465 switch (instr
->intrinsic
) {
1466 case nir_intrinsic_ssbo_atomic_add
:
1467 name
= "llvm.amdgcn.buffer.atomic.add";
1469 case nir_intrinsic_ssbo_atomic_imin
:
1470 name
= "llvm.amdgcn.buffer.atomic.smin";
1472 case nir_intrinsic_ssbo_atomic_umin
:
1473 name
= "llvm.amdgcn.buffer.atomic.umin";
1475 case nir_intrinsic_ssbo_atomic_imax
:
1476 name
= "llvm.amdgcn.buffer.atomic.smax";
1478 case nir_intrinsic_ssbo_atomic_umax
:
1479 name
= "llvm.amdgcn.buffer.atomic.umax";
1481 case nir_intrinsic_ssbo_atomic_and
:
1482 name
= "llvm.amdgcn.buffer.atomic.and";
1484 case nir_intrinsic_ssbo_atomic_or
:
1485 name
= "llvm.amdgcn.buffer.atomic.or";
1487 case nir_intrinsic_ssbo_atomic_xor
:
1488 name
= "llvm.amdgcn.buffer.atomic.xor";
1490 case nir_intrinsic_ssbo_atomic_exchange
:
1491 name
= "llvm.amdgcn.buffer.atomic.swap";
1493 case nir_intrinsic_ssbo_atomic_comp_swap
:
1494 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1500 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1503 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1504 const nir_intrinsic_instr
*instr
)
1506 LLVMValueRef results
[2];
1507 int load_components
;
1508 int num_components
= instr
->num_components
;
1509 if (instr
->dest
.ssa
.bit_size
== 64)
1510 num_components
*= 2;
1512 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1513 load_components
= MIN2(num_components
- i
, 4);
1514 const char *load_name
;
1515 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1516 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1517 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1519 if (load_components
== 3)
1520 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1521 else if (load_components
> 1)
1522 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1524 if (load_components
>= 3)
1525 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1526 else if (load_components
== 2)
1527 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1528 else if (load_components
== 1)
1529 load_name
= "llvm.amdgcn.buffer.load.f32";
1531 unreachable("unhandled number of components");
1533 LLVMValueRef params
[] = {
1534 ctx
->abi
->load_ssbo(ctx
->abi
,
1535 get_src(ctx
, instr
->src
[0]),
1543 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1547 LLVMValueRef ret
= results
[0];
1548 if (num_components
> 4 || num_components
== 3) {
1549 LLVMValueRef masks
[] = {
1550 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1551 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1552 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1553 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1556 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1557 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1558 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1561 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1562 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1565 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1566 const nir_intrinsic_instr
*instr
)
1569 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1570 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1571 int num_components
= instr
->num_components
;
1573 if (ctx
->abi
->load_ubo
)
1574 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1576 if (instr
->dest
.ssa
.bit_size
== 64)
1577 num_components
*= 2;
1579 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1580 NULL
, 0, false, false, true, true);
1581 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1582 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1583 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1587 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1588 bool vs_in
, unsigned *vertex_index_out
,
1589 LLVMValueRef
*vertex_index_ref
,
1590 unsigned *const_out
, LLVMValueRef
*indir_out
)
1592 unsigned const_offset
= 0;
1593 nir_deref
*tail
= &deref
->deref
;
1594 LLVMValueRef offset
= NULL
;
1596 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1598 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1599 if (vertex_index_out
)
1600 *vertex_index_out
= deref_array
->base_offset
;
1602 if (vertex_index_ref
) {
1603 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1604 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1605 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1607 *vertex_index_ref
= vtx
;
1611 if (deref
->var
->data
.compact
) {
1612 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1613 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1614 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1615 /* We always lower indirect dereferences for "compact" array vars. */
1616 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1618 const_offset
= deref_array
->base_offset
;
1622 while (tail
->child
!= NULL
) {
1623 const struct glsl_type
*parent_type
= tail
->type
;
1626 if (tail
->deref_type
== nir_deref_type_array
) {
1627 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1628 LLVMValueRef index
, stride
, local_offset
;
1629 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1631 const_offset
+= size
* deref_array
->base_offset
;
1632 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1635 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1636 index
= get_src(ctx
, deref_array
->indirect
);
1637 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1638 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1641 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1643 offset
= local_offset
;
1644 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1645 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1647 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1648 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1649 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1652 unreachable("unsupported deref type");
1656 if (const_offset
&& offset
)
1657 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1658 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1661 *const_out
= const_offset
;
1662 *indir_out
= offset
;
1666 build_gep_for_deref(struct ac_nir_context
*ctx
,
1667 nir_deref_var
*deref
)
1669 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1670 assert(entry
->data
);
1671 LLVMValueRef val
= entry
->data
;
1672 nir_deref
*tail
= deref
->deref
.child
;
1673 while (tail
!= NULL
) {
1674 LLVMValueRef offset
;
1675 switch (tail
->deref_type
) {
1676 case nir_deref_type_array
: {
1677 nir_deref_array
*array
= nir_deref_as_array(tail
);
1678 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1679 if (array
->deref_array_type
==
1680 nir_deref_array_type_indirect
) {
1681 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1688 case nir_deref_type_struct
: {
1689 nir_deref_struct
*deref_struct
=
1690 nir_deref_as_struct(tail
);
1691 offset
= LLVMConstInt(ctx
->ac
.i32
,
1692 deref_struct
->index
, 0);
1696 unreachable("bad deref type");
1698 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1704 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1705 nir_intrinsic_instr
*instr
,
1708 LLVMValueRef result
;
1709 LLVMValueRef vertex_index
= NULL
;
1710 LLVMValueRef indir_index
= NULL
;
1711 unsigned const_index
= 0;
1712 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1713 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1714 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1715 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1717 get_deref_offset(ctx
, instr
->variables
[0],
1718 false, NULL
, is_patch
? NULL
: &vertex_index
,
1719 &const_index
, &indir_index
);
1721 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1723 LLVMTypeRef src_component_type
;
1724 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1725 src_component_type
= LLVMGetElementType(dest_type
);
1727 src_component_type
= dest_type
;
1729 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1730 vertex_index
, indir_index
,
1731 const_index
, location
, driver_location
,
1732 instr
->variables
[0]->var
->data
.location_frac
,
1733 instr
->num_components
,
1734 is_patch
, is_compact
, load_inputs
);
1735 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1738 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1739 nir_intrinsic_instr
*instr
)
1741 LLVMValueRef values
[8];
1742 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1743 int ve
= instr
->dest
.ssa
.num_components
;
1744 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1745 LLVMValueRef indir_index
;
1747 unsigned const_index
;
1748 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1749 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1750 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1751 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1752 &const_index
, &indir_index
);
1754 if (instr
->dest
.ssa
.bit_size
== 64)
1757 switch (instr
->variables
[0]->var
->data
.mode
) {
1758 case nir_var_shader_in
:
1759 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1760 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1761 return load_tess_varyings(ctx
, instr
, true);
1764 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1765 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1766 LLVMValueRef indir_index
;
1767 unsigned const_index
, vertex_index
;
1768 get_deref_offset(ctx
, instr
->variables
[0],
1769 false, &vertex_index
, NULL
,
1770 &const_index
, &indir_index
);
1772 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1773 instr
->variables
[0]->var
->data
.driver_location
,
1774 instr
->variables
[0]->var
->data
.location_frac
,
1775 instr
->num_components
, vertex_index
, const_index
, type
);
1778 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1780 unsigned count
= glsl_count_attribute_slots(
1781 instr
->variables
[0]->var
->type
,
1782 ctx
->stage
== MESA_SHADER_VERTEX
);
1784 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1785 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1786 stride
, false, true);
1788 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1792 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1796 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1798 unsigned count
= glsl_count_attribute_slots(
1799 instr
->variables
[0]->var
->type
, false);
1801 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1802 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1803 stride
, true, true);
1805 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1809 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1813 case nir_var_shared
: {
1814 LLVMValueRef address
= build_gep_for_deref(ctx
,
1815 instr
->variables
[0]);
1816 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1817 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1818 get_def_type(ctx
, &instr
->dest
.ssa
),
1821 case nir_var_shader_out
:
1822 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1823 return load_tess_varyings(ctx
, instr
, false);
1826 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1828 unsigned count
= glsl_count_attribute_slots(
1829 instr
->variables
[0]->var
->type
, false);
1831 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1832 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1833 stride
, true, true);
1835 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1839 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1840 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1846 unreachable("unhandle variable mode");
1848 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1849 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1853 visit_store_var(struct ac_nir_context
*ctx
,
1854 nir_intrinsic_instr
*instr
)
1856 LLVMValueRef temp_ptr
, value
;
1857 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1858 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1859 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1860 int writemask
= instr
->const_index
[0];
1861 LLVMValueRef indir_index
;
1862 unsigned const_index
;
1863 get_deref_offset(ctx
, instr
->variables
[0], false,
1864 NULL
, NULL
, &const_index
, &indir_index
);
1866 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1868 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1869 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1872 writemask
= widen_mask(writemask
, 2);
1875 writemask
= writemask
<< comp
;
1877 switch (instr
->variables
[0]->var
->data
.mode
) {
1878 case nir_var_shader_out
:
1880 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1881 LLVMValueRef vertex_index
= NULL
;
1882 LLVMValueRef indir_index
= NULL
;
1883 unsigned const_index
= 0;
1884 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1886 get_deref_offset(ctx
, instr
->variables
[0],
1887 false, NULL
, is_patch
? NULL
: &vertex_index
,
1888 &const_index
, &indir_index
);
1890 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1891 vertex_index
, indir_index
,
1892 const_index
, src
, writemask
);
1896 for (unsigned chan
= 0; chan
< 8; chan
++) {
1898 if (!(writemask
& (1 << chan
)))
1901 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1903 if (instr
->variables
[0]->var
->data
.compact
)
1906 unsigned count
= glsl_count_attribute_slots(
1907 instr
->variables
[0]->var
->type
, false);
1909 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1910 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1911 stride
, true, true);
1913 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1914 value
, indir_index
, "");
1915 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1916 count
, stride
, tmp_vec
);
1919 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1921 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1926 for (unsigned chan
= 0; chan
< 8; chan
++) {
1927 if (!(writemask
& (1 << chan
)))
1930 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1932 unsigned count
= glsl_count_attribute_slots(
1933 instr
->variables
[0]->var
->type
, false);
1935 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1936 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1939 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1940 value
, indir_index
, "");
1941 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1944 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1946 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1950 case nir_var_shared
: {
1951 int writemask
= instr
->const_index
[0];
1952 LLVMValueRef address
= build_gep_for_deref(ctx
,
1953 instr
->variables
[0]);
1954 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1955 unsigned components
=
1956 glsl_get_vector_elements(
1957 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1958 if (writemask
== (1 << components
) - 1) {
1959 val
= LLVMBuildBitCast(
1960 ctx
->ac
.builder
, val
,
1961 LLVMGetElementType(LLVMTypeOf(address
)), "");
1962 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1964 for (unsigned chan
= 0; chan
< 4; chan
++) {
1965 if (!(writemask
& (1 << chan
)))
1968 LLVMBuildStructGEP(ctx
->ac
.builder
,
1970 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1972 src
= LLVMBuildBitCast(
1973 ctx
->ac
.builder
, src
,
1974 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
1975 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
1985 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
1988 case GLSL_SAMPLER_DIM_BUF
:
1990 case GLSL_SAMPLER_DIM_1D
:
1991 return array
? 2 : 1;
1992 case GLSL_SAMPLER_DIM_2D
:
1993 return array
? 3 : 2;
1994 case GLSL_SAMPLER_DIM_MS
:
1995 return array
? 4 : 3;
1996 case GLSL_SAMPLER_DIM_3D
:
1997 case GLSL_SAMPLER_DIM_CUBE
:
1999 case GLSL_SAMPLER_DIM_RECT
:
2000 case GLSL_SAMPLER_DIM_SUBPASS
:
2002 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2011 glsl_is_array_image(const struct glsl_type
*type
)
2013 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2015 if (glsl_sampler_type_is_array(type
))
2018 return dim
== GLSL_SAMPLER_DIM_CUBE
||
2019 dim
== GLSL_SAMPLER_DIM_3D
||
2020 dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2021 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
;
2025 /* Adjust the sample index according to FMASK.
2027 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2028 * which is the identity mapping. Each nibble says which physical sample
2029 * should be fetched to get that sample.
2031 * For example, 0x11111100 means there are only 2 samples stored and
2032 * the second sample covers 3/4 of the pixel. When reading samples 0
2033 * and 1, return physical sample 0 (determined by the first two 0s
2034 * in FMASK), otherwise return physical sample 1.
2036 * The sample index should be adjusted as follows:
2037 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2039 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2040 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2041 LLVMValueRef coord_z
,
2042 LLVMValueRef sample_index
,
2043 LLVMValueRef fmask_desc_ptr
)
2045 LLVMValueRef fmask_load_address
[4];
2048 fmask_load_address
[0] = coord_x
;
2049 fmask_load_address
[1] = coord_y
;
2051 fmask_load_address
[2] = coord_z
;
2052 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
2055 struct ac_image_args args
= {0};
2057 args
.opcode
= ac_image_load
;
2058 args
.da
= coord_z
? true : false;
2059 args
.resource
= fmask_desc_ptr
;
2061 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
2063 res
= ac_build_image_opcode(ctx
, &args
);
2065 res
= ac_to_integer(ctx
, res
);
2066 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2067 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2069 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2073 LLVMValueRef sample_index4
=
2074 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2075 LLVMValueRef shifted_fmask
=
2076 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2077 LLVMValueRef final_sample
=
2078 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2080 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2081 * resource descriptor is 0 (invalid),
2083 LLVMValueRef fmask_desc
=
2084 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2087 LLVMValueRef fmask_word1
=
2088 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2091 LLVMValueRef word1_is_nonzero
=
2092 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2093 fmask_word1
, ctx
->i32_0
, "");
2095 /* Replace the MSAA sample index. */
2097 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2098 final_sample
, sample_index
, "");
2099 return sample_index
;
2102 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
2103 const nir_intrinsic_instr
*instr
)
2105 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2107 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2108 LLVMValueRef coords
[4];
2109 LLVMValueRef masks
[] = {
2110 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2111 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2114 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2117 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2118 bool is_array
= glsl_sampler_type_is_array(type
);
2119 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2120 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2121 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2122 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2123 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2124 count
= image_type_to_components_count(dim
, is_array
);
2127 LLVMValueRef fmask_load_address
[3];
2130 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2131 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2133 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2135 fmask_load_address
[2] = NULL
;
2137 for (chan
= 0; chan
< 2; ++chan
)
2138 fmask_load_address
[chan
] =
2139 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2140 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2141 ctx
->ac
.i32
, ""), "");
2142 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2144 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2145 fmask_load_address
[0],
2146 fmask_load_address
[1],
2147 fmask_load_address
[2],
2149 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
2151 if (count
== 1 && !gfx9_1d
) {
2152 if (instr
->src
[0].ssa
->num_components
)
2153 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2160 for (chan
= 0; chan
< count
; ++chan
) {
2161 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2164 for (chan
= 0; chan
< 2; ++chan
)
2165 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2166 ctx
->ac
.i32
, ""), "");
2167 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2173 coords
[2] = coords
[1];
2174 coords
[1] = ctx
->ac
.i32_0
;
2176 coords
[1] = ctx
->ac
.i32_0
;
2181 coords
[count
] = sample_index
;
2186 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
2189 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2194 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2195 const nir_intrinsic_instr
*instr
)
2197 LLVMValueRef params
[7];
2199 char intrinsic_name
[64];
2200 const nir_variable
*var
= instr
->variables
[0]->var
;
2201 const struct glsl_type
*type
= var
->type
;
2203 if(instr
->variables
[0]->deref
.child
)
2204 type
= instr
->variables
[0]->deref
.child
->type
;
2206 type
= glsl_without_array(type
);
2208 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2209 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2210 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2211 unsigned num_channels
= util_last_bit(mask
);
2212 LLVMValueRef rsrc
, vindex
;
2214 rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
2215 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2218 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2219 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2220 ctx
->ac
.i32_0
, num_channels
,
2222 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2224 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2225 res
= ac_to_integer(&ctx
->ac
, res
);
2227 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2228 LLVMValueRef slc
= ctx
->ac
.i1false
;
2230 params
[0] = get_image_coords(ctx
, instr
);
2231 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2232 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2233 params
[3] = (var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2234 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2236 params
[5] = ctx
->ac
.i1false
;
2239 ac_get_image_intr_name("llvm.amdgcn.image.load",
2240 ctx
->ac
.v4f32
, /* vdata */
2241 LLVMTypeOf(params
[0]), /* coords */
2242 LLVMTypeOf(params
[1]), /* rsrc */
2243 intrinsic_name
, sizeof(intrinsic_name
));
2245 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
2246 params
, 7, AC_FUNC_ATTR_READONLY
);
2248 return ac_to_integer(&ctx
->ac
, res
);
2251 static void visit_image_store(struct ac_nir_context
*ctx
,
2252 nir_intrinsic_instr
*instr
)
2254 LLVMValueRef params
[8];
2255 char intrinsic_name
[64];
2256 const nir_variable
*var
= instr
->variables
[0]->var
;
2257 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2258 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2259 LLVMValueRef glc
= ctx
->ac
.i1false
;
2260 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2262 glc
= ctx
->ac
.i1true
;
2264 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2265 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
2267 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2268 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2269 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2270 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2272 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2273 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2274 elem_count
, stride
, "");
2276 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2277 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2280 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2282 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2283 ctx
->ac
.i32_0
, ""); /* vindex */
2284 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2285 params
[4] = glc
; /* glc */
2286 params
[5] = ctx
->ac
.i1false
; /* slc */
2287 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2290 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2291 LLVMValueRef slc
= ctx
->ac
.i1false
;
2293 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2294 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2295 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
2296 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2297 params
[4] = (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2298 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2300 params
[6] = ctx
->ac
.i1false
;
2303 ac_get_image_intr_name("llvm.amdgcn.image.store",
2304 LLVMTypeOf(params
[0]), /* vdata */
2305 LLVMTypeOf(params
[1]), /* coords */
2306 LLVMTypeOf(params
[2]), /* rsrc */
2307 intrinsic_name
, sizeof(intrinsic_name
));
2309 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
2315 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2316 const nir_intrinsic_instr
*instr
)
2318 LLVMValueRef params
[7];
2319 int param_count
= 0;
2320 const nir_variable
*var
= instr
->variables
[0]->var
;
2322 const char *atomic_name
;
2323 char intrinsic_name
[41];
2324 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2325 MAYBE_UNUSED
int length
;
2327 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2329 switch (instr
->intrinsic
) {
2330 case nir_intrinsic_image_var_atomic_add
:
2331 atomic_name
= "add";
2333 case nir_intrinsic_image_var_atomic_min
:
2334 atomic_name
= is_unsigned
? "umin" : "smin";
2336 case nir_intrinsic_image_var_atomic_max
:
2337 atomic_name
= is_unsigned
? "umax" : "smax";
2339 case nir_intrinsic_image_var_atomic_and
:
2340 atomic_name
= "and";
2342 case nir_intrinsic_image_var_atomic_or
:
2345 case nir_intrinsic_image_var_atomic_xor
:
2346 atomic_name
= "xor";
2348 case nir_intrinsic_image_var_atomic_exchange
:
2349 atomic_name
= "swap";
2351 case nir_intrinsic_image_var_atomic_comp_swap
:
2352 atomic_name
= "cmpswap";
2358 if (instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
)
2359 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2360 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2362 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2363 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
2365 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2366 ctx
->ac
.i32_0
, ""); /* vindex */
2367 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2368 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2370 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2371 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2373 char coords_type
[8];
2375 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2376 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
2378 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
2379 params
[param_count
++] = glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
2380 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2382 build_int_type_name(LLVMTypeOf(coords
),
2383 coords_type
, sizeof(coords_type
));
2385 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2386 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
2389 assert(length
< sizeof(intrinsic_name
));
2390 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
2393 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2394 const nir_intrinsic_instr
*instr
)
2396 const nir_variable
*var
= instr
->variables
[0]->var
;
2397 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2399 struct ac_image_args args
= { 0 };
2400 args
.da
= glsl_is_array_image(type
);
2402 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2403 AC_DESC_IMAGE
, NULL
, true, false);
2404 args
.opcode
= ac_image_get_resinfo
;
2405 args
.addr
= ctx
->ac
.i32_0
;
2407 return ac_build_image_opcode(&ctx
->ac
, &args
);
2410 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2411 const nir_intrinsic_instr
*instr
)
2414 const nir_variable
*var
= instr
->variables
[0]->var
;
2415 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2417 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2418 return get_buffer_size(ctx
,
2419 get_sampler_desc(ctx
, instr
->variables
[0],
2420 AC_DESC_BUFFER
, NULL
, true, false), true);
2422 struct ac_image_args args
= { 0 };
2424 args
.da
= glsl_is_array_image(type
);
2426 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2427 args
.opcode
= ac_image_get_resinfo
;
2428 args
.addr
= ctx
->ac
.i32_0
;
2430 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2432 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2434 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2435 glsl_sampler_type_is_array(type
)) {
2436 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2437 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2438 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2439 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2441 if (ctx
->ac
.chip_class
>= GFX9
&&
2442 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2443 glsl_sampler_type_is_array(type
)) {
2444 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2445 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2452 #define NOOP_WAITCNT 0xf7f
2453 #define LGKM_CNT 0x07f
2454 #define VM_CNT 0xf70
2456 static void emit_membar(struct ac_llvm_context
*ac
,
2457 const nir_intrinsic_instr
*instr
)
2459 unsigned waitcnt
= NOOP_WAITCNT
;
2461 switch (instr
->intrinsic
) {
2462 case nir_intrinsic_memory_barrier
:
2463 case nir_intrinsic_group_memory_barrier
:
2464 waitcnt
&= VM_CNT
& LGKM_CNT
;
2466 case nir_intrinsic_memory_barrier_atomic_counter
:
2467 case nir_intrinsic_memory_barrier_buffer
:
2468 case nir_intrinsic_memory_barrier_image
:
2471 case nir_intrinsic_memory_barrier_shared
:
2472 waitcnt
&= LGKM_CNT
;
2477 if (waitcnt
!= NOOP_WAITCNT
)
2478 ac_build_waitcnt(ac
, waitcnt
);
2481 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2483 /* SI only (thanks to a hw bug workaround):
2484 * The real barrier instruction isn’t needed, because an entire patch
2485 * always fits into a single wave.
2487 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2488 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2491 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2492 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2495 static void emit_discard(struct ac_nir_context
*ctx
,
2496 const nir_intrinsic_instr
*instr
)
2500 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2501 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2502 get_src(ctx
, instr
->src
[0]),
2505 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2506 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2509 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2513 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2515 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2516 "llvm.amdgcn.ps.live",
2517 ctx
->ac
.i1
, NULL
, 0,
2518 AC_FUNC_ATTR_READNONE
);
2519 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2520 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2524 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2526 LLVMValueRef result
;
2527 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2528 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2529 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2531 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2535 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2537 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2538 LLVMValueRef result
;
2539 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2540 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2541 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2543 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2548 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2550 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2551 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2552 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2554 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2559 visit_first_invocation(struct ac_nir_context
*ctx
)
2561 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2563 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2564 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2565 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2567 ctx
->ac
.i64
, args
, 2,
2568 AC_FUNC_ATTR_NOUNWIND
|
2569 AC_FUNC_ATTR_READNONE
);
2571 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2575 visit_load_shared(struct ac_nir_context
*ctx
,
2576 const nir_intrinsic_instr
*instr
)
2578 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2580 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2582 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2583 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2584 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2585 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2588 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2589 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2593 visit_store_shared(struct ac_nir_context
*ctx
,
2594 const nir_intrinsic_instr
*instr
)
2596 LLVMValueRef derived_ptr
, data
,index
;
2597 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2599 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2600 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2602 int writemask
= nir_intrinsic_write_mask(instr
);
2603 for (int chan
= 0; chan
< 4; chan
++) {
2604 if (!(writemask
& (1 << chan
))) {
2607 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2608 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2609 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2610 LLVMBuildStore(builder
, data
, derived_ptr
);
2614 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2615 const nir_intrinsic_instr
*instr
,
2616 LLVMValueRef ptr
, int src_idx
)
2618 LLVMValueRef result
;
2619 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2621 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2622 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2623 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2624 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2626 LLVMAtomicOrderingSequentiallyConsistent
,
2627 LLVMAtomicOrderingSequentiallyConsistent
,
2630 LLVMAtomicRMWBinOp op
;
2631 switch (instr
->intrinsic
) {
2632 case nir_intrinsic_var_atomic_add
:
2633 case nir_intrinsic_shared_atomic_add
:
2634 op
= LLVMAtomicRMWBinOpAdd
;
2636 case nir_intrinsic_var_atomic_umin
:
2637 case nir_intrinsic_shared_atomic_umin
:
2638 op
= LLVMAtomicRMWBinOpUMin
;
2640 case nir_intrinsic_var_atomic_umax
:
2641 case nir_intrinsic_shared_atomic_umax
:
2642 op
= LLVMAtomicRMWBinOpUMax
;
2644 case nir_intrinsic_var_atomic_imin
:
2645 case nir_intrinsic_shared_atomic_imin
:
2646 op
= LLVMAtomicRMWBinOpMin
;
2648 case nir_intrinsic_var_atomic_imax
:
2649 case nir_intrinsic_shared_atomic_imax
:
2650 op
= LLVMAtomicRMWBinOpMax
;
2652 case nir_intrinsic_var_atomic_and
:
2653 case nir_intrinsic_shared_atomic_and
:
2654 op
= LLVMAtomicRMWBinOpAnd
;
2656 case nir_intrinsic_var_atomic_or
:
2657 case nir_intrinsic_shared_atomic_or
:
2658 op
= LLVMAtomicRMWBinOpOr
;
2660 case nir_intrinsic_var_atomic_xor
:
2661 case nir_intrinsic_shared_atomic_xor
:
2662 op
= LLVMAtomicRMWBinOpXor
;
2664 case nir_intrinsic_var_atomic_exchange
:
2665 case nir_intrinsic_shared_atomic_exchange
:
2666 op
= LLVMAtomicRMWBinOpXchg
;
2672 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2673 LLVMAtomicOrderingSequentiallyConsistent
,
2679 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2681 LLVMValueRef values
[2];
2682 LLVMValueRef pos
[2];
2684 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2685 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2687 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2688 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2689 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2692 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2693 const nir_intrinsic_instr
*instr
)
2695 LLVMValueRef result
[4];
2696 LLVMValueRef interp_param
, attr_number
;
2699 LLVMValueRef src_c0
= NULL
;
2700 LLVMValueRef src_c1
= NULL
;
2701 LLVMValueRef src0
= NULL
;
2702 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2703 switch (instr
->intrinsic
) {
2704 case nir_intrinsic_interp_var_at_centroid
:
2705 location
= INTERP_CENTROID
;
2707 case nir_intrinsic_interp_var_at_sample
:
2708 case nir_intrinsic_interp_var_at_offset
:
2709 location
= INTERP_CENTER
;
2710 src0
= get_src(ctx
, instr
->src
[0]);
2716 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2717 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2718 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2719 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2720 LLVMValueRef sample_position
;
2721 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2723 /* fetch sample ID */
2724 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2726 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2727 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2728 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2729 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2731 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2732 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2734 if (location
== INTERP_CENTER
) {
2735 LLVMValueRef ij_out
[2];
2736 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2739 * take the I then J parameters, and the DDX/Y for it, and
2740 * calculate the IJ inputs for the interpolator.
2741 * temp1 = ddx * offset/sample.x + I;
2742 * interp_param.I = ddy * offset/sample.y + temp1;
2743 * temp1 = ddx * offset/sample.x + J;
2744 * interp_param.J = ddy * offset/sample.y + temp1;
2746 for (unsigned i
= 0; i
< 2; i
++) {
2747 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2748 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2749 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2750 ddxy_out
, ix_ll
, "");
2751 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2752 ddxy_out
, iy_ll
, "");
2753 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2754 interp_param
, ix_ll
, "");
2755 LLVMValueRef temp1
, temp2
;
2757 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2760 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2761 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2763 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2764 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2766 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2767 temp2
, ctx
->ac
.i32
, "");
2769 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2773 for (chan
= 0; chan
< 4; chan
++) {
2774 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2777 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2778 interp_param
, ctx
->ac
.v2f32
, "");
2779 LLVMValueRef i
= LLVMBuildExtractElement(
2780 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2781 LLVMValueRef j
= LLVMBuildExtractElement(
2782 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2784 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2785 llvm_chan
, attr_number
,
2786 ctx
->abi
->prim_mask
, i
, j
);
2788 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2789 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2790 llvm_chan
, attr_number
,
2791 ctx
->abi
->prim_mask
);
2794 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2795 instr
->variables
[0]->var
->data
.location_frac
);
2798 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2799 nir_intrinsic_instr
*instr
)
2801 LLVMValueRef result
= NULL
;
2803 switch (instr
->intrinsic
) {
2804 case nir_intrinsic_ballot
:
2805 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2807 case nir_intrinsic_read_invocation
:
2808 case nir_intrinsic_read_first_invocation
: {
2809 LLVMValueRef args
[2];
2812 args
[0] = get_src(ctx
, instr
->src
[0]);
2815 const char *intr_name
;
2816 if (instr
->intrinsic
== nir_intrinsic_read_invocation
) {
2818 intr_name
= "llvm.amdgcn.readlane";
2821 args
[1] = get_src(ctx
, instr
->src
[1]);
2824 intr_name
= "llvm.amdgcn.readfirstlane";
2827 /* We currently have no other way to prevent LLVM from lifting the icmp
2828 * calls to a dominating basic block.
2830 ac_build_optimization_barrier(&ctx
->ac
, &args
[0]);
2832 result
= ac_build_intrinsic(&ctx
->ac
, intr_name
,
2833 ctx
->ac
.i32
, args
, num_args
,
2834 AC_FUNC_ATTR_READNONE
|
2835 AC_FUNC_ATTR_CONVERGENT
);
2838 case nir_intrinsic_load_subgroup_invocation
:
2839 result
= ac_get_thread_id(&ctx
->ac
);
2841 case nir_intrinsic_load_work_group_id
: {
2842 LLVMValueRef values
[3];
2844 for (int i
= 0; i
< 3; i
++) {
2845 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2846 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2849 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2852 case nir_intrinsic_load_base_vertex
: {
2853 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2856 case nir_intrinsic_load_local_group_size
:
2857 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2859 case nir_intrinsic_load_vertex_id
:
2860 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2861 ctx
->abi
->base_vertex
, "");
2863 case nir_intrinsic_load_vertex_id_zero_base
: {
2864 result
= ctx
->abi
->vertex_id
;
2867 case nir_intrinsic_load_local_invocation_id
: {
2868 result
= ctx
->abi
->local_invocation_ids
;
2871 case nir_intrinsic_load_base_instance
:
2872 result
= ctx
->abi
->start_instance
;
2874 case nir_intrinsic_load_draw_id
:
2875 result
= ctx
->abi
->draw_id
;
2877 case nir_intrinsic_load_view_index
:
2878 result
= ctx
->abi
->view_index
;
2880 case nir_intrinsic_load_invocation_id
:
2881 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2882 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2884 result
= ctx
->abi
->gs_invocation_id
;
2886 case nir_intrinsic_load_primitive_id
:
2887 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2888 result
= ctx
->abi
->gs_prim_id
;
2889 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2890 result
= ctx
->abi
->tcs_patch_id
;
2891 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2892 result
= ctx
->abi
->tes_patch_id
;
2894 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2896 case nir_intrinsic_load_sample_id
:
2897 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2899 case nir_intrinsic_load_sample_pos
:
2900 result
= load_sample_pos(ctx
);
2902 case nir_intrinsic_load_sample_mask_in
:
2903 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2905 case nir_intrinsic_load_frag_coord
: {
2906 LLVMValueRef values
[4] = {
2907 ctx
->abi
->frag_pos
[0],
2908 ctx
->abi
->frag_pos
[1],
2909 ctx
->abi
->frag_pos
[2],
2910 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2912 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2915 case nir_intrinsic_load_front_face
:
2916 result
= ctx
->abi
->front_face
;
2918 case nir_intrinsic_load_helper_invocation
:
2919 result
= visit_load_helper_invocation(ctx
);
2921 case nir_intrinsic_load_instance_id
:
2922 result
= ctx
->abi
->instance_id
;
2924 case nir_intrinsic_load_num_work_groups
:
2925 result
= ctx
->abi
->num_work_groups
;
2927 case nir_intrinsic_load_local_invocation_index
:
2928 result
= visit_load_local_invocation_index(ctx
);
2930 case nir_intrinsic_load_subgroup_id
:
2931 result
= visit_load_subgroup_id(ctx
);
2933 case nir_intrinsic_load_num_subgroups
:
2934 result
= visit_load_num_subgroups(ctx
);
2936 case nir_intrinsic_first_invocation
:
2937 result
= visit_first_invocation(ctx
);
2939 case nir_intrinsic_load_push_constant
:
2940 result
= visit_load_push_constant(ctx
, instr
);
2942 case nir_intrinsic_vulkan_resource_index
: {
2943 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2944 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2945 unsigned binding
= nir_intrinsic_binding(instr
);
2947 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2951 case nir_intrinsic_vulkan_resource_reindex
:
2952 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2954 case nir_intrinsic_store_ssbo
:
2955 visit_store_ssbo(ctx
, instr
);
2957 case nir_intrinsic_load_ssbo
:
2958 result
= visit_load_buffer(ctx
, instr
);
2960 case nir_intrinsic_ssbo_atomic_add
:
2961 case nir_intrinsic_ssbo_atomic_imin
:
2962 case nir_intrinsic_ssbo_atomic_umin
:
2963 case nir_intrinsic_ssbo_atomic_imax
:
2964 case nir_intrinsic_ssbo_atomic_umax
:
2965 case nir_intrinsic_ssbo_atomic_and
:
2966 case nir_intrinsic_ssbo_atomic_or
:
2967 case nir_intrinsic_ssbo_atomic_xor
:
2968 case nir_intrinsic_ssbo_atomic_exchange
:
2969 case nir_intrinsic_ssbo_atomic_comp_swap
:
2970 result
= visit_atomic_ssbo(ctx
, instr
);
2972 case nir_intrinsic_load_ubo
:
2973 result
= visit_load_ubo_buffer(ctx
, instr
);
2975 case nir_intrinsic_get_buffer_size
:
2976 result
= visit_get_buffer_size(ctx
, instr
);
2978 case nir_intrinsic_load_var
:
2979 result
= visit_load_var(ctx
, instr
);
2981 case nir_intrinsic_store_var
:
2982 visit_store_var(ctx
, instr
);
2984 case nir_intrinsic_load_shared
:
2985 result
= visit_load_shared(ctx
, instr
);
2987 case nir_intrinsic_store_shared
:
2988 visit_store_shared(ctx
, instr
);
2990 case nir_intrinsic_image_var_samples
:
2991 result
= visit_image_samples(ctx
, instr
);
2993 case nir_intrinsic_image_var_load
:
2994 result
= visit_image_load(ctx
, instr
);
2996 case nir_intrinsic_image_var_store
:
2997 visit_image_store(ctx
, instr
);
2999 case nir_intrinsic_image_var_atomic_add
:
3000 case nir_intrinsic_image_var_atomic_min
:
3001 case nir_intrinsic_image_var_atomic_max
:
3002 case nir_intrinsic_image_var_atomic_and
:
3003 case nir_intrinsic_image_var_atomic_or
:
3004 case nir_intrinsic_image_var_atomic_xor
:
3005 case nir_intrinsic_image_var_atomic_exchange
:
3006 case nir_intrinsic_image_var_atomic_comp_swap
:
3007 result
= visit_image_atomic(ctx
, instr
);
3009 case nir_intrinsic_image_var_size
:
3010 result
= visit_image_size(ctx
, instr
);
3012 case nir_intrinsic_shader_clock
:
3013 result
= ac_build_shader_clock(&ctx
->ac
);
3015 case nir_intrinsic_discard
:
3016 case nir_intrinsic_discard_if
:
3017 emit_discard(ctx
, instr
);
3019 case nir_intrinsic_memory_barrier
:
3020 case nir_intrinsic_group_memory_barrier
:
3021 case nir_intrinsic_memory_barrier_atomic_counter
:
3022 case nir_intrinsic_memory_barrier_buffer
:
3023 case nir_intrinsic_memory_barrier_image
:
3024 case nir_intrinsic_memory_barrier_shared
:
3025 emit_membar(&ctx
->ac
, instr
);
3027 case nir_intrinsic_barrier
:
3028 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3030 case nir_intrinsic_shared_atomic_add
:
3031 case nir_intrinsic_shared_atomic_imin
:
3032 case nir_intrinsic_shared_atomic_umin
:
3033 case nir_intrinsic_shared_atomic_imax
:
3034 case nir_intrinsic_shared_atomic_umax
:
3035 case nir_intrinsic_shared_atomic_and
:
3036 case nir_intrinsic_shared_atomic_or
:
3037 case nir_intrinsic_shared_atomic_xor
:
3038 case nir_intrinsic_shared_atomic_exchange
:
3039 case nir_intrinsic_shared_atomic_comp_swap
: {
3040 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3041 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3044 case nir_intrinsic_var_atomic_add
:
3045 case nir_intrinsic_var_atomic_imin
:
3046 case nir_intrinsic_var_atomic_umin
:
3047 case nir_intrinsic_var_atomic_imax
:
3048 case nir_intrinsic_var_atomic_umax
:
3049 case nir_intrinsic_var_atomic_and
:
3050 case nir_intrinsic_var_atomic_or
:
3051 case nir_intrinsic_var_atomic_xor
:
3052 case nir_intrinsic_var_atomic_exchange
:
3053 case nir_intrinsic_var_atomic_comp_swap
: {
3054 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3055 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3058 case nir_intrinsic_interp_var_at_centroid
:
3059 case nir_intrinsic_interp_var_at_sample
:
3060 case nir_intrinsic_interp_var_at_offset
:
3061 result
= visit_interp(ctx
, instr
);
3063 case nir_intrinsic_emit_vertex
:
3064 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3066 case nir_intrinsic_end_primitive
:
3067 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3069 case nir_intrinsic_load_tess_coord
:
3070 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3072 case nir_intrinsic_load_tess_level_outer
:
3073 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3075 case nir_intrinsic_load_tess_level_inner
:
3076 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3078 case nir_intrinsic_load_patch_vertices_in
:
3079 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3081 case nir_intrinsic_vote_all
: {
3082 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3083 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3086 case nir_intrinsic_vote_any
: {
3087 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3088 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3092 fprintf(stderr
, "Unknown intrinsic: ");
3093 nir_print_instr(&instr
->instr
, stderr
);
3094 fprintf(stderr
, "\n");
3098 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3102 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3103 const nir_deref_var
*deref
,
3104 enum ac_descriptor_type desc_type
,
3105 const nir_tex_instr
*tex_instr
,
3106 bool image
, bool write
)
3108 LLVMValueRef index
= NULL
;
3109 unsigned constant_index
= 0;
3110 unsigned descriptor_set
;
3111 unsigned base_index
;
3112 bool bindless
= false;
3115 assert(tex_instr
&& !image
);
3117 base_index
= tex_instr
->sampler_index
;
3119 const nir_deref
*tail
= &deref
->deref
;
3120 while (tail
->child
) {
3121 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3122 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3127 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3129 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3130 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3132 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3133 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3138 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3141 constant_index
+= child
->base_offset
* array_size
;
3143 tail
= &child
->deref
;
3145 descriptor_set
= deref
->var
->data
.descriptor_set
;
3147 if (deref
->var
->data
.bindless
) {
3148 bindless
= deref
->var
->data
.bindless
;
3149 base_index
= deref
->var
->data
.driver_location
;
3151 base_index
= deref
->var
->data
.binding
;
3155 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3158 constant_index
, index
,
3159 desc_type
, image
, write
, bindless
);
3162 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
3163 struct ac_image_args
*args
,
3164 const nir_tex_instr
*instr
,
3166 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3167 LLVMValueRef
*param
, unsigned count
,
3170 unsigned is_rect
= 0;
3171 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3173 if (op
== nir_texop_lod
)
3175 /* Pad to power of two vector */
3176 while (count
< util_next_power_of_two(count
))
3177 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3180 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
3182 args
->addr
= param
[0];
3184 args
->resource
= res_ptr
;
3185 args
->sampler
= samp_ptr
;
3187 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3188 args
->addr
= param
[0];
3192 args
->dmask
= dmask
;
3193 args
->unorm
= is_rect
;
3197 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3200 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3201 * filtering manually. The driver sets img7 to a mask clearing
3202 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3203 * s_and_b32 samp0, samp0, img7
3206 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3208 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3209 LLVMValueRef res
, LLVMValueRef samp
)
3211 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3212 LLVMValueRef img7
, samp0
;
3214 if (ctx
->ac
.chip_class
>= VI
)
3217 img7
= LLVMBuildExtractElement(builder
, res
,
3218 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3219 samp0
= LLVMBuildExtractElement(builder
, samp
,
3220 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3221 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3222 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3223 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3226 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3227 nir_tex_instr
*instr
,
3228 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3229 LLVMValueRef
*fmask_ptr
)
3231 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3232 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
3234 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
3237 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
3239 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
3240 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3241 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3243 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3244 instr
->op
== nir_texop_samples_identical
))
3245 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
3248 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3251 coord
= ac_to_float(ctx
, coord
);
3252 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3253 coord
= ac_to_integer(ctx
, coord
);
3257 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3259 LLVMValueRef result
= NULL
;
3260 struct ac_image_args args
= { 0 };
3261 unsigned dmask
= 0xf;
3262 LLVMValueRef address
[16];
3263 LLVMValueRef coords
[5];
3264 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3265 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3266 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3267 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3268 LLVMValueRef derivs
[6];
3269 unsigned chan
, count
= 0;
3270 unsigned const_src
= 0, num_deriv_comp
= 0;
3271 bool lod_is_zero
= false;
3273 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3275 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3276 switch (instr
->src
[i
].src_type
) {
3277 case nir_tex_src_coord
:
3278 coord
= get_src(ctx
, instr
->src
[i
].src
);
3280 case nir_tex_src_projector
:
3282 case nir_tex_src_comparator
:
3283 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3285 case nir_tex_src_offset
:
3286 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3289 case nir_tex_src_bias
:
3290 bias
= get_src(ctx
, instr
->src
[i
].src
);
3292 case nir_tex_src_lod
: {
3293 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3295 if (val
&& val
->i32
[0] == 0)
3297 lod
= get_src(ctx
, instr
->src
[i
].src
);
3300 case nir_tex_src_ms_index
:
3301 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3303 case nir_tex_src_ms_mcs
:
3305 case nir_tex_src_ddx
:
3306 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3307 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3309 case nir_tex_src_ddy
:
3310 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3312 case nir_tex_src_texture_offset
:
3313 case nir_tex_src_sampler_offset
:
3314 case nir_tex_src_plane
:
3320 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3321 result
= get_buffer_size(ctx
, res_ptr
, true);
3325 if (instr
->op
== nir_texop_texture_samples
) {
3326 LLVMValueRef res
, samples
, is_msaa
;
3327 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
3328 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3329 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3330 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3331 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3332 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3333 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3334 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3335 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3337 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3338 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3339 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3340 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3341 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3343 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3350 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3351 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3353 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3354 LLVMValueRef offset
[3], pack
;
3355 for (chan
= 0; chan
< 3; ++chan
)
3356 offset
[chan
] = ctx
->ac
.i32_0
;
3359 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
3360 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
3361 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3362 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3364 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3365 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3367 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3368 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3369 address
[count
++] = pack
;
3372 /* pack LOD bias value */
3373 if (instr
->op
== nir_texop_txb
&& bias
) {
3374 address
[count
++] = bias
;
3377 /* Pack depth comparison value */
3378 if (instr
->is_shadow
&& comparator
) {
3379 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
3380 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
3382 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3383 * so the depth comparison value isn't clamped for Z16 and
3384 * Z24 anymore. Do it manually here.
3386 * It's unnecessary if the original texture format was
3387 * Z32_FLOAT, but we don't know that here.
3389 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3390 z
= ac_build_clamp(&ctx
->ac
, z
);
3392 address
[count
++] = z
;
3395 /* pack derivatives */
3397 int num_src_deriv_channels
, num_dest_deriv_channels
;
3398 switch (instr
->sampler_dim
) {
3399 case GLSL_SAMPLER_DIM_3D
:
3400 case GLSL_SAMPLER_DIM_CUBE
:
3402 num_src_deriv_channels
= 3;
3403 num_dest_deriv_channels
= 3;
3405 case GLSL_SAMPLER_DIM_2D
:
3407 num_src_deriv_channels
= 2;
3408 num_dest_deriv_channels
= 2;
3411 case GLSL_SAMPLER_DIM_1D
:
3412 num_src_deriv_channels
= 1;
3413 if (ctx
->ac
.chip_class
>= GFX9
) {
3414 num_dest_deriv_channels
= 2;
3417 num_dest_deriv_channels
= 1;
3423 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3424 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3425 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3427 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3428 derivs
[i
] = ctx
->ac
.f32_0
;
3429 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3433 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3434 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3435 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
3436 if (instr
->coord_components
== 3)
3437 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3438 ac_prepare_cube_coords(&ctx
->ac
,
3439 instr
->op
== nir_texop_txd
, instr
->is_array
,
3440 instr
->op
== nir_texop_lod
, coords
, derivs
);
3446 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3447 address
[count
++] = derivs
[i
];
3450 /* Pack texture coordinates */
3452 address
[count
++] = coords
[0];
3453 if (instr
->coord_components
> 1) {
3454 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3455 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
3457 address
[count
++] = coords
[1];
3459 if (instr
->coord_components
> 2) {
3460 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3461 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3462 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3463 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3465 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3466 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
3468 address
[count
++] = coords
[2];
3471 if (ctx
->ac
.chip_class
>= GFX9
) {
3472 LLVMValueRef filler
;
3473 if (instr
->op
== nir_texop_txf
)
3474 filler
= ctx
->ac
.i32_0
;
3476 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3478 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
3479 /* No nir_texop_lod, because it does not take a slice
3480 * even with array textures. */
3481 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
3482 address
[count
] = address
[count
- 1];
3483 address
[count
- 1] = filler
;
3486 address
[count
++] = filler
;
3492 if (lod
&& ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && !lod_is_zero
)) {
3493 address
[count
++] = lod
;
3494 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3495 address
[count
++] = sample_index
;
3496 } else if(instr
->op
== nir_texop_txs
) {
3499 address
[count
++] = lod
;
3501 address
[count
++] = ctx
->ac
.i32_0
;
3504 for (chan
= 0; chan
< count
; chan
++) {
3505 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3506 address
[chan
], ctx
->ac
.i32
, "");
3509 if (instr
->op
== nir_texop_samples_identical
) {
3510 LLVMValueRef txf_address
[4];
3511 struct ac_image_args txf_args
= { 0 };
3512 unsigned txf_count
= count
;
3513 memcpy(txf_address
, address
, sizeof(txf_address
));
3515 if (!instr
->is_array
)
3516 txf_address
[2] = ctx
->ac
.i32_0
;
3517 txf_address
[3] = ctx
->ac
.i32_0
;
3519 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
3521 txf_address
, txf_count
, 0xf);
3523 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
3525 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3526 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3530 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3531 instr
->op
!= nir_texop_txs
) {
3532 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3533 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
3536 instr
->is_array
? address
[2] : NULL
,
3537 address
[sample_chan
],
3541 if (offsets
&& instr
->op
== nir_texop_txf
) {
3542 nir_const_value
*const_offset
=
3543 nir_src_as_const_value(instr
->src
[const_src
].src
);
3544 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3545 assert(const_offset
);
3546 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3547 if (num_offsets
> 2)
3548 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
3549 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
3550 if (num_offsets
> 1)
3551 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
3552 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
3553 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
3554 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
3558 /* TODO TG4 support */
3559 if (instr
->op
== nir_texop_tg4
) {
3560 if (instr
->is_shadow
)
3563 dmask
= 1 << instr
->component
;
3565 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
3566 res_ptr
, samp_ptr
, address
, count
, dmask
);
3568 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
3570 if (instr
->op
== nir_texop_query_levels
)
3571 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3572 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3573 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3574 instr
->op
!= nir_texop_tg4
)
3575 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3576 else if (instr
->op
== nir_texop_txs
&&
3577 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3579 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3580 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3581 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3582 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3583 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3584 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3585 instr
->op
== nir_texop_txs
&&
3586 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3588 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3589 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3590 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3592 } else if (instr
->dest
.ssa
.num_components
!= 4)
3593 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3597 assert(instr
->dest
.is_ssa
);
3598 result
= ac_to_integer(&ctx
->ac
, result
);
3599 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3604 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3606 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3607 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3609 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3610 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3613 static void visit_post_phi(struct ac_nir_context
*ctx
,
3614 nir_phi_instr
*instr
,
3615 LLVMValueRef llvm_phi
)
3617 nir_foreach_phi_src(src
, instr
) {
3618 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3619 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3621 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3625 static void phi_post_pass(struct ac_nir_context
*ctx
)
3627 struct hash_entry
*entry
;
3628 hash_table_foreach(ctx
->phis
, entry
) {
3629 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3630 (LLVMValueRef
)entry
->data
);
3635 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3636 const nir_ssa_undef_instr
*instr
)
3638 unsigned num_components
= instr
->def
.num_components
;
3639 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3642 if (num_components
== 1)
3643 undef
= LLVMGetUndef(type
);
3645 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3647 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3650 static void visit_jump(struct ac_llvm_context
*ctx
,
3651 const nir_jump_instr
*instr
)
3653 switch (instr
->type
) {
3654 case nir_jump_break
:
3655 ac_build_break(ctx
);
3657 case nir_jump_continue
:
3658 ac_build_continue(ctx
);
3661 fprintf(stderr
, "Unknown NIR jump instr: ");
3662 nir_print_instr(&instr
->instr
, stderr
);
3663 fprintf(stderr
, "\n");
3668 static void visit_cf_list(struct ac_nir_context
*ctx
,
3669 struct exec_list
*list
);
3671 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3673 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3674 nir_foreach_instr(instr
, block
)
3676 switch (instr
->type
) {
3677 case nir_instr_type_alu
:
3678 visit_alu(ctx
, nir_instr_as_alu(instr
));
3680 case nir_instr_type_load_const
:
3681 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3683 case nir_instr_type_intrinsic
:
3684 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3686 case nir_instr_type_tex
:
3687 visit_tex(ctx
, nir_instr_as_tex(instr
));
3689 case nir_instr_type_phi
:
3690 visit_phi(ctx
, nir_instr_as_phi(instr
));
3692 case nir_instr_type_ssa_undef
:
3693 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3695 case nir_instr_type_jump
:
3696 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3699 fprintf(stderr
, "Unknown NIR instr type: ");
3700 nir_print_instr(instr
, stderr
);
3701 fprintf(stderr
, "\n");
3706 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3709 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3711 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3713 nir_block
*then_block
=
3714 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3716 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3718 visit_cf_list(ctx
, &if_stmt
->then_list
);
3720 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3721 nir_block
*else_block
=
3722 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3724 ac_build_else(&ctx
->ac
, else_block
->index
);
3725 visit_cf_list(ctx
, &if_stmt
->else_list
);
3728 ac_build_endif(&ctx
->ac
, then_block
->index
);
3731 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3733 nir_block
*first_loop_block
=
3734 (nir_block
*) exec_list_get_head(&loop
->body
);
3736 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3738 visit_cf_list(ctx
, &loop
->body
);
3740 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3743 static void visit_cf_list(struct ac_nir_context
*ctx
,
3744 struct exec_list
*list
)
3746 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3748 switch (node
->type
) {
3749 case nir_cf_node_block
:
3750 visit_block(ctx
, nir_cf_node_as_block(node
));
3753 case nir_cf_node_if
:
3754 visit_if(ctx
, nir_cf_node_as_if(node
));
3757 case nir_cf_node_loop
:
3758 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3768 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3769 struct ac_shader_abi
*abi
,
3770 struct nir_shader
*nir
,
3771 struct nir_variable
*variable
,
3772 gl_shader_stage stage
)
3774 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3775 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3777 /* tess ctrl has it's own load/store paths for outputs */
3778 if (stage
== MESA_SHADER_TESS_CTRL
)
3781 if (stage
== MESA_SHADER_VERTEX
||
3782 stage
== MESA_SHADER_TESS_EVAL
||
3783 stage
== MESA_SHADER_GEOMETRY
) {
3784 int idx
= variable
->data
.location
+ variable
->data
.index
;
3785 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3786 int length
= nir
->info
.clip_distance_array_size
+
3787 nir
->info
.cull_distance_array_size
;
3796 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3797 for (unsigned chan
= 0; chan
< 4; chan
++) {
3798 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3799 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3805 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3806 enum glsl_base_type type
)
3810 case GLSL_TYPE_UINT
:
3811 case GLSL_TYPE_BOOL
:
3812 case GLSL_TYPE_SUBROUTINE
:
3814 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3816 case GLSL_TYPE_INT64
:
3817 case GLSL_TYPE_UINT64
:
3819 case GLSL_TYPE_DOUBLE
:
3822 unreachable("unknown GLSL type");
3827 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3828 const struct glsl_type
*type
)
3830 if (glsl_type_is_scalar(type
)) {
3831 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3834 if (glsl_type_is_vector(type
)) {
3835 return LLVMVectorType(
3836 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3837 glsl_get_vector_elements(type
));
3840 if (glsl_type_is_matrix(type
)) {
3841 return LLVMArrayType(
3842 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3843 glsl_get_matrix_columns(type
));
3846 if (glsl_type_is_array(type
)) {
3847 return LLVMArrayType(
3848 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3849 glsl_get_length(type
));
3852 assert(glsl_type_is_struct(type
));
3854 LLVMTypeRef member_types
[glsl_get_length(type
)];
3856 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3858 glsl_to_llvm_type(ac
,
3859 glsl_get_struct_field(type
, i
));
3862 return LLVMStructTypeInContext(ac
->context
, member_types
,
3863 glsl_get_length(type
), false);
3867 setup_locals(struct ac_nir_context
*ctx
,
3868 struct nir_function
*func
)
3871 ctx
->num_locals
= 0;
3872 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3873 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3874 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3875 variable
->data
.location_frac
= 0;
3876 ctx
->num_locals
+= attrib_count
;
3878 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3882 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3883 for (j
= 0; j
< 4; j
++) {
3884 ctx
->locals
[i
* 4 + j
] =
3885 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3891 setup_shared(struct ac_nir_context
*ctx
,
3892 struct nir_shader
*nir
)
3894 nir_foreach_variable(variable
, &nir
->shared
) {
3895 LLVMValueRef shared
=
3896 LLVMAddGlobalInAddressSpace(
3897 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3898 variable
->name
? variable
->name
: "",
3899 AC_LOCAL_ADDR_SPACE
);
3900 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3904 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3905 struct nir_shader
*nir
)
3907 struct ac_nir_context ctx
= {};
3908 struct nir_function
*func
;
3913 ctx
.stage
= nir
->info
.stage
;
3915 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3917 nir_foreach_variable(variable
, &nir
->outputs
)
3918 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3921 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3922 _mesa_key_pointer_equal
);
3923 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3924 _mesa_key_pointer_equal
);
3925 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3926 _mesa_key_pointer_equal
);
3928 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3930 setup_locals(&ctx
, func
);
3932 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3933 setup_shared(&ctx
, nir
);
3935 visit_cf_list(&ctx
, &func
->impl
->body
);
3936 phi_post_pass(&ctx
);
3938 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3939 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3943 ralloc_free(ctx
.defs
);
3944 ralloc_free(ctx
.phis
);
3945 ralloc_free(ctx
.vars
);
3949 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3951 /* While it would be nice not to have this flag, we are constrained
3952 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3955 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3957 /* TODO: Indirect indexing of GS inputs is unimplemented.
3959 * TCS and TES load inputs directly from LDS or offchip memory, so
3960 * indirect indexing is trivial.
3962 nir_variable_mode indirect_mask
= 0;
3963 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3964 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3965 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3966 !llvm_has_working_vgpr_indexing
)) {
3967 indirect_mask
|= nir_var_shader_in
;
3969 if (!llvm_has_working_vgpr_indexing
&&
3970 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3971 indirect_mask
|= nir_var_shader_out
;
3973 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3974 * smart enough to handle indirects without causing excess spilling
3975 * causing the gpu to hang.
3977 * See the following thread for more details of the problem:
3978 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3980 indirect_mask
|= nir_var_local
;
3982 nir_lower_indirect_derefs(nir
, indirect_mask
);