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
;
43 LLVMValueRef
*ssa_defs
;
45 struct hash_table
*defs
;
46 struct hash_table
*phis
;
47 struct hash_table
*vars
;
49 LLVMValueRef main_function
;
50 LLVMBasicBlockRef continue_block
;
51 LLVMBasicBlockRef break_block
;
57 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
58 nir_deref_instr
*deref_instr
,
59 enum ac_descriptor_type desc_type
,
60 const nir_instr
*instr
,
61 bool image
, bool write
);
64 build_store_values_extended(struct ac_llvm_context
*ac
,
67 unsigned value_stride
,
70 LLVMBuilderRef builder
= ac
->builder
;
73 for (i
= 0; i
< value_count
; i
++) {
74 LLVMValueRef ptr
= values
[i
* value_stride
];
75 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
76 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
77 LLVMBuildStore(builder
, value
, ptr
);
81 static enum ac_image_dim
82 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
86 case GLSL_SAMPLER_DIM_1D
:
87 if (ctx
->chip_class
>= GFX9
)
88 return is_array
? ac_image_2darray
: ac_image_2d
;
89 return is_array
? ac_image_1darray
: ac_image_1d
;
90 case GLSL_SAMPLER_DIM_2D
:
91 case GLSL_SAMPLER_DIM_RECT
:
92 case GLSL_SAMPLER_DIM_EXTERNAL
:
93 return is_array
? ac_image_2darray
: ac_image_2d
;
94 case GLSL_SAMPLER_DIM_3D
:
96 case GLSL_SAMPLER_DIM_CUBE
:
98 case GLSL_SAMPLER_DIM_MS
:
99 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
100 case GLSL_SAMPLER_DIM_SUBPASS
:
101 return ac_image_2darray
;
102 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
103 return ac_image_2darraymsaa
;
105 unreachable("bad sampler dim");
109 static enum ac_image_dim
110 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
113 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
115 if (dim
== ac_image_cube
||
116 (ctx
->chip_class
<= GFX8
&& dim
== ac_image_3d
))
117 dim
= ac_image_2darray
;
122 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
123 const nir_ssa_def
*def
)
125 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
126 if (def
->num_components
> 1) {
127 type
= LLVMVectorType(type
, def
->num_components
);
132 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
135 return nir
->ssa_defs
[src
.ssa
->index
];
139 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
141 LLVMValueRef ptr
= get_src(ctx
, src
);
142 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
143 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
145 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
146 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
149 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
150 const struct nir_block
*b
)
152 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
153 return (LLVMBasicBlockRef
)entry
->data
;
156 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
158 unsigned num_components
)
160 LLVMValueRef value
= get_src(ctx
, src
.src
);
161 bool need_swizzle
= false;
164 unsigned src_components
= ac_get_llvm_num_components(value
);
165 for (unsigned i
= 0; i
< num_components
; ++i
) {
166 assert(src
.swizzle
[i
] < src_components
);
167 if (src
.swizzle
[i
] != i
)
171 if (need_swizzle
|| num_components
!= src_components
) {
172 LLVMValueRef masks
[] = {
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
176 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
178 if (src_components
> 1 && num_components
== 1) {
179 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
181 } else if (src_components
== 1 && num_components
> 1) {
182 LLVMValueRef values
[] = {value
, value
, value
, value
};
183 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
185 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
186 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
195 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
196 LLVMIntPredicate pred
, LLVMValueRef src0
,
199 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
200 return LLVMBuildSelect(ctx
->builder
, result
,
201 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
205 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
206 LLVMRealPredicate pred
, LLVMValueRef src0
,
210 src0
= ac_to_float(ctx
, src0
);
211 src1
= ac_to_float(ctx
, src1
);
212 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
213 return LLVMBuildSelect(ctx
->builder
, result
,
214 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
218 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
220 LLVMTypeRef result_type
,
224 LLVMValueRef params
[] = {
225 ac_to_float(ctx
, src0
),
228 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
229 ac_get_elem_bits(ctx
, result_type
));
230 assert(length
< sizeof(name
));
231 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
234 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
236 LLVMTypeRef result_type
,
237 LLVMValueRef src0
, LLVMValueRef src1
)
240 LLVMValueRef params
[] = {
241 ac_to_float(ctx
, src0
),
242 ac_to_float(ctx
, src1
),
245 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
246 ac_get_elem_bits(ctx
, result_type
));
247 assert(length
< sizeof(name
));
248 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
251 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
253 LLVMTypeRef result_type
,
254 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
257 LLVMValueRef params
[] = {
258 ac_to_float(ctx
, src0
),
259 ac_to_float(ctx
, src1
),
260 ac_to_float(ctx
, src2
),
263 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
264 ac_get_elem_bits(ctx
, result_type
));
265 assert(length
< sizeof(name
));
266 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
269 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
270 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
272 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
274 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
276 return LLVMBuildSelect(ctx
->builder
, v
,
277 ac_to_integer_or_pointer(ctx
, src1
),
278 ac_to_integer_or_pointer(ctx
, src2
), "");
281 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
284 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
287 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
289 LLVMValueRef src0
, LLVMValueRef src1
)
291 LLVMTypeRef ret_type
;
292 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
294 LLVMValueRef params
[] = { src0
, src1
};
295 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
298 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
299 params
, 2, AC_FUNC_ATTR_READNONE
);
301 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
302 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
306 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
310 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
311 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
313 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
317 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
321 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
323 unreachable("Unsupported bit size.");
327 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
330 src0
= ac_to_float(ctx
, src0
);
331 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
332 return LLVMBuildSExt(ctx
->builder
,
333 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
337 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
341 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
345 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
347 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
351 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
353 unreachable("Unsupported bit size.");
357 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
360 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
361 return LLVMBuildSExt(ctx
->builder
,
362 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
366 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
370 LLVMValueRef cond
= NULL
;
372 src0
= ac_to_float(ctx
, src0
);
373 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
375 if (ctx
->chip_class
>= GFX8
) {
376 LLVMValueRef args
[2];
377 /* Check if the result is a denormal - and flush to 0 if so. */
379 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
380 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
383 /* need to convert back up to f32 */
384 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
386 if (ctx
->chip_class
>= GFX8
)
387 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
391 * so compare the result and flush to 0 if it's smaller.
393 LLVMValueRef temp
, cond2
;
394 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
395 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
396 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
398 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
399 temp
, ctx
->f32_0
, "");
400 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
401 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
406 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
407 LLVMValueRef src0
, LLVMValueRef src1
)
409 LLVMValueRef dst64
, result
;
410 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
411 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
413 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
414 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
415 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
419 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
420 LLVMValueRef src0
, LLVMValueRef src1
)
422 LLVMValueRef dst64
, result
;
423 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
424 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
426 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
427 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
428 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
432 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
434 const LLVMValueRef srcs
[3])
438 if (HAVE_LLVM
>= 0x0800) {
439 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
440 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
441 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
443 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
444 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
446 LLVMValueRef zero
= ctx
->i32_0
;
447 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
448 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
450 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
451 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
452 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
458 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
459 LLVMValueRef src0
, LLVMValueRef src1
,
460 LLVMValueRef src2
, LLVMValueRef src3
)
462 LLVMValueRef bfi_args
[3], result
;
464 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
465 LLVMBuildSub(ctx
->builder
,
466 LLVMBuildShl(ctx
->builder
,
471 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
474 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
477 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
478 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
480 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
481 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
482 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
484 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
488 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
491 LLVMValueRef comp
[2];
493 src0
= ac_to_float(ctx
, src0
);
494 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
495 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
497 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
501 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
504 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
505 LLVMValueRef temps
[2], val
;
508 for (i
= 0; i
< 2; i
++) {
509 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
510 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
511 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
512 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
514 return ac_build_gather_values(ctx
, temps
, 2);
517 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
525 if (op
== nir_op_fddx_fine
)
526 mask
= AC_TID_MASK_LEFT
;
527 else if (op
== nir_op_fddy_fine
)
528 mask
= AC_TID_MASK_TOP
;
530 mask
= AC_TID_MASK_TOP_LEFT
;
532 /* for DDX we want to next X pixel, DDY next Y pixel. */
533 if (op
== nir_op_fddx_fine
||
534 op
== nir_op_fddx_coarse
||
540 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
544 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
546 LLVMValueRef src
[4], result
= NULL
;
547 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
548 unsigned src_components
;
549 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
551 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
558 case nir_op_pack_half_2x16
:
561 case nir_op_unpack_half_2x16
:
564 case nir_op_cube_face_coord
:
565 case nir_op_cube_face_index
:
569 src_components
= num_components
;
572 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
573 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
580 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
581 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
584 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
587 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
590 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
593 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
594 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
595 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
598 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
599 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
600 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
603 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
606 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
609 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
612 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
615 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
616 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
617 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
618 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
619 ac_to_float_type(&ctx
->ac
, def_type
), result
);
620 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
621 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
624 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
625 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
626 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
629 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
632 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
635 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
638 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
639 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
640 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
643 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
644 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
647 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
650 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
653 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
656 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
657 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
658 LLVMTypeOf(src
[0]), "");
659 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
660 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
661 LLVMTypeOf(src
[0]), "");
662 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
665 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
666 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
667 LLVMTypeOf(src
[0]), "");
668 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
669 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
670 LLVMTypeOf(src
[0]), "");
671 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
674 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
675 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
676 LLVMTypeOf(src
[0]), "");
677 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
678 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
679 LLVMTypeOf(src
[0]), "");
680 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
683 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
686 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
689 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
692 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
701 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
704 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
707 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
710 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
713 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
714 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
717 result
= emit_iabs(&ctx
->ac
, src
[0]);
720 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
723 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
726 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
729 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
732 result
= ac_build_isign(&ctx
->ac
, src
[0],
733 instr
->dest
.dest
.ssa
.bit_size
);
736 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
737 result
= ac_build_fsign(&ctx
->ac
, src
[0],
738 instr
->dest
.dest
.ssa
.bit_size
);
741 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
742 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
745 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
746 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
749 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
750 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
752 case nir_op_fround_even
:
753 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
754 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
757 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
758 result
= ac_build_fract(&ctx
->ac
, src
[0],
759 instr
->dest
.dest
.ssa
.bit_size
);
762 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
763 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
766 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
767 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
770 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
771 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
774 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
775 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
778 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
779 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
782 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
783 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
784 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
786 case nir_op_frexp_exp
:
787 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
788 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
789 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
790 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
791 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
794 case nir_op_frexp_sig
:
795 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
796 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
797 instr
->dest
.dest
.ssa
.bit_size
);
800 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
801 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
804 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
805 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
806 if (ctx
->ac
.chip_class
< GFX9
&&
807 instr
->dest
.dest
.ssa
.bit_size
== 32) {
808 /* Only pre-GFX9 chips do not flush denorms. */
809 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
810 ac_to_float_type(&ctx
->ac
, def_type
),
815 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
816 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
817 if (ctx
->ac
.chip_class
< GFX9
&&
818 instr
->dest
.dest
.ssa
.bit_size
== 32) {
819 /* Only pre-GFX9 chips do not flush denorms. */
820 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
821 ac_to_float_type(&ctx
->ac
, def_type
),
826 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
827 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
830 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
831 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
832 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
833 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
834 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
836 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
838 case nir_op_ibitfield_extract
:
839 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
841 case nir_op_ubitfield_extract
:
842 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
844 case nir_op_bitfield_insert
:
845 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
847 case nir_op_bitfield_reverse
:
848 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
850 case nir_op_bit_count
:
851 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
856 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
857 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
858 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
871 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
872 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
877 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
882 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
884 case nir_op_f2f16_rtz
:
885 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
886 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
887 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
888 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
889 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
890 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
892 case nir_op_f2f16_rtne
:
896 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
897 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
898 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
900 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
906 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
907 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
909 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
915 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
916 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
918 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
921 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
923 case nir_op_find_lsb
:
924 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
926 case nir_op_ufind_msb
:
927 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
929 case nir_op_ifind_msb
:
930 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
932 case nir_op_uadd_carry
:
933 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
935 case nir_op_usub_borrow
:
936 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
941 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
944 result
= emit_f2b(&ctx
->ac
, src
[0]);
950 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
953 result
= emit_i2b(&ctx
->ac
, src
[0]);
955 case nir_op_fquantize2f16
:
956 result
= emit_f2f16(&ctx
->ac
, src
[0]);
958 case nir_op_umul_high
:
959 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
961 case nir_op_imul_high
:
962 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
964 case nir_op_pack_half_2x16
:
965 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
967 case nir_op_unpack_half_2x16
:
968 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
972 case nir_op_fddx_fine
:
973 case nir_op_fddy_fine
:
974 case nir_op_fddx_coarse
:
975 case nir_op_fddy_coarse
:
976 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
979 case nir_op_unpack_64_2x32_split_x
: {
980 assert(ac_get_llvm_num_components(src
[0]) == 1);
981 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
984 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
989 case nir_op_unpack_64_2x32_split_y
: {
990 assert(ac_get_llvm_num_components(src
[0]) == 1);
991 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
994 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
999 case nir_op_pack_64_2x32_split
: {
1000 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1001 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1005 case nir_op_pack_32_2x16_split
: {
1006 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1007 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1011 case nir_op_unpack_32_2x16_split_x
: {
1012 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1015 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1020 case nir_op_unpack_32_2x16_split_y
: {
1021 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1024 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1029 case nir_op_cube_face_coord
: {
1030 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1031 LLVMValueRef results
[2];
1033 for (unsigned chan
= 0; chan
< 3; chan
++)
1034 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1035 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1036 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1037 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1038 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1039 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1040 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1041 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1042 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1043 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1044 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1045 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1046 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1050 case nir_op_cube_face_index
: {
1051 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1053 for (unsigned chan
= 0; chan
< 3; chan
++)
1054 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1055 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1056 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1061 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1062 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1063 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1064 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1067 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1068 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1071 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1072 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1075 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1076 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1077 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1078 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1081 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1082 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1085 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1086 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1088 case nir_op_fmed3
: {
1089 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1090 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1091 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1092 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1093 instr
->dest
.dest
.ssa
.bit_size
);
1096 case nir_op_imed3
: {
1097 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1098 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1099 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1100 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1103 case nir_op_umed3
: {
1104 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1105 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1106 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1107 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1112 fprintf(stderr
, "Unknown NIR alu instr: ");
1113 nir_print_instr(&instr
->instr
, stderr
);
1114 fprintf(stderr
, "\n");
1119 assert(instr
->dest
.dest
.is_ssa
);
1120 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1121 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1125 static void visit_load_const(struct ac_nir_context
*ctx
,
1126 const nir_load_const_instr
*instr
)
1128 LLVMValueRef values
[4], value
= NULL
;
1129 LLVMTypeRef element_type
=
1130 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1132 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1133 switch (instr
->def
.bit_size
) {
1135 values
[i
] = LLVMConstInt(element_type
,
1136 instr
->value
[i
].u8
, false);
1139 values
[i
] = LLVMConstInt(element_type
,
1140 instr
->value
[i
].u16
, false);
1143 values
[i
] = LLVMConstInt(element_type
,
1144 instr
->value
[i
].u32
, false);
1147 values
[i
] = LLVMConstInt(element_type
,
1148 instr
->value
[i
].u64
, false);
1152 "unsupported nir load_const bit_size: %d\n",
1153 instr
->def
.bit_size
);
1157 if (instr
->def
.num_components
> 1) {
1158 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1162 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1166 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1169 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1170 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1173 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1174 /* On GFX8, the descriptor contains the size in bytes,
1175 * but TXQ must return the size in elements.
1176 * The stride is always non-zero for resources using TXQ.
1178 LLVMValueRef stride
=
1179 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1181 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1182 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1183 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1184 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1186 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1191 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1193 struct ac_image_args
*args
,
1194 const nir_tex_instr
*instr
)
1196 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1197 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1198 LLVMValueRef half_texel
[2];
1199 LLVMValueRef compare_cube_wa
= NULL
;
1200 LLVMValueRef result
;
1204 struct ac_image_args txq_args
= { 0 };
1206 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1207 txq_args
.opcode
= ac_image_get_resinfo
;
1208 txq_args
.dmask
= 0xf;
1209 txq_args
.lod
= ctx
->i32_0
;
1210 txq_args
.resource
= args
->resource
;
1211 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1212 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1214 for (unsigned c
= 0; c
< 2; c
++) {
1215 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1216 LLVMConstInt(ctx
->i32
, c
, false), "");
1217 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1218 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1219 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1220 LLVMConstReal(ctx
->f32
, -0.5), "");
1224 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1226 for (unsigned c
= 0; c
< 2; c
++) {
1228 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1229 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1233 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1234 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1235 * workaround by sampling using a scaled type and converting.
1236 * This is taken from amdgpu-pro shaders.
1238 /* NOTE this produces some ugly code compared to amdgpu-pro,
1239 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1240 * and then reads them back. -pro generates two selects,
1241 * one s_cmp for the descriptor rewriting
1242 * one v_cmp for the coordinate and result changes.
1244 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1245 LLVMValueRef tmp
, tmp2
;
1247 /* workaround 8/8/8/8 uint/sint cube gather bug */
1248 /* first detect it then change to a scaled read and f2i */
1249 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1252 /* extract the DATA_FORMAT */
1253 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1254 LLVMConstInt(ctx
->i32
, 6, false), false);
1256 /* is the DATA_FORMAT == 8_8_8_8 */
1257 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1259 if (stype
== GLSL_TYPE_UINT
)
1260 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1261 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1262 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1264 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1265 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1266 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1268 /* replace the NUM FORMAT in the descriptor */
1269 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false), "");
1270 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1272 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1274 /* don't modify the coordinates for this case */
1275 for (unsigned c
= 0; c
< 2; ++c
)
1276 args
->coords
[c
] = LLVMBuildSelect(
1277 ctx
->builder
, compare_cube_wa
,
1278 orig_coords
[c
], args
->coords
[c
], "");
1281 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1282 result
= ac_build_image_opcode(ctx
, args
);
1284 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1285 LLVMValueRef tmp
, tmp2
;
1287 /* if the cube workaround is in place, f2i the result. */
1288 for (unsigned c
= 0; c
< 4; c
++) {
1289 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1290 if (stype
== GLSL_TYPE_UINT
)
1291 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1293 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1294 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1295 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1296 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1297 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1298 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1304 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1306 nir_deref_instr
*texture_deref_instr
= NULL
;
1308 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1309 switch (instr
->src
[i
].src_type
) {
1310 case nir_tex_src_texture_deref
:
1311 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1317 return texture_deref_instr
;
1320 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1321 const nir_tex_instr
*instr
,
1322 struct ac_image_args
*args
)
1324 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1325 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1327 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1328 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1332 util_last_bit(mask
),
1335 return ac_build_buffer_load_format(&ctx
->ac
,
1339 util_last_bit(mask
),
1344 args
->opcode
= ac_image_sample
;
1346 switch (instr
->op
) {
1348 case nir_texop_txf_ms
:
1349 case nir_texop_samples_identical
:
1350 args
->opcode
= args
->level_zero
||
1351 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1352 ac_image_load
: ac_image_load_mip
;
1353 args
->level_zero
= false;
1356 case nir_texop_query_levels
:
1357 args
->opcode
= ac_image_get_resinfo
;
1359 args
->lod
= ctx
->ac
.i32_0
;
1360 args
->level_zero
= false;
1363 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1365 args
->level_zero
= true;
1369 args
->opcode
= ac_image_gather4
;
1370 args
->level_zero
= true;
1373 args
->opcode
= ac_image_get_lod
;
1379 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1380 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1381 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1382 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1383 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1384 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1385 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1389 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1390 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1391 if ((args
->dim
== ac_image_2darray
||
1392 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1393 args
->coords
[1] = ctx
->ac
.i32_0
;
1397 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1398 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1399 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1400 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1401 /* Prevent texture instructions with implicit derivatives from being
1402 * sinked into branches. */
1403 switch (instr
->op
) {
1407 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1414 return ac_build_image_opcode(&ctx
->ac
, args
);
1417 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1418 nir_intrinsic_instr
*instr
)
1420 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1421 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1423 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1424 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1428 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1429 nir_intrinsic_instr
*instr
)
1431 LLVMValueRef ptr
, addr
;
1432 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1433 unsigned index
= nir_intrinsic_base(instr
);
1435 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1436 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1438 /* Load constant values from user SGPRS when possible, otherwise
1439 * fallback to the default path that loads directly from memory.
1441 if (LLVMIsConstant(src0
) &&
1442 instr
->dest
.ssa
.bit_size
== 32) {
1443 unsigned count
= instr
->dest
.ssa
.num_components
;
1444 unsigned offset
= index
;
1446 offset
+= LLVMConstIntGetZExtValue(src0
);
1449 offset
-= ctx
->abi
->base_inline_push_consts
;
1451 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1452 return ac_build_gather_values(&ctx
->ac
,
1453 ctx
->abi
->inline_push_consts
+ offset
,
1458 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->abi
->push_constants
, &addr
, 1, "");
1460 if (instr
->dest
.ssa
.bit_size
== 8) {
1461 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1462 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1463 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1464 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1466 LLVMValueRef params
[3];
1467 if (load_dwords
> 1) {
1468 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1469 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1470 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1472 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1473 params
[0] = ctx
->ac
.i32_0
;
1477 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1479 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1480 if (instr
->dest
.ssa
.num_components
> 1)
1481 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1483 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1484 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1485 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1486 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1487 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1488 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1489 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1490 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1491 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1492 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1493 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1494 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1495 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1496 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1497 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1498 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1499 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1502 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1504 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1507 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1508 const nir_intrinsic_instr
*instr
)
1510 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1512 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1515 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1517 uint32_t new_mask
= 0;
1518 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1519 if (mask
& (1u << i
))
1520 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1524 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1525 unsigned start
, unsigned count
)
1527 LLVMValueRef mask
[] = {
1528 ctx
->i32_0
, ctx
->i32_1
,
1529 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1531 unsigned src_elements
= ac_get_llvm_num_components(src
);
1533 if (count
== src_elements
) {
1536 } else if (count
== 1) {
1537 assert(start
< src_elements
);
1538 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1540 assert(start
+ count
<= src_elements
);
1542 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1543 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1547 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1548 enum gl_access_qualifier access
,
1549 bool may_store_unaligned
,
1550 bool writeonly_memory
)
1552 unsigned cache_policy
= 0;
1554 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1555 * store opcodes not aligned to a dword are affected. The only way to
1556 * get unaligned stores is through shader images.
1558 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1559 /* If this is write-only, don't keep data in L1 to prevent
1560 * evicting L1 cache lines that may be needed by other
1564 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1565 cache_policy
|= ac_glc
;
1568 return cache_policy
;
1571 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1572 nir_intrinsic_instr
*instr
)
1574 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1575 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1576 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1577 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1578 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1579 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1581 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1582 get_src(ctx
, instr
->src
[1]), true);
1583 LLVMValueRef base_data
= src_data
;
1584 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1585 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1589 LLVMValueRef data
, offset
;
1590 LLVMTypeRef data_type
;
1592 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1594 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1595 * writes into a 2-element and a 1-element write. */
1597 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1598 writemask
|= 1 << (start
+ 2);
1601 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1603 /* we can only store 4 DWords at the same time.
1604 * can only happen for 64 Bit vectors. */
1605 if (num_bytes
> 16) {
1606 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1611 /* check alignment of 16 Bit stores */
1612 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1613 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1617 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1619 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1620 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1622 if (num_bytes
== 1) {
1623 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1624 offset
, ctx
->ac
.i32_0
,
1625 cache_policy
& ac_glc
,
1627 } else if (num_bytes
== 2) {
1628 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1629 offset
, ctx
->ac
.i32_0
,
1630 cache_policy
& ac_glc
,
1633 int num_channels
= num_bytes
/ 4;
1635 switch (num_bytes
) {
1636 case 16: /* v4f32 */
1637 data_type
= ctx
->ac
.v4f32
;
1639 case 12: /* v3f32 */
1640 data_type
= ctx
->ac
.v3f32
;
1643 data_type
= ctx
->ac
.v2f32
;
1646 data_type
= ctx
->ac
.f32
;
1649 unreachable("Malformed vector store.");
1651 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1653 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1654 num_channels
, offset
,
1656 cache_policy
& ac_glc
,
1657 false, writeonly_memory
,
1663 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1664 const nir_intrinsic_instr
*instr
)
1666 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1668 char name
[64], type
[8];
1669 LLVMValueRef params
[6];
1672 switch (instr
->intrinsic
) {
1673 case nir_intrinsic_ssbo_atomic_add
:
1676 case nir_intrinsic_ssbo_atomic_imin
:
1679 case nir_intrinsic_ssbo_atomic_umin
:
1682 case nir_intrinsic_ssbo_atomic_imax
:
1685 case nir_intrinsic_ssbo_atomic_umax
:
1688 case nir_intrinsic_ssbo_atomic_and
:
1691 case nir_intrinsic_ssbo_atomic_or
:
1694 case nir_intrinsic_ssbo_atomic_xor
:
1697 case nir_intrinsic_ssbo_atomic_exchange
:
1700 case nir_intrinsic_ssbo_atomic_comp_swap
:
1707 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1708 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1710 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1711 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1712 get_src(ctx
, instr
->src
[0]),
1715 if (HAVE_LLVM
>= 0x900) {
1716 /* XXX: The new raw/struct atomic intrinsics are buggy with
1717 * LLVM 8, see r358579.
1719 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1720 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1721 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1723 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1724 snprintf(name
, sizeof(name
),
1725 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1727 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1728 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1729 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1731 assert(return_type
== ctx
->ac
.i32
);
1732 snprintf(name
, sizeof(name
),
1733 "llvm.amdgcn.buffer.atomic.%s", op
);
1736 return ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1740 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1741 const nir_intrinsic_instr
*instr
)
1743 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1744 int num_components
= instr
->num_components
;
1745 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1746 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1748 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1749 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1750 get_src(ctx
, instr
->src
[0]), false);
1751 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1753 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1754 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1756 LLVMValueRef results
[4];
1757 for (int i
= 0; i
< num_components
;) {
1758 int num_elems
= num_components
- i
;
1759 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1761 if (num_elems
* elem_size_bytes
> 16)
1762 num_elems
= 16 / elem_size_bytes
;
1763 int load_bytes
= num_elems
* elem_size_bytes
;
1765 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1769 if (load_bytes
== 1) {
1770 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1775 cache_policy
& ac_glc
);
1776 } else if (load_bytes
== 2) {
1777 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1782 cache_policy
& ac_glc
);
1784 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1786 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1787 vindex
, offset
, immoffset
, 0,
1788 cache_policy
& ac_glc
, 0,
1792 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1793 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1794 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1796 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1797 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1799 for (unsigned j
= 0; j
< num_elems
; j
++) {
1800 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1805 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1808 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1809 const nir_intrinsic_instr
*instr
)
1812 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1813 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1814 int num_components
= instr
->num_components
;
1816 if (ctx
->abi
->load_ubo
)
1817 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1819 if (instr
->dest
.ssa
.bit_size
== 64)
1820 num_components
*= 2;
1822 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1823 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1824 LLVMValueRef results
[num_components
];
1825 for (unsigned i
= 0; i
< num_components
; ++i
) {
1826 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1829 if (load_bytes
== 1) {
1830 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1837 assert(load_bytes
== 2);
1838 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1846 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1848 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1849 NULL
, 0, false, false, true, true);
1851 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1854 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1855 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1859 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1860 bool vs_in
, unsigned *vertex_index_out
,
1861 LLVMValueRef
*vertex_index_ref
,
1862 unsigned *const_out
, LLVMValueRef
*indir_out
)
1864 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1865 nir_deref_path path
;
1866 unsigned idx_lvl
= 1;
1868 nir_deref_path_init(&path
, instr
, NULL
);
1870 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1871 if (vertex_index_ref
) {
1872 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1873 if (vertex_index_out
)
1874 *vertex_index_out
= 0;
1876 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1881 uint32_t const_offset
= 0;
1882 LLVMValueRef offset
= NULL
;
1884 if (var
->data
.compact
) {
1885 assert(instr
->deref_type
== nir_deref_type_array
);
1886 const_offset
= nir_src_as_uint(instr
->arr
.index
);
1890 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1891 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1892 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1893 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1895 for (unsigned i
= 0; i
< index
; i
++) {
1896 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1897 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1899 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1900 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1901 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1902 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1904 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1908 unreachable("Uhandled deref type in get_deref_instr_offset");
1912 nir_deref_path_finish(&path
);
1914 if (const_offset
&& offset
)
1915 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1916 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1919 *const_out
= const_offset
;
1920 *indir_out
= offset
;
1923 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1924 nir_intrinsic_instr
*instr
,
1927 LLVMValueRef result
;
1928 LLVMValueRef vertex_index
= NULL
;
1929 LLVMValueRef indir_index
= NULL
;
1930 unsigned const_index
= 0;
1932 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1934 unsigned location
= var
->data
.location
;
1935 unsigned driver_location
= var
->data
.driver_location
;
1936 const bool is_patch
= var
->data
.patch
;
1937 const bool is_compact
= var
->data
.compact
;
1939 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1940 false, NULL
, is_patch
? NULL
: &vertex_index
,
1941 &const_index
, &indir_index
);
1943 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1945 LLVMTypeRef src_component_type
;
1946 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1947 src_component_type
= LLVMGetElementType(dest_type
);
1949 src_component_type
= dest_type
;
1951 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1952 vertex_index
, indir_index
,
1953 const_index
, location
, driver_location
,
1954 var
->data
.location_frac
,
1955 instr
->num_components
,
1956 is_patch
, is_compact
, load_inputs
);
1957 if (instr
->dest
.ssa
.bit_size
== 16) {
1958 result
= ac_to_integer(&ctx
->ac
, result
);
1959 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1961 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1965 type_scalar_size_bytes(const struct glsl_type
*type
)
1967 assert(glsl_type_is_vector_or_scalar(type
) ||
1968 glsl_type_is_matrix(type
));
1969 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1972 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1973 nir_intrinsic_instr
*instr
)
1975 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1976 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1978 LLVMValueRef values
[8];
1980 int ve
= instr
->dest
.ssa
.num_components
;
1982 LLVMValueRef indir_index
;
1984 unsigned const_index
;
1985 unsigned stride
= 4;
1986 int mode
= deref
->mode
;
1989 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1990 var
->data
.mode
== nir_var_shader_in
;
1991 idx
= var
->data
.driver_location
;
1992 comp
= var
->data
.location_frac
;
1993 mode
= var
->data
.mode
;
1995 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1996 &const_index
, &indir_index
);
1998 if (var
->data
.compact
) {
2000 const_index
+= comp
;
2005 if (instr
->dest
.ssa
.bit_size
== 64 &&
2006 (deref
->mode
== nir_var_shader_in
||
2007 deref
->mode
== nir_var_shader_out
||
2008 deref
->mode
== nir_var_function_temp
))
2012 case nir_var_shader_in
:
2013 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2014 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2015 return load_tess_varyings(ctx
, instr
, true);
2018 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2019 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2020 LLVMValueRef indir_index
;
2021 unsigned const_index
, vertex_index
;
2022 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2023 &const_index
, &indir_index
);
2025 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2026 var
->data
.driver_location
,
2027 var
->data
.location_frac
,
2028 instr
->num_components
, vertex_index
, const_index
, type
);
2031 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2033 unsigned count
= glsl_count_attribute_slots(
2035 ctx
->stage
== MESA_SHADER_VERTEX
);
2037 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2038 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2039 stride
, false, true);
2041 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2045 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2048 case nir_var_function_temp
:
2049 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2051 unsigned count
= glsl_count_attribute_slots(
2054 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2055 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2056 stride
, true, true);
2058 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2062 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2066 case nir_var_mem_shared
: {
2067 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2068 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2069 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2070 get_def_type(ctx
, &instr
->dest
.ssa
),
2073 case nir_var_shader_out
:
2074 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2075 return load_tess_varyings(ctx
, instr
, false);
2078 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2080 unsigned count
= glsl_count_attribute_slots(
2083 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2084 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2085 stride
, true, true);
2087 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2091 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2092 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2097 case nir_var_mem_global
: {
2098 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2099 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2100 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2101 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2103 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2104 if (stride
!= natural_stride
) {
2105 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2106 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2107 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2109 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2110 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2111 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2112 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2114 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2116 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2117 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2118 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2119 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2124 unreachable("unhandle variable mode");
2126 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2127 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2131 visit_store_var(struct ac_nir_context
*ctx
,
2132 nir_intrinsic_instr
*instr
)
2134 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2135 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2137 LLVMValueRef temp_ptr
, value
;
2140 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2141 int writemask
= instr
->const_index
[0];
2142 LLVMValueRef indir_index
;
2143 unsigned const_index
;
2146 get_deref_offset(ctx
, deref
, false,
2147 NULL
, NULL
, &const_index
, &indir_index
);
2148 idx
= var
->data
.driver_location
;
2149 comp
= var
->data
.location_frac
;
2151 if (var
->data
.compact
) {
2152 const_index
+= comp
;
2157 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2158 (deref
->mode
== nir_var_shader_out
||
2159 deref
->mode
== nir_var_function_temp
)) {
2161 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2162 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2165 writemask
= widen_mask(writemask
, 2);
2168 writemask
= writemask
<< comp
;
2170 switch (deref
->mode
) {
2171 case nir_var_shader_out
:
2173 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2174 LLVMValueRef vertex_index
= NULL
;
2175 LLVMValueRef indir_index
= NULL
;
2176 unsigned const_index
= 0;
2177 const bool is_patch
= var
->data
.patch
;
2179 get_deref_offset(ctx
, deref
, false, NULL
,
2180 is_patch
? NULL
: &vertex_index
,
2181 &const_index
, &indir_index
);
2183 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2184 vertex_index
, indir_index
,
2185 const_index
, src
, writemask
);
2189 for (unsigned chan
= 0; chan
< 8; chan
++) {
2191 if (!(writemask
& (1 << chan
)))
2194 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2196 if (var
->data
.compact
)
2199 unsigned count
= glsl_count_attribute_slots(
2202 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2203 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2204 stride
, true, true);
2206 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2207 value
, indir_index
, "");
2208 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2209 count
, stride
, tmp_vec
);
2212 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2214 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2218 case nir_var_function_temp
:
2219 for (unsigned chan
= 0; chan
< 8; chan
++) {
2220 if (!(writemask
& (1 << chan
)))
2223 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2225 unsigned count
= glsl_count_attribute_slots(
2228 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2229 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2232 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2233 value
, indir_index
, "");
2234 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2237 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2239 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2244 case nir_var_mem_global
:
2245 case nir_var_mem_shared
: {
2246 int writemask
= instr
->const_index
[0];
2247 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2248 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2250 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2251 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2252 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2254 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2255 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2256 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2258 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2259 stride
== natural_stride
) {
2260 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2261 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2262 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2264 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2265 LLVMGetElementType(LLVMTypeOf(address
)), "");
2266 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2268 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2269 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2270 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2271 for (unsigned chan
= 0; chan
< 4; chan
++) {
2272 if (!(writemask
& (1 << chan
)))
2275 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2277 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2278 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2280 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2281 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2282 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2293 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2296 case GLSL_SAMPLER_DIM_BUF
:
2298 case GLSL_SAMPLER_DIM_1D
:
2299 return array
? 2 : 1;
2300 case GLSL_SAMPLER_DIM_2D
:
2301 return array
? 3 : 2;
2302 case GLSL_SAMPLER_DIM_MS
:
2303 return array
? 4 : 3;
2304 case GLSL_SAMPLER_DIM_3D
:
2305 case GLSL_SAMPLER_DIM_CUBE
:
2307 case GLSL_SAMPLER_DIM_RECT
:
2308 case GLSL_SAMPLER_DIM_SUBPASS
:
2310 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2318 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2319 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2320 LLVMValueRef coord_z
,
2321 LLVMValueRef sample_index
,
2322 LLVMValueRef fmask_desc_ptr
)
2324 unsigned sample_chan
= coord_z
? 3 : 2;
2325 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2326 addr
[sample_chan
] = sample_index
;
2328 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2329 return addr
[sample_chan
];
2332 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2334 assert(instr
->src
[0].is_ssa
);
2335 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2338 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2339 const nir_intrinsic_instr
*instr
,
2340 enum ac_descriptor_type desc_type
,
2343 nir_deref_instr
*deref_instr
=
2344 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2345 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2347 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2350 static void get_image_coords(struct ac_nir_context
*ctx
,
2351 const nir_intrinsic_instr
*instr
,
2352 struct ac_image_args
*args
,
2353 enum glsl_sampler_dim dim
,
2356 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2357 LLVMValueRef masks
[] = {
2358 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2359 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2361 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2364 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2365 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2366 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2367 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2368 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2369 count
= image_type_to_components_count(dim
, is_array
);
2371 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2372 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2373 LLVMValueRef fmask_load_address
[3];
2376 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2377 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2379 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2381 fmask_load_address
[2] = NULL
;
2383 for (chan
= 0; chan
< 2; ++chan
)
2384 fmask_load_address
[chan
] =
2385 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2386 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2387 ctx
->ac
.i32
, ""), "");
2388 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2390 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2391 fmask_load_address
[0],
2392 fmask_load_address
[1],
2393 fmask_load_address
[2],
2395 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2396 AC_DESC_FMASK
, &instr
->instr
, false, false));
2398 if (count
== 1 && !gfx9_1d
) {
2399 if (instr
->src
[1].ssa
->num_components
)
2400 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2402 args
->coords
[0] = src0
;
2407 for (chan
= 0; chan
< count
; ++chan
) {
2408 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2411 for (chan
= 0; chan
< 2; ++chan
) {
2412 args
->coords
[chan
] = LLVMBuildAdd(
2413 ctx
->ac
.builder
, args
->coords
[chan
],
2415 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2416 ctx
->ac
.i32
, ""), "");
2418 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2419 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2425 args
->coords
[2] = args
->coords
[1];
2426 args
->coords
[1] = ctx
->ac
.i32_0
;
2428 args
->coords
[1] = ctx
->ac
.i32_0
;
2433 args
->coords
[count
] = sample_index
;
2439 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2440 const nir_intrinsic_instr
*instr
,
2441 bool write
, bool atomic
)
2443 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2444 if (ctx
->abi
->gfx9_stride_size_workaround
||
2445 (ctx
->abi
->gfx9_stride_size_workaround_for_atomic
&& atomic
)) {
2446 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2447 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2448 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2450 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2451 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2452 elem_count
, stride
, "");
2454 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2455 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2460 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2461 const nir_intrinsic_instr
*instr
,
2466 enum glsl_sampler_dim dim
;
2467 enum gl_access_qualifier access
;
2470 dim
= nir_intrinsic_image_dim(instr
);
2471 access
= nir_intrinsic_access(instr
);
2472 is_array
= nir_intrinsic_image_array(instr
);
2474 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2475 const struct glsl_type
*type
= image_deref
->type
;
2476 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2477 dim
= glsl_get_sampler_dim(type
);
2478 access
= var
->data
.image
.access
;
2479 is_array
= glsl_sampler_type_is_array(type
);
2482 struct ac_image_args args
= {};
2484 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2486 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2487 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2488 unsigned num_channels
= util_last_bit(mask
);
2489 LLVMValueRef rsrc
, vindex
;
2491 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false, false);
2492 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2495 /* TODO: set "can_speculate" when OpenGL needs it. */
2496 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2497 ctx
->ac
.i32_0
, num_channels
,
2498 !!(args
.cache_policy
& ac_glc
),
2500 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2502 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2503 res
= ac_to_integer(&ctx
->ac
, res
);
2505 args
.opcode
= ac_image_load
;
2506 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2507 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2508 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2510 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2512 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2517 static void visit_image_store(struct ac_nir_context
*ctx
,
2518 nir_intrinsic_instr
*instr
,
2523 enum glsl_sampler_dim dim
;
2524 enum gl_access_qualifier access
;
2527 dim
= nir_intrinsic_image_dim(instr
);
2528 access
= nir_intrinsic_access(instr
);
2529 is_array
= nir_intrinsic_image_array(instr
);
2531 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2532 const struct glsl_type
*type
= image_deref
->type
;
2533 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2534 dim
= glsl_get_sampler_dim(type
);
2535 access
= var
->data
.image
.access
;
2536 is_array
= glsl_sampler_type_is_array(type
);
2539 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2540 struct ac_image_args args
= {};
2542 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2544 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2545 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true, false);
2546 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2547 unsigned src_channels
= ac_get_llvm_num_components(src
);
2548 LLVMValueRef vindex
;
2550 if (src_channels
== 3)
2551 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2553 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2554 get_src(ctx
, instr
->src
[1]),
2557 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2558 ctx
->ac
.i32_0
, src_channels
,
2559 args
.cache_policy
& ac_glc
, false,
2562 args
.opcode
= ac_image_store
;
2563 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2564 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2565 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2566 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2569 ac_build_image_opcode(&ctx
->ac
, &args
);
2574 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2575 const nir_intrinsic_instr
*instr
,
2578 LLVMValueRef params
[7];
2579 int param_count
= 0;
2581 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2582 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2583 const char *atomic_name
;
2584 char intrinsic_name
[64];
2585 enum ac_atomic_op atomic_subop
;
2586 MAYBE_UNUSED
int length
;
2588 enum glsl_sampler_dim dim
;
2589 bool is_unsigned
= false;
2592 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_min
||
2593 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_max
) {
2594 const GLenum format
= nir_intrinsic_format(instr
);
2595 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2596 is_unsigned
= format
== GL_R32UI
;
2598 dim
= nir_intrinsic_image_dim(instr
);
2599 is_array
= nir_intrinsic_image_array(instr
);
2601 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2602 is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2603 dim
= glsl_get_sampler_dim(type
);
2604 is_array
= glsl_sampler_type_is_array(type
);
2607 switch (instr
->intrinsic
) {
2608 case nir_intrinsic_bindless_image_atomic_add
:
2609 case nir_intrinsic_image_deref_atomic_add
:
2610 atomic_name
= "add";
2611 atomic_subop
= ac_atomic_add
;
2613 case nir_intrinsic_bindless_image_atomic_min
:
2614 case nir_intrinsic_image_deref_atomic_min
:
2615 atomic_name
= is_unsigned
? "umin" : "smin";
2616 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2618 case nir_intrinsic_bindless_image_atomic_max
:
2619 case nir_intrinsic_image_deref_atomic_max
:
2620 atomic_name
= is_unsigned
? "umax" : "smax";
2621 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2623 case nir_intrinsic_bindless_image_atomic_and
:
2624 case nir_intrinsic_image_deref_atomic_and
:
2625 atomic_name
= "and";
2626 atomic_subop
= ac_atomic_and
;
2628 case nir_intrinsic_bindless_image_atomic_or
:
2629 case nir_intrinsic_image_deref_atomic_or
:
2631 atomic_subop
= ac_atomic_or
;
2633 case nir_intrinsic_bindless_image_atomic_xor
:
2634 case nir_intrinsic_image_deref_atomic_xor
:
2635 atomic_name
= "xor";
2636 atomic_subop
= ac_atomic_xor
;
2638 case nir_intrinsic_bindless_image_atomic_exchange
:
2639 case nir_intrinsic_image_deref_atomic_exchange
:
2640 atomic_name
= "swap";
2641 atomic_subop
= ac_atomic_swap
;
2643 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2644 case nir_intrinsic_image_deref_atomic_comp_swap
:
2645 atomic_name
= "cmpswap";
2646 atomic_subop
= 0; /* not used */
2653 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2654 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2656 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2657 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true, true);
2658 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2659 ctx
->ac
.i32_0
, ""); /* vindex */
2660 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2661 if (HAVE_LLVM
>= 0x900) {
2662 /* XXX: The new raw/struct atomic intrinsics are buggy
2663 * with LLVM 8, see r358579.
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
, dim
, is_array
);
2688 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2689 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2691 return ac_build_image_opcode(&ctx
->ac
, &args
);
2695 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2696 const nir_intrinsic_instr
*instr
,
2699 enum glsl_sampler_dim dim
;
2702 dim
= nir_intrinsic_image_dim(instr
);
2703 is_array
= nir_intrinsic_image_array(instr
);
2705 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2706 dim
= glsl_get_sampler_dim(type
);
2707 is_array
= glsl_sampler_type_is_array(type
);
2710 struct ac_image_args args
= { 0 };
2711 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, dim
, is_array
);
2713 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2714 args
.opcode
= ac_image_get_resinfo
;
2715 args
.lod
= ctx
->ac
.i32_0
;
2716 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2718 return ac_build_image_opcode(&ctx
->ac
, &args
);
2721 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2722 const nir_intrinsic_instr
*instr
,
2727 enum glsl_sampler_dim dim
;
2730 dim
= nir_intrinsic_image_dim(instr
);
2731 is_array
= nir_intrinsic_image_array(instr
);
2733 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2734 dim
= glsl_get_sampler_dim(type
);
2735 is_array
= glsl_sampler_type_is_array(type
);
2738 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2739 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2741 struct ac_image_args args
= { 0 };
2743 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2745 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2746 args
.opcode
= ac_image_get_resinfo
;
2747 args
.lod
= ctx
->ac
.i32_0
;
2748 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2750 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2752 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2754 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2755 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2756 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2757 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2758 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2760 if (ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2761 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2762 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2769 static void emit_membar(struct ac_llvm_context
*ac
,
2770 const nir_intrinsic_instr
*instr
)
2772 unsigned waitcnt
= NOOP_WAITCNT
;
2774 switch (instr
->intrinsic
) {
2775 case nir_intrinsic_memory_barrier
:
2776 case nir_intrinsic_group_memory_barrier
:
2777 waitcnt
&= VM_CNT
& LGKM_CNT
;
2779 case nir_intrinsic_memory_barrier_atomic_counter
:
2780 case nir_intrinsic_memory_barrier_buffer
:
2781 case nir_intrinsic_memory_barrier_image
:
2784 case nir_intrinsic_memory_barrier_shared
:
2785 waitcnt
&= LGKM_CNT
;
2790 if (waitcnt
!= NOOP_WAITCNT
)
2791 ac_build_waitcnt(ac
, waitcnt
);
2794 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2796 /* GFX6 only (thanks to a hw bug workaround):
2797 * The real barrier instruction isn’t needed, because an entire patch
2798 * always fits into a single wave.
2800 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
2801 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2804 ac_build_s_barrier(ac
);
2807 static void emit_discard(struct ac_nir_context
*ctx
,
2808 const nir_intrinsic_instr
*instr
)
2812 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2813 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2814 get_src(ctx
, instr
->src
[0]),
2817 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2818 cond
= ctx
->ac
.i1false
;
2821 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2825 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2827 LLVMValueRef result
;
2828 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2829 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2830 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2832 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2836 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2838 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2839 LLVMValueRef result
;
2840 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2841 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2842 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2844 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2849 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2851 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2852 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2853 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2855 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2860 visit_first_invocation(struct ac_nir_context
*ctx
)
2862 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2864 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2865 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2866 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2868 ctx
->ac
.i64
, args
, 2,
2869 AC_FUNC_ATTR_NOUNWIND
|
2870 AC_FUNC_ATTR_READNONE
);
2872 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2876 visit_load_shared(struct ac_nir_context
*ctx
,
2877 const nir_intrinsic_instr
*instr
)
2879 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2881 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2883 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2884 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2885 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2886 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2889 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2890 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2894 visit_store_shared(struct ac_nir_context
*ctx
,
2895 const nir_intrinsic_instr
*instr
)
2897 LLVMValueRef derived_ptr
, data
,index
;
2898 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2900 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2901 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2903 int writemask
= nir_intrinsic_write_mask(instr
);
2904 for (int chan
= 0; chan
< 4; chan
++) {
2905 if (!(writemask
& (1 << chan
))) {
2908 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2909 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2910 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2911 LLVMBuildStore(builder
, data
, derived_ptr
);
2915 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2916 const nir_intrinsic_instr
*instr
,
2917 LLVMValueRef ptr
, int src_idx
)
2919 LLVMValueRef result
;
2920 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2922 const char *sync_scope
= HAVE_LLVM
>= 0x0900 ? "workgroup-one-as" : "workgroup";
2924 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2925 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2926 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2927 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
2928 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2930 LLVMAtomicRMWBinOp op
;
2931 switch (instr
->intrinsic
) {
2932 case nir_intrinsic_shared_atomic_add
:
2933 case nir_intrinsic_deref_atomic_add
:
2934 op
= LLVMAtomicRMWBinOpAdd
;
2936 case nir_intrinsic_shared_atomic_umin
:
2937 case nir_intrinsic_deref_atomic_umin
:
2938 op
= LLVMAtomicRMWBinOpUMin
;
2940 case nir_intrinsic_shared_atomic_umax
:
2941 case nir_intrinsic_deref_atomic_umax
:
2942 op
= LLVMAtomicRMWBinOpUMax
;
2944 case nir_intrinsic_shared_atomic_imin
:
2945 case nir_intrinsic_deref_atomic_imin
:
2946 op
= LLVMAtomicRMWBinOpMin
;
2948 case nir_intrinsic_shared_atomic_imax
:
2949 case nir_intrinsic_deref_atomic_imax
:
2950 op
= LLVMAtomicRMWBinOpMax
;
2952 case nir_intrinsic_shared_atomic_and
:
2953 case nir_intrinsic_deref_atomic_and
:
2954 op
= LLVMAtomicRMWBinOpAnd
;
2956 case nir_intrinsic_shared_atomic_or
:
2957 case nir_intrinsic_deref_atomic_or
:
2958 op
= LLVMAtomicRMWBinOpOr
;
2960 case nir_intrinsic_shared_atomic_xor
:
2961 case nir_intrinsic_deref_atomic_xor
:
2962 op
= LLVMAtomicRMWBinOpXor
;
2964 case nir_intrinsic_shared_atomic_exchange
:
2965 case nir_intrinsic_deref_atomic_exchange
:
2966 op
= LLVMAtomicRMWBinOpXchg
;
2972 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
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
= ac_build_ddxy_interp(&ctx
->ac
, 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 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3077 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* nir_src_as_uint(deref_instr
->arr
.index
), false);
3079 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3081 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3082 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3085 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3086 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3087 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3088 LLVMValueRef offset
;
3089 unsigned sidx
= deref_instr
->strct
.index
;
3090 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3091 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_struct_location_offset(deref_instr
->type
, sidx
), false);
3092 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3094 unreachable("Unsupported deref type");
3099 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3100 for (chan
= 0; chan
< 4; chan
++) {
3101 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3102 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3104 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3105 LLVMValueRef v
, attr_number
;
3107 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3109 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3110 interp_param
, ctx
->ac
.v2f32
, "");
3111 LLVMValueRef i
= LLVMBuildExtractElement(
3112 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3113 LLVMValueRef j
= LLVMBuildExtractElement(
3114 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3116 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3117 ctx
->abi
->prim_mask
, i
, j
);
3119 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3120 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3123 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3124 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3127 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3130 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3131 var
->data
.location_frac
);
3134 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3135 nir_intrinsic_instr
*instr
)
3137 LLVMValueRef result
= NULL
;
3139 switch (instr
->intrinsic
) {
3140 case nir_intrinsic_ballot
:
3141 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3143 case nir_intrinsic_read_invocation
:
3144 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3145 get_src(ctx
, instr
->src
[1]));
3147 case nir_intrinsic_read_first_invocation
:
3148 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3150 case nir_intrinsic_load_subgroup_invocation
:
3151 result
= ac_get_thread_id(&ctx
->ac
);
3153 case nir_intrinsic_load_work_group_id
: {
3154 LLVMValueRef values
[3];
3156 for (int i
= 0; i
< 3; i
++) {
3157 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3158 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3161 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3164 case nir_intrinsic_load_base_vertex
:
3165 case nir_intrinsic_load_first_vertex
:
3166 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3168 case nir_intrinsic_load_local_group_size
:
3169 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3171 case nir_intrinsic_load_vertex_id
:
3172 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3173 ctx
->abi
->base_vertex
, "");
3175 case nir_intrinsic_load_vertex_id_zero_base
: {
3176 result
= ctx
->abi
->vertex_id
;
3179 case nir_intrinsic_load_local_invocation_id
: {
3180 result
= ctx
->abi
->local_invocation_ids
;
3183 case nir_intrinsic_load_base_instance
:
3184 result
= ctx
->abi
->start_instance
;
3186 case nir_intrinsic_load_draw_id
:
3187 result
= ctx
->abi
->draw_id
;
3189 case nir_intrinsic_load_view_index
:
3190 result
= ctx
->abi
->view_index
;
3192 case nir_intrinsic_load_invocation_id
:
3193 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3194 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3196 result
= ctx
->abi
->gs_invocation_id
;
3198 case nir_intrinsic_load_primitive_id
:
3199 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3200 result
= ctx
->abi
->gs_prim_id
;
3201 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3202 result
= ctx
->abi
->tcs_patch_id
;
3203 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3204 result
= ctx
->abi
->tes_patch_id
;
3206 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3208 case nir_intrinsic_load_sample_id
:
3209 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3211 case nir_intrinsic_load_sample_pos
:
3212 result
= load_sample_pos(ctx
);
3214 case nir_intrinsic_load_sample_mask_in
:
3215 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3217 case nir_intrinsic_load_frag_coord
: {
3218 LLVMValueRef values
[4] = {
3219 ctx
->abi
->frag_pos
[0],
3220 ctx
->abi
->frag_pos
[1],
3221 ctx
->abi
->frag_pos
[2],
3222 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3224 result
= ac_to_integer(&ctx
->ac
,
3225 ac_build_gather_values(&ctx
->ac
, values
, 4));
3228 case nir_intrinsic_load_front_face
:
3229 result
= ctx
->abi
->front_face
;
3231 case nir_intrinsic_load_helper_invocation
:
3232 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3234 case nir_intrinsic_load_instance_id
:
3235 result
= ctx
->abi
->instance_id
;
3237 case nir_intrinsic_load_num_work_groups
:
3238 result
= ctx
->abi
->num_work_groups
;
3240 case nir_intrinsic_load_local_invocation_index
:
3241 result
= visit_load_local_invocation_index(ctx
);
3243 case nir_intrinsic_load_subgroup_id
:
3244 result
= visit_load_subgroup_id(ctx
);
3246 case nir_intrinsic_load_num_subgroups
:
3247 result
= visit_load_num_subgroups(ctx
);
3249 case nir_intrinsic_first_invocation
:
3250 result
= visit_first_invocation(ctx
);
3252 case nir_intrinsic_load_push_constant
:
3253 result
= visit_load_push_constant(ctx
, instr
);
3255 case nir_intrinsic_vulkan_resource_index
: {
3256 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3257 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3258 unsigned binding
= nir_intrinsic_binding(instr
);
3260 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3264 case nir_intrinsic_vulkan_resource_reindex
:
3265 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3267 case nir_intrinsic_store_ssbo
:
3268 visit_store_ssbo(ctx
, instr
);
3270 case nir_intrinsic_load_ssbo
:
3271 result
= visit_load_buffer(ctx
, instr
);
3273 case nir_intrinsic_ssbo_atomic_add
:
3274 case nir_intrinsic_ssbo_atomic_imin
:
3275 case nir_intrinsic_ssbo_atomic_umin
:
3276 case nir_intrinsic_ssbo_atomic_imax
:
3277 case nir_intrinsic_ssbo_atomic_umax
:
3278 case nir_intrinsic_ssbo_atomic_and
:
3279 case nir_intrinsic_ssbo_atomic_or
:
3280 case nir_intrinsic_ssbo_atomic_xor
:
3281 case nir_intrinsic_ssbo_atomic_exchange
:
3282 case nir_intrinsic_ssbo_atomic_comp_swap
:
3283 result
= visit_atomic_ssbo(ctx
, instr
);
3285 case nir_intrinsic_load_ubo
:
3286 result
= visit_load_ubo_buffer(ctx
, instr
);
3288 case nir_intrinsic_get_buffer_size
:
3289 result
= visit_get_buffer_size(ctx
, instr
);
3291 case nir_intrinsic_load_deref
:
3292 result
= visit_load_var(ctx
, instr
);
3294 case nir_intrinsic_store_deref
:
3295 visit_store_var(ctx
, instr
);
3297 case nir_intrinsic_load_shared
:
3298 result
= visit_load_shared(ctx
, instr
);
3300 case nir_intrinsic_store_shared
:
3301 visit_store_shared(ctx
, instr
);
3303 case nir_intrinsic_bindless_image_samples
:
3304 result
= visit_image_samples(ctx
, instr
, true);
3306 case nir_intrinsic_image_deref_samples
:
3307 result
= visit_image_samples(ctx
, instr
, false);
3309 case nir_intrinsic_bindless_image_load
:
3310 result
= visit_image_load(ctx
, instr
, true);
3312 case nir_intrinsic_image_deref_load
:
3313 result
= visit_image_load(ctx
, instr
, false);
3315 case nir_intrinsic_bindless_image_store
:
3316 visit_image_store(ctx
, instr
, true);
3318 case nir_intrinsic_image_deref_store
:
3319 visit_image_store(ctx
, instr
, false);
3321 case nir_intrinsic_bindless_image_atomic_add
:
3322 case nir_intrinsic_bindless_image_atomic_min
:
3323 case nir_intrinsic_bindless_image_atomic_max
:
3324 case nir_intrinsic_bindless_image_atomic_and
:
3325 case nir_intrinsic_bindless_image_atomic_or
:
3326 case nir_intrinsic_bindless_image_atomic_xor
:
3327 case nir_intrinsic_bindless_image_atomic_exchange
:
3328 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3329 result
= visit_image_atomic(ctx
, instr
, true);
3331 case nir_intrinsic_image_deref_atomic_add
:
3332 case nir_intrinsic_image_deref_atomic_min
:
3333 case nir_intrinsic_image_deref_atomic_max
:
3334 case nir_intrinsic_image_deref_atomic_and
:
3335 case nir_intrinsic_image_deref_atomic_or
:
3336 case nir_intrinsic_image_deref_atomic_xor
:
3337 case nir_intrinsic_image_deref_atomic_exchange
:
3338 case nir_intrinsic_image_deref_atomic_comp_swap
:
3339 result
= visit_image_atomic(ctx
, instr
, false);
3341 case nir_intrinsic_bindless_image_size
:
3342 result
= visit_image_size(ctx
, instr
, true);
3344 case nir_intrinsic_image_deref_size
:
3345 result
= visit_image_size(ctx
, instr
, false);
3347 case nir_intrinsic_shader_clock
:
3348 result
= ac_build_shader_clock(&ctx
->ac
);
3350 case nir_intrinsic_discard
:
3351 case nir_intrinsic_discard_if
:
3352 emit_discard(ctx
, instr
);
3354 case nir_intrinsic_memory_barrier
:
3355 case nir_intrinsic_group_memory_barrier
:
3356 case nir_intrinsic_memory_barrier_atomic_counter
:
3357 case nir_intrinsic_memory_barrier_buffer
:
3358 case nir_intrinsic_memory_barrier_image
:
3359 case nir_intrinsic_memory_barrier_shared
:
3360 emit_membar(&ctx
->ac
, instr
);
3362 case nir_intrinsic_barrier
:
3363 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3365 case nir_intrinsic_shared_atomic_add
:
3366 case nir_intrinsic_shared_atomic_imin
:
3367 case nir_intrinsic_shared_atomic_umin
:
3368 case nir_intrinsic_shared_atomic_imax
:
3369 case nir_intrinsic_shared_atomic_umax
:
3370 case nir_intrinsic_shared_atomic_and
:
3371 case nir_intrinsic_shared_atomic_or
:
3372 case nir_intrinsic_shared_atomic_xor
:
3373 case nir_intrinsic_shared_atomic_exchange
:
3374 case nir_intrinsic_shared_atomic_comp_swap
: {
3375 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3376 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3379 case nir_intrinsic_deref_atomic_add
:
3380 case nir_intrinsic_deref_atomic_imin
:
3381 case nir_intrinsic_deref_atomic_umin
:
3382 case nir_intrinsic_deref_atomic_imax
:
3383 case nir_intrinsic_deref_atomic_umax
:
3384 case nir_intrinsic_deref_atomic_and
:
3385 case nir_intrinsic_deref_atomic_or
:
3386 case nir_intrinsic_deref_atomic_xor
:
3387 case nir_intrinsic_deref_atomic_exchange
:
3388 case nir_intrinsic_deref_atomic_comp_swap
: {
3389 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3390 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3393 case nir_intrinsic_interp_deref_at_centroid
:
3394 case nir_intrinsic_interp_deref_at_sample
:
3395 case nir_intrinsic_interp_deref_at_offset
:
3396 result
= visit_interp(ctx
, instr
);
3398 case nir_intrinsic_emit_vertex
:
3399 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3401 case nir_intrinsic_end_primitive
:
3402 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3404 case nir_intrinsic_load_tess_coord
:
3405 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3407 case nir_intrinsic_load_tess_level_outer
:
3408 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3410 case nir_intrinsic_load_tess_level_inner
:
3411 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3413 case nir_intrinsic_load_patch_vertices_in
:
3414 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3416 case nir_intrinsic_vote_all
: {
3417 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3418 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3421 case nir_intrinsic_vote_any
: {
3422 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3423 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3426 case nir_intrinsic_shuffle
:
3427 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3428 get_src(ctx
, instr
->src
[1]));
3430 case nir_intrinsic_reduce
:
3431 result
= ac_build_reduce(&ctx
->ac
,
3432 get_src(ctx
, instr
->src
[0]),
3433 instr
->const_index
[0],
3434 instr
->const_index
[1]);
3436 case nir_intrinsic_inclusive_scan
:
3437 result
= ac_build_inclusive_scan(&ctx
->ac
,
3438 get_src(ctx
, instr
->src
[0]),
3439 instr
->const_index
[0]);
3441 case nir_intrinsic_exclusive_scan
:
3442 result
= ac_build_exclusive_scan(&ctx
->ac
,
3443 get_src(ctx
, instr
->src
[0]),
3444 instr
->const_index
[0]);
3446 case nir_intrinsic_quad_broadcast
: {
3447 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
3448 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3449 lane
, lane
, lane
, lane
);
3452 case nir_intrinsic_quad_swap_horizontal
:
3453 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3455 case nir_intrinsic_quad_swap_vertical
:
3456 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3458 case nir_intrinsic_quad_swap_diagonal
:
3459 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3461 case nir_intrinsic_quad_swizzle_amd
: {
3462 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3463 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3464 mask
& 0x3, (mask
>> 2) & 0x3,
3465 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
3468 case nir_intrinsic_masked_swizzle_amd
: {
3469 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3470 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
3473 case nir_intrinsic_write_invocation_amd
:
3474 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3475 get_src(ctx
, instr
->src
[1]),
3476 get_src(ctx
, instr
->src
[2]));
3478 case nir_intrinsic_mbcnt_amd
:
3479 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3482 fprintf(stderr
, "Unknown intrinsic: ");
3483 nir_print_instr(&instr
->instr
, stderr
);
3484 fprintf(stderr
, "\n");
3488 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3492 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3493 unsigned base_index
,
3494 unsigned constant_index
,
3495 LLVMValueRef dynamic_index
)
3497 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3498 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3499 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3501 /* Bindless uniforms are 64bit so multiple index by 8 */
3502 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3503 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3505 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3507 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3508 NULL
, 0, false, false, true, true);
3510 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3513 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3514 nir_deref_instr
*deref_instr
,
3515 enum ac_descriptor_type desc_type
,
3516 const nir_instr
*instr
,
3517 bool image
, bool write
)
3519 LLVMValueRef index
= NULL
;
3520 unsigned constant_index
= 0;
3521 unsigned descriptor_set
;
3522 unsigned base_index
;
3523 bool bindless
= false;
3528 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3531 index
= get_src(ctx
, img_instr
->src
[0]);
3533 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3534 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3535 nir_tex_src_sampler_handle
);
3536 if (sampSrcIdx
!= -1) {
3539 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3541 assert(tex_instr
&& !image
);
3542 base_index
= tex_instr
->sampler_index
;
3546 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3547 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3548 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3552 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3553 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
3555 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3557 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3558 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3563 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3566 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3567 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3568 unsigned sidx
= deref_instr
->strct
.index
;
3569 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3570 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3572 unreachable("Unsupported deref type");
3575 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3577 if (deref_instr
->var
->data
.bindless
) {
3578 /* For now just assert on unhandled variable types */
3579 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3581 base_index
= deref_instr
->var
->data
.driver_location
;
3584 index
= index
? index
: ctx
->ac
.i32_0
;
3585 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3586 constant_index
, index
);
3588 base_index
= deref_instr
->var
->data
.binding
;
3591 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3594 constant_index
, index
,
3595 desc_type
, image
, write
, bindless
);
3598 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3601 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3602 * filtering manually. The driver sets img7 to a mask clearing
3603 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3604 * s_and_b32 samp0, samp0, img7
3607 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3609 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3610 LLVMValueRef res
, LLVMValueRef samp
)
3612 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3613 LLVMValueRef img7
, samp0
;
3615 if (ctx
->ac
.chip_class
>= GFX8
)
3618 img7
= LLVMBuildExtractElement(builder
, res
,
3619 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3620 samp0
= LLVMBuildExtractElement(builder
, samp
,
3621 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3622 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3623 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3624 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3627 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3628 nir_tex_instr
*instr
,
3629 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3630 LLVMValueRef
*fmask_ptr
)
3632 nir_deref_instr
*texture_deref_instr
= NULL
;
3633 nir_deref_instr
*sampler_deref_instr
= NULL
;
3636 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3637 switch (instr
->src
[i
].src_type
) {
3638 case nir_tex_src_texture_deref
:
3639 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3641 case nir_tex_src_sampler_deref
:
3642 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3644 case nir_tex_src_plane
:
3645 plane
= nir_src_as_int(instr
->src
[i
].src
);
3652 if (!sampler_deref_instr
)
3653 sampler_deref_instr
= texture_deref_instr
;
3655 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
3658 assert(instr
->op
!= nir_texop_txf_ms
&&
3659 instr
->op
!= nir_texop_samples_identical
);
3660 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
3662 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
3665 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
, false, false);
3668 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3669 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3670 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3672 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3673 instr
->op
== nir_texop_samples_identical
))
3674 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3677 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3680 coord
= ac_to_float(ctx
, coord
);
3681 coord
= ac_build_round(ctx
, coord
);
3682 coord
= ac_to_integer(ctx
, coord
);
3686 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3688 LLVMValueRef result
= NULL
;
3689 struct ac_image_args args
= { 0 };
3690 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3691 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3692 unsigned offset_src
= 0;
3694 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3696 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3697 switch (instr
->src
[i
].src_type
) {
3698 case nir_tex_src_coord
: {
3699 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3700 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3701 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3704 case nir_tex_src_projector
:
3706 case nir_tex_src_comparator
:
3707 if (instr
->is_shadow
)
3708 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3710 case nir_tex_src_offset
:
3711 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3714 case nir_tex_src_bias
:
3715 if (instr
->op
== nir_texop_txb
)
3716 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3718 case nir_tex_src_lod
: {
3719 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
3720 args
.level_zero
= true;
3722 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3725 case nir_tex_src_ms_index
:
3726 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3728 case nir_tex_src_ms_mcs
:
3730 case nir_tex_src_ddx
:
3731 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3733 case nir_tex_src_ddy
:
3734 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3736 case nir_tex_src_texture_offset
:
3737 case nir_tex_src_sampler_offset
:
3738 case nir_tex_src_plane
:
3744 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3745 result
= get_buffer_size(ctx
, args
.resource
, true);
3749 if (instr
->op
== nir_texop_texture_samples
) {
3750 LLVMValueRef res
, samples
, is_msaa
;
3751 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3752 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3753 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3754 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3755 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3756 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3757 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3758 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3759 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3761 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3762 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3763 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3764 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3765 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3767 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3773 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3774 LLVMValueRef offset
[3], pack
;
3775 for (unsigned chan
= 0; chan
< 3; ++chan
)
3776 offset
[chan
] = ctx
->ac
.i32_0
;
3778 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3779 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3780 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3781 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3782 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3784 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3785 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3787 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3788 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3792 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3793 * so the depth comparison value isn't clamped for Z16 and
3794 * Z24 anymore. Do it manually here.
3796 * It's unnecessary if the original texture format was
3797 * Z32_FLOAT, but we don't know that here.
3799 if (args
.compare
&& ctx
->ac
.chip_class
>= GFX8
&& ctx
->abi
->clamp_shadow_reference
)
3800 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3802 /* pack derivatives */
3804 int num_src_deriv_channels
, num_dest_deriv_channels
;
3805 switch (instr
->sampler_dim
) {
3806 case GLSL_SAMPLER_DIM_3D
:
3807 case GLSL_SAMPLER_DIM_CUBE
:
3808 num_src_deriv_channels
= 3;
3809 num_dest_deriv_channels
= 3;
3811 case GLSL_SAMPLER_DIM_2D
:
3813 num_src_deriv_channels
= 2;
3814 num_dest_deriv_channels
= 2;
3816 case GLSL_SAMPLER_DIM_1D
:
3817 num_src_deriv_channels
= 1;
3818 if (ctx
->ac
.chip_class
>= GFX9
) {
3819 num_dest_deriv_channels
= 2;
3821 num_dest_deriv_channels
= 1;
3826 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3827 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3828 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3829 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3830 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3832 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3833 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3834 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3838 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3839 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3840 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3841 if (instr
->coord_components
== 3)
3842 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3843 ac_prepare_cube_coords(&ctx
->ac
,
3844 instr
->op
== nir_texop_txd
, instr
->is_array
,
3845 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3848 /* Texture coordinates fixups */
3849 if (instr
->coord_components
> 1 &&
3850 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3852 instr
->op
!= nir_texop_txf
) {
3853 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3856 if (instr
->coord_components
> 2 &&
3857 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3858 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3859 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3860 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3862 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3863 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3866 if (ctx
->ac
.chip_class
>= GFX9
&&
3867 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3868 instr
->op
!= nir_texop_lod
) {
3869 LLVMValueRef filler
;
3870 if (instr
->op
== nir_texop_txf
)
3871 filler
= ctx
->ac
.i32_0
;
3873 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3875 if (instr
->is_array
)
3876 args
.coords
[2] = args
.coords
[1];
3877 args
.coords
[1] = filler
;
3880 /* Pack sample index */
3881 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3882 args
.coords
[instr
->coord_components
] = sample_index
;
3884 if (instr
->op
== nir_texop_samples_identical
) {
3885 struct ac_image_args txf_args
= { 0 };
3886 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3888 txf_args
.dmask
= 0xf;
3889 txf_args
.resource
= fmask_ptr
;
3890 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3891 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3893 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3894 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3898 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3899 instr
->op
!= nir_texop_txs
) {
3900 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3901 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3902 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3903 instr
->is_array
? args
.coords
[2] : NULL
,
3904 args
.coords
[sample_chan
], fmask_ptr
);
3907 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3908 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3909 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3910 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3911 args
.coords
[i
] = LLVMBuildAdd(
3912 ctx
->ac
.builder
, args
.coords
[i
],
3913 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
3918 /* DMASK was repurposed for GATHER4. 4 components are always
3919 * returned and DMASK works like a swizzle - it selects
3920 * the component to fetch. The only valid DMASK values are
3921 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3922 * (red,red,red,red) etc.) The ISA document doesn't mention
3926 if (instr
->op
== nir_texop_tg4
) {
3927 if (instr
->is_shadow
)
3930 args
.dmask
= 1 << instr
->component
;
3933 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3934 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3935 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3937 if (instr
->op
== nir_texop_query_levels
)
3938 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3939 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3940 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3941 instr
->op
!= nir_texop_tg4
)
3942 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3943 else if (instr
->op
== nir_texop_txs
&&
3944 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3946 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3947 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3948 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3949 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3950 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3951 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3952 instr
->op
== nir_texop_txs
&&
3953 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3955 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3956 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3957 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3959 } else if (instr
->dest
.ssa
.num_components
!= 4)
3960 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3964 assert(instr
->dest
.is_ssa
);
3965 result
= ac_to_integer(&ctx
->ac
, result
);
3966 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3971 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3973 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3974 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3976 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3977 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3980 static void visit_post_phi(struct ac_nir_context
*ctx
,
3981 nir_phi_instr
*instr
,
3982 LLVMValueRef llvm_phi
)
3984 nir_foreach_phi_src(src
, instr
) {
3985 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3986 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3988 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3992 static void phi_post_pass(struct ac_nir_context
*ctx
)
3994 hash_table_foreach(ctx
->phis
, entry
) {
3995 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3996 (LLVMValueRef
)entry
->data
);
4001 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4002 const nir_ssa_undef_instr
*instr
)
4004 unsigned num_components
= instr
->def
.num_components
;
4005 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4008 if (num_components
== 1)
4009 undef
= LLVMGetUndef(type
);
4011 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4013 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4016 static void visit_jump(struct ac_llvm_context
*ctx
,
4017 const nir_jump_instr
*instr
)
4019 switch (instr
->type
) {
4020 case nir_jump_break
:
4021 ac_build_break(ctx
);
4023 case nir_jump_continue
:
4024 ac_build_continue(ctx
);
4027 fprintf(stderr
, "Unknown NIR jump instr: ");
4028 nir_print_instr(&instr
->instr
, stderr
);
4029 fprintf(stderr
, "\n");
4035 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4036 enum glsl_base_type type
)
4040 case GLSL_TYPE_UINT
:
4041 case GLSL_TYPE_BOOL
:
4042 case GLSL_TYPE_SUBROUTINE
:
4044 case GLSL_TYPE_INT8
:
4045 case GLSL_TYPE_UINT8
:
4047 case GLSL_TYPE_INT16
:
4048 case GLSL_TYPE_UINT16
:
4050 case GLSL_TYPE_FLOAT
:
4052 case GLSL_TYPE_FLOAT16
:
4054 case GLSL_TYPE_INT64
:
4055 case GLSL_TYPE_UINT64
:
4057 case GLSL_TYPE_DOUBLE
:
4060 unreachable("unknown GLSL type");
4065 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4066 const struct glsl_type
*type
)
4068 if (glsl_type_is_scalar(type
)) {
4069 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4072 if (glsl_type_is_vector(type
)) {
4073 return LLVMVectorType(
4074 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4075 glsl_get_vector_elements(type
));
4078 if (glsl_type_is_matrix(type
)) {
4079 return LLVMArrayType(
4080 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4081 glsl_get_matrix_columns(type
));
4084 if (glsl_type_is_array(type
)) {
4085 return LLVMArrayType(
4086 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4087 glsl_get_length(type
));
4090 assert(glsl_type_is_struct_or_ifc(type
));
4092 LLVMTypeRef member_types
[glsl_get_length(type
)];
4094 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4096 glsl_to_llvm_type(ac
,
4097 glsl_get_struct_field(type
, i
));
4100 return LLVMStructTypeInContext(ac
->context
, member_types
,
4101 glsl_get_length(type
), false);
4104 static void visit_deref(struct ac_nir_context
*ctx
,
4105 nir_deref_instr
*instr
)
4107 if (instr
->mode
!= nir_var_mem_shared
&&
4108 instr
->mode
!= nir_var_mem_global
)
4111 LLVMValueRef result
= NULL
;
4112 switch(instr
->deref_type
) {
4113 case nir_deref_type_var
: {
4114 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4115 result
= entry
->data
;
4118 case nir_deref_type_struct
:
4119 if (instr
->mode
== nir_var_mem_global
) {
4120 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4121 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4122 instr
->strct
.index
);
4123 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4124 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4126 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4127 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4130 case nir_deref_type_array
:
4131 if (instr
->mode
== nir_var_mem_global
) {
4132 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4133 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4135 if ((glsl_type_is_matrix(parent
->type
) &&
4136 glsl_matrix_type_is_row_major(parent
->type
)) ||
4137 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4138 stride
= type_scalar_size_bytes(parent
->type
);
4141 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4142 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4143 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4145 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4147 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4149 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4150 get_src(ctx
, instr
->arr
.index
));
4153 case nir_deref_type_ptr_as_array
:
4154 if (instr
->mode
== nir_var_mem_global
) {
4155 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4157 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4158 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4159 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4161 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4163 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4165 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4166 get_src(ctx
, instr
->arr
.index
));
4169 case nir_deref_type_cast
: {
4170 result
= get_src(ctx
, instr
->parent
);
4172 /* We can't use the structs from LLVM because the shader
4173 * specifies its own offsets. */
4174 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4175 if (instr
->mode
== nir_var_mem_shared
)
4176 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4178 unsigned address_space
;
4180 switch(instr
->mode
) {
4181 case nir_var_mem_shared
:
4182 address_space
= AC_ADDR_SPACE_LDS
;
4184 case nir_var_mem_global
:
4185 address_space
= AC_ADDR_SPACE_GLOBAL
;
4188 unreachable("Unhandled address space");
4191 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4193 if (LLVMTypeOf(result
) != type
) {
4194 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4195 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4198 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4205 unreachable("Unhandled deref_instr deref type");
4208 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4211 static void visit_cf_list(struct ac_nir_context
*ctx
,
4212 struct exec_list
*list
);
4214 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4216 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4217 nir_foreach_instr(instr
, block
)
4219 switch (instr
->type
) {
4220 case nir_instr_type_alu
:
4221 visit_alu(ctx
, nir_instr_as_alu(instr
));
4223 case nir_instr_type_load_const
:
4224 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4226 case nir_instr_type_intrinsic
:
4227 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4229 case nir_instr_type_tex
:
4230 visit_tex(ctx
, nir_instr_as_tex(instr
));
4232 case nir_instr_type_phi
:
4233 visit_phi(ctx
, nir_instr_as_phi(instr
));
4235 case nir_instr_type_ssa_undef
:
4236 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4238 case nir_instr_type_jump
:
4239 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4241 case nir_instr_type_deref
:
4242 visit_deref(ctx
, nir_instr_as_deref(instr
));
4245 fprintf(stderr
, "Unknown NIR instr type: ");
4246 nir_print_instr(instr
, stderr
);
4247 fprintf(stderr
, "\n");
4252 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4255 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4257 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4259 nir_block
*then_block
=
4260 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4262 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4264 visit_cf_list(ctx
, &if_stmt
->then_list
);
4266 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4267 nir_block
*else_block
=
4268 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4270 ac_build_else(&ctx
->ac
, else_block
->index
);
4271 visit_cf_list(ctx
, &if_stmt
->else_list
);
4274 ac_build_endif(&ctx
->ac
, then_block
->index
);
4277 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4279 nir_block
*first_loop_block
=
4280 (nir_block
*) exec_list_get_head(&loop
->body
);
4282 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4284 visit_cf_list(ctx
, &loop
->body
);
4286 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4289 static void visit_cf_list(struct ac_nir_context
*ctx
,
4290 struct exec_list
*list
)
4292 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4294 switch (node
->type
) {
4295 case nir_cf_node_block
:
4296 visit_block(ctx
, nir_cf_node_as_block(node
));
4299 case nir_cf_node_if
:
4300 visit_if(ctx
, nir_cf_node_as_if(node
));
4303 case nir_cf_node_loop
:
4304 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4314 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4315 struct ac_shader_abi
*abi
,
4316 struct nir_shader
*nir
,
4317 struct nir_variable
*variable
,
4318 gl_shader_stage stage
)
4320 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4321 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4323 /* tess ctrl has it's own load/store paths for outputs */
4324 if (stage
== MESA_SHADER_TESS_CTRL
)
4327 if (stage
== MESA_SHADER_VERTEX
||
4328 stage
== MESA_SHADER_TESS_EVAL
||
4329 stage
== MESA_SHADER_GEOMETRY
) {
4330 int idx
= variable
->data
.location
+ variable
->data
.index
;
4331 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4332 int length
= nir
->info
.clip_distance_array_size
+
4333 nir
->info
.cull_distance_array_size
;
4342 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4343 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4344 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4345 for (unsigned chan
= 0; chan
< 4; chan
++) {
4346 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4347 ac_build_alloca_undef(ctx
, type
, "");
4353 setup_locals(struct ac_nir_context
*ctx
,
4354 struct nir_function
*func
)
4357 ctx
->num_locals
= 0;
4358 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4359 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4360 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4361 variable
->data
.location_frac
= 0;
4362 ctx
->num_locals
+= attrib_count
;
4364 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4368 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4369 for (j
= 0; j
< 4; j
++) {
4370 ctx
->locals
[i
* 4 + j
] =
4371 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4377 setup_shared(struct ac_nir_context
*ctx
,
4378 struct nir_shader
*nir
)
4380 nir_foreach_variable(variable
, &nir
->shared
) {
4381 LLVMValueRef shared
=
4382 LLVMAddGlobalInAddressSpace(
4383 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4384 variable
->name
? variable
->name
: "",
4386 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4390 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4391 struct nir_shader
*nir
)
4393 struct ac_nir_context ctx
= {};
4394 struct nir_function
*func
;
4399 ctx
.stage
= nir
->info
.stage
;
4400 ctx
.info
= &nir
->info
;
4402 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4404 nir_foreach_variable(variable
, &nir
->outputs
)
4405 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4408 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4409 _mesa_key_pointer_equal
);
4410 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4411 _mesa_key_pointer_equal
);
4412 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4413 _mesa_key_pointer_equal
);
4415 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4417 nir_index_ssa_defs(func
->impl
);
4418 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4420 setup_locals(&ctx
, func
);
4422 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4423 setup_shared(&ctx
, nir
);
4425 visit_cf_list(&ctx
, &func
->impl
->body
);
4426 phi_post_pass(&ctx
);
4428 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4429 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4434 ralloc_free(ctx
.defs
);
4435 ralloc_free(ctx
.phis
);
4436 ralloc_free(ctx
.vars
);
4440 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4442 /* While it would be nice not to have this flag, we are constrained
4443 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4446 bool llvm_has_working_vgpr_indexing
= chip_class
<= GFX8
;
4448 /* TODO: Indirect indexing of GS inputs is unimplemented.
4450 * TCS and TES load inputs directly from LDS or offchip memory, so
4451 * indirect indexing is trivial.
4453 nir_variable_mode indirect_mask
= 0;
4454 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4455 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4456 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4457 !llvm_has_working_vgpr_indexing
)) {
4458 indirect_mask
|= nir_var_shader_in
;
4460 if (!llvm_has_working_vgpr_indexing
&&
4461 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4462 indirect_mask
|= nir_var_shader_out
;
4464 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4465 * smart enough to handle indirects without causing excess spilling
4466 * causing the gpu to hang.
4468 * See the following thread for more details of the problem:
4469 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4471 indirect_mask
|= nir_var_function_temp
;
4473 nir_lower_indirect_derefs(nir
, indirect_mask
);
4477 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4479 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4483 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4485 if (var
->data
.mode
!= nir_var_shader_out
)
4488 unsigned writemask
= 0;
4489 const int location
= var
->data
.location
;
4490 unsigned first_component
= var
->data
.location_frac
;
4491 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4493 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4494 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4495 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4496 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4502 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4503 unsigned *cond_block_tf_writemask
,
4504 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4506 switch (cf_node
->type
) {
4507 case nir_cf_node_block
: {
4508 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4509 nir_foreach_instr(instr
, block
) {
4510 if (instr
->type
!= nir_instr_type_intrinsic
)
4513 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4514 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4516 /* If we find a barrier in nested control flow put this in the
4517 * too hard basket. In GLSL this is not possible but it is in
4521 *tessfactors_are_def_in_all_invocs
= false;
4525 /* The following case must be prevented:
4526 * gl_TessLevelInner = ...;
4528 * if (gl_InvocationID == 1)
4529 * gl_TessLevelInner = ...;
4531 * If you consider disjoint code segments separated by barriers, each
4532 * such segment that writes tess factor channels should write the same
4533 * channels in all codepaths within that segment.
4535 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4536 /* Accumulate the result: */
4537 *tessfactors_are_def_in_all_invocs
&=
4538 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4540 /* Analyze the next code segment from scratch. */
4541 *upper_block_tf_writemask
= 0;
4542 *cond_block_tf_writemask
= 0;
4545 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4550 case nir_cf_node_if
: {
4551 unsigned then_tessfactor_writemask
= 0;
4552 unsigned else_tessfactor_writemask
= 0;
4554 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4555 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4556 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4557 cond_block_tf_writemask
,
4558 tessfactors_are_def_in_all_invocs
, true);
4561 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4562 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4563 cond_block_tf_writemask
,
4564 tessfactors_are_def_in_all_invocs
, true);
4567 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4568 /* If both statements write the same tess factor channels,
4569 * we can say that the upper block writes them too.
4571 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4572 else_tessfactor_writemask
;
4573 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4574 else_tessfactor_writemask
;
4579 case nir_cf_node_loop
: {
4580 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4581 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4582 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4583 cond_block_tf_writemask
,
4584 tessfactors_are_def_in_all_invocs
, true);
4590 unreachable("unknown cf node type");
4595 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4597 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4599 /* The pass works as follows:
4600 * If all codepaths write tess factors, we can say that all
4601 * invocations define tess factors.
4603 * Each tess factor channel is tracked separately.
4605 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4606 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4608 /* Initial value = true. Here the pass will accumulate results from
4609 * multiple segments surrounded by barriers. If tess factors aren't
4610 * written at all, it's a shader bug and we don't care if this will be
4613 bool tessfactors_are_def_in_all_invocs
= true;
4615 nir_foreach_function(function
, nir
) {
4616 if (function
->impl
) {
4617 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4618 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4619 &cond_block_tf_writemask
,
4620 &tessfactors_are_def_in_all_invocs
,
4626 /* Accumulate the result for the last code segment separated by a
4629 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4630 tessfactors_are_def_in_all_invocs
&=
4631 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4634 return tessfactors_are_def_in_all_invocs
;