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 "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
36 struct ac_nir_context
{
37 struct ac_llvm_context ac
;
38 struct ac_shader_abi
*abi
;
40 gl_shader_stage stage
;
42 LLVMValueRef
*ssa_defs
;
44 struct hash_table
*defs
;
45 struct hash_table
*phis
;
46 struct hash_table
*vars
;
48 LLVMValueRef main_function
;
49 LLVMBasicBlockRef continue_block
;
50 LLVMBasicBlockRef break_block
;
56 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
273 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
275 return LLVMBuildSelect(ctx
->builder
, v
,
276 ac_to_integer_or_pointer(ctx
, src1
),
277 ac_to_integer_or_pointer(ctx
, src2
), "");
280 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
281 LLVMIntPredicate pred
,
282 LLVMValueRef src0
, LLVMValueRef src1
)
284 return LLVMBuildSelect(ctx
->builder
,
285 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
290 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
293 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
294 LLVMBuildNeg(ctx
->builder
, src0
, ""));
297 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
299 LLVMValueRef src0
, LLVMValueRef src1
)
301 LLVMTypeRef ret_type
;
302 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
304 LLVMValueRef params
[] = { src0
, src1
};
305 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
308 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
309 params
, 2, AC_FUNC_ATTR_READNONE
);
311 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
312 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
316 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
320 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
321 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
323 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
328 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
331 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
334 src0
= ac_to_float(ctx
, src0
);
335 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
336 return LLVMBuildSExt(ctx
->builder
,
337 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
341 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
345 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
349 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
351 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
355 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
357 unreachable("Unsupported bit size.");
361 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
364 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
365 return LLVMBuildSExt(ctx
->builder
,
366 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
370 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
374 LLVMValueRef cond
= NULL
;
376 src0
= ac_to_float(ctx
, src0
);
377 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
379 if (ctx
->chip_class
>= VI
) {
380 LLVMValueRef args
[2];
381 /* Check if the result is a denormal - and flush to 0 if so. */
383 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
384 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
387 /* need to convert back up to f32 */
388 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
390 if (ctx
->chip_class
>= VI
)
391 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
394 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
395 * so compare the result and flush to 0 if it's smaller.
397 LLVMValueRef temp
, cond2
;
398 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
399 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
400 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
402 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
403 temp
, ctx
->f32_0
, "");
404 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
405 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
410 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
411 LLVMValueRef src0
, LLVMValueRef src1
)
413 LLVMValueRef dst64
, result
;
414 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
415 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
417 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
418 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
419 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
423 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
424 LLVMValueRef src0
, LLVMValueRef src1
)
426 LLVMValueRef dst64
, result
;
427 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
428 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
430 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
431 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
432 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
436 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
438 const LLVMValueRef srcs
[3])
442 if (HAVE_LLVM
>= 0x0800) {
443 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
444 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
445 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
447 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
448 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
450 LLVMValueRef zero
= ctx
->i32_0
;
451 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
452 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
454 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
455 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
456 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
462 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
463 LLVMValueRef src0
, LLVMValueRef src1
,
464 LLVMValueRef src2
, LLVMValueRef src3
)
466 LLVMValueRef bfi_args
[3], result
;
468 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
469 LLVMBuildSub(ctx
->builder
,
470 LLVMBuildShl(ctx
->builder
,
475 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
478 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
481 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
482 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
484 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
485 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
486 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
488 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
492 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
495 LLVMValueRef comp
[2];
497 src0
= ac_to_float(ctx
, src0
);
498 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
499 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
501 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
505 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
508 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
509 LLVMValueRef temps
[2], val
;
512 for (i
= 0; i
< 2; i
++) {
513 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
514 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
515 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
516 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
518 return ac_build_gather_values(ctx
, temps
, 2);
521 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
529 if (op
== nir_op_fddx_fine
)
530 mask
= AC_TID_MASK_LEFT
;
531 else if (op
== nir_op_fddy_fine
)
532 mask
= AC_TID_MASK_TOP
;
534 mask
= AC_TID_MASK_TOP_LEFT
;
536 /* for DDX we want to next X pixel, DDY next Y pixel. */
537 if (op
== nir_op_fddx_fine
||
538 op
== nir_op_fddx_coarse
||
544 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
549 * this takes an I,J coordinate pair,
550 * and works out the X and Y derivatives.
551 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
553 static LLVMValueRef
emit_ddxy_interp(
554 struct ac_nir_context
*ctx
,
555 LLVMValueRef interp_ij
)
557 LLVMValueRef result
[4], a
;
560 for (i
= 0; i
< 2; i
++) {
561 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
562 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
563 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
564 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
566 return ac_build_gather_values(&ctx
->ac
, result
, 4);
569 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
571 LLVMValueRef src
[4], result
= NULL
;
572 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
573 unsigned src_components
;
574 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
576 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
583 case nir_op_pack_half_2x16
:
586 case nir_op_unpack_half_2x16
:
589 case nir_op_cube_face_coord
:
590 case nir_op_cube_face_index
:
594 src_components
= num_components
;
597 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
598 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
606 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
607 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
610 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
613 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
616 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
619 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
620 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
621 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
624 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
625 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
626 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
629 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
632 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
635 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
638 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
641 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
642 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
643 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
644 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
645 ac_to_float_type(&ctx
->ac
, def_type
), result
);
646 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
647 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
650 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
651 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
652 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
655 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
658 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
661 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
664 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
665 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
666 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
669 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
670 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
673 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
676 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
679 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
682 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
683 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
684 LLVMTypeOf(src
[0]), "");
685 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
686 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
687 LLVMTypeOf(src
[0]), "");
688 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
691 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
692 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
693 LLVMTypeOf(src
[0]), "");
694 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
695 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
696 LLVMTypeOf(src
[0]), "");
697 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
700 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
701 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
702 LLVMTypeOf(src
[0]), "");
703 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
704 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
705 LLVMTypeOf(src
[0]), "");
706 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
709 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
712 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
715 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
718 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
721 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
724 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
727 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
730 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
733 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
736 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
739 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
740 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
743 result
= emit_iabs(&ctx
->ac
, src
[0]);
746 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
749 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
752 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
755 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
758 result
= ac_build_isign(&ctx
->ac
, src
[0],
759 instr
->dest
.dest
.ssa
.bit_size
);
762 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
763 result
= ac_build_fsign(&ctx
->ac
, src
[0],
764 instr
->dest
.dest
.ssa
.bit_size
);
767 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
768 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
771 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
772 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
775 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
776 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
778 case nir_op_fround_even
:
779 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
780 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
783 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
784 result
= ac_build_fract(&ctx
->ac
, src
[0],
785 instr
->dest
.dest
.ssa
.bit_size
);
788 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
789 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
792 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
793 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
796 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
797 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
800 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
801 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
804 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
805 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
808 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
809 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
810 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
812 case nir_op_frexp_exp
:
813 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
814 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
815 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
816 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
817 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
820 case nir_op_frexp_sig
:
821 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
822 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
823 instr
->dest
.dest
.ssa
.bit_size
);
826 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
827 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
830 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
831 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
832 if (ctx
->ac
.chip_class
< GFX9
&&
833 instr
->dest
.dest
.ssa
.bit_size
== 32) {
834 /* Only pre-GFX9 chips do not flush denorms. */
835 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
836 ac_to_float_type(&ctx
->ac
, def_type
),
841 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
842 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
843 if (ctx
->ac
.chip_class
< GFX9
&&
844 instr
->dest
.dest
.ssa
.bit_size
== 32) {
845 /* Only pre-GFX9 chips do not flush denorms. */
846 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
847 ac_to_float_type(&ctx
->ac
, def_type
),
852 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
853 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
856 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
857 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
858 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
859 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
860 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
862 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
864 case nir_op_ibitfield_extract
:
865 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
867 case nir_op_ubitfield_extract
:
868 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
870 case nir_op_bitfield_insert
:
871 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
873 case nir_op_bitfield_reverse
:
874 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
876 case nir_op_bit_count
:
877 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
882 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
883 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
884 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
890 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
891 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
897 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
898 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
903 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
904 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
909 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
910 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
912 case nir_op_f2f16_rtz
:
913 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
914 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
915 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
916 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
917 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
918 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
920 case nir_op_f2f16_rtne
:
924 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
925 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
926 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
928 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
934 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
935 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
936 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
938 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
944 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
945 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
946 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
948 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
951 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
953 case nir_op_find_lsb
:
954 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
955 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
957 case nir_op_ufind_msb
:
958 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
959 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
961 case nir_op_ifind_msb
:
962 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
963 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
965 case nir_op_uadd_carry
:
966 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
967 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
968 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
970 case nir_op_usub_borrow
:
971 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
972 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
973 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
978 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
981 result
= emit_f2b(&ctx
->ac
, src
[0]);
987 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
990 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
991 result
= emit_i2b(&ctx
->ac
, src
[0]);
993 case nir_op_fquantize2f16
:
994 result
= emit_f2f16(&ctx
->ac
, src
[0]);
996 case nir_op_umul_high
:
997 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
998 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
999 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1001 case nir_op_imul_high
:
1002 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1003 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1004 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1006 case nir_op_pack_half_2x16
:
1007 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1009 case nir_op_unpack_half_2x16
:
1010 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1014 case nir_op_fddx_fine
:
1015 case nir_op_fddy_fine
:
1016 case nir_op_fddx_coarse
:
1017 case nir_op_fddy_coarse
:
1018 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1021 case nir_op_unpack_64_2x32_split_x
: {
1022 assert(ac_get_llvm_num_components(src
[0]) == 1);
1023 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1026 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1031 case nir_op_unpack_64_2x32_split_y
: {
1032 assert(ac_get_llvm_num_components(src
[0]) == 1);
1033 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1036 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1041 case nir_op_pack_64_2x32_split
: {
1042 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1043 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1044 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1048 case nir_op_pack_32_2x16_split
: {
1049 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1050 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1054 case nir_op_unpack_32_2x16_split_x
: {
1055 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1058 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1063 case nir_op_unpack_32_2x16_split_y
: {
1064 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1067 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1072 case nir_op_cube_face_coord
: {
1073 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1074 LLVMValueRef results
[2];
1076 for (unsigned chan
= 0; chan
< 3; chan
++)
1077 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1078 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1079 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1080 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1081 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1082 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1086 case nir_op_cube_face_index
: {
1087 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1089 for (unsigned chan
= 0; chan
< 3; chan
++)
1090 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1091 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1092 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1097 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1098 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1099 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1100 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1103 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1104 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1107 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1108 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1111 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1112 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1113 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1114 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1117 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1118 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1121 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1122 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1124 case nir_op_fmed3
: {
1125 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1126 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1127 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1128 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1129 instr
->dest
.dest
.ssa
.bit_size
);
1132 case nir_op_imed3
: {
1133 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1134 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1135 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1136 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1139 case nir_op_umed3
: {
1140 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1141 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1142 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1143 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1148 fprintf(stderr
, "Unknown NIR alu instr: ");
1149 nir_print_instr(&instr
->instr
, stderr
);
1150 fprintf(stderr
, "\n");
1155 assert(instr
->dest
.dest
.is_ssa
);
1156 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1157 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1161 static void visit_load_const(struct ac_nir_context
*ctx
,
1162 const nir_load_const_instr
*instr
)
1164 LLVMValueRef values
[4], value
= NULL
;
1165 LLVMTypeRef element_type
=
1166 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1168 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1169 switch (instr
->def
.bit_size
) {
1171 values
[i
] = LLVMConstInt(element_type
,
1172 instr
->value
.u8
[i
], false);
1175 values
[i
] = LLVMConstInt(element_type
,
1176 instr
->value
.u16
[i
], false);
1179 values
[i
] = LLVMConstInt(element_type
,
1180 instr
->value
.u32
[i
], false);
1183 values
[i
] = LLVMConstInt(element_type
,
1184 instr
->value
.u64
[i
], false);
1188 "unsupported nir load_const bit_size: %d\n",
1189 instr
->def
.bit_size
);
1193 if (instr
->def
.num_components
> 1) {
1194 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1198 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1202 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1205 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1206 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1209 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1210 /* On VI, the descriptor contains the size in bytes,
1211 * but TXQ must return the size in elements.
1212 * The stride is always non-zero for resources using TXQ.
1214 LLVMValueRef stride
=
1215 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1217 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1218 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1219 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1220 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1222 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1227 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1229 struct ac_image_args
*args
,
1230 const nir_tex_instr
*instr
)
1232 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1233 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1234 LLVMValueRef half_texel
[2];
1235 LLVMValueRef compare_cube_wa
= NULL
;
1236 LLVMValueRef result
;
1240 struct ac_image_args txq_args
= { 0 };
1242 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1243 txq_args
.opcode
= ac_image_get_resinfo
;
1244 txq_args
.dmask
= 0xf;
1245 txq_args
.lod
= ctx
->i32_0
;
1246 txq_args
.resource
= args
->resource
;
1247 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1248 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1250 for (unsigned c
= 0; c
< 2; c
++) {
1251 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1252 LLVMConstInt(ctx
->i32
, c
, false), "");
1253 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1254 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1255 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1256 LLVMConstReal(ctx
->f32
, -0.5), "");
1260 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1262 for (unsigned c
= 0; c
< 2; c
++) {
1264 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1265 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1269 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1270 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1271 * workaround by sampling using a scaled type and converting.
1272 * This is taken from amdgpu-pro shaders.
1274 /* NOTE this produces some ugly code compared to amdgpu-pro,
1275 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1276 * and then reads them back. -pro generates two selects,
1277 * one s_cmp for the descriptor rewriting
1278 * one v_cmp for the coordinate and result changes.
1280 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1281 LLVMValueRef tmp
, tmp2
;
1283 /* workaround 8/8/8/8 uint/sint cube gather bug */
1284 /* first detect it then change to a scaled read and f2i */
1285 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1288 /* extract the DATA_FORMAT */
1289 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1290 LLVMConstInt(ctx
->i32
, 6, false), false);
1292 /* is the DATA_FORMAT == 8_8_8_8 */
1293 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1295 if (stype
== GLSL_TYPE_UINT
)
1296 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1297 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1298 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1300 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1301 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1302 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1304 /* replace the NUM FORMAT in the descriptor */
1305 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1306 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1308 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1310 /* don't modify the coordinates for this case */
1311 for (unsigned c
= 0; c
< 2; ++c
)
1312 args
->coords
[c
] = LLVMBuildSelect(
1313 ctx
->builder
, compare_cube_wa
,
1314 orig_coords
[c
], args
->coords
[c
], "");
1317 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1318 result
= ac_build_image_opcode(ctx
, args
);
1320 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1321 LLVMValueRef tmp
, tmp2
;
1323 /* if the cube workaround is in place, f2i the result. */
1324 for (unsigned c
= 0; c
< 4; c
++) {
1325 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1326 if (stype
== GLSL_TYPE_UINT
)
1327 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1329 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1330 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1331 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1332 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1333 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1334 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1340 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1342 nir_deref_instr
*texture_deref_instr
= NULL
;
1344 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1345 switch (instr
->src
[i
].src_type
) {
1346 case nir_tex_src_texture_deref
:
1347 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1353 return texture_deref_instr
;
1356 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1357 const nir_tex_instr
*instr
,
1358 struct ac_image_args
*args
)
1360 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1361 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1363 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1364 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1368 util_last_bit(mask
),
1371 return ac_build_buffer_load_format(&ctx
->ac
,
1375 util_last_bit(mask
),
1380 args
->opcode
= ac_image_sample
;
1382 switch (instr
->op
) {
1384 case nir_texop_txf_ms
:
1385 case nir_texop_samples_identical
:
1386 args
->opcode
= args
->level_zero
||
1387 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1388 ac_image_load
: ac_image_load_mip
;
1389 args
->level_zero
= false;
1392 case nir_texop_query_levels
:
1393 args
->opcode
= ac_image_get_resinfo
;
1395 args
->lod
= ctx
->ac
.i32_0
;
1396 args
->level_zero
= false;
1399 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1401 args
->level_zero
= true;
1405 args
->opcode
= ac_image_gather4
;
1406 args
->level_zero
= true;
1409 args
->opcode
= ac_image_get_lod
;
1415 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1416 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1417 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1418 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1419 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1420 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1421 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1425 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1426 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1427 if ((args
->dim
== ac_image_2darray
||
1428 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1429 args
->coords
[1] = ctx
->ac
.i32_0
;
1433 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1434 return ac_build_image_opcode(&ctx
->ac
, args
);
1437 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1438 nir_intrinsic_instr
*instr
)
1440 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1441 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1443 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1444 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1448 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1449 nir_intrinsic_instr
*instr
)
1451 LLVMValueRef ptr
, addr
;
1452 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1453 unsigned index
= nir_intrinsic_base(instr
);
1455 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1456 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1458 /* Load constant values from user SGPRS when possible, otherwise
1459 * fallback to the default path that loads directly from memory.
1461 if (LLVMIsConstant(src0
) &&
1462 instr
->dest
.ssa
.bit_size
== 32) {
1463 unsigned count
= instr
->dest
.ssa
.num_components
;
1464 unsigned offset
= index
;
1466 offset
+= LLVMConstIntGetZExtValue(src0
);
1469 offset
-= ctx
->abi
->base_inline_push_consts
;
1471 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1472 return ac_build_gather_values(&ctx
->ac
,
1473 ctx
->abi
->inline_push_consts
+ offset
,
1478 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1480 if (instr
->dest
.ssa
.bit_size
== 8) {
1481 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1482 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1483 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1484 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1486 LLVMValueRef params
[3];
1487 if (load_dwords
> 1) {
1488 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1489 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1490 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1492 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1493 params
[0] = ctx
->ac
.i32_0
;
1497 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1499 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1500 if (instr
->dest
.ssa
.num_components
> 1)
1501 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1503 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1504 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1505 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1506 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1507 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1508 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1509 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1510 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1511 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1512 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1513 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1514 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1515 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1516 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1517 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1518 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1519 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1522 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1524 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1527 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1528 const nir_intrinsic_instr
*instr
)
1530 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1532 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1535 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1537 uint32_t new_mask
= 0;
1538 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1539 if (mask
& (1u << i
))
1540 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1544 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1545 unsigned start
, unsigned count
)
1547 LLVMValueRef mask
[] = {
1548 ctx
->i32_0
, ctx
->i32_1
,
1549 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1551 unsigned src_elements
= ac_get_llvm_num_components(src
);
1553 if (count
== src_elements
) {
1556 } else if (count
== 1) {
1557 assert(start
< src_elements
);
1558 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1560 assert(start
+ count
<= src_elements
);
1562 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1563 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1567 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1568 enum gl_access_qualifier access
,
1569 bool may_store_unaligned
,
1570 bool writeonly_memory
)
1572 unsigned cache_policy
= 0;
1574 /* SI has a TC L1 bug causing corruption of 8bit/16bit stores. All
1575 * store opcodes not aligned to a dword are affected. The only way to
1576 * get unaligned stores is through shader images.
1578 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== SI
) ||
1579 /* If this is write-only, don't keep data in L1 to prevent
1580 * evicting L1 cache lines that may be needed by other
1584 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1585 cache_policy
|= ac_glc
;
1588 return cache_policy
;
1591 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1592 nir_intrinsic_instr
*instr
)
1594 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1595 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1596 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1597 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1598 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1599 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1601 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1602 get_src(ctx
, instr
->src
[1]), true);
1603 LLVMValueRef base_data
= src_data
;
1604 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1605 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1609 LLVMValueRef data
, offset
;
1610 LLVMTypeRef data_type
;
1612 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1614 /* Due to an LLVM limitation, split 3-element writes
1615 * into a 2-element and a 1-element write. */
1617 writemask
|= 1 << (start
+ 2);
1620 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1622 /* we can only store 4 DWords at the same time.
1623 * can only happen for 64 Bit vectors. */
1624 if (num_bytes
> 16) {
1625 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1630 /* check alignment of 16 Bit stores */
1631 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1632 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1636 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1638 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1639 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1641 if (num_bytes
== 1) {
1642 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1643 offset
, ctx
->ac
.i32_0
,
1644 cache_policy
& ac_glc
,
1646 } else if (num_bytes
== 2) {
1647 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1648 offset
, ctx
->ac
.i32_0
,
1649 cache_policy
& ac_glc
,
1652 int num_channels
= num_bytes
/ 4;
1654 switch (num_bytes
) {
1655 case 16: /* v4f32 */
1656 data_type
= ctx
->ac
.v4f32
;
1659 data_type
= ctx
->ac
.v2f32
;
1662 data_type
= ctx
->ac
.f32
;
1665 unreachable("Malformed vector store.");
1667 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1669 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1670 num_channels
, offset
,
1672 cache_policy
& ac_glc
,
1673 false, writeonly_memory
,
1679 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1680 const nir_intrinsic_instr
*instr
)
1683 LLVMValueRef params
[6];
1686 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1687 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1689 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1690 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1691 get_src(ctx
, instr
->src
[0]),
1693 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1694 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1695 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1697 switch (instr
->intrinsic
) {
1698 case nir_intrinsic_ssbo_atomic_add
:
1699 name
= "llvm.amdgcn.buffer.atomic.add";
1701 case nir_intrinsic_ssbo_atomic_imin
:
1702 name
= "llvm.amdgcn.buffer.atomic.smin";
1704 case nir_intrinsic_ssbo_atomic_umin
:
1705 name
= "llvm.amdgcn.buffer.atomic.umin";
1707 case nir_intrinsic_ssbo_atomic_imax
:
1708 name
= "llvm.amdgcn.buffer.atomic.smax";
1710 case nir_intrinsic_ssbo_atomic_umax
:
1711 name
= "llvm.amdgcn.buffer.atomic.umax";
1713 case nir_intrinsic_ssbo_atomic_and
:
1714 name
= "llvm.amdgcn.buffer.atomic.and";
1716 case nir_intrinsic_ssbo_atomic_or
:
1717 name
= "llvm.amdgcn.buffer.atomic.or";
1719 case nir_intrinsic_ssbo_atomic_xor
:
1720 name
= "llvm.amdgcn.buffer.atomic.xor";
1722 case nir_intrinsic_ssbo_atomic_exchange
:
1723 name
= "llvm.amdgcn.buffer.atomic.swap";
1725 case nir_intrinsic_ssbo_atomic_comp_swap
:
1726 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1732 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1735 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1736 const nir_intrinsic_instr
*instr
)
1738 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1739 int num_components
= instr
->num_components
;
1740 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1741 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1743 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1744 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1745 get_src(ctx
, instr
->src
[0]), false);
1746 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1748 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1749 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1751 LLVMValueRef results
[4];
1752 for (int i
= 0; i
< num_components
;) {
1753 int num_elems
= num_components
- i
;
1754 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1756 if (num_elems
* elem_size_bytes
> 16)
1757 num_elems
= 16 / elem_size_bytes
;
1758 int load_bytes
= num_elems
* elem_size_bytes
;
1760 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1764 if (load_bytes
== 1) {
1765 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1770 cache_policy
& ac_glc
);
1771 } else if (load_bytes
== 2) {
1772 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1777 cache_policy
& ac_glc
);
1779 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1781 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1782 vindex
, offset
, immoffset
, 0,
1783 cache_policy
& ac_glc
, 0,
1787 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1788 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1789 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1791 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1792 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1794 for (unsigned j
= 0; j
< num_elems
; j
++) {
1795 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1800 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1803 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1804 const nir_intrinsic_instr
*instr
)
1807 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1808 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1809 int num_components
= instr
->num_components
;
1811 if (ctx
->abi
->load_ubo
)
1812 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1814 if (instr
->dest
.ssa
.bit_size
== 64)
1815 num_components
*= 2;
1817 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1818 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1819 LLVMValueRef results
[num_components
];
1820 for (unsigned i
= 0; i
< num_components
; ++i
) {
1821 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1824 if (load_bytes
== 1) {
1825 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1832 assert(load_bytes
== 2);
1833 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1841 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1843 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1844 NULL
, 0, false, false, true, true);
1846 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1849 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1850 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1854 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1855 bool vs_in
, unsigned *vertex_index_out
,
1856 LLVMValueRef
*vertex_index_ref
,
1857 unsigned *const_out
, LLVMValueRef
*indir_out
)
1859 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1860 nir_deref_path path
;
1861 unsigned idx_lvl
= 1;
1863 nir_deref_path_init(&path
, instr
, NULL
);
1865 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1866 if (vertex_index_ref
) {
1867 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1868 if (vertex_index_out
)
1869 *vertex_index_out
= 0;
1871 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1873 *vertex_index_out
= v
->u32
[0];
1878 uint32_t const_offset
= 0;
1879 LLVMValueRef offset
= NULL
;
1881 if (var
->data
.compact
) {
1882 assert(instr
->deref_type
== nir_deref_type_array
);
1883 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1885 const_offset
= v
->u32
[0];
1889 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1890 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1891 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1892 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1894 for (unsigned i
= 0; i
< index
; i
++) {
1895 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1896 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1898 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1899 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1900 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1901 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1903 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1907 unreachable("Uhandled deref type in get_deref_instr_offset");
1911 nir_deref_path_finish(&path
);
1913 if (const_offset
&& offset
)
1914 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1915 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1918 *const_out
= const_offset
;
1919 *indir_out
= offset
;
1922 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1923 nir_intrinsic_instr
*instr
,
1926 LLVMValueRef result
;
1927 LLVMValueRef vertex_index
= NULL
;
1928 LLVMValueRef indir_index
= NULL
;
1929 unsigned const_index
= 0;
1931 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1933 unsigned location
= var
->data
.location
;
1934 unsigned driver_location
= var
->data
.driver_location
;
1935 const bool is_patch
= var
->data
.patch
;
1936 const bool is_compact
= var
->data
.compact
;
1938 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1939 false, NULL
, is_patch
? NULL
: &vertex_index
,
1940 &const_index
, &indir_index
);
1942 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1944 LLVMTypeRef src_component_type
;
1945 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1946 src_component_type
= LLVMGetElementType(dest_type
);
1948 src_component_type
= dest_type
;
1950 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1951 vertex_index
, indir_index
,
1952 const_index
, location
, driver_location
,
1953 var
->data
.location_frac
,
1954 instr
->num_components
,
1955 is_patch
, is_compact
, load_inputs
);
1956 if (instr
->dest
.ssa
.bit_size
== 16) {
1957 result
= ac_to_integer(&ctx
->ac
, result
);
1958 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1960 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1964 type_scalar_size_bytes(const struct glsl_type
*type
)
1966 assert(glsl_type_is_vector_or_scalar(type
) ||
1967 glsl_type_is_matrix(type
));
1968 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1971 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1972 nir_intrinsic_instr
*instr
)
1974 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1975 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1977 LLVMValueRef values
[8];
1979 int ve
= instr
->dest
.ssa
.num_components
;
1981 LLVMValueRef indir_index
;
1983 unsigned const_index
;
1984 unsigned stride
= 4;
1985 int mode
= deref
->mode
;
1988 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1989 var
->data
.mode
== nir_var_shader_in
;
1990 idx
= var
->data
.driver_location
;
1991 comp
= var
->data
.location_frac
;
1992 mode
= var
->data
.mode
;
1994 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1995 &const_index
, &indir_index
);
1997 if (var
->data
.compact
) {
1999 const_index
+= comp
;
2004 if (instr
->dest
.ssa
.bit_size
== 64 &&
2005 (deref
->mode
== nir_var_shader_in
||
2006 deref
->mode
== nir_var_shader_out
||
2007 deref
->mode
== nir_var_function_temp
))
2011 case nir_var_shader_in
:
2012 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2013 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2014 return load_tess_varyings(ctx
, instr
, true);
2017 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2018 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2019 LLVMValueRef indir_index
;
2020 unsigned const_index
, vertex_index
;
2021 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2022 &const_index
, &indir_index
);
2024 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2025 var
->data
.driver_location
,
2026 var
->data
.location_frac
,
2027 instr
->num_components
, vertex_index
, const_index
, type
);
2030 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2032 unsigned count
= glsl_count_attribute_slots(
2034 ctx
->stage
== MESA_SHADER_VERTEX
);
2036 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2037 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2038 stride
, false, true);
2040 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2044 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2047 case nir_var_function_temp
:
2048 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2050 unsigned count
= glsl_count_attribute_slots(
2053 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2054 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2055 stride
, true, true);
2057 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2061 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2065 case nir_var_mem_shared
: {
2066 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2067 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2068 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2069 get_def_type(ctx
, &instr
->dest
.ssa
),
2072 case nir_var_shader_out
:
2073 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2074 return load_tess_varyings(ctx
, instr
, false);
2077 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2079 unsigned count
= glsl_count_attribute_slots(
2082 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2083 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2084 stride
, true, true);
2086 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2090 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2091 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2096 case nir_var_mem_global
: {
2097 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2098 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2099 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2100 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2102 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2103 if (stride
!= natural_stride
) {
2104 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2105 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2106 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2108 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2109 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2110 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2111 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2113 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2115 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2116 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2117 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2118 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2123 unreachable("unhandle variable mode");
2125 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2126 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2130 visit_store_var(struct ac_nir_context
*ctx
,
2131 nir_intrinsic_instr
*instr
)
2133 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2134 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2136 LLVMValueRef temp_ptr
, value
;
2139 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2140 int writemask
= instr
->const_index
[0];
2141 LLVMValueRef indir_index
;
2142 unsigned const_index
;
2145 get_deref_offset(ctx
, deref
, false,
2146 NULL
, NULL
, &const_index
, &indir_index
);
2147 idx
= var
->data
.driver_location
;
2148 comp
= var
->data
.location_frac
;
2150 if (var
->data
.compact
) {
2151 const_index
+= comp
;
2156 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2157 (deref
->mode
== nir_var_shader_out
||
2158 deref
->mode
== nir_var_function_temp
)) {
2160 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2161 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2164 writemask
= widen_mask(writemask
, 2);
2167 writemask
= writemask
<< comp
;
2169 switch (deref
->mode
) {
2170 case nir_var_shader_out
:
2172 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2173 LLVMValueRef vertex_index
= NULL
;
2174 LLVMValueRef indir_index
= NULL
;
2175 unsigned const_index
= 0;
2176 const bool is_patch
= var
->data
.patch
;
2178 get_deref_offset(ctx
, deref
, false, NULL
,
2179 is_patch
? NULL
: &vertex_index
,
2180 &const_index
, &indir_index
);
2182 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2183 vertex_index
, indir_index
,
2184 const_index
, src
, writemask
);
2188 for (unsigned chan
= 0; chan
< 8; chan
++) {
2190 if (!(writemask
& (1 << chan
)))
2193 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2195 if (var
->data
.compact
)
2198 unsigned count
= glsl_count_attribute_slots(
2201 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2202 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2203 stride
, true, true);
2205 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2206 value
, indir_index
, "");
2207 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2208 count
, stride
, tmp_vec
);
2211 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2213 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2217 case nir_var_function_temp
:
2218 for (unsigned chan
= 0; chan
< 8; chan
++) {
2219 if (!(writemask
& (1 << chan
)))
2222 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2224 unsigned count
= glsl_count_attribute_slots(
2227 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2228 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2231 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2232 value
, indir_index
, "");
2233 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2236 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2238 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2243 case nir_var_mem_global
:
2244 case nir_var_mem_shared
: {
2245 int writemask
= instr
->const_index
[0];
2246 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2247 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2249 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2250 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2251 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2253 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2254 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2255 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2257 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2258 stride
== natural_stride
) {
2259 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2260 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2261 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2263 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2264 LLVMGetElementType(LLVMTypeOf(address
)), "");
2265 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2267 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2268 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2269 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2270 for (unsigned chan
= 0; chan
< 4; chan
++) {
2271 if (!(writemask
& (1 << chan
)))
2274 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2276 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2277 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2279 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2280 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2281 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2292 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2295 case GLSL_SAMPLER_DIM_BUF
:
2297 case GLSL_SAMPLER_DIM_1D
:
2298 return array
? 2 : 1;
2299 case GLSL_SAMPLER_DIM_2D
:
2300 return array
? 3 : 2;
2301 case GLSL_SAMPLER_DIM_MS
:
2302 return array
? 4 : 3;
2303 case GLSL_SAMPLER_DIM_3D
:
2304 case GLSL_SAMPLER_DIM_CUBE
:
2306 case GLSL_SAMPLER_DIM_RECT
:
2307 case GLSL_SAMPLER_DIM_SUBPASS
:
2309 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2318 /* Adjust the sample index according to FMASK.
2320 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2321 * which is the identity mapping. Each nibble says which physical sample
2322 * should be fetched to get that sample.
2324 * For example, 0x11111100 means there are only 2 samples stored and
2325 * the second sample covers 3/4 of the pixel. When reading samples 0
2326 * and 1, return physical sample 0 (determined by the first two 0s
2327 * in FMASK), otherwise return physical sample 1.
2329 * The sample index should be adjusted as follows:
2330 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2332 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2333 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2334 LLVMValueRef coord_z
,
2335 LLVMValueRef sample_index
,
2336 LLVMValueRef fmask_desc_ptr
)
2338 struct ac_image_args args
= {0};
2341 args
.coords
[0] = coord_x
;
2342 args
.coords
[1] = coord_y
;
2344 args
.coords
[2] = coord_z
;
2346 args
.opcode
= ac_image_load
;
2347 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2348 args
.resource
= fmask_desc_ptr
;
2350 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2352 res
= ac_build_image_opcode(ctx
, &args
);
2354 res
= ac_to_integer(ctx
, res
);
2355 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2356 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2358 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2362 LLVMValueRef sample_index4
=
2363 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2364 LLVMValueRef shifted_fmask
=
2365 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2366 LLVMValueRef final_sample
=
2367 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2369 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2370 * resource descriptor is 0 (invalid),
2372 LLVMValueRef fmask_desc
=
2373 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2376 LLVMValueRef fmask_word1
=
2377 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2380 LLVMValueRef word1_is_nonzero
=
2381 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2382 fmask_word1
, ctx
->i32_0
, "");
2384 /* Replace the MSAA sample index. */
2386 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2387 final_sample
, sample_index
, "");
2388 return sample_index
;
2391 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2393 assert(instr
->src
[0].is_ssa
);
2394 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2397 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2398 const nir_intrinsic_instr
*instr
,
2399 enum ac_descriptor_type desc_type
,
2402 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2405 static void get_image_coords(struct ac_nir_context
*ctx
,
2406 const nir_intrinsic_instr
*instr
,
2407 struct ac_image_args
*args
)
2409 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2411 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2412 LLVMValueRef masks
[] = {
2413 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2414 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2416 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2419 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2420 bool is_array
= glsl_sampler_type_is_array(type
);
2421 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2422 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2423 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2424 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2425 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2426 count
= image_type_to_components_count(dim
, is_array
);
2428 if (is_ms
&& instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
2429 LLVMValueRef fmask_load_address
[3];
2432 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2433 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2435 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2437 fmask_load_address
[2] = NULL
;
2439 for (chan
= 0; chan
< 2; ++chan
)
2440 fmask_load_address
[chan
] =
2441 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2442 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2443 ctx
->ac
.i32
, ""), "");
2444 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2446 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2447 fmask_load_address
[0],
2448 fmask_load_address
[1],
2449 fmask_load_address
[2],
2451 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2452 AC_DESC_FMASK
, NULL
, false, false));
2454 if (count
== 1 && !gfx9_1d
) {
2455 if (instr
->src
[1].ssa
->num_components
)
2456 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2458 args
->coords
[0] = src0
;
2463 for (chan
= 0; chan
< count
; ++chan
) {
2464 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2467 for (chan
= 0; chan
< 2; ++chan
) {
2468 args
->coords
[chan
] = LLVMBuildAdd(
2469 ctx
->ac
.builder
, args
->coords
[chan
],
2471 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2472 ctx
->ac
.i32
, ""), "");
2474 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2475 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2481 args
->coords
[2] = args
->coords
[1];
2482 args
->coords
[1] = ctx
->ac
.i32_0
;
2484 args
->coords
[1] = ctx
->ac
.i32_0
;
2489 args
->coords
[count
] = sample_index
;
2495 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2496 const nir_intrinsic_instr
*instr
, bool write
)
2498 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2499 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2500 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2501 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2502 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2504 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2505 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2506 elem_count
, stride
, "");
2508 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2509 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2514 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2515 const nir_intrinsic_instr
*instr
)
2518 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2519 const struct glsl_type
*type
= image_deref
->type
;
2520 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2521 struct ac_image_args args
= {};
2524 get_cache_policy(ctx
, var
->data
.image
.access
, false, false);
2526 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2527 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2528 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2529 unsigned num_channels
= util_last_bit(mask
);
2530 LLVMValueRef rsrc
, vindex
;
2532 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2533 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2536 /* TODO: set "can_speculate" when OpenGL needs it. */
2537 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2538 ctx
->ac
.i32_0
, num_channels
,
2539 !!(args
.cache_policy
& ac_glc
),
2541 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2543 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2544 res
= ac_to_integer(&ctx
->ac
, res
);
2546 args
.opcode
= ac_image_load
;
2547 get_image_coords(ctx
, instr
, &args
);
2548 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2549 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2550 glsl_sampler_type_is_array(type
));
2552 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2554 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2556 return ac_to_integer(&ctx
->ac
, res
);
2559 static void visit_image_store(struct ac_nir_context
*ctx
,
2560 nir_intrinsic_instr
*instr
)
2562 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2563 const struct glsl_type
*type
= image_deref
->type
;
2564 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2565 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2566 bool writeonly_memory
= var
->data
.image
.access
& ACCESS_NON_READABLE
;
2567 struct ac_image_args args
= {};
2569 args
.cache_policy
= get_cache_policy(ctx
, var
->data
.image
.access
, true,
2572 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2573 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2574 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2575 unsigned src_channels
= ac_get_llvm_num_components(src
);
2576 LLVMValueRef vindex
;
2578 if (src_channels
== 3)
2579 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2581 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2582 get_src(ctx
, instr
->src
[1]),
2585 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2586 ctx
->ac
.i32_0
, src_channels
,
2587 args
.cache_policy
& ac_glc
,
2590 args
.opcode
= ac_image_store
;
2591 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2592 get_image_coords(ctx
, instr
, &args
);
2593 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2594 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2595 glsl_sampler_type_is_array(type
));
2598 ac_build_image_opcode(&ctx
->ac
, &args
);
2603 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2604 const nir_intrinsic_instr
*instr
)
2606 LLVMValueRef params
[7];
2607 int param_count
= 0;
2608 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2610 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2611 const char *atomic_name
;
2612 char intrinsic_name
[64];
2613 enum ac_atomic_op atomic_subop
;
2614 MAYBE_UNUSED
int length
;
2616 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2618 switch (instr
->intrinsic
) {
2619 case nir_intrinsic_image_deref_atomic_add
:
2620 atomic_name
= "add";
2621 atomic_subop
= ac_atomic_add
;
2623 case nir_intrinsic_image_deref_atomic_min
:
2624 atomic_name
= is_unsigned
? "umin" : "smin";
2625 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2627 case nir_intrinsic_image_deref_atomic_max
:
2628 atomic_name
= is_unsigned
? "umax" : "smax";
2629 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2631 case nir_intrinsic_image_deref_atomic_and
:
2632 atomic_name
= "and";
2633 atomic_subop
= ac_atomic_and
;
2635 case nir_intrinsic_image_deref_atomic_or
:
2637 atomic_subop
= ac_atomic_or
;
2639 case nir_intrinsic_image_deref_atomic_xor
:
2640 atomic_name
= "xor";
2641 atomic_subop
= ac_atomic_xor
;
2643 case nir_intrinsic_image_deref_atomic_exchange
:
2644 atomic_name
= "swap";
2645 atomic_subop
= ac_atomic_swap
;
2647 case nir_intrinsic_image_deref_atomic_comp_swap
:
2648 atomic_name
= "cmpswap";
2649 atomic_subop
= 0; /* not used */
2656 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2657 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2659 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2660 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2661 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2662 ctx
->ac
.i32_0
, ""); /* vindex */
2663 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2664 if (HAVE_LLVM
>= 0x800) {
2665 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2666 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2668 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2669 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2671 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2673 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2674 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2677 assert(length
< sizeof(intrinsic_name
));
2678 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2679 params
, param_count
, 0);
2681 struct ac_image_args args
= {};
2682 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2683 args
.atomic
= atomic_subop
;
2684 args
.data
[0] = params
[0];
2686 args
.data
[1] = params
[1];
2687 get_image_coords(ctx
, instr
, &args
);
2688 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2689 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2690 glsl_sampler_type_is_array(type
));
2692 return ac_build_image_opcode(&ctx
->ac
, &args
);
2696 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2697 const nir_intrinsic_instr
*instr
)
2699 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2701 struct ac_image_args args
= { 0 };
2702 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2703 glsl_sampler_type_is_array(type
));
2705 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2706 args
.opcode
= ac_image_get_resinfo
;
2707 args
.lod
= ctx
->ac
.i32_0
;
2708 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2710 return ac_build_image_opcode(&ctx
->ac
, &args
);
2713 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2714 const nir_intrinsic_instr
*instr
)
2717 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2719 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2720 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2722 struct ac_image_args args
= { 0 };
2724 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2725 glsl_sampler_type_is_array(type
));
2727 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2728 args
.opcode
= ac_image_get_resinfo
;
2729 args
.lod
= ctx
->ac
.i32_0
;
2730 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2732 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2734 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2736 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2737 glsl_sampler_type_is_array(type
)) {
2738 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2739 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2740 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2741 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2743 if (ctx
->ac
.chip_class
>= GFX9
&&
2744 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2745 glsl_sampler_type_is_array(type
)) {
2746 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2747 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2754 static void emit_membar(struct ac_llvm_context
*ac
,
2755 const nir_intrinsic_instr
*instr
)
2757 unsigned waitcnt
= NOOP_WAITCNT
;
2759 switch (instr
->intrinsic
) {
2760 case nir_intrinsic_memory_barrier
:
2761 case nir_intrinsic_group_memory_barrier
:
2762 waitcnt
&= VM_CNT
& LGKM_CNT
;
2764 case nir_intrinsic_memory_barrier_atomic_counter
:
2765 case nir_intrinsic_memory_barrier_buffer
:
2766 case nir_intrinsic_memory_barrier_image
:
2769 case nir_intrinsic_memory_barrier_shared
:
2770 waitcnt
&= LGKM_CNT
;
2775 if (waitcnt
!= NOOP_WAITCNT
)
2776 ac_build_waitcnt(ac
, waitcnt
);
2779 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2781 /* SI only (thanks to a hw bug workaround):
2782 * The real barrier instruction isn’t needed, because an entire patch
2783 * always fits into a single wave.
2785 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2786 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2789 ac_build_s_barrier(ac
);
2792 static void emit_discard(struct ac_nir_context
*ctx
,
2793 const nir_intrinsic_instr
*instr
)
2797 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2798 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2799 get_src(ctx
, instr
->src
[0]),
2802 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2803 cond
= ctx
->ac
.i1false
;
2806 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2810 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2812 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2813 "llvm.amdgcn.ps.live",
2814 ctx
->ac
.i1
, NULL
, 0,
2815 AC_FUNC_ATTR_READNONE
);
2816 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2817 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2821 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2823 LLVMValueRef result
;
2824 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2825 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2826 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2828 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2832 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2834 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2835 LLVMValueRef result
;
2836 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2837 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2838 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2840 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2845 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2847 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2848 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2849 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2851 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2856 visit_first_invocation(struct ac_nir_context
*ctx
)
2858 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2860 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2861 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2862 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2864 ctx
->ac
.i64
, args
, 2,
2865 AC_FUNC_ATTR_NOUNWIND
|
2866 AC_FUNC_ATTR_READNONE
);
2868 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2872 visit_load_shared(struct ac_nir_context
*ctx
,
2873 const nir_intrinsic_instr
*instr
)
2875 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2877 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2879 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2880 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2881 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2882 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2885 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2886 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2890 visit_store_shared(struct ac_nir_context
*ctx
,
2891 const nir_intrinsic_instr
*instr
)
2893 LLVMValueRef derived_ptr
, data
,index
;
2894 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2896 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2897 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2899 int writemask
= nir_intrinsic_write_mask(instr
);
2900 for (int chan
= 0; chan
< 4; chan
++) {
2901 if (!(writemask
& (1 << chan
))) {
2904 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2905 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2906 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2907 LLVMBuildStore(builder
, data
, derived_ptr
);
2911 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2912 const nir_intrinsic_instr
*instr
,
2913 LLVMValueRef ptr
, int src_idx
)
2915 LLVMValueRef result
;
2916 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2918 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2919 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2920 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2921 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2923 LLVMAtomicOrderingSequentiallyConsistent
,
2924 LLVMAtomicOrderingSequentiallyConsistent
,
2926 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2928 LLVMAtomicRMWBinOp op
;
2929 switch (instr
->intrinsic
) {
2930 case nir_intrinsic_shared_atomic_add
:
2931 case nir_intrinsic_deref_atomic_add
:
2932 op
= LLVMAtomicRMWBinOpAdd
;
2934 case nir_intrinsic_shared_atomic_umin
:
2935 case nir_intrinsic_deref_atomic_umin
:
2936 op
= LLVMAtomicRMWBinOpUMin
;
2938 case nir_intrinsic_shared_atomic_umax
:
2939 case nir_intrinsic_deref_atomic_umax
:
2940 op
= LLVMAtomicRMWBinOpUMax
;
2942 case nir_intrinsic_shared_atomic_imin
:
2943 case nir_intrinsic_deref_atomic_imin
:
2944 op
= LLVMAtomicRMWBinOpMin
;
2946 case nir_intrinsic_shared_atomic_imax
:
2947 case nir_intrinsic_deref_atomic_imax
:
2948 op
= LLVMAtomicRMWBinOpMax
;
2950 case nir_intrinsic_shared_atomic_and
:
2951 case nir_intrinsic_deref_atomic_and
:
2952 op
= LLVMAtomicRMWBinOpAnd
;
2954 case nir_intrinsic_shared_atomic_or
:
2955 case nir_intrinsic_deref_atomic_or
:
2956 op
= LLVMAtomicRMWBinOpOr
;
2958 case nir_intrinsic_shared_atomic_xor
:
2959 case nir_intrinsic_deref_atomic_xor
:
2960 op
= LLVMAtomicRMWBinOpXor
;
2962 case nir_intrinsic_shared_atomic_exchange
:
2963 case nir_intrinsic_deref_atomic_exchange
:
2964 op
= LLVMAtomicRMWBinOpXchg
;
2970 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2971 LLVMAtomicOrderingSequentiallyConsistent
,
2977 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2979 LLVMValueRef values
[2];
2980 LLVMValueRef pos
[2];
2982 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2983 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2985 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2986 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2987 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2990 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2991 const nir_intrinsic_instr
*instr
)
2993 LLVMValueRef result
[4];
2994 LLVMValueRef interp_param
;
2997 LLVMValueRef src_c0
= NULL
;
2998 LLVMValueRef src_c1
= NULL
;
2999 LLVMValueRef src0
= NULL
;
3001 nir_deref_instr
*deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3002 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
3003 int input_base
= ctx
->abi
->fs_input_attr_indices
[var
->data
.location
- VARYING_SLOT_VAR0
];
3004 switch (instr
->intrinsic
) {
3005 case nir_intrinsic_interp_deref_at_centroid
:
3006 location
= INTERP_CENTROID
;
3008 case nir_intrinsic_interp_deref_at_sample
:
3009 case nir_intrinsic_interp_deref_at_offset
:
3010 location
= INTERP_CENTER
;
3011 src0
= get_src(ctx
, instr
->src
[1]);
3017 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
3018 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
3019 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
3020 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
3021 LLVMValueRef sample_position
;
3022 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3024 /* fetch sample ID */
3025 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
3027 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
3028 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3029 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
3030 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3032 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
3034 if (location
== INTERP_CENTER
) {
3035 LLVMValueRef ij_out
[2];
3036 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3039 * take the I then J parameters, and the DDX/Y for it, and
3040 * calculate the IJ inputs for the interpolator.
3041 * temp1 = ddx * offset/sample.x + I;
3042 * interp_param.I = ddy * offset/sample.y + temp1;
3043 * temp1 = ddx * offset/sample.x + J;
3044 * interp_param.J = ddy * offset/sample.y + temp1;
3046 for (unsigned i
= 0; i
< 2; i
++) {
3047 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3048 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3049 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3050 ddxy_out
, ix_ll
, "");
3051 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3052 ddxy_out
, iy_ll
, "");
3053 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3054 interp_param
, ix_ll
, "");
3055 LLVMValueRef temp1
, temp2
;
3057 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3060 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3061 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3063 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3064 temp2
, ctx
->ac
.i32
, "");
3066 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3070 LLVMValueRef attrib_idx
= ctx
->ac
.i32_0
;
3071 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3072 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3073 unsigned array_size
= glsl_count_attribute_slots(deref_instr
->type
, false);
3075 LLVMValueRef offset
;
3076 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3078 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* const_value
->u32
[0], false);
3080 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3082 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3083 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3086 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3087 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3088 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3089 LLVMValueRef offset
;
3090 unsigned sidx
= deref_instr
->strct
.index
;
3091 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3092 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_struct_location_offset(deref_instr
->type
, sidx
), false);
3093 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3095 unreachable("Unsupported deref type");
3100 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3101 for (chan
= 0; chan
< 4; chan
++) {
3102 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3103 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3105 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3106 LLVMValueRef v
, attr_number
;
3108 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3110 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3111 interp_param
, ctx
->ac
.v2f32
, "");
3112 LLVMValueRef i
= LLVMBuildExtractElement(
3113 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3114 LLVMValueRef j
= LLVMBuildExtractElement(
3115 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3117 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3118 ctx
->abi
->prim_mask
, i
, j
);
3120 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3121 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3124 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3125 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3128 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3131 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3132 var
->data
.location_frac
);
3135 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3136 nir_intrinsic_instr
*instr
)
3138 LLVMValueRef result
= NULL
;
3140 switch (instr
->intrinsic
) {
3141 case nir_intrinsic_ballot
:
3142 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3144 case nir_intrinsic_read_invocation
:
3145 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3146 get_src(ctx
, instr
->src
[1]));
3148 case nir_intrinsic_read_first_invocation
:
3149 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3151 case nir_intrinsic_load_subgroup_invocation
:
3152 result
= ac_get_thread_id(&ctx
->ac
);
3154 case nir_intrinsic_load_work_group_id
: {
3155 LLVMValueRef values
[3];
3157 for (int i
= 0; i
< 3; i
++) {
3158 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3159 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3162 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3165 case nir_intrinsic_load_base_vertex
:
3166 case nir_intrinsic_load_first_vertex
:
3167 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3169 case nir_intrinsic_load_local_group_size
:
3170 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3172 case nir_intrinsic_load_vertex_id
:
3173 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3174 ctx
->abi
->base_vertex
, "");
3176 case nir_intrinsic_load_vertex_id_zero_base
: {
3177 result
= ctx
->abi
->vertex_id
;
3180 case nir_intrinsic_load_local_invocation_id
: {
3181 result
= ctx
->abi
->local_invocation_ids
;
3184 case nir_intrinsic_load_base_instance
:
3185 result
= ctx
->abi
->start_instance
;
3187 case nir_intrinsic_load_draw_id
:
3188 result
= ctx
->abi
->draw_id
;
3190 case nir_intrinsic_load_view_index
:
3191 result
= ctx
->abi
->view_index
;
3193 case nir_intrinsic_load_invocation_id
:
3194 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3195 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3197 result
= ctx
->abi
->gs_invocation_id
;
3199 case nir_intrinsic_load_primitive_id
:
3200 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3201 result
= ctx
->abi
->gs_prim_id
;
3202 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3203 result
= ctx
->abi
->tcs_patch_id
;
3204 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3205 result
= ctx
->abi
->tes_patch_id
;
3207 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3209 case nir_intrinsic_load_sample_id
:
3210 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3212 case nir_intrinsic_load_sample_pos
:
3213 result
= load_sample_pos(ctx
);
3215 case nir_intrinsic_load_sample_mask_in
:
3216 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3218 case nir_intrinsic_load_frag_coord
: {
3219 LLVMValueRef values
[4] = {
3220 ctx
->abi
->frag_pos
[0],
3221 ctx
->abi
->frag_pos
[1],
3222 ctx
->abi
->frag_pos
[2],
3223 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3225 result
= ac_to_integer(&ctx
->ac
,
3226 ac_build_gather_values(&ctx
->ac
, values
, 4));
3229 case nir_intrinsic_load_front_face
:
3230 result
= ctx
->abi
->front_face
;
3232 case nir_intrinsic_load_helper_invocation
:
3233 result
= visit_load_helper_invocation(ctx
);
3235 case nir_intrinsic_load_instance_id
:
3236 result
= ctx
->abi
->instance_id
;
3238 case nir_intrinsic_load_num_work_groups
:
3239 result
= ctx
->abi
->num_work_groups
;
3241 case nir_intrinsic_load_local_invocation_index
:
3242 result
= visit_load_local_invocation_index(ctx
);
3244 case nir_intrinsic_load_subgroup_id
:
3245 result
= visit_load_subgroup_id(ctx
);
3247 case nir_intrinsic_load_num_subgroups
:
3248 result
= visit_load_num_subgroups(ctx
);
3250 case nir_intrinsic_first_invocation
:
3251 result
= visit_first_invocation(ctx
);
3253 case nir_intrinsic_load_push_constant
:
3254 result
= visit_load_push_constant(ctx
, instr
);
3256 case nir_intrinsic_vulkan_resource_index
: {
3257 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3258 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3259 unsigned binding
= nir_intrinsic_binding(instr
);
3261 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3265 case nir_intrinsic_vulkan_resource_reindex
:
3266 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3268 case nir_intrinsic_store_ssbo
:
3269 visit_store_ssbo(ctx
, instr
);
3271 case nir_intrinsic_load_ssbo
:
3272 result
= visit_load_buffer(ctx
, instr
);
3274 case nir_intrinsic_ssbo_atomic_add
:
3275 case nir_intrinsic_ssbo_atomic_imin
:
3276 case nir_intrinsic_ssbo_atomic_umin
:
3277 case nir_intrinsic_ssbo_atomic_imax
:
3278 case nir_intrinsic_ssbo_atomic_umax
:
3279 case nir_intrinsic_ssbo_atomic_and
:
3280 case nir_intrinsic_ssbo_atomic_or
:
3281 case nir_intrinsic_ssbo_atomic_xor
:
3282 case nir_intrinsic_ssbo_atomic_exchange
:
3283 case nir_intrinsic_ssbo_atomic_comp_swap
:
3284 result
= visit_atomic_ssbo(ctx
, instr
);
3286 case nir_intrinsic_load_ubo
:
3287 result
= visit_load_ubo_buffer(ctx
, instr
);
3289 case nir_intrinsic_get_buffer_size
:
3290 result
= visit_get_buffer_size(ctx
, instr
);
3292 case nir_intrinsic_load_deref
:
3293 result
= visit_load_var(ctx
, instr
);
3295 case nir_intrinsic_store_deref
:
3296 visit_store_var(ctx
, instr
);
3298 case nir_intrinsic_load_shared
:
3299 result
= visit_load_shared(ctx
, instr
);
3301 case nir_intrinsic_store_shared
:
3302 visit_store_shared(ctx
, instr
);
3304 case nir_intrinsic_image_deref_samples
:
3305 result
= visit_image_samples(ctx
, instr
);
3307 case nir_intrinsic_image_deref_load
:
3308 result
= visit_image_load(ctx
, instr
);
3310 case nir_intrinsic_image_deref_store
:
3311 visit_image_store(ctx
, instr
);
3313 case nir_intrinsic_image_deref_atomic_add
:
3314 case nir_intrinsic_image_deref_atomic_min
:
3315 case nir_intrinsic_image_deref_atomic_max
:
3316 case nir_intrinsic_image_deref_atomic_and
:
3317 case nir_intrinsic_image_deref_atomic_or
:
3318 case nir_intrinsic_image_deref_atomic_xor
:
3319 case nir_intrinsic_image_deref_atomic_exchange
:
3320 case nir_intrinsic_image_deref_atomic_comp_swap
:
3321 result
= visit_image_atomic(ctx
, instr
);
3323 case nir_intrinsic_image_deref_size
:
3324 result
= visit_image_size(ctx
, instr
);
3326 case nir_intrinsic_shader_clock
:
3327 result
= ac_build_shader_clock(&ctx
->ac
);
3329 case nir_intrinsic_discard
:
3330 case nir_intrinsic_discard_if
:
3331 emit_discard(ctx
, instr
);
3333 case nir_intrinsic_memory_barrier
:
3334 case nir_intrinsic_group_memory_barrier
:
3335 case nir_intrinsic_memory_barrier_atomic_counter
:
3336 case nir_intrinsic_memory_barrier_buffer
:
3337 case nir_intrinsic_memory_barrier_image
:
3338 case nir_intrinsic_memory_barrier_shared
:
3339 emit_membar(&ctx
->ac
, instr
);
3341 case nir_intrinsic_barrier
:
3342 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3344 case nir_intrinsic_shared_atomic_add
:
3345 case nir_intrinsic_shared_atomic_imin
:
3346 case nir_intrinsic_shared_atomic_umin
:
3347 case nir_intrinsic_shared_atomic_imax
:
3348 case nir_intrinsic_shared_atomic_umax
:
3349 case nir_intrinsic_shared_atomic_and
:
3350 case nir_intrinsic_shared_atomic_or
:
3351 case nir_intrinsic_shared_atomic_xor
:
3352 case nir_intrinsic_shared_atomic_exchange
:
3353 case nir_intrinsic_shared_atomic_comp_swap
: {
3354 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3355 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3358 case nir_intrinsic_deref_atomic_add
:
3359 case nir_intrinsic_deref_atomic_imin
:
3360 case nir_intrinsic_deref_atomic_umin
:
3361 case nir_intrinsic_deref_atomic_imax
:
3362 case nir_intrinsic_deref_atomic_umax
:
3363 case nir_intrinsic_deref_atomic_and
:
3364 case nir_intrinsic_deref_atomic_or
:
3365 case nir_intrinsic_deref_atomic_xor
:
3366 case nir_intrinsic_deref_atomic_exchange
:
3367 case nir_intrinsic_deref_atomic_comp_swap
: {
3368 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3369 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3372 case nir_intrinsic_interp_deref_at_centroid
:
3373 case nir_intrinsic_interp_deref_at_sample
:
3374 case nir_intrinsic_interp_deref_at_offset
:
3375 result
= visit_interp(ctx
, instr
);
3377 case nir_intrinsic_emit_vertex
:
3378 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3380 case nir_intrinsic_end_primitive
:
3381 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3383 case nir_intrinsic_load_tess_coord
:
3384 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3386 case nir_intrinsic_load_tess_level_outer
:
3387 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3389 case nir_intrinsic_load_tess_level_inner
:
3390 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3392 case nir_intrinsic_load_patch_vertices_in
:
3393 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3395 case nir_intrinsic_vote_all
: {
3396 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3397 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3400 case nir_intrinsic_vote_any
: {
3401 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3402 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3405 case nir_intrinsic_shuffle
:
3406 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3407 get_src(ctx
, instr
->src
[1]));
3409 case nir_intrinsic_reduce
:
3410 result
= ac_build_reduce(&ctx
->ac
,
3411 get_src(ctx
, instr
->src
[0]),
3412 instr
->const_index
[0],
3413 instr
->const_index
[1]);
3415 case nir_intrinsic_inclusive_scan
:
3416 result
= ac_build_inclusive_scan(&ctx
->ac
,
3417 get_src(ctx
, instr
->src
[0]),
3418 instr
->const_index
[0]);
3420 case nir_intrinsic_exclusive_scan
:
3421 result
= ac_build_exclusive_scan(&ctx
->ac
,
3422 get_src(ctx
, instr
->src
[0]),
3423 instr
->const_index
[0]);
3425 case nir_intrinsic_quad_broadcast
: {
3426 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3427 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3428 lane
, lane
, lane
, lane
);
3431 case nir_intrinsic_quad_swap_horizontal
:
3432 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3434 case nir_intrinsic_quad_swap_vertical
:
3435 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3437 case nir_intrinsic_quad_swap_diagonal
:
3438 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3441 fprintf(stderr
, "Unknown intrinsic: ");
3442 nir_print_instr(&instr
->instr
, stderr
);
3443 fprintf(stderr
, "\n");
3447 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3451 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3452 unsigned base_index
,
3453 unsigned constant_index
,
3454 LLVMValueRef dynamic_index
)
3456 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3457 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3458 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3460 /* Bindless uniforms are 64bit so multiple index by 8 */
3461 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3462 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3464 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3466 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3467 NULL
, 0, false, false, true, true);
3469 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3472 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3473 nir_deref_instr
*deref_instr
,
3474 enum ac_descriptor_type desc_type
,
3475 const nir_tex_instr
*tex_instr
,
3476 bool image
, bool write
)
3478 LLVMValueRef index
= NULL
;
3479 unsigned constant_index
= 0;
3480 unsigned descriptor_set
;
3481 unsigned base_index
;
3482 bool bindless
= false;
3485 assert(tex_instr
&& !image
);
3487 base_index
= tex_instr
->sampler_index
;
3489 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3490 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3491 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3495 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3497 constant_index
+= array_size
* const_value
->u32
[0];
3499 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3501 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3502 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3507 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3510 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3511 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3512 unsigned sidx
= deref_instr
->strct
.index
;
3513 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3514 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3516 unreachable("Unsupported deref type");
3519 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3521 if (deref_instr
->var
->data
.bindless
) {
3522 /* For now just assert on unhandled variable types */
3523 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3525 base_index
= deref_instr
->var
->data
.driver_location
;
3528 index
= index
? index
: ctx
->ac
.i32_0
;
3529 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3530 constant_index
, index
);
3532 base_index
= deref_instr
->var
->data
.binding
;
3535 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3538 constant_index
, index
,
3539 desc_type
, image
, write
, bindless
);
3542 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3545 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3546 * filtering manually. The driver sets img7 to a mask clearing
3547 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3548 * s_and_b32 samp0, samp0, img7
3551 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3553 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3554 LLVMValueRef res
, LLVMValueRef samp
)
3556 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3557 LLVMValueRef img7
, samp0
;
3559 if (ctx
->ac
.chip_class
>= VI
)
3562 img7
= LLVMBuildExtractElement(builder
, res
,
3563 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3564 samp0
= LLVMBuildExtractElement(builder
, samp
,
3565 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3566 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3567 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3568 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3571 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3572 nir_tex_instr
*instr
,
3573 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3574 LLVMValueRef
*fmask_ptr
)
3576 nir_deref_instr
*texture_deref_instr
= NULL
;
3577 nir_deref_instr
*sampler_deref_instr
= NULL
;
3579 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3580 switch (instr
->src
[i
].src_type
) {
3581 case nir_tex_src_texture_deref
:
3582 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3584 case nir_tex_src_sampler_deref
:
3585 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3592 if (!sampler_deref_instr
)
3593 sampler_deref_instr
= texture_deref_instr
;
3595 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3596 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3598 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3600 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3601 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3602 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3604 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3605 instr
->op
== nir_texop_samples_identical
))
3606 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3609 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3612 coord
= ac_to_float(ctx
, coord
);
3613 coord
= ac_build_round(ctx
, coord
);
3614 coord
= ac_to_integer(ctx
, coord
);
3618 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3620 LLVMValueRef result
= NULL
;
3621 struct ac_image_args args
= { 0 };
3622 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3623 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3624 unsigned offset_src
= 0;
3626 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3628 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3629 switch (instr
->src
[i
].src_type
) {
3630 case nir_tex_src_coord
: {
3631 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3632 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3633 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3636 case nir_tex_src_projector
:
3638 case nir_tex_src_comparator
:
3639 if (instr
->is_shadow
)
3640 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3642 case nir_tex_src_offset
:
3643 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3646 case nir_tex_src_bias
:
3647 if (instr
->op
== nir_texop_txb
)
3648 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3650 case nir_tex_src_lod
: {
3651 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3653 if (val
&& val
->i32
[0] == 0)
3654 args
.level_zero
= true;
3656 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3659 case nir_tex_src_ms_index
:
3660 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3662 case nir_tex_src_ms_mcs
:
3664 case nir_tex_src_ddx
:
3665 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3667 case nir_tex_src_ddy
:
3668 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3670 case nir_tex_src_texture_offset
:
3671 case nir_tex_src_sampler_offset
:
3672 case nir_tex_src_plane
:
3678 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3679 result
= get_buffer_size(ctx
, args
.resource
, true);
3683 if (instr
->op
== nir_texop_texture_samples
) {
3684 LLVMValueRef res
, samples
, is_msaa
;
3685 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3686 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3687 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3688 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3689 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3690 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3691 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3692 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3693 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3695 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3696 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3697 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3698 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3699 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3701 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3707 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3708 LLVMValueRef offset
[3], pack
;
3709 for (unsigned chan
= 0; chan
< 3; ++chan
)
3710 offset
[chan
] = ctx
->ac
.i32_0
;
3712 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3713 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3714 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3715 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3716 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3718 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3719 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3721 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3722 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3726 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3727 * so the depth comparison value isn't clamped for Z16 and
3728 * Z24 anymore. Do it manually here.
3730 * It's unnecessary if the original texture format was
3731 * Z32_FLOAT, but we don't know that here.
3733 if (args
.compare
&& ctx
->ac
.chip_class
>= VI
&& ctx
->abi
->clamp_shadow_reference
)
3734 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3736 /* pack derivatives */
3738 int num_src_deriv_channels
, num_dest_deriv_channels
;
3739 switch (instr
->sampler_dim
) {
3740 case GLSL_SAMPLER_DIM_3D
:
3741 case GLSL_SAMPLER_DIM_CUBE
:
3742 num_src_deriv_channels
= 3;
3743 num_dest_deriv_channels
= 3;
3745 case GLSL_SAMPLER_DIM_2D
:
3747 num_src_deriv_channels
= 2;
3748 num_dest_deriv_channels
= 2;
3750 case GLSL_SAMPLER_DIM_1D
:
3751 num_src_deriv_channels
= 1;
3752 if (ctx
->ac
.chip_class
>= GFX9
) {
3753 num_dest_deriv_channels
= 2;
3755 num_dest_deriv_channels
= 1;
3760 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3761 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3762 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3763 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3764 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3766 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3767 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3768 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3772 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3773 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3774 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3775 if (instr
->coord_components
== 3)
3776 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3777 ac_prepare_cube_coords(&ctx
->ac
,
3778 instr
->op
== nir_texop_txd
, instr
->is_array
,
3779 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3782 /* Texture coordinates fixups */
3783 if (instr
->coord_components
> 1 &&
3784 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3786 instr
->op
!= nir_texop_txf
) {
3787 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3790 if (instr
->coord_components
> 2 &&
3791 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3792 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3793 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3794 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3796 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3797 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3800 if (ctx
->ac
.chip_class
>= GFX9
&&
3801 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3802 instr
->op
!= nir_texop_lod
) {
3803 LLVMValueRef filler
;
3804 if (instr
->op
== nir_texop_txf
)
3805 filler
= ctx
->ac
.i32_0
;
3807 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3809 if (instr
->is_array
)
3810 args
.coords
[2] = args
.coords
[1];
3811 args
.coords
[1] = filler
;
3814 /* Pack sample index */
3815 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3816 args
.coords
[instr
->coord_components
] = sample_index
;
3818 if (instr
->op
== nir_texop_samples_identical
) {
3819 struct ac_image_args txf_args
= { 0 };
3820 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3822 txf_args
.dmask
= 0xf;
3823 txf_args
.resource
= fmask_ptr
;
3824 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3825 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3827 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3828 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3832 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3833 instr
->op
!= nir_texop_txs
) {
3834 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3835 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3836 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3837 instr
->is_array
? args
.coords
[2] : NULL
,
3838 args
.coords
[sample_chan
], fmask_ptr
);
3841 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3842 nir_const_value
*const_offset
=
3843 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3844 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3845 assert(const_offset
);
3846 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3847 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3848 args
.coords
[i
] = LLVMBuildAdd(
3849 ctx
->ac
.builder
, args
.coords
[i
],
3850 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3855 /* TODO TG4 support */
3857 if (instr
->op
== nir_texop_tg4
) {
3858 if (instr
->is_shadow
)
3861 args
.dmask
= 1 << instr
->component
;
3864 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3865 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3866 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3868 if (instr
->op
== nir_texop_query_levels
)
3869 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3870 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3871 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3872 instr
->op
!= nir_texop_tg4
)
3873 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3874 else if (instr
->op
== nir_texop_txs
&&
3875 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3877 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3878 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3879 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3880 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3881 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3882 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3883 instr
->op
== nir_texop_txs
&&
3884 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3886 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3887 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3888 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3890 } else if (instr
->dest
.ssa
.num_components
!= 4)
3891 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3895 assert(instr
->dest
.is_ssa
);
3896 result
= ac_to_integer(&ctx
->ac
, result
);
3897 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3902 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3904 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3905 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3907 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3908 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3911 static void visit_post_phi(struct ac_nir_context
*ctx
,
3912 nir_phi_instr
*instr
,
3913 LLVMValueRef llvm_phi
)
3915 nir_foreach_phi_src(src
, instr
) {
3916 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3917 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3919 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3923 static void phi_post_pass(struct ac_nir_context
*ctx
)
3925 hash_table_foreach(ctx
->phis
, entry
) {
3926 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3927 (LLVMValueRef
)entry
->data
);
3932 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3933 const nir_ssa_undef_instr
*instr
)
3935 unsigned num_components
= instr
->def
.num_components
;
3936 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3939 if (num_components
== 1)
3940 undef
= LLVMGetUndef(type
);
3942 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3944 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3947 static void visit_jump(struct ac_llvm_context
*ctx
,
3948 const nir_jump_instr
*instr
)
3950 switch (instr
->type
) {
3951 case nir_jump_break
:
3952 ac_build_break(ctx
);
3954 case nir_jump_continue
:
3955 ac_build_continue(ctx
);
3958 fprintf(stderr
, "Unknown NIR jump instr: ");
3959 nir_print_instr(&instr
->instr
, stderr
);
3960 fprintf(stderr
, "\n");
3966 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3967 enum glsl_base_type type
)
3971 case GLSL_TYPE_UINT
:
3972 case GLSL_TYPE_BOOL
:
3973 case GLSL_TYPE_SUBROUTINE
:
3975 case GLSL_TYPE_INT8
:
3976 case GLSL_TYPE_UINT8
:
3978 case GLSL_TYPE_INT16
:
3979 case GLSL_TYPE_UINT16
:
3981 case GLSL_TYPE_FLOAT
:
3983 case GLSL_TYPE_FLOAT16
:
3985 case GLSL_TYPE_INT64
:
3986 case GLSL_TYPE_UINT64
:
3988 case GLSL_TYPE_DOUBLE
:
3991 unreachable("unknown GLSL type");
3996 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3997 const struct glsl_type
*type
)
3999 if (glsl_type_is_scalar(type
)) {
4000 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4003 if (glsl_type_is_vector(type
)) {
4004 return LLVMVectorType(
4005 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4006 glsl_get_vector_elements(type
));
4009 if (glsl_type_is_matrix(type
)) {
4010 return LLVMArrayType(
4011 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4012 glsl_get_matrix_columns(type
));
4015 if (glsl_type_is_array(type
)) {
4016 return LLVMArrayType(
4017 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4018 glsl_get_length(type
));
4021 assert(glsl_type_is_struct_or_ifc(type
));
4023 LLVMTypeRef member_types
[glsl_get_length(type
)];
4025 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4027 glsl_to_llvm_type(ac
,
4028 glsl_get_struct_field(type
, i
));
4031 return LLVMStructTypeInContext(ac
->context
, member_types
,
4032 glsl_get_length(type
), false);
4035 static void visit_deref(struct ac_nir_context
*ctx
,
4036 nir_deref_instr
*instr
)
4038 if (instr
->mode
!= nir_var_mem_shared
&&
4039 instr
->mode
!= nir_var_mem_global
)
4042 LLVMValueRef result
= NULL
;
4043 switch(instr
->deref_type
) {
4044 case nir_deref_type_var
: {
4045 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4046 result
= entry
->data
;
4049 case nir_deref_type_struct
:
4050 if (instr
->mode
== nir_var_mem_global
) {
4051 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4052 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4053 instr
->strct
.index
);
4054 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4055 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4057 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4058 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4061 case nir_deref_type_array
:
4062 if (instr
->mode
== nir_var_mem_global
) {
4063 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4064 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4066 if ((glsl_type_is_matrix(parent
->type
) &&
4067 glsl_matrix_type_is_row_major(parent
->type
)) ||
4068 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4069 stride
= type_scalar_size_bytes(parent
->type
);
4072 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4073 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4074 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4076 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4078 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4080 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4081 get_src(ctx
, instr
->arr
.index
));
4084 case nir_deref_type_ptr_as_array
:
4085 if (instr
->mode
== nir_var_mem_global
) {
4086 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4088 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4089 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4090 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4092 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4094 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4096 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4097 get_src(ctx
, instr
->arr
.index
));
4100 case nir_deref_type_cast
: {
4101 result
= get_src(ctx
, instr
->parent
);
4103 /* We can't use the structs from LLVM because the shader
4104 * specifies its own offsets. */
4105 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4106 if (instr
->mode
== nir_var_mem_shared
)
4107 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4109 unsigned address_space
;
4111 switch(instr
->mode
) {
4112 case nir_var_mem_shared
:
4113 address_space
= AC_ADDR_SPACE_LDS
;
4115 case nir_var_mem_global
:
4116 address_space
= AC_ADDR_SPACE_GLOBAL
;
4119 unreachable("Unhandled address space");
4122 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4124 if (LLVMTypeOf(result
) != type
) {
4125 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4126 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4129 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4136 unreachable("Unhandled deref_instr deref type");
4139 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4142 static void visit_cf_list(struct ac_nir_context
*ctx
,
4143 struct exec_list
*list
);
4145 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4147 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4148 nir_foreach_instr(instr
, block
)
4150 switch (instr
->type
) {
4151 case nir_instr_type_alu
:
4152 visit_alu(ctx
, nir_instr_as_alu(instr
));
4154 case nir_instr_type_load_const
:
4155 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4157 case nir_instr_type_intrinsic
:
4158 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4160 case nir_instr_type_tex
:
4161 visit_tex(ctx
, nir_instr_as_tex(instr
));
4163 case nir_instr_type_phi
:
4164 visit_phi(ctx
, nir_instr_as_phi(instr
));
4166 case nir_instr_type_ssa_undef
:
4167 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4169 case nir_instr_type_jump
:
4170 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4172 case nir_instr_type_deref
:
4173 visit_deref(ctx
, nir_instr_as_deref(instr
));
4176 fprintf(stderr
, "Unknown NIR instr type: ");
4177 nir_print_instr(instr
, stderr
);
4178 fprintf(stderr
, "\n");
4183 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4186 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4188 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4190 nir_block
*then_block
=
4191 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4193 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4195 visit_cf_list(ctx
, &if_stmt
->then_list
);
4197 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4198 nir_block
*else_block
=
4199 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4201 ac_build_else(&ctx
->ac
, else_block
->index
);
4202 visit_cf_list(ctx
, &if_stmt
->else_list
);
4205 ac_build_endif(&ctx
->ac
, then_block
->index
);
4208 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4210 nir_block
*first_loop_block
=
4211 (nir_block
*) exec_list_get_head(&loop
->body
);
4213 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4215 visit_cf_list(ctx
, &loop
->body
);
4217 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4220 static void visit_cf_list(struct ac_nir_context
*ctx
,
4221 struct exec_list
*list
)
4223 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4225 switch (node
->type
) {
4226 case nir_cf_node_block
:
4227 visit_block(ctx
, nir_cf_node_as_block(node
));
4230 case nir_cf_node_if
:
4231 visit_if(ctx
, nir_cf_node_as_if(node
));
4234 case nir_cf_node_loop
:
4235 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4245 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4246 struct ac_shader_abi
*abi
,
4247 struct nir_shader
*nir
,
4248 struct nir_variable
*variable
,
4249 gl_shader_stage stage
)
4251 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4252 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4254 /* tess ctrl has it's own load/store paths for outputs */
4255 if (stage
== MESA_SHADER_TESS_CTRL
)
4258 if (stage
== MESA_SHADER_VERTEX
||
4259 stage
== MESA_SHADER_TESS_EVAL
||
4260 stage
== MESA_SHADER_GEOMETRY
) {
4261 int idx
= variable
->data
.location
+ variable
->data
.index
;
4262 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4263 int length
= nir
->info
.clip_distance_array_size
+
4264 nir
->info
.cull_distance_array_size
;
4273 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4274 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4275 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4276 for (unsigned chan
= 0; chan
< 4; chan
++) {
4277 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4278 ac_build_alloca_undef(ctx
, type
, "");
4284 setup_locals(struct ac_nir_context
*ctx
,
4285 struct nir_function
*func
)
4288 ctx
->num_locals
= 0;
4289 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4290 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4291 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4292 variable
->data
.location_frac
= 0;
4293 ctx
->num_locals
+= attrib_count
;
4295 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4299 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4300 for (j
= 0; j
< 4; j
++) {
4301 ctx
->locals
[i
* 4 + j
] =
4302 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4308 setup_shared(struct ac_nir_context
*ctx
,
4309 struct nir_shader
*nir
)
4311 nir_foreach_variable(variable
, &nir
->shared
) {
4312 LLVMValueRef shared
=
4313 LLVMAddGlobalInAddressSpace(
4314 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4315 variable
->name
? variable
->name
: "",
4317 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4321 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4322 struct nir_shader
*nir
)
4324 struct ac_nir_context ctx
= {};
4325 struct nir_function
*func
;
4330 ctx
.stage
= nir
->info
.stage
;
4332 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4334 nir_foreach_variable(variable
, &nir
->outputs
)
4335 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4338 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4339 _mesa_key_pointer_equal
);
4340 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4341 _mesa_key_pointer_equal
);
4342 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4343 _mesa_key_pointer_equal
);
4345 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4347 nir_index_ssa_defs(func
->impl
);
4348 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4350 setup_locals(&ctx
, func
);
4352 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4353 setup_shared(&ctx
, nir
);
4355 visit_cf_list(&ctx
, &func
->impl
->body
);
4356 phi_post_pass(&ctx
);
4358 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4359 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4364 ralloc_free(ctx
.defs
);
4365 ralloc_free(ctx
.phis
);
4366 ralloc_free(ctx
.vars
);
4370 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4372 /* While it would be nice not to have this flag, we are constrained
4373 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4376 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4378 /* TODO: Indirect indexing of GS inputs is unimplemented.
4380 * TCS and TES load inputs directly from LDS or offchip memory, so
4381 * indirect indexing is trivial.
4383 nir_variable_mode indirect_mask
= 0;
4384 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4385 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4386 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4387 !llvm_has_working_vgpr_indexing
)) {
4388 indirect_mask
|= nir_var_shader_in
;
4390 if (!llvm_has_working_vgpr_indexing
&&
4391 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4392 indirect_mask
|= nir_var_shader_out
;
4394 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4395 * smart enough to handle indirects without causing excess spilling
4396 * causing the gpu to hang.
4398 * See the following thread for more details of the problem:
4399 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4401 indirect_mask
|= nir_var_function_temp
;
4403 nir_lower_indirect_derefs(nir
, indirect_mask
);
4407 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4409 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4413 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4415 if (var
->data
.mode
!= nir_var_shader_out
)
4418 unsigned writemask
= 0;
4419 const int location
= var
->data
.location
;
4420 unsigned first_component
= var
->data
.location_frac
;
4421 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4423 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4424 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4425 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4426 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4432 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4433 unsigned *cond_block_tf_writemask
,
4434 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4436 switch (cf_node
->type
) {
4437 case nir_cf_node_block
: {
4438 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4439 nir_foreach_instr(instr
, block
) {
4440 if (instr
->type
!= nir_instr_type_intrinsic
)
4443 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4444 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4446 /* If we find a barrier in nested control flow put this in the
4447 * too hard basket. In GLSL this is not possible but it is in
4451 *tessfactors_are_def_in_all_invocs
= false;
4455 /* The following case must be prevented:
4456 * gl_TessLevelInner = ...;
4458 * if (gl_InvocationID == 1)
4459 * gl_TessLevelInner = ...;
4461 * If you consider disjoint code segments separated by barriers, each
4462 * such segment that writes tess factor channels should write the same
4463 * channels in all codepaths within that segment.
4465 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4466 /* Accumulate the result: */
4467 *tessfactors_are_def_in_all_invocs
&=
4468 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4470 /* Analyze the next code segment from scratch. */
4471 *upper_block_tf_writemask
= 0;
4472 *cond_block_tf_writemask
= 0;
4475 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4480 case nir_cf_node_if
: {
4481 unsigned then_tessfactor_writemask
= 0;
4482 unsigned else_tessfactor_writemask
= 0;
4484 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4485 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4486 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4487 cond_block_tf_writemask
,
4488 tessfactors_are_def_in_all_invocs
, true);
4491 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4492 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4493 cond_block_tf_writemask
,
4494 tessfactors_are_def_in_all_invocs
, true);
4497 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4498 /* If both statements write the same tess factor channels,
4499 * we can say that the upper block writes them too.
4501 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4502 else_tessfactor_writemask
;
4503 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4504 else_tessfactor_writemask
;
4509 case nir_cf_node_loop
: {
4510 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4511 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4512 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4513 cond_block_tf_writemask
,
4514 tessfactors_are_def_in_all_invocs
, true);
4520 unreachable("unknown cf node type");
4525 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4527 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4529 /* The pass works as follows:
4530 * If all codepaths write tess factors, we can say that all
4531 * invocations define tess factors.
4533 * Each tess factor channel is tracked separately.
4535 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4536 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4538 /* Initial value = true. Here the pass will accumulate results from
4539 * multiple segments surrounded by barriers. If tess factors aren't
4540 * written at all, it's a shader bug and we don't care if this will be
4543 bool tessfactors_are_def_in_all_invocs
= true;
4545 nir_foreach_function(function
, nir
) {
4546 if (function
->impl
) {
4547 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4548 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4549 &cond_block_tf_writemask
,
4550 &tessfactors_are_def_in_all_invocs
,
4556 /* Accumulate the result for the last code segment separated by a
4559 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4560 tessfactors_are_def_in_all_invocs
&=
4561 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4564 return tessfactors_are_def_in_all_invocs
;