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_bfm(struct ac_llvm_context
*ctx
,
459 LLVMValueRef bits
, LLVMValueRef offset
)
461 /* mask = ((1 << bits) - 1) << offset */
462 return LLVMBuildShl(ctx
->builder
,
463 LLVMBuildSub(ctx
->builder
,
464 LLVMBuildShl(ctx
->builder
,
471 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
472 LLVMValueRef mask
, LLVMValueRef insert
,
476 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
477 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
479 return LLVMBuildXor(ctx
->builder
, base
,
480 LLVMBuildAnd(ctx
->builder
, mask
,
481 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
484 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
487 LLVMValueRef comp
[2];
489 src0
= ac_to_float(ctx
, src0
);
490 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
491 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
493 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
497 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
500 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
501 LLVMValueRef temps
[2], val
;
504 for (i
= 0; i
< 2; i
++) {
505 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
506 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
507 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
508 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
510 return ac_build_gather_values(ctx
, temps
, 2);
513 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
521 if (op
== nir_op_fddx_fine
)
522 mask
= AC_TID_MASK_LEFT
;
523 else if (op
== nir_op_fddy_fine
)
524 mask
= AC_TID_MASK_TOP
;
526 mask
= AC_TID_MASK_TOP_LEFT
;
528 /* for DDX we want to next X pixel, DDY next Y pixel. */
529 if (op
== nir_op_fddx_fine
||
530 op
== nir_op_fddx_coarse
||
536 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
540 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
542 LLVMValueRef src
[4], result
= NULL
;
543 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
544 unsigned src_components
;
545 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
547 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
554 case nir_op_pack_half_2x16
:
557 case nir_op_unpack_half_2x16
:
560 case nir_op_cube_face_coord
:
561 case nir_op_cube_face_index
:
565 src_components
= num_components
;
568 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
569 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
576 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
577 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
580 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
583 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
586 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
589 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
590 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
591 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
594 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
595 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
596 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
599 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
602 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
605 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
608 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
611 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
612 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
613 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
614 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
615 ac_to_float_type(&ctx
->ac
, def_type
), result
);
616 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
617 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
620 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
621 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
622 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
625 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
628 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
631 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
634 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
635 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
636 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
639 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
640 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
643 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
646 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
649 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
652 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
653 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
654 LLVMTypeOf(src
[0]), "");
655 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
656 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
657 LLVMTypeOf(src
[0]), "");
658 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
661 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
662 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
663 LLVMTypeOf(src
[0]), "");
664 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
665 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
666 LLVMTypeOf(src
[0]), "");
667 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
670 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
671 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
672 LLVMTypeOf(src
[0]), "");
673 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
674 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
675 LLVMTypeOf(src
[0]), "");
676 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
679 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
682 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
685 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
688 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
691 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
694 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
697 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
700 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
703 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
706 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
709 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
710 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
713 result
= emit_iabs(&ctx
->ac
, src
[0]);
716 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
719 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
722 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
725 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
728 result
= ac_build_isign(&ctx
->ac
, src
[0],
729 instr
->dest
.dest
.ssa
.bit_size
);
732 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
733 result
= ac_build_fsign(&ctx
->ac
, src
[0],
734 instr
->dest
.dest
.ssa
.bit_size
);
737 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
738 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
741 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
742 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
745 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
746 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
748 case nir_op_fround_even
:
749 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
750 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
753 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
754 result
= ac_build_fract(&ctx
->ac
, src
[0],
755 instr
->dest
.dest
.ssa
.bit_size
);
758 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
759 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
762 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
763 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
766 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
767 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
770 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
771 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
774 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
775 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
778 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
779 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
780 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
782 case nir_op_frexp_exp
:
783 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
784 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
785 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
786 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
787 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
790 case nir_op_frexp_sig
:
791 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
792 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
793 instr
->dest
.dest
.ssa
.bit_size
);
796 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
797 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
800 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
801 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
802 if (ctx
->ac
.chip_class
< GFX9
&&
803 instr
->dest
.dest
.ssa
.bit_size
== 32) {
804 /* Only pre-GFX9 chips do not flush denorms. */
805 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
806 ac_to_float_type(&ctx
->ac
, def_type
),
811 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
812 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
813 if (ctx
->ac
.chip_class
< GFX9
&&
814 instr
->dest
.dest
.ssa
.bit_size
== 32) {
815 /* Only pre-GFX9 chips do not flush denorms. */
816 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
817 ac_to_float_type(&ctx
->ac
, def_type
),
822 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
823 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
826 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
827 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
828 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
829 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
830 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
832 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
835 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
837 case nir_op_bitfield_select
:
838 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
840 case nir_op_ibitfield_extract
:
841 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
843 case nir_op_ubitfield_extract
:
844 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
846 case nir_op_bitfield_reverse
:
847 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
849 case nir_op_bit_count
:
850 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
855 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
856 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
857 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
863 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
864 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
876 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
881 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
883 case nir_op_f2f16_rtz
:
884 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
885 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
886 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
887 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
888 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
889 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
891 case nir_op_f2f16_rtne
:
895 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
896 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
897 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
899 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
905 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
906 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
908 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
914 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
915 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
917 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
920 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
922 case nir_op_find_lsb
:
923 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
925 case nir_op_ufind_msb
:
926 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
928 case nir_op_ifind_msb
:
929 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
931 case nir_op_uadd_carry
:
932 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
934 case nir_op_usub_borrow
:
935 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
940 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
943 result
= emit_f2b(&ctx
->ac
, src
[0]);
949 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
952 result
= emit_i2b(&ctx
->ac
, src
[0]);
954 case nir_op_fquantize2f16
:
955 result
= emit_f2f16(&ctx
->ac
, src
[0]);
957 case nir_op_umul_high
:
958 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
960 case nir_op_imul_high
:
961 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
963 case nir_op_pack_half_2x16
:
964 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
966 case nir_op_unpack_half_2x16
:
967 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
971 case nir_op_fddx_fine
:
972 case nir_op_fddy_fine
:
973 case nir_op_fddx_coarse
:
974 case nir_op_fddy_coarse
:
975 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
978 case nir_op_unpack_64_2x32_split_x
: {
979 assert(ac_get_llvm_num_components(src
[0]) == 1);
980 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
983 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
988 case nir_op_unpack_64_2x32_split_y
: {
989 assert(ac_get_llvm_num_components(src
[0]) == 1);
990 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
993 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
998 case nir_op_pack_64_2x32_split
: {
999 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1000 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1004 case nir_op_pack_32_2x16_split
: {
1005 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1006 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1010 case nir_op_unpack_32_2x16_split_x
: {
1011 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1014 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1019 case nir_op_unpack_32_2x16_split_y
: {
1020 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1023 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1028 case nir_op_cube_face_coord
: {
1029 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1030 LLVMValueRef results
[2];
1032 for (unsigned chan
= 0; chan
< 3; chan
++)
1033 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1034 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1035 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1036 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1037 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1038 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1039 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1040 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1041 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1042 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1043 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1044 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1045 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1049 case nir_op_cube_face_index
: {
1050 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1052 for (unsigned chan
= 0; chan
< 3; chan
++)
1053 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1054 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1055 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1060 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1061 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1062 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1063 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1066 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1067 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1070 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1071 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1074 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1075 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1076 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1077 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1080 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1081 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1084 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1085 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1087 case nir_op_fmed3
: {
1088 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1089 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1090 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1091 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1092 instr
->dest
.dest
.ssa
.bit_size
);
1095 case nir_op_imed3
: {
1096 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1097 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1098 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1099 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1102 case nir_op_umed3
: {
1103 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1104 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1105 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1106 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1111 fprintf(stderr
, "Unknown NIR alu instr: ");
1112 nir_print_instr(&instr
->instr
, stderr
);
1113 fprintf(stderr
, "\n");
1118 assert(instr
->dest
.dest
.is_ssa
);
1119 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1120 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1124 static void visit_load_const(struct ac_nir_context
*ctx
,
1125 const nir_load_const_instr
*instr
)
1127 LLVMValueRef values
[4], value
= NULL
;
1128 LLVMTypeRef element_type
=
1129 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1131 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1132 switch (instr
->def
.bit_size
) {
1134 values
[i
] = LLVMConstInt(element_type
,
1135 instr
->value
[i
].u8
, false);
1138 values
[i
] = LLVMConstInt(element_type
,
1139 instr
->value
[i
].u16
, false);
1142 values
[i
] = LLVMConstInt(element_type
,
1143 instr
->value
[i
].u32
, false);
1146 values
[i
] = LLVMConstInt(element_type
,
1147 instr
->value
[i
].u64
, false);
1151 "unsupported nir load_const bit_size: %d\n",
1152 instr
->def
.bit_size
);
1156 if (instr
->def
.num_components
> 1) {
1157 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1161 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1165 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1168 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1169 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1172 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1173 /* On GFX8, the descriptor contains the size in bytes,
1174 * but TXQ must return the size in elements.
1175 * The stride is always non-zero for resources using TXQ.
1177 LLVMValueRef stride
=
1178 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1180 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1181 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1182 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1183 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1185 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1190 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1192 struct ac_image_args
*args
,
1193 const nir_tex_instr
*instr
)
1195 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1196 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1197 LLVMValueRef half_texel
[2];
1198 LLVMValueRef compare_cube_wa
= NULL
;
1199 LLVMValueRef result
;
1203 struct ac_image_args txq_args
= { 0 };
1205 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1206 txq_args
.opcode
= ac_image_get_resinfo
;
1207 txq_args
.dmask
= 0xf;
1208 txq_args
.lod
= ctx
->i32_0
;
1209 txq_args
.resource
= args
->resource
;
1210 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1211 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1213 for (unsigned c
= 0; c
< 2; c
++) {
1214 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1215 LLVMConstInt(ctx
->i32
, c
, false), "");
1216 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1217 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1218 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1219 LLVMConstReal(ctx
->f32
, -0.5), "");
1223 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1225 for (unsigned c
= 0; c
< 2; c
++) {
1227 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1228 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1232 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1233 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1234 * workaround by sampling using a scaled type and converting.
1235 * This is taken from amdgpu-pro shaders.
1237 /* NOTE this produces some ugly code compared to amdgpu-pro,
1238 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1239 * and then reads them back. -pro generates two selects,
1240 * one s_cmp for the descriptor rewriting
1241 * one v_cmp for the coordinate and result changes.
1243 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1244 LLVMValueRef tmp
, tmp2
;
1246 /* workaround 8/8/8/8 uint/sint cube gather bug */
1247 /* first detect it then change to a scaled read and f2i */
1248 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1251 /* extract the DATA_FORMAT */
1252 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1253 LLVMConstInt(ctx
->i32
, 6, false), false);
1255 /* is the DATA_FORMAT == 8_8_8_8 */
1256 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1258 if (stype
== GLSL_TYPE_UINT
)
1259 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1260 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1261 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1263 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1264 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1265 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1267 /* replace the NUM FORMAT in the descriptor */
1268 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false), "");
1269 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1271 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1273 /* don't modify the coordinates for this case */
1274 for (unsigned c
= 0; c
< 2; ++c
)
1275 args
->coords
[c
] = LLVMBuildSelect(
1276 ctx
->builder
, compare_cube_wa
,
1277 orig_coords
[c
], args
->coords
[c
], "");
1280 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1281 result
= ac_build_image_opcode(ctx
, args
);
1283 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1284 LLVMValueRef tmp
, tmp2
;
1286 /* if the cube workaround is in place, f2i the result. */
1287 for (unsigned c
= 0; c
< 4; c
++) {
1288 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1289 if (stype
== GLSL_TYPE_UINT
)
1290 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1292 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1293 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1294 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1295 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1296 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1297 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1303 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1305 nir_deref_instr
*texture_deref_instr
= NULL
;
1307 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1308 switch (instr
->src
[i
].src_type
) {
1309 case nir_tex_src_texture_deref
:
1310 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1316 return texture_deref_instr
;
1319 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1320 const nir_tex_instr
*instr
,
1321 struct ac_image_args
*args
)
1323 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1324 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1326 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1327 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1331 util_last_bit(mask
),
1334 return ac_build_buffer_load_format(&ctx
->ac
,
1338 util_last_bit(mask
),
1343 args
->opcode
= ac_image_sample
;
1345 switch (instr
->op
) {
1347 case nir_texop_txf_ms
:
1348 case nir_texop_samples_identical
:
1349 args
->opcode
= args
->level_zero
||
1350 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1351 ac_image_load
: ac_image_load_mip
;
1352 args
->level_zero
= false;
1355 case nir_texop_query_levels
:
1356 args
->opcode
= ac_image_get_resinfo
;
1358 args
->lod
= ctx
->ac
.i32_0
;
1359 args
->level_zero
= false;
1362 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1364 args
->level_zero
= true;
1368 args
->opcode
= ac_image_gather4
;
1369 args
->level_zero
= true;
1372 args
->opcode
= ac_image_get_lod
;
1378 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1379 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1380 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1381 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1382 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1383 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1384 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1388 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1389 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1390 if ((args
->dim
== ac_image_2darray
||
1391 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1392 args
->coords
[1] = ctx
->ac
.i32_0
;
1396 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1397 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1398 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1399 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1400 /* Prevent texture instructions with implicit derivatives from being
1401 * sinked into branches. */
1402 switch (instr
->op
) {
1406 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1413 return ac_build_image_opcode(&ctx
->ac
, args
);
1416 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1417 nir_intrinsic_instr
*instr
)
1419 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1420 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1422 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1423 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1427 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1428 nir_intrinsic_instr
*instr
)
1430 LLVMValueRef ptr
, addr
;
1431 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1432 unsigned index
= nir_intrinsic_base(instr
);
1434 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1435 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1437 /* Load constant values from user SGPRS when possible, otherwise
1438 * fallback to the default path that loads directly from memory.
1440 if (LLVMIsConstant(src0
) &&
1441 instr
->dest
.ssa
.bit_size
== 32) {
1442 unsigned count
= instr
->dest
.ssa
.num_components
;
1443 unsigned offset
= index
;
1445 offset
+= LLVMConstIntGetZExtValue(src0
);
1448 offset
-= ctx
->abi
->base_inline_push_consts
;
1450 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1451 return ac_build_gather_values(&ctx
->ac
,
1452 ctx
->abi
->inline_push_consts
+ offset
,
1457 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->abi
->push_constants
, &addr
, 1, "");
1459 if (instr
->dest
.ssa
.bit_size
== 8) {
1460 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1461 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1462 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1463 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1465 LLVMValueRef params
[3];
1466 if (load_dwords
> 1) {
1467 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1468 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1469 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1471 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1472 params
[0] = ctx
->ac
.i32_0
;
1476 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1478 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1479 if (instr
->dest
.ssa
.num_components
> 1)
1480 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1482 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1483 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1484 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1485 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1486 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1487 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1488 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1489 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1490 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1491 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1492 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1493 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1494 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1495 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1496 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1497 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1498 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1501 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1503 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1506 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1507 const nir_intrinsic_instr
*instr
)
1509 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1511 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1514 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1516 uint32_t new_mask
= 0;
1517 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1518 if (mask
& (1u << i
))
1519 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1523 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1524 unsigned start
, unsigned count
)
1526 LLVMValueRef mask
[] = {
1527 ctx
->i32_0
, ctx
->i32_1
,
1528 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1530 unsigned src_elements
= ac_get_llvm_num_components(src
);
1532 if (count
== src_elements
) {
1535 } else if (count
== 1) {
1536 assert(start
< src_elements
);
1537 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1539 assert(start
+ count
<= src_elements
);
1541 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1542 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1546 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1547 enum gl_access_qualifier access
,
1548 bool may_store_unaligned
,
1549 bool writeonly_memory
)
1551 unsigned cache_policy
= 0;
1553 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1554 * store opcodes not aligned to a dword are affected. The only way to
1555 * get unaligned stores is through shader images.
1557 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1558 /* If this is write-only, don't keep data in L1 to prevent
1559 * evicting L1 cache lines that may be needed by other
1563 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1564 cache_policy
|= ac_glc
;
1567 return cache_policy
;
1570 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1571 nir_intrinsic_instr
*instr
)
1573 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1574 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1575 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1576 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1577 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1578 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1580 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1581 get_src(ctx
, instr
->src
[1]), true);
1582 LLVMValueRef base_data
= src_data
;
1583 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1584 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1588 LLVMValueRef data
, offset
;
1589 LLVMTypeRef data_type
;
1591 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1593 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1594 * writes into a 2-element and a 1-element write. */
1596 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1597 writemask
|= 1 << (start
+ 2);
1600 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1602 /* we can only store 4 DWords at the same time.
1603 * can only happen for 64 Bit vectors. */
1604 if (num_bytes
> 16) {
1605 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1610 /* check alignment of 16 Bit stores */
1611 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1612 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1616 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1618 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1619 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1621 if (num_bytes
== 1) {
1622 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1623 offset
, ctx
->ac
.i32_0
,
1624 cache_policy
& ac_glc
);
1625 } else if (num_bytes
== 2) {
1626 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1627 offset
, ctx
->ac
.i32_0
,
1628 cache_policy
& ac_glc
);
1630 int num_channels
= num_bytes
/ 4;
1632 switch (num_bytes
) {
1633 case 16: /* v4f32 */
1634 data_type
= ctx
->ac
.v4f32
;
1636 case 12: /* v3f32 */
1637 data_type
= ctx
->ac
.v3f32
;
1640 data_type
= ctx
->ac
.v2f32
;
1643 data_type
= ctx
->ac
.f32
;
1646 unreachable("Malformed vector store.");
1648 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1650 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1651 num_channels
, offset
,
1653 cache_policy
& ac_glc
,
1659 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1660 const nir_intrinsic_instr
*instr
)
1662 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1664 char name
[64], type
[8];
1665 LLVMValueRef params
[6];
1668 switch (instr
->intrinsic
) {
1669 case nir_intrinsic_ssbo_atomic_add
:
1672 case nir_intrinsic_ssbo_atomic_imin
:
1675 case nir_intrinsic_ssbo_atomic_umin
:
1678 case nir_intrinsic_ssbo_atomic_imax
:
1681 case nir_intrinsic_ssbo_atomic_umax
:
1684 case nir_intrinsic_ssbo_atomic_and
:
1687 case nir_intrinsic_ssbo_atomic_or
:
1690 case nir_intrinsic_ssbo_atomic_xor
:
1693 case nir_intrinsic_ssbo_atomic_exchange
:
1696 case nir_intrinsic_ssbo_atomic_comp_swap
:
1703 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1704 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1706 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1707 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1708 get_src(ctx
, instr
->src
[0]),
1711 if (HAVE_LLVM
>= 0x900) {
1712 /* XXX: The new raw/struct atomic intrinsics are buggy with
1713 * LLVM 8, see r358579.
1715 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1716 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1717 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1719 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1720 snprintf(name
, sizeof(name
),
1721 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1723 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1724 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1725 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1727 assert(return_type
== ctx
->ac
.i32
);
1728 snprintf(name
, sizeof(name
),
1729 "llvm.amdgcn.buffer.atomic.%s", op
);
1732 return ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1736 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1737 const nir_intrinsic_instr
*instr
)
1739 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1740 int num_components
= instr
->num_components
;
1741 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1742 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1744 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1745 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1746 get_src(ctx
, instr
->src
[0]), false);
1747 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1749 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1750 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1752 LLVMValueRef results
[4];
1753 for (int i
= 0; i
< num_components
;) {
1754 int num_elems
= num_components
- i
;
1755 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1757 if (num_elems
* elem_size_bytes
> 16)
1758 num_elems
= 16 / elem_size_bytes
;
1759 int load_bytes
= num_elems
* elem_size_bytes
;
1761 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1765 if (load_bytes
== 1) {
1766 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1771 cache_policy
& ac_glc
);
1772 } else if (load_bytes
== 2) {
1773 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1778 cache_policy
& ac_glc
);
1780 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1781 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
1783 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1784 vindex
, offset
, immoffset
, 0,
1785 cache_policy
& ac_glc
, 0,
1786 can_speculate
, false);
1789 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1790 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1791 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1793 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1794 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1796 for (unsigned j
= 0; j
< num_elems
; j
++) {
1797 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1802 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1805 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1806 const nir_intrinsic_instr
*instr
)
1809 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1810 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1811 int num_components
= instr
->num_components
;
1813 if (ctx
->abi
->load_ubo
)
1814 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1816 if (instr
->dest
.ssa
.bit_size
== 64)
1817 num_components
*= 2;
1819 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1820 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1821 LLVMValueRef results
[num_components
];
1822 for (unsigned i
= 0; i
< num_components
; ++i
) {
1823 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1826 if (load_bytes
== 1) {
1827 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1834 assert(load_bytes
== 2);
1835 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1843 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1845 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1846 NULL
, 0, false, false, true, true);
1848 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1851 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1852 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1856 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1857 bool vs_in
, unsigned *vertex_index_out
,
1858 LLVMValueRef
*vertex_index_ref
,
1859 unsigned *const_out
, LLVMValueRef
*indir_out
)
1861 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1862 nir_deref_path path
;
1863 unsigned idx_lvl
= 1;
1865 nir_deref_path_init(&path
, instr
, NULL
);
1867 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1868 if (vertex_index_ref
) {
1869 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1870 if (vertex_index_out
)
1871 *vertex_index_out
= 0;
1873 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1878 uint32_t const_offset
= 0;
1879 LLVMValueRef offset
= NULL
;
1881 if (var
->data
.compact
) {
1882 assert(instr
->deref_type
== nir_deref_type_array
);
1883 const_offset
= nir_src_as_uint(instr
->arr
.index
);
1887 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1888 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1889 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1890 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1892 for (unsigned i
= 0; i
< index
; i
++) {
1893 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1894 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1896 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1897 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1898 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1899 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1901 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1905 unreachable("Uhandled deref type in get_deref_instr_offset");
1909 nir_deref_path_finish(&path
);
1911 if (const_offset
&& offset
)
1912 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1913 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1916 *const_out
= const_offset
;
1917 *indir_out
= offset
;
1920 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1921 nir_intrinsic_instr
*instr
,
1924 LLVMValueRef result
;
1925 LLVMValueRef vertex_index
= NULL
;
1926 LLVMValueRef indir_index
= NULL
;
1927 unsigned const_index
= 0;
1929 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1931 unsigned location
= var
->data
.location
;
1932 unsigned driver_location
= var
->data
.driver_location
;
1933 const bool is_patch
= var
->data
.patch
;
1934 const bool is_compact
= var
->data
.compact
;
1936 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1937 false, NULL
, is_patch
? NULL
: &vertex_index
,
1938 &const_index
, &indir_index
);
1940 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1942 LLVMTypeRef src_component_type
;
1943 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1944 src_component_type
= LLVMGetElementType(dest_type
);
1946 src_component_type
= dest_type
;
1948 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1949 vertex_index
, indir_index
,
1950 const_index
, location
, driver_location
,
1951 var
->data
.location_frac
,
1952 instr
->num_components
,
1953 is_patch
, is_compact
, load_inputs
);
1954 if (instr
->dest
.ssa
.bit_size
== 16) {
1955 result
= ac_to_integer(&ctx
->ac
, result
);
1956 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1958 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1962 type_scalar_size_bytes(const struct glsl_type
*type
)
1964 assert(glsl_type_is_vector_or_scalar(type
) ||
1965 glsl_type_is_matrix(type
));
1966 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1969 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1970 nir_intrinsic_instr
*instr
)
1972 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1973 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1975 LLVMValueRef values
[8];
1977 int ve
= instr
->dest
.ssa
.num_components
;
1979 LLVMValueRef indir_index
;
1981 unsigned const_index
;
1982 unsigned stride
= 4;
1983 int mode
= deref
->mode
;
1986 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1987 var
->data
.mode
== nir_var_shader_in
;
1988 idx
= var
->data
.driver_location
;
1989 comp
= var
->data
.location_frac
;
1990 mode
= var
->data
.mode
;
1992 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1993 &const_index
, &indir_index
);
1995 if (var
->data
.compact
) {
1997 const_index
+= comp
;
2002 if (instr
->dest
.ssa
.bit_size
== 64 &&
2003 (deref
->mode
== nir_var_shader_in
||
2004 deref
->mode
== nir_var_shader_out
||
2005 deref
->mode
== nir_var_function_temp
))
2009 case nir_var_shader_in
:
2010 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2011 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2012 return load_tess_varyings(ctx
, instr
, true);
2015 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2016 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2017 LLVMValueRef indir_index
;
2018 unsigned const_index
, vertex_index
;
2019 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2020 &const_index
, &indir_index
);
2022 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2023 var
->data
.driver_location
,
2024 var
->data
.location_frac
,
2025 instr
->num_components
, vertex_index
, const_index
, type
);
2028 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2030 unsigned count
= glsl_count_attribute_slots(
2032 ctx
->stage
== MESA_SHADER_VERTEX
);
2034 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2035 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2036 stride
, false, true);
2038 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2042 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2045 case nir_var_function_temp
:
2046 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2048 unsigned count
= glsl_count_attribute_slots(
2051 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2052 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2053 stride
, true, true);
2055 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2059 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2063 case nir_var_mem_shared
: {
2064 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2065 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2066 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2067 get_def_type(ctx
, &instr
->dest
.ssa
),
2070 case nir_var_shader_out
:
2071 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2072 return load_tess_varyings(ctx
, instr
, false);
2075 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2077 unsigned count
= glsl_count_attribute_slots(
2080 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2081 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2082 stride
, true, true);
2084 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2088 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2089 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2094 case nir_var_mem_global
: {
2095 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2096 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2097 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2098 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2100 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2101 if (stride
!= natural_stride
) {
2102 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2103 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2104 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2106 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2107 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2108 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2109 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2111 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2113 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2114 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2115 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2116 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2121 unreachable("unhandle variable mode");
2123 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2124 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2128 visit_store_var(struct ac_nir_context
*ctx
,
2129 nir_intrinsic_instr
*instr
)
2131 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2132 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2134 LLVMValueRef temp_ptr
, value
;
2137 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2138 int writemask
= instr
->const_index
[0];
2139 LLVMValueRef indir_index
;
2140 unsigned const_index
;
2143 get_deref_offset(ctx
, deref
, false,
2144 NULL
, NULL
, &const_index
, &indir_index
);
2145 idx
= var
->data
.driver_location
;
2146 comp
= var
->data
.location_frac
;
2148 if (var
->data
.compact
) {
2149 const_index
+= comp
;
2154 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2155 (deref
->mode
== nir_var_shader_out
||
2156 deref
->mode
== nir_var_function_temp
)) {
2158 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2159 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2162 writemask
= widen_mask(writemask
, 2);
2165 writemask
= writemask
<< comp
;
2167 switch (deref
->mode
) {
2168 case nir_var_shader_out
:
2170 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2171 LLVMValueRef vertex_index
= NULL
;
2172 LLVMValueRef indir_index
= NULL
;
2173 unsigned const_index
= 0;
2174 const bool is_patch
= var
->data
.patch
;
2176 get_deref_offset(ctx
, deref
, false, NULL
,
2177 is_patch
? NULL
: &vertex_index
,
2178 &const_index
, &indir_index
);
2180 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2181 vertex_index
, indir_index
,
2182 const_index
, src
, writemask
);
2186 for (unsigned chan
= 0; chan
< 8; chan
++) {
2188 if (!(writemask
& (1 << chan
)))
2191 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2193 if (var
->data
.compact
)
2196 unsigned count
= glsl_count_attribute_slots(
2199 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2200 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2201 stride
, true, true);
2203 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2204 value
, indir_index
, "");
2205 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2206 count
, stride
, tmp_vec
);
2209 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2211 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2215 case nir_var_function_temp
:
2216 for (unsigned chan
= 0; chan
< 8; chan
++) {
2217 if (!(writemask
& (1 << chan
)))
2220 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2222 unsigned count
= glsl_count_attribute_slots(
2225 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2226 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2229 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2230 value
, indir_index
, "");
2231 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2234 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2236 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2241 case nir_var_mem_global
:
2242 case nir_var_mem_shared
: {
2243 int writemask
= instr
->const_index
[0];
2244 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2245 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2247 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2248 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2249 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2251 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2252 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2253 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2255 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2256 stride
== natural_stride
) {
2257 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2258 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2259 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2261 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2262 LLVMGetElementType(LLVMTypeOf(address
)), "");
2263 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2265 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2266 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2267 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2268 for (unsigned chan
= 0; chan
< 4; chan
++) {
2269 if (!(writemask
& (1 << chan
)))
2272 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2274 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2275 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2277 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2278 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2279 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2290 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2293 case GLSL_SAMPLER_DIM_BUF
:
2295 case GLSL_SAMPLER_DIM_1D
:
2296 return array
? 2 : 1;
2297 case GLSL_SAMPLER_DIM_2D
:
2298 return array
? 3 : 2;
2299 case GLSL_SAMPLER_DIM_MS
:
2300 return array
? 4 : 3;
2301 case GLSL_SAMPLER_DIM_3D
:
2302 case GLSL_SAMPLER_DIM_CUBE
:
2304 case GLSL_SAMPLER_DIM_RECT
:
2305 case GLSL_SAMPLER_DIM_SUBPASS
:
2307 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2315 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2316 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2317 LLVMValueRef coord_z
,
2318 LLVMValueRef sample_index
,
2319 LLVMValueRef fmask_desc_ptr
)
2321 unsigned sample_chan
= coord_z
? 3 : 2;
2322 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2323 addr
[sample_chan
] = sample_index
;
2325 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2326 return addr
[sample_chan
];
2329 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2331 assert(instr
->src
[0].is_ssa
);
2332 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2335 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2336 const nir_intrinsic_instr
*instr
,
2337 enum ac_descriptor_type desc_type
,
2340 nir_deref_instr
*deref_instr
=
2341 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2342 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2344 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2347 static void get_image_coords(struct ac_nir_context
*ctx
,
2348 const nir_intrinsic_instr
*instr
,
2349 struct ac_image_args
*args
,
2350 enum glsl_sampler_dim dim
,
2353 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2354 LLVMValueRef masks
[] = {
2355 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2356 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2358 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2361 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2362 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2363 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2364 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2365 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2366 count
= image_type_to_components_count(dim
, is_array
);
2368 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2369 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2370 LLVMValueRef fmask_load_address
[3];
2373 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2374 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2376 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2378 fmask_load_address
[2] = NULL
;
2380 for (chan
= 0; chan
< 2; ++chan
)
2381 fmask_load_address
[chan
] =
2382 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2383 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2384 ctx
->ac
.i32
, ""), "");
2385 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2387 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2388 fmask_load_address
[0],
2389 fmask_load_address
[1],
2390 fmask_load_address
[2],
2392 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2393 AC_DESC_FMASK
, &instr
->instr
, false, false));
2395 if (count
== 1 && !gfx9_1d
) {
2396 if (instr
->src
[1].ssa
->num_components
)
2397 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2399 args
->coords
[0] = src0
;
2404 for (chan
= 0; chan
< count
; ++chan
) {
2405 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2408 for (chan
= 0; chan
< 2; ++chan
) {
2409 args
->coords
[chan
] = LLVMBuildAdd(
2410 ctx
->ac
.builder
, args
->coords
[chan
],
2412 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2413 ctx
->ac
.i32
, ""), "");
2415 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2416 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2422 args
->coords
[2] = args
->coords
[1];
2423 args
->coords
[1] = ctx
->ac
.i32_0
;
2425 args
->coords
[1] = ctx
->ac
.i32_0
;
2430 args
->coords
[count
] = sample_index
;
2436 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2437 const nir_intrinsic_instr
*instr
,
2438 bool write
, bool atomic
)
2440 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2441 if (ctx
->abi
->gfx9_stride_size_workaround
||
2442 (ctx
->abi
->gfx9_stride_size_workaround_for_atomic
&& atomic
)) {
2443 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2444 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2445 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2447 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2448 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2449 elem_count
, stride
, "");
2451 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2452 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2457 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2458 const nir_intrinsic_instr
*instr
,
2463 enum glsl_sampler_dim dim
;
2464 enum gl_access_qualifier access
;
2467 dim
= nir_intrinsic_image_dim(instr
);
2468 access
= nir_intrinsic_access(instr
);
2469 is_array
= nir_intrinsic_image_array(instr
);
2471 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2472 const struct glsl_type
*type
= image_deref
->type
;
2473 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2474 dim
= glsl_get_sampler_dim(type
);
2475 access
= var
->data
.image
.access
;
2476 is_array
= glsl_sampler_type_is_array(type
);
2479 struct ac_image_args args
= {};
2481 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2483 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2484 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2485 unsigned num_channels
= util_last_bit(mask
);
2486 LLVMValueRef rsrc
, vindex
;
2488 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false, false);
2489 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2492 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2493 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2494 ctx
->ac
.i32_0
, num_channels
,
2495 !!(args
.cache_policy
& ac_glc
),
2497 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2499 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2500 res
= ac_to_integer(&ctx
->ac
, res
);
2502 args
.opcode
= ac_image_load
;
2503 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2504 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2505 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2507 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2509 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2514 static void visit_image_store(struct ac_nir_context
*ctx
,
2515 nir_intrinsic_instr
*instr
,
2520 enum glsl_sampler_dim dim
;
2521 enum gl_access_qualifier access
;
2524 dim
= nir_intrinsic_image_dim(instr
);
2525 access
= nir_intrinsic_access(instr
);
2526 is_array
= nir_intrinsic_image_array(instr
);
2528 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2529 const struct glsl_type
*type
= image_deref
->type
;
2530 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2531 dim
= glsl_get_sampler_dim(type
);
2532 access
= var
->data
.image
.access
;
2533 is_array
= glsl_sampler_type_is_array(type
);
2536 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2537 struct ac_image_args args
= {};
2539 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2541 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2542 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true, false);
2543 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2544 unsigned src_channels
= ac_get_llvm_num_components(src
);
2545 LLVMValueRef vindex
;
2547 if (src_channels
== 3)
2548 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2550 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2551 get_src(ctx
, instr
->src
[1]),
2554 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2555 ctx
->ac
.i32_0
, src_channels
,
2556 args
.cache_policy
& ac_glc
, false);
2558 args
.opcode
= ac_image_store
;
2559 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2560 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2561 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2562 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2565 ac_build_image_opcode(&ctx
->ac
, &args
);
2570 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2571 const nir_intrinsic_instr
*instr
,
2574 LLVMValueRef params
[7];
2575 int param_count
= 0;
2577 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2578 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2579 const char *atomic_name
;
2580 char intrinsic_name
[64];
2581 enum ac_atomic_op atomic_subop
;
2582 MAYBE_UNUSED
int length
;
2584 enum glsl_sampler_dim dim
;
2585 bool is_unsigned
= false;
2588 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_min
||
2589 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_max
) {
2590 const GLenum format
= nir_intrinsic_format(instr
);
2591 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2592 is_unsigned
= format
== GL_R32UI
;
2594 dim
= nir_intrinsic_image_dim(instr
);
2595 is_array
= nir_intrinsic_image_array(instr
);
2597 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2598 is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2599 dim
= glsl_get_sampler_dim(type
);
2600 is_array
= glsl_sampler_type_is_array(type
);
2603 switch (instr
->intrinsic
) {
2604 case nir_intrinsic_bindless_image_atomic_add
:
2605 case nir_intrinsic_image_deref_atomic_add
:
2606 atomic_name
= "add";
2607 atomic_subop
= ac_atomic_add
;
2609 case nir_intrinsic_bindless_image_atomic_min
:
2610 case nir_intrinsic_image_deref_atomic_min
:
2611 atomic_name
= is_unsigned
? "umin" : "smin";
2612 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2614 case nir_intrinsic_bindless_image_atomic_max
:
2615 case nir_intrinsic_image_deref_atomic_max
:
2616 atomic_name
= is_unsigned
? "umax" : "smax";
2617 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2619 case nir_intrinsic_bindless_image_atomic_and
:
2620 case nir_intrinsic_image_deref_atomic_and
:
2621 atomic_name
= "and";
2622 atomic_subop
= ac_atomic_and
;
2624 case nir_intrinsic_bindless_image_atomic_or
:
2625 case nir_intrinsic_image_deref_atomic_or
:
2627 atomic_subop
= ac_atomic_or
;
2629 case nir_intrinsic_bindless_image_atomic_xor
:
2630 case nir_intrinsic_image_deref_atomic_xor
:
2631 atomic_name
= "xor";
2632 atomic_subop
= ac_atomic_xor
;
2634 case nir_intrinsic_bindless_image_atomic_exchange
:
2635 case nir_intrinsic_image_deref_atomic_exchange
:
2636 atomic_name
= "swap";
2637 atomic_subop
= ac_atomic_swap
;
2639 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2640 case nir_intrinsic_image_deref_atomic_comp_swap
:
2641 atomic_name
= "cmpswap";
2642 atomic_subop
= 0; /* not used */
2649 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2650 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2652 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2653 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true, true);
2654 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2655 ctx
->ac
.i32_0
, ""); /* vindex */
2656 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2657 if (HAVE_LLVM
>= 0x900) {
2658 /* XXX: The new raw/struct atomic intrinsics are buggy
2659 * with LLVM 8, see r358579.
2661 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2662 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2664 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2665 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2667 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2669 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2670 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2673 assert(length
< sizeof(intrinsic_name
));
2674 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2675 params
, param_count
, 0);
2677 struct ac_image_args args
= {};
2678 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2679 args
.atomic
= atomic_subop
;
2680 args
.data
[0] = params
[0];
2682 args
.data
[1] = params
[1];
2683 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2684 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2685 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2687 return ac_build_image_opcode(&ctx
->ac
, &args
);
2691 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2692 const nir_intrinsic_instr
*instr
,
2695 enum glsl_sampler_dim dim
;
2698 dim
= nir_intrinsic_image_dim(instr
);
2699 is_array
= nir_intrinsic_image_array(instr
);
2701 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2702 dim
= glsl_get_sampler_dim(type
);
2703 is_array
= glsl_sampler_type_is_array(type
);
2706 struct ac_image_args args
= { 0 };
2707 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, dim
, is_array
);
2709 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2710 args
.opcode
= ac_image_get_resinfo
;
2711 args
.lod
= ctx
->ac
.i32_0
;
2712 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2714 return ac_build_image_opcode(&ctx
->ac
, &args
);
2717 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2718 const nir_intrinsic_instr
*instr
,
2723 enum glsl_sampler_dim dim
;
2726 dim
= nir_intrinsic_image_dim(instr
);
2727 is_array
= nir_intrinsic_image_array(instr
);
2729 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2730 dim
= glsl_get_sampler_dim(type
);
2731 is_array
= glsl_sampler_type_is_array(type
);
2734 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2735 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2737 struct ac_image_args args
= { 0 };
2739 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2741 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2742 args
.opcode
= ac_image_get_resinfo
;
2743 args
.lod
= ctx
->ac
.i32_0
;
2744 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2746 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2748 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2750 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2751 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2752 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2753 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2754 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2756 if (ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2757 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2758 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2765 static void emit_membar(struct ac_llvm_context
*ac
,
2766 const nir_intrinsic_instr
*instr
)
2768 unsigned waitcnt
= NOOP_WAITCNT
;
2770 switch (instr
->intrinsic
) {
2771 case nir_intrinsic_memory_barrier
:
2772 case nir_intrinsic_group_memory_barrier
:
2773 waitcnt
&= VM_CNT
& LGKM_CNT
;
2775 case nir_intrinsic_memory_barrier_atomic_counter
:
2776 case nir_intrinsic_memory_barrier_buffer
:
2777 case nir_intrinsic_memory_barrier_image
:
2780 case nir_intrinsic_memory_barrier_shared
:
2781 waitcnt
&= LGKM_CNT
;
2786 if (waitcnt
!= NOOP_WAITCNT
)
2787 ac_build_waitcnt(ac
, waitcnt
);
2790 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2792 /* GFX6 only (thanks to a hw bug workaround):
2793 * The real barrier instruction isn’t needed, because an entire patch
2794 * always fits into a single wave.
2796 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
2797 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2800 ac_build_s_barrier(ac
);
2803 static void emit_discard(struct ac_nir_context
*ctx
,
2804 const nir_intrinsic_instr
*instr
)
2808 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2809 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2810 get_src(ctx
, instr
->src
[0]),
2813 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2814 cond
= ctx
->ac
.i1false
;
2817 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2821 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2823 LLVMValueRef result
;
2824 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2825 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2826 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2828 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2832 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2834 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2835 LLVMValueRef result
;
2836 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2837 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2838 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2840 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2845 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2847 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2848 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2849 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2851 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2856 visit_first_invocation(struct ac_nir_context
*ctx
)
2858 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2860 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2861 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2862 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2864 ctx
->ac
.i64
, args
, 2,
2865 AC_FUNC_ATTR_NOUNWIND
|
2866 AC_FUNC_ATTR_READNONE
);
2868 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2872 visit_load_shared(struct ac_nir_context
*ctx
,
2873 const nir_intrinsic_instr
*instr
)
2875 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2877 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2879 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2880 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2881 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2882 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2885 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2886 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2890 visit_store_shared(struct ac_nir_context
*ctx
,
2891 const nir_intrinsic_instr
*instr
)
2893 LLVMValueRef derived_ptr
, data
,index
;
2894 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2896 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2897 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2899 int writemask
= nir_intrinsic_write_mask(instr
);
2900 for (int chan
= 0; chan
< 4; chan
++) {
2901 if (!(writemask
& (1 << chan
))) {
2904 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2905 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2906 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2907 LLVMBuildStore(builder
, data
, derived_ptr
);
2911 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2912 const nir_intrinsic_instr
*instr
,
2913 LLVMValueRef ptr
, int src_idx
)
2915 LLVMValueRef result
;
2916 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2918 const char *sync_scope
= HAVE_LLVM
>= 0x0900 ? "workgroup-one-as" : "workgroup";
2920 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2921 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2922 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2923 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
2924 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2926 LLVMAtomicRMWBinOp op
;
2927 switch (instr
->intrinsic
) {
2928 case nir_intrinsic_shared_atomic_add
:
2929 case nir_intrinsic_deref_atomic_add
:
2930 op
= LLVMAtomicRMWBinOpAdd
;
2932 case nir_intrinsic_shared_atomic_umin
:
2933 case nir_intrinsic_deref_atomic_umin
:
2934 op
= LLVMAtomicRMWBinOpUMin
;
2936 case nir_intrinsic_shared_atomic_umax
:
2937 case nir_intrinsic_deref_atomic_umax
:
2938 op
= LLVMAtomicRMWBinOpUMax
;
2940 case nir_intrinsic_shared_atomic_imin
:
2941 case nir_intrinsic_deref_atomic_imin
:
2942 op
= LLVMAtomicRMWBinOpMin
;
2944 case nir_intrinsic_shared_atomic_imax
:
2945 case nir_intrinsic_deref_atomic_imax
:
2946 op
= LLVMAtomicRMWBinOpMax
;
2948 case nir_intrinsic_shared_atomic_and
:
2949 case nir_intrinsic_deref_atomic_and
:
2950 op
= LLVMAtomicRMWBinOpAnd
;
2952 case nir_intrinsic_shared_atomic_or
:
2953 case nir_intrinsic_deref_atomic_or
:
2954 op
= LLVMAtomicRMWBinOpOr
;
2956 case nir_intrinsic_shared_atomic_xor
:
2957 case nir_intrinsic_deref_atomic_xor
:
2958 op
= LLVMAtomicRMWBinOpXor
;
2960 case nir_intrinsic_shared_atomic_exchange
:
2961 case nir_intrinsic_deref_atomic_exchange
:
2962 op
= LLVMAtomicRMWBinOpXchg
;
2968 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
2973 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2975 LLVMValueRef values
[2];
2976 LLVMValueRef pos
[2];
2978 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2979 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2981 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2982 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2983 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2986 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2987 const nir_intrinsic_instr
*instr
)
2989 LLVMValueRef result
[4];
2990 LLVMValueRef interp_param
;
2993 LLVMValueRef src_c0
= NULL
;
2994 LLVMValueRef src_c1
= NULL
;
2995 LLVMValueRef src0
= NULL
;
2997 nir_deref_instr
*deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2998 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
2999 int input_base
= ctx
->abi
->fs_input_attr_indices
[var
->data
.location
- VARYING_SLOT_VAR0
];
3000 switch (instr
->intrinsic
) {
3001 case nir_intrinsic_interp_deref_at_centroid
:
3002 location
= INTERP_CENTROID
;
3004 case nir_intrinsic_interp_deref_at_sample
:
3005 case nir_intrinsic_interp_deref_at_offset
:
3006 location
= INTERP_CENTER
;
3007 src0
= get_src(ctx
, instr
->src
[1]);
3013 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
3014 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
3015 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
3016 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
3017 LLVMValueRef sample_position
;
3018 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3020 /* fetch sample ID */
3021 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
3023 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
3024 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3025 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
3026 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3028 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
3030 if (location
== INTERP_CENTER
) {
3031 LLVMValueRef ij_out
[2];
3032 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3035 * take the I then J parameters, and the DDX/Y for it, and
3036 * calculate the IJ inputs for the interpolator.
3037 * temp1 = ddx * offset/sample.x + I;
3038 * interp_param.I = ddy * offset/sample.y + temp1;
3039 * temp1 = ddx * offset/sample.x + J;
3040 * interp_param.J = ddy * offset/sample.y + temp1;
3042 for (unsigned i
= 0; i
< 2; i
++) {
3043 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3044 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3045 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3046 ddxy_out
, ix_ll
, "");
3047 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3048 ddxy_out
, iy_ll
, "");
3049 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3050 interp_param
, ix_ll
, "");
3051 LLVMValueRef temp1
, temp2
;
3053 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3056 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3057 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3059 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3060 temp2
, ctx
->ac
.i32
, "");
3062 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3066 LLVMValueRef attrib_idx
= ctx
->ac
.i32_0
;
3067 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3068 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3069 unsigned array_size
= glsl_count_attribute_slots(deref_instr
->type
, false);
3071 LLVMValueRef offset
;
3072 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3073 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* nir_src_as_uint(deref_instr
->arr
.index
), false);
3075 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3077 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3078 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3081 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3082 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3083 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3084 LLVMValueRef offset
;
3085 unsigned sidx
= deref_instr
->strct
.index
;
3086 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3087 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_struct_location_offset(deref_instr
->type
, sidx
), false);
3088 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3090 unreachable("Unsupported deref type");
3095 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3096 for (chan
= 0; chan
< 4; chan
++) {
3097 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3098 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3100 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3101 LLVMValueRef v
, attr_number
;
3103 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3105 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3106 interp_param
, ctx
->ac
.v2f32
, "");
3107 LLVMValueRef i
= LLVMBuildExtractElement(
3108 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3109 LLVMValueRef j
= LLVMBuildExtractElement(
3110 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3112 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3113 ctx
->abi
->prim_mask
, i
, j
);
3115 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3116 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3119 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3120 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3123 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3126 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3127 var
->data
.location_frac
);
3130 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3131 nir_intrinsic_instr
*instr
)
3133 LLVMValueRef result
= NULL
;
3135 switch (instr
->intrinsic
) {
3136 case nir_intrinsic_ballot
:
3137 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3139 case nir_intrinsic_read_invocation
:
3140 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3141 get_src(ctx
, instr
->src
[1]));
3143 case nir_intrinsic_read_first_invocation
:
3144 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3146 case nir_intrinsic_load_subgroup_invocation
:
3147 result
= ac_get_thread_id(&ctx
->ac
);
3149 case nir_intrinsic_load_work_group_id
: {
3150 LLVMValueRef values
[3];
3152 for (int i
= 0; i
< 3; i
++) {
3153 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3154 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3157 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3160 case nir_intrinsic_load_base_vertex
:
3161 case nir_intrinsic_load_first_vertex
:
3162 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3164 case nir_intrinsic_load_local_group_size
:
3165 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3167 case nir_intrinsic_load_vertex_id
:
3168 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3169 ctx
->abi
->base_vertex
, "");
3171 case nir_intrinsic_load_vertex_id_zero_base
: {
3172 result
= ctx
->abi
->vertex_id
;
3175 case nir_intrinsic_load_local_invocation_id
: {
3176 result
= ctx
->abi
->local_invocation_ids
;
3179 case nir_intrinsic_load_base_instance
:
3180 result
= ctx
->abi
->start_instance
;
3182 case nir_intrinsic_load_draw_id
:
3183 result
= ctx
->abi
->draw_id
;
3185 case nir_intrinsic_load_view_index
:
3186 result
= ctx
->abi
->view_index
;
3188 case nir_intrinsic_load_invocation_id
:
3189 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3190 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3192 result
= ctx
->abi
->gs_invocation_id
;
3194 case nir_intrinsic_load_primitive_id
:
3195 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3196 result
= ctx
->abi
->gs_prim_id
;
3197 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3198 result
= ctx
->abi
->tcs_patch_id
;
3199 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3200 result
= ctx
->abi
->tes_patch_id
;
3202 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3204 case nir_intrinsic_load_sample_id
:
3205 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3207 case nir_intrinsic_load_sample_pos
:
3208 result
= load_sample_pos(ctx
);
3210 case nir_intrinsic_load_sample_mask_in
:
3211 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3213 case nir_intrinsic_load_frag_coord
: {
3214 LLVMValueRef values
[4] = {
3215 ctx
->abi
->frag_pos
[0],
3216 ctx
->abi
->frag_pos
[1],
3217 ctx
->abi
->frag_pos
[2],
3218 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3220 result
= ac_to_integer(&ctx
->ac
,
3221 ac_build_gather_values(&ctx
->ac
, values
, 4));
3224 case nir_intrinsic_load_front_face
:
3225 result
= ctx
->abi
->front_face
;
3227 case nir_intrinsic_load_helper_invocation
:
3228 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3230 case nir_intrinsic_load_instance_id
:
3231 result
= ctx
->abi
->instance_id
;
3233 case nir_intrinsic_load_num_work_groups
:
3234 result
= ctx
->abi
->num_work_groups
;
3236 case nir_intrinsic_load_local_invocation_index
:
3237 result
= visit_load_local_invocation_index(ctx
);
3239 case nir_intrinsic_load_subgroup_id
:
3240 result
= visit_load_subgroup_id(ctx
);
3242 case nir_intrinsic_load_num_subgroups
:
3243 result
= visit_load_num_subgroups(ctx
);
3245 case nir_intrinsic_first_invocation
:
3246 result
= visit_first_invocation(ctx
);
3248 case nir_intrinsic_load_push_constant
:
3249 result
= visit_load_push_constant(ctx
, instr
);
3251 case nir_intrinsic_vulkan_resource_index
: {
3252 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3253 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3254 unsigned binding
= nir_intrinsic_binding(instr
);
3256 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3260 case nir_intrinsic_vulkan_resource_reindex
:
3261 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3263 case nir_intrinsic_store_ssbo
:
3264 visit_store_ssbo(ctx
, instr
);
3266 case nir_intrinsic_load_ssbo
:
3267 result
= visit_load_buffer(ctx
, instr
);
3269 case nir_intrinsic_ssbo_atomic_add
:
3270 case nir_intrinsic_ssbo_atomic_imin
:
3271 case nir_intrinsic_ssbo_atomic_umin
:
3272 case nir_intrinsic_ssbo_atomic_imax
:
3273 case nir_intrinsic_ssbo_atomic_umax
:
3274 case nir_intrinsic_ssbo_atomic_and
:
3275 case nir_intrinsic_ssbo_atomic_or
:
3276 case nir_intrinsic_ssbo_atomic_xor
:
3277 case nir_intrinsic_ssbo_atomic_exchange
:
3278 case nir_intrinsic_ssbo_atomic_comp_swap
:
3279 result
= visit_atomic_ssbo(ctx
, instr
);
3281 case nir_intrinsic_load_ubo
:
3282 result
= visit_load_ubo_buffer(ctx
, instr
);
3284 case nir_intrinsic_get_buffer_size
:
3285 result
= visit_get_buffer_size(ctx
, instr
);
3287 case nir_intrinsic_load_deref
:
3288 result
= visit_load_var(ctx
, instr
);
3290 case nir_intrinsic_store_deref
:
3291 visit_store_var(ctx
, instr
);
3293 case nir_intrinsic_load_shared
:
3294 result
= visit_load_shared(ctx
, instr
);
3296 case nir_intrinsic_store_shared
:
3297 visit_store_shared(ctx
, instr
);
3299 case nir_intrinsic_bindless_image_samples
:
3300 result
= visit_image_samples(ctx
, instr
, true);
3302 case nir_intrinsic_image_deref_samples
:
3303 result
= visit_image_samples(ctx
, instr
, false);
3305 case nir_intrinsic_bindless_image_load
:
3306 result
= visit_image_load(ctx
, instr
, true);
3308 case nir_intrinsic_image_deref_load
:
3309 result
= visit_image_load(ctx
, instr
, false);
3311 case nir_intrinsic_bindless_image_store
:
3312 visit_image_store(ctx
, instr
, true);
3314 case nir_intrinsic_image_deref_store
:
3315 visit_image_store(ctx
, instr
, false);
3317 case nir_intrinsic_bindless_image_atomic_add
:
3318 case nir_intrinsic_bindless_image_atomic_min
:
3319 case nir_intrinsic_bindless_image_atomic_max
:
3320 case nir_intrinsic_bindless_image_atomic_and
:
3321 case nir_intrinsic_bindless_image_atomic_or
:
3322 case nir_intrinsic_bindless_image_atomic_xor
:
3323 case nir_intrinsic_bindless_image_atomic_exchange
:
3324 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3325 result
= visit_image_atomic(ctx
, instr
, true);
3327 case nir_intrinsic_image_deref_atomic_add
:
3328 case nir_intrinsic_image_deref_atomic_min
:
3329 case nir_intrinsic_image_deref_atomic_max
:
3330 case nir_intrinsic_image_deref_atomic_and
:
3331 case nir_intrinsic_image_deref_atomic_or
:
3332 case nir_intrinsic_image_deref_atomic_xor
:
3333 case nir_intrinsic_image_deref_atomic_exchange
:
3334 case nir_intrinsic_image_deref_atomic_comp_swap
:
3335 result
= visit_image_atomic(ctx
, instr
, false);
3337 case nir_intrinsic_bindless_image_size
:
3338 result
= visit_image_size(ctx
, instr
, true);
3340 case nir_intrinsic_image_deref_size
:
3341 result
= visit_image_size(ctx
, instr
, false);
3343 case nir_intrinsic_shader_clock
:
3344 result
= ac_build_shader_clock(&ctx
->ac
);
3346 case nir_intrinsic_discard
:
3347 case nir_intrinsic_discard_if
:
3348 emit_discard(ctx
, instr
);
3350 case nir_intrinsic_memory_barrier
:
3351 case nir_intrinsic_group_memory_barrier
:
3352 case nir_intrinsic_memory_barrier_atomic_counter
:
3353 case nir_intrinsic_memory_barrier_buffer
:
3354 case nir_intrinsic_memory_barrier_image
:
3355 case nir_intrinsic_memory_barrier_shared
:
3356 emit_membar(&ctx
->ac
, instr
);
3358 case nir_intrinsic_barrier
:
3359 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3361 case nir_intrinsic_shared_atomic_add
:
3362 case nir_intrinsic_shared_atomic_imin
:
3363 case nir_intrinsic_shared_atomic_umin
:
3364 case nir_intrinsic_shared_atomic_imax
:
3365 case nir_intrinsic_shared_atomic_umax
:
3366 case nir_intrinsic_shared_atomic_and
:
3367 case nir_intrinsic_shared_atomic_or
:
3368 case nir_intrinsic_shared_atomic_xor
:
3369 case nir_intrinsic_shared_atomic_exchange
:
3370 case nir_intrinsic_shared_atomic_comp_swap
: {
3371 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3372 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3375 case nir_intrinsic_deref_atomic_add
:
3376 case nir_intrinsic_deref_atomic_imin
:
3377 case nir_intrinsic_deref_atomic_umin
:
3378 case nir_intrinsic_deref_atomic_imax
:
3379 case nir_intrinsic_deref_atomic_umax
:
3380 case nir_intrinsic_deref_atomic_and
:
3381 case nir_intrinsic_deref_atomic_or
:
3382 case nir_intrinsic_deref_atomic_xor
:
3383 case nir_intrinsic_deref_atomic_exchange
:
3384 case nir_intrinsic_deref_atomic_comp_swap
: {
3385 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3386 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3389 case nir_intrinsic_interp_deref_at_centroid
:
3390 case nir_intrinsic_interp_deref_at_sample
:
3391 case nir_intrinsic_interp_deref_at_offset
:
3392 result
= visit_interp(ctx
, instr
);
3394 case nir_intrinsic_emit_vertex
:
3395 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3397 case nir_intrinsic_end_primitive
:
3398 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3400 case nir_intrinsic_load_tess_coord
:
3401 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3403 case nir_intrinsic_load_tess_level_outer
:
3404 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3406 case nir_intrinsic_load_tess_level_inner
:
3407 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3409 case nir_intrinsic_load_patch_vertices_in
:
3410 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3412 case nir_intrinsic_vote_all
: {
3413 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3414 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3417 case nir_intrinsic_vote_any
: {
3418 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3419 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3422 case nir_intrinsic_shuffle
:
3423 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3424 get_src(ctx
, instr
->src
[1]));
3426 case nir_intrinsic_reduce
:
3427 result
= ac_build_reduce(&ctx
->ac
,
3428 get_src(ctx
, instr
->src
[0]),
3429 instr
->const_index
[0],
3430 instr
->const_index
[1]);
3432 case nir_intrinsic_inclusive_scan
:
3433 result
= ac_build_inclusive_scan(&ctx
->ac
,
3434 get_src(ctx
, instr
->src
[0]),
3435 instr
->const_index
[0]);
3437 case nir_intrinsic_exclusive_scan
:
3438 result
= ac_build_exclusive_scan(&ctx
->ac
,
3439 get_src(ctx
, instr
->src
[0]),
3440 instr
->const_index
[0]);
3442 case nir_intrinsic_quad_broadcast
: {
3443 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
3444 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3445 lane
, lane
, lane
, lane
);
3448 case nir_intrinsic_quad_swap_horizontal
:
3449 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3451 case nir_intrinsic_quad_swap_vertical
:
3452 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3454 case nir_intrinsic_quad_swap_diagonal
:
3455 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3457 case nir_intrinsic_quad_swizzle_amd
: {
3458 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3459 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3460 mask
& 0x3, (mask
>> 2) & 0x3,
3461 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
3464 case nir_intrinsic_masked_swizzle_amd
: {
3465 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3466 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
3469 case nir_intrinsic_write_invocation_amd
:
3470 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3471 get_src(ctx
, instr
->src
[1]),
3472 get_src(ctx
, instr
->src
[2]));
3474 case nir_intrinsic_mbcnt_amd
:
3475 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3478 fprintf(stderr
, "Unknown intrinsic: ");
3479 nir_print_instr(&instr
->instr
, stderr
);
3480 fprintf(stderr
, "\n");
3484 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3488 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3489 unsigned base_index
,
3490 unsigned constant_index
,
3491 LLVMValueRef dynamic_index
)
3493 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3494 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3495 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3497 /* Bindless uniforms are 64bit so multiple index by 8 */
3498 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3499 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3501 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3503 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3504 NULL
, 0, false, false, true, true);
3506 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3509 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3510 nir_deref_instr
*deref_instr
,
3511 enum ac_descriptor_type desc_type
,
3512 const nir_instr
*instr
,
3513 bool image
, bool write
)
3515 LLVMValueRef index
= NULL
;
3516 unsigned constant_index
= 0;
3517 unsigned descriptor_set
;
3518 unsigned base_index
;
3519 bool bindless
= false;
3524 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3527 index
= get_src(ctx
, img_instr
->src
[0]);
3529 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3530 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3531 nir_tex_src_sampler_handle
);
3532 if (sampSrcIdx
!= -1) {
3535 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3537 assert(tex_instr
&& !image
);
3538 base_index
= tex_instr
->sampler_index
;
3542 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3543 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3544 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3548 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3549 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
3551 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3553 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3554 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3559 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3562 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3563 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3564 unsigned sidx
= deref_instr
->strct
.index
;
3565 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3566 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3568 unreachable("Unsupported deref type");
3571 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3573 if (deref_instr
->var
->data
.bindless
) {
3574 /* For now just assert on unhandled variable types */
3575 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3577 base_index
= deref_instr
->var
->data
.driver_location
;
3580 index
= index
? index
: ctx
->ac
.i32_0
;
3581 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3582 constant_index
, index
);
3584 base_index
= deref_instr
->var
->data
.binding
;
3587 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3590 constant_index
, index
,
3591 desc_type
, image
, write
, bindless
);
3594 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3597 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3598 * filtering manually. The driver sets img7 to a mask clearing
3599 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3600 * s_and_b32 samp0, samp0, img7
3603 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3605 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3606 LLVMValueRef res
, LLVMValueRef samp
)
3608 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3609 LLVMValueRef img7
, samp0
;
3611 if (ctx
->ac
.chip_class
>= GFX8
)
3614 img7
= LLVMBuildExtractElement(builder
, res
,
3615 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3616 samp0
= LLVMBuildExtractElement(builder
, samp
,
3617 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3618 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3619 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3620 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3623 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3624 nir_tex_instr
*instr
,
3625 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3626 LLVMValueRef
*fmask_ptr
)
3628 nir_deref_instr
*texture_deref_instr
= NULL
;
3629 nir_deref_instr
*sampler_deref_instr
= NULL
;
3632 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3633 switch (instr
->src
[i
].src_type
) {
3634 case nir_tex_src_texture_deref
:
3635 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3637 case nir_tex_src_sampler_deref
:
3638 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3640 case nir_tex_src_plane
:
3641 plane
= nir_src_as_int(instr
->src
[i
].src
);
3648 if (!sampler_deref_instr
)
3649 sampler_deref_instr
= texture_deref_instr
;
3651 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
3654 assert(instr
->op
!= nir_texop_txf_ms
&&
3655 instr
->op
!= nir_texop_samples_identical
);
3656 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
3658 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
3661 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
, false, false);
3664 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3665 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3666 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3668 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3669 instr
->op
== nir_texop_samples_identical
))
3670 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3673 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3676 coord
= ac_to_float(ctx
, coord
);
3677 coord
= ac_build_round(ctx
, coord
);
3678 coord
= ac_to_integer(ctx
, coord
);
3682 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3684 LLVMValueRef result
= NULL
;
3685 struct ac_image_args args
= { 0 };
3686 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3687 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3688 unsigned offset_src
= 0;
3690 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3692 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3693 switch (instr
->src
[i
].src_type
) {
3694 case nir_tex_src_coord
: {
3695 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3696 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3697 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3700 case nir_tex_src_projector
:
3702 case nir_tex_src_comparator
:
3703 if (instr
->is_shadow
)
3704 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3706 case nir_tex_src_offset
:
3707 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3710 case nir_tex_src_bias
:
3711 if (instr
->op
== nir_texop_txb
)
3712 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3714 case nir_tex_src_lod
: {
3715 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
3716 args
.level_zero
= true;
3718 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3721 case nir_tex_src_ms_index
:
3722 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3724 case nir_tex_src_ms_mcs
:
3726 case nir_tex_src_ddx
:
3727 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3729 case nir_tex_src_ddy
:
3730 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3732 case nir_tex_src_texture_offset
:
3733 case nir_tex_src_sampler_offset
:
3734 case nir_tex_src_plane
:
3740 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3741 result
= get_buffer_size(ctx
, args
.resource
, true);
3745 if (instr
->op
== nir_texop_texture_samples
) {
3746 LLVMValueRef res
, samples
, is_msaa
;
3747 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3748 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3749 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3750 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3751 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3752 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3753 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3754 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3755 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3757 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3758 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3759 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3760 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3761 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3763 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3769 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3770 LLVMValueRef offset
[3], pack
;
3771 for (unsigned chan
= 0; chan
< 3; ++chan
)
3772 offset
[chan
] = ctx
->ac
.i32_0
;
3774 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3775 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3776 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3777 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3778 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3780 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3781 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3783 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3784 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3788 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3789 * so the depth comparison value isn't clamped for Z16 and
3790 * Z24 anymore. Do it manually here.
3792 * It's unnecessary if the original texture format was
3793 * Z32_FLOAT, but we don't know that here.
3795 if (args
.compare
&& ctx
->ac
.chip_class
>= GFX8
&& ctx
->abi
->clamp_shadow_reference
)
3796 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3798 /* pack derivatives */
3800 int num_src_deriv_channels
, num_dest_deriv_channels
;
3801 switch (instr
->sampler_dim
) {
3802 case GLSL_SAMPLER_DIM_3D
:
3803 case GLSL_SAMPLER_DIM_CUBE
:
3804 num_src_deriv_channels
= 3;
3805 num_dest_deriv_channels
= 3;
3807 case GLSL_SAMPLER_DIM_2D
:
3809 num_src_deriv_channels
= 2;
3810 num_dest_deriv_channels
= 2;
3812 case GLSL_SAMPLER_DIM_1D
:
3813 num_src_deriv_channels
= 1;
3814 if (ctx
->ac
.chip_class
>= GFX9
) {
3815 num_dest_deriv_channels
= 2;
3817 num_dest_deriv_channels
= 1;
3822 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3823 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3824 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3825 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3826 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3828 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3829 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3830 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3834 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3835 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3836 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3837 if (instr
->coord_components
== 3)
3838 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3839 ac_prepare_cube_coords(&ctx
->ac
,
3840 instr
->op
== nir_texop_txd
, instr
->is_array
,
3841 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3844 /* Texture coordinates fixups */
3845 if (instr
->coord_components
> 1 &&
3846 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3848 instr
->op
!= nir_texop_txf
) {
3849 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3852 if (instr
->coord_components
> 2 &&
3853 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3854 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3855 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3856 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3858 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3859 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3862 if (ctx
->ac
.chip_class
>= GFX9
&&
3863 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3864 instr
->op
!= nir_texop_lod
) {
3865 LLVMValueRef filler
;
3866 if (instr
->op
== nir_texop_txf
)
3867 filler
= ctx
->ac
.i32_0
;
3869 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3871 if (instr
->is_array
)
3872 args
.coords
[2] = args
.coords
[1];
3873 args
.coords
[1] = filler
;
3876 /* Pack sample index */
3877 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3878 args
.coords
[instr
->coord_components
] = sample_index
;
3880 if (instr
->op
== nir_texop_samples_identical
) {
3881 struct ac_image_args txf_args
= { 0 };
3882 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3884 txf_args
.dmask
= 0xf;
3885 txf_args
.resource
= fmask_ptr
;
3886 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3887 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3889 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3890 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3894 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3895 instr
->op
!= nir_texop_txs
) {
3896 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3897 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3898 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3899 instr
->is_array
? args
.coords
[2] : NULL
,
3900 args
.coords
[sample_chan
], fmask_ptr
);
3903 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3904 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3905 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3906 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3907 args
.coords
[i
] = LLVMBuildAdd(
3908 ctx
->ac
.builder
, args
.coords
[i
],
3909 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
3914 /* DMASK was repurposed for GATHER4. 4 components are always
3915 * returned and DMASK works like a swizzle - it selects
3916 * the component to fetch. The only valid DMASK values are
3917 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3918 * (red,red,red,red) etc.) The ISA document doesn't mention
3922 if (instr
->op
== nir_texop_tg4
) {
3923 if (instr
->is_shadow
)
3926 args
.dmask
= 1 << instr
->component
;
3929 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3930 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3931 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3933 if (instr
->op
== nir_texop_query_levels
)
3934 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3935 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3936 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3937 instr
->op
!= nir_texop_tg4
)
3938 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3939 else if (instr
->op
== nir_texop_txs
&&
3940 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3942 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3943 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3944 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3945 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3946 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3947 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3948 instr
->op
== nir_texop_txs
&&
3949 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3951 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3952 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3953 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3955 } else if (instr
->dest
.ssa
.num_components
!= 4)
3956 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3960 assert(instr
->dest
.is_ssa
);
3961 result
= ac_to_integer(&ctx
->ac
, result
);
3962 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3967 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3969 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3970 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3972 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3973 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3976 static void visit_post_phi(struct ac_nir_context
*ctx
,
3977 nir_phi_instr
*instr
,
3978 LLVMValueRef llvm_phi
)
3980 nir_foreach_phi_src(src
, instr
) {
3981 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3982 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3984 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3988 static void phi_post_pass(struct ac_nir_context
*ctx
)
3990 hash_table_foreach(ctx
->phis
, entry
) {
3991 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3992 (LLVMValueRef
)entry
->data
);
3997 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3998 const nir_ssa_undef_instr
*instr
)
4000 unsigned num_components
= instr
->def
.num_components
;
4001 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4004 if (num_components
== 1)
4005 undef
= LLVMGetUndef(type
);
4007 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4009 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4012 static void visit_jump(struct ac_llvm_context
*ctx
,
4013 const nir_jump_instr
*instr
)
4015 switch (instr
->type
) {
4016 case nir_jump_break
:
4017 ac_build_break(ctx
);
4019 case nir_jump_continue
:
4020 ac_build_continue(ctx
);
4023 fprintf(stderr
, "Unknown NIR jump instr: ");
4024 nir_print_instr(&instr
->instr
, stderr
);
4025 fprintf(stderr
, "\n");
4031 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4032 enum glsl_base_type type
)
4036 case GLSL_TYPE_UINT
:
4037 case GLSL_TYPE_BOOL
:
4038 case GLSL_TYPE_SUBROUTINE
:
4040 case GLSL_TYPE_INT8
:
4041 case GLSL_TYPE_UINT8
:
4043 case GLSL_TYPE_INT16
:
4044 case GLSL_TYPE_UINT16
:
4046 case GLSL_TYPE_FLOAT
:
4048 case GLSL_TYPE_FLOAT16
:
4050 case GLSL_TYPE_INT64
:
4051 case GLSL_TYPE_UINT64
:
4053 case GLSL_TYPE_DOUBLE
:
4056 unreachable("unknown GLSL type");
4061 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4062 const struct glsl_type
*type
)
4064 if (glsl_type_is_scalar(type
)) {
4065 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4068 if (glsl_type_is_vector(type
)) {
4069 return LLVMVectorType(
4070 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4071 glsl_get_vector_elements(type
));
4074 if (glsl_type_is_matrix(type
)) {
4075 return LLVMArrayType(
4076 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4077 glsl_get_matrix_columns(type
));
4080 if (glsl_type_is_array(type
)) {
4081 return LLVMArrayType(
4082 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4083 glsl_get_length(type
));
4086 assert(glsl_type_is_struct_or_ifc(type
));
4088 LLVMTypeRef member_types
[glsl_get_length(type
)];
4090 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4092 glsl_to_llvm_type(ac
,
4093 glsl_get_struct_field(type
, i
));
4096 return LLVMStructTypeInContext(ac
->context
, member_types
,
4097 glsl_get_length(type
), false);
4100 static void visit_deref(struct ac_nir_context
*ctx
,
4101 nir_deref_instr
*instr
)
4103 if (instr
->mode
!= nir_var_mem_shared
&&
4104 instr
->mode
!= nir_var_mem_global
)
4107 LLVMValueRef result
= NULL
;
4108 switch(instr
->deref_type
) {
4109 case nir_deref_type_var
: {
4110 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4111 result
= entry
->data
;
4114 case nir_deref_type_struct
:
4115 if (instr
->mode
== nir_var_mem_global
) {
4116 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4117 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4118 instr
->strct
.index
);
4119 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4120 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4122 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4123 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4126 case nir_deref_type_array
:
4127 if (instr
->mode
== nir_var_mem_global
) {
4128 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4129 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4131 if ((glsl_type_is_matrix(parent
->type
) &&
4132 glsl_matrix_type_is_row_major(parent
->type
)) ||
4133 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4134 stride
= type_scalar_size_bytes(parent
->type
);
4137 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4138 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4139 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4141 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4143 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4145 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4146 get_src(ctx
, instr
->arr
.index
));
4149 case nir_deref_type_ptr_as_array
:
4150 if (instr
->mode
== nir_var_mem_global
) {
4151 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4153 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4154 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4155 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4157 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4159 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4161 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4162 get_src(ctx
, instr
->arr
.index
));
4165 case nir_deref_type_cast
: {
4166 result
= get_src(ctx
, instr
->parent
);
4168 /* We can't use the structs from LLVM because the shader
4169 * specifies its own offsets. */
4170 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4171 if (instr
->mode
== nir_var_mem_shared
)
4172 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4174 unsigned address_space
;
4176 switch(instr
->mode
) {
4177 case nir_var_mem_shared
:
4178 address_space
= AC_ADDR_SPACE_LDS
;
4180 case nir_var_mem_global
:
4181 address_space
= AC_ADDR_SPACE_GLOBAL
;
4184 unreachable("Unhandled address space");
4187 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4189 if (LLVMTypeOf(result
) != type
) {
4190 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4191 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4194 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4201 unreachable("Unhandled deref_instr deref type");
4204 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4207 static void visit_cf_list(struct ac_nir_context
*ctx
,
4208 struct exec_list
*list
);
4210 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4212 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4213 nir_foreach_instr(instr
, block
)
4215 switch (instr
->type
) {
4216 case nir_instr_type_alu
:
4217 visit_alu(ctx
, nir_instr_as_alu(instr
));
4219 case nir_instr_type_load_const
:
4220 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4222 case nir_instr_type_intrinsic
:
4223 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4225 case nir_instr_type_tex
:
4226 visit_tex(ctx
, nir_instr_as_tex(instr
));
4228 case nir_instr_type_phi
:
4229 visit_phi(ctx
, nir_instr_as_phi(instr
));
4231 case nir_instr_type_ssa_undef
:
4232 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4234 case nir_instr_type_jump
:
4235 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4237 case nir_instr_type_deref
:
4238 visit_deref(ctx
, nir_instr_as_deref(instr
));
4241 fprintf(stderr
, "Unknown NIR instr type: ");
4242 nir_print_instr(instr
, stderr
);
4243 fprintf(stderr
, "\n");
4248 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4251 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4253 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4255 nir_block
*then_block
=
4256 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4258 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4260 visit_cf_list(ctx
, &if_stmt
->then_list
);
4262 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4263 nir_block
*else_block
=
4264 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4266 ac_build_else(&ctx
->ac
, else_block
->index
);
4267 visit_cf_list(ctx
, &if_stmt
->else_list
);
4270 ac_build_endif(&ctx
->ac
, then_block
->index
);
4273 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4275 nir_block
*first_loop_block
=
4276 (nir_block
*) exec_list_get_head(&loop
->body
);
4278 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4280 visit_cf_list(ctx
, &loop
->body
);
4282 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4285 static void visit_cf_list(struct ac_nir_context
*ctx
,
4286 struct exec_list
*list
)
4288 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4290 switch (node
->type
) {
4291 case nir_cf_node_block
:
4292 visit_block(ctx
, nir_cf_node_as_block(node
));
4295 case nir_cf_node_if
:
4296 visit_if(ctx
, nir_cf_node_as_if(node
));
4299 case nir_cf_node_loop
:
4300 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4310 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4311 struct ac_shader_abi
*abi
,
4312 struct nir_shader
*nir
,
4313 struct nir_variable
*variable
,
4314 gl_shader_stage stage
)
4316 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4317 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4319 /* tess ctrl has it's own load/store paths for outputs */
4320 if (stage
== MESA_SHADER_TESS_CTRL
)
4323 if (stage
== MESA_SHADER_VERTEX
||
4324 stage
== MESA_SHADER_TESS_EVAL
||
4325 stage
== MESA_SHADER_GEOMETRY
) {
4326 int idx
= variable
->data
.location
+ variable
->data
.index
;
4327 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4328 int length
= nir
->info
.clip_distance_array_size
+
4329 nir
->info
.cull_distance_array_size
;
4338 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4339 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4340 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4341 for (unsigned chan
= 0; chan
< 4; chan
++) {
4342 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4343 ac_build_alloca_undef(ctx
, type
, "");
4349 setup_locals(struct ac_nir_context
*ctx
,
4350 struct nir_function
*func
)
4353 ctx
->num_locals
= 0;
4354 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4355 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4356 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4357 variable
->data
.location_frac
= 0;
4358 ctx
->num_locals
+= attrib_count
;
4360 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4364 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4365 for (j
= 0; j
< 4; j
++) {
4366 ctx
->locals
[i
* 4 + j
] =
4367 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4373 setup_shared(struct ac_nir_context
*ctx
,
4374 struct nir_shader
*nir
)
4376 nir_foreach_variable(variable
, &nir
->shared
) {
4377 LLVMValueRef shared
=
4378 LLVMAddGlobalInAddressSpace(
4379 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4380 variable
->name
? variable
->name
: "",
4382 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4386 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4387 struct nir_shader
*nir
)
4389 struct ac_nir_context ctx
= {};
4390 struct nir_function
*func
;
4395 ctx
.stage
= nir
->info
.stage
;
4396 ctx
.info
= &nir
->info
;
4398 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4400 nir_foreach_variable(variable
, &nir
->outputs
)
4401 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4404 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4405 _mesa_key_pointer_equal
);
4406 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4407 _mesa_key_pointer_equal
);
4408 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4409 _mesa_key_pointer_equal
);
4411 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4413 nir_index_ssa_defs(func
->impl
);
4414 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4416 setup_locals(&ctx
, func
);
4418 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4419 setup_shared(&ctx
, nir
);
4421 visit_cf_list(&ctx
, &func
->impl
->body
);
4422 phi_post_pass(&ctx
);
4424 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4425 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4430 ralloc_free(ctx
.defs
);
4431 ralloc_free(ctx
.phis
);
4432 ralloc_free(ctx
.vars
);
4436 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4438 /* While it would be nice not to have this flag, we are constrained
4439 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4442 bool llvm_has_working_vgpr_indexing
= chip_class
<= GFX8
;
4444 /* TODO: Indirect indexing of GS inputs is unimplemented.
4446 * TCS and TES load inputs directly from LDS or offchip memory, so
4447 * indirect indexing is trivial.
4449 nir_variable_mode indirect_mask
= 0;
4450 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4451 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4452 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4453 !llvm_has_working_vgpr_indexing
)) {
4454 indirect_mask
|= nir_var_shader_in
;
4456 if (!llvm_has_working_vgpr_indexing
&&
4457 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4458 indirect_mask
|= nir_var_shader_out
;
4460 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4461 * smart enough to handle indirects without causing excess spilling
4462 * causing the gpu to hang.
4464 * See the following thread for more details of the problem:
4465 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4467 indirect_mask
|= nir_var_function_temp
;
4469 nir_lower_indirect_derefs(nir
, indirect_mask
);
4473 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4475 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4479 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4481 if (var
->data
.mode
!= nir_var_shader_out
)
4484 unsigned writemask
= 0;
4485 const int location
= var
->data
.location
;
4486 unsigned first_component
= var
->data
.location_frac
;
4487 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4489 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4490 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4491 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4492 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4498 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4499 unsigned *cond_block_tf_writemask
,
4500 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4502 switch (cf_node
->type
) {
4503 case nir_cf_node_block
: {
4504 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4505 nir_foreach_instr(instr
, block
) {
4506 if (instr
->type
!= nir_instr_type_intrinsic
)
4509 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4510 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4512 /* If we find a barrier in nested control flow put this in the
4513 * too hard basket. In GLSL this is not possible but it is in
4517 *tessfactors_are_def_in_all_invocs
= false;
4521 /* The following case must be prevented:
4522 * gl_TessLevelInner = ...;
4524 * if (gl_InvocationID == 1)
4525 * gl_TessLevelInner = ...;
4527 * If you consider disjoint code segments separated by barriers, each
4528 * such segment that writes tess factor channels should write the same
4529 * channels in all codepaths within that segment.
4531 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4532 /* Accumulate the result: */
4533 *tessfactors_are_def_in_all_invocs
&=
4534 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4536 /* Analyze the next code segment from scratch. */
4537 *upper_block_tf_writemask
= 0;
4538 *cond_block_tf_writemask
= 0;
4541 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4546 case nir_cf_node_if
: {
4547 unsigned then_tessfactor_writemask
= 0;
4548 unsigned else_tessfactor_writemask
= 0;
4550 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4551 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4552 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4553 cond_block_tf_writemask
,
4554 tessfactors_are_def_in_all_invocs
, true);
4557 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4558 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4559 cond_block_tf_writemask
,
4560 tessfactors_are_def_in_all_invocs
, true);
4563 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4564 /* If both statements write the same tess factor channels,
4565 * we can say that the upper block writes them too.
4567 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4568 else_tessfactor_writemask
;
4569 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4570 else_tessfactor_writemask
;
4575 case nir_cf_node_loop
: {
4576 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4577 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4578 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4579 cond_block_tf_writemask
,
4580 tessfactors_are_def_in_all_invocs
, true);
4586 unreachable("unknown cf node type");
4591 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4593 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4595 /* The pass works as follows:
4596 * If all codepaths write tess factors, we can say that all
4597 * invocations define tess factors.
4599 * Each tess factor channel is tracked separately.
4601 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4602 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4604 /* Initial value = true. Here the pass will accumulate results from
4605 * multiple segments surrounded by barriers. If tess factors aren't
4606 * written at all, it's a shader bug and we don't care if this will be
4609 bool tessfactors_are_def_in_all_invocs
= true;
4611 nir_foreach_function(function
, nir
) {
4612 if (function
->impl
) {
4613 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4614 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4615 &cond_block_tf_writemask
,
4616 &tessfactors_are_def_in_all_invocs
,
4622 /* Accumulate the result for the last code segment separated by a
4625 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4626 tessfactors_are_def_in_all_invocs
&=
4627 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4630 return tessfactors_are_def_in_all_invocs
;