2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
36 struct ac_nir_context
{
37 struct ac_llvm_context ac
;
38 struct ac_shader_abi
*abi
;
40 gl_shader_stage stage
;
42 LLVMValueRef
*ssa_defs
;
44 struct hash_table
*defs
;
45 struct hash_table
*phis
;
46 struct hash_table
*vars
;
48 LLVMValueRef main_function
;
49 LLVMBasicBlockRef continue_block
;
50 LLVMBasicBlockRef break_block
;
56 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
273 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
275 return LLVMBuildSelect(ctx
->builder
, v
,
276 ac_to_integer_or_pointer(ctx
, src1
),
277 ac_to_integer_or_pointer(ctx
, src2
), "");
280 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
281 LLVMIntPredicate pred
,
282 LLVMValueRef src0
, LLVMValueRef src1
)
284 return LLVMBuildSelect(ctx
->builder
,
285 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
290 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
293 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
294 LLVMBuildNeg(ctx
->builder
, src0
, ""));
297 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
299 LLVMValueRef src0
, LLVMValueRef src1
)
301 LLVMTypeRef ret_type
;
302 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
304 LLVMValueRef params
[] = { src0
, src1
};
305 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
308 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
309 params
, 2, AC_FUNC_ATTR_READNONE
);
311 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
312 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
316 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
320 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
321 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
323 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
328 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
331 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
334 src0
= ac_to_float(ctx
, src0
);
335 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
336 return LLVMBuildSExt(ctx
->builder
,
337 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
341 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
345 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
350 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
353 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
356 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
357 return LLVMBuildSExt(ctx
->builder
,
358 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
362 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
366 LLVMValueRef cond
= NULL
;
368 src0
= ac_to_float(ctx
, src0
);
369 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
371 if (ctx
->chip_class
>= VI
) {
372 LLVMValueRef args
[2];
373 /* Check if the result is a denormal - and flush to 0 if so. */
375 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
376 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
379 /* need to convert back up to f32 */
380 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
382 if (ctx
->chip_class
>= VI
)
383 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
386 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
387 * so compare the result and flush to 0 if it's smaller.
389 LLVMValueRef temp
, cond2
;
390 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
391 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
392 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
394 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
395 temp
, ctx
->f32_0
, "");
396 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
397 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
402 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
403 LLVMValueRef src0
, LLVMValueRef src1
)
405 LLVMValueRef dst64
, result
;
406 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
407 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
409 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
410 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
411 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
415 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
416 LLVMValueRef src0
, LLVMValueRef src1
)
418 LLVMValueRef dst64
, result
;
419 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
420 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
422 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
423 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
424 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
428 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
430 const LLVMValueRef srcs
[3])
434 if (HAVE_LLVM
>= 0x0800) {
435 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
436 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
437 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
439 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
440 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
442 LLVMValueRef zero
= ctx
->i32_0
;
443 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
444 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
446 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
447 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
448 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
454 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
455 LLVMValueRef src0
, LLVMValueRef src1
,
456 LLVMValueRef src2
, LLVMValueRef src3
)
458 LLVMValueRef bfi_args
[3], result
;
460 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
461 LLVMBuildSub(ctx
->builder
,
462 LLVMBuildShl(ctx
->builder
,
467 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
470 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
473 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
474 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
476 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
477 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
478 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
480 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
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
);
541 * this takes an I,J coordinate pair,
542 * and works out the X and Y derivatives.
543 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
545 static LLVMValueRef
emit_ddxy_interp(
546 struct ac_nir_context
*ctx
,
547 LLVMValueRef interp_ij
)
549 LLVMValueRef result
[4], a
;
552 for (i
= 0; i
< 2; i
++) {
553 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
554 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
555 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
556 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
558 return ac_build_gather_values(&ctx
->ac
, result
, 4);
561 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
563 LLVMValueRef src
[4], result
= NULL
;
564 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
565 unsigned src_components
;
566 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
568 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
575 case nir_op_pack_half_2x16
:
578 case nir_op_unpack_half_2x16
:
581 case nir_op_cube_face_coord
:
582 case nir_op_cube_face_index
:
586 src_components
= num_components
;
589 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
590 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
598 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
599 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
602 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
605 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
608 result
= LLVMBuildAdd(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
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
616 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
617 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
618 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
621 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
624 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
627 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
630 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
633 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
634 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
635 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
636 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
637 ac_to_float_type(&ctx
->ac
, def_type
), result
);
638 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
639 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
642 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
643 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
644 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
647 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
650 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
653 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
656 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
657 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
658 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
661 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
662 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
665 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
668 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
671 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
674 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
675 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
676 LLVMTypeOf(src
[0]), ""),
680 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
681 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
682 LLVMTypeOf(src
[0]), ""),
686 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
687 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
688 LLVMTypeOf(src
[0]), ""),
692 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
701 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
704 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
707 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
710 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
713 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
716 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
719 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
722 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
723 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
726 result
= emit_iabs(&ctx
->ac
, src
[0]);
729 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
732 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
735 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
738 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
741 result
= ac_build_isign(&ctx
->ac
, src
[0],
742 instr
->dest
.dest
.ssa
.bit_size
);
745 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
746 result
= ac_build_fsign(&ctx
->ac
, src
[0],
747 instr
->dest
.dest
.ssa
.bit_size
);
750 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
751 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
754 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
755 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
758 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
759 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
761 case nir_op_fround_even
:
762 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
763 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
766 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
767 result
= ac_build_fract(&ctx
->ac
, src
[0],
768 instr
->dest
.dest
.ssa
.bit_size
);
771 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
772 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
775 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
776 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
779 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
780 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
783 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
784 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
787 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
788 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
791 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
792 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
793 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
795 case nir_op_frexp_exp
:
796 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
797 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
798 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
801 case nir_op_frexp_sig
:
802 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
803 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
804 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
807 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
808 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
811 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
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_2f_param(&ctx
->ac
, "llvm.minnum",
823 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
824 if (ctx
->ac
.chip_class
< GFX9
&&
825 instr
->dest
.dest
.ssa
.bit_size
== 32) {
826 /* Only pre-GFX9 chips do not flush denorms. */
827 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
828 ac_to_float_type(&ctx
->ac
, def_type
),
833 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
834 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
837 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
838 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
839 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
840 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
841 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
843 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
845 case nir_op_ibitfield_extract
:
846 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
848 case nir_op_ubitfield_extract
:
849 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
851 case nir_op_bitfield_insert
:
852 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
854 case nir_op_bitfield_reverse
:
855 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
857 case nir_op_bit_count
:
858 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
863 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
864 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
865 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
876 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
877 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
888 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
889 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
891 case nir_op_f2f16_rtz
:
892 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
893 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
894 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
895 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
896 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
897 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
899 case nir_op_f2f16_rtne
:
903 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
904 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
905 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
907 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
912 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
913 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
914 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
916 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
921 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
922 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
923 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
925 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
928 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
930 case nir_op_find_lsb
:
931 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
932 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
934 case nir_op_ufind_msb
:
935 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
936 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
938 case nir_op_ifind_msb
:
939 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
940 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
942 case nir_op_uadd_carry
:
943 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
944 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
945 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
947 case nir_op_usub_borrow
:
948 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
949 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
950 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
955 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
958 result
= emit_f2b(&ctx
->ac
, src
[0]);
963 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
966 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
967 result
= emit_i2b(&ctx
->ac
, src
[0]);
969 case nir_op_fquantize2f16
:
970 result
= emit_f2f16(&ctx
->ac
, src
[0]);
972 case nir_op_umul_high
:
973 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
974 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
975 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
977 case nir_op_imul_high
:
978 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
979 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
980 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
982 case nir_op_pack_half_2x16
:
983 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
985 case nir_op_unpack_half_2x16
:
986 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
990 case nir_op_fddx_fine
:
991 case nir_op_fddy_fine
:
992 case nir_op_fddx_coarse
:
993 case nir_op_fddy_coarse
:
994 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
997 case nir_op_unpack_64_2x32_split_x
: {
998 assert(ac_get_llvm_num_components(src
[0]) == 1);
999 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1002 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1007 case nir_op_unpack_64_2x32_split_y
: {
1008 assert(ac_get_llvm_num_components(src
[0]) == 1);
1009 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1012 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1017 case nir_op_pack_64_2x32_split
: {
1018 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1019 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1020 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1024 case nir_op_cube_face_coord
: {
1025 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1026 LLVMValueRef results
[2];
1028 for (unsigned chan
= 0; chan
< 3; chan
++)
1029 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1030 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1031 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1032 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1033 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1034 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1038 case nir_op_cube_face_index
: {
1039 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1041 for (unsigned chan
= 0; chan
< 3; chan
++)
1042 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1043 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1044 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1049 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1050 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1051 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1052 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1055 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1056 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1059 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1060 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1063 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1064 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1065 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1066 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1069 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1070 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1073 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1074 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1076 case nir_op_fmed3
: {
1077 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1078 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1079 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1080 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1081 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1082 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1083 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1084 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1087 case nir_op_imed3
: {
1088 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1089 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1090 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1091 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1094 case nir_op_umed3
: {
1095 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1096 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1097 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1098 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1103 fprintf(stderr
, "Unknown NIR alu instr: ");
1104 nir_print_instr(&instr
->instr
, stderr
);
1105 fprintf(stderr
, "\n");
1110 assert(instr
->dest
.dest
.is_ssa
);
1111 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1112 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1116 static void visit_load_const(struct ac_nir_context
*ctx
,
1117 const nir_load_const_instr
*instr
)
1119 LLVMValueRef values
[4], value
= NULL
;
1120 LLVMTypeRef element_type
=
1121 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1123 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1124 switch (instr
->def
.bit_size
) {
1126 values
[i
] = LLVMConstInt(element_type
,
1127 instr
->value
.u8
[i
], false);
1130 values
[i
] = LLVMConstInt(element_type
,
1131 instr
->value
.u16
[i
], false);
1134 values
[i
] = LLVMConstInt(element_type
,
1135 instr
->value
.u32
[i
], false);
1138 values
[i
] = LLVMConstInt(element_type
,
1139 instr
->value
.u64
[i
], false);
1143 "unsupported nir load_const bit_size: %d\n",
1144 instr
->def
.bit_size
);
1148 if (instr
->def
.num_components
> 1) {
1149 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1153 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1157 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1160 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1161 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1164 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1165 /* On VI, the descriptor contains the size in bytes,
1166 * but TXQ must return the size in elements.
1167 * The stride is always non-zero for resources using TXQ.
1169 LLVMValueRef stride
=
1170 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1172 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1173 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1174 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1175 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1177 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1182 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1184 struct ac_image_args
*args
,
1185 const nir_tex_instr
*instr
)
1187 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1188 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1189 LLVMValueRef half_texel
[2];
1190 LLVMValueRef compare_cube_wa
= NULL
;
1191 LLVMValueRef result
;
1195 struct ac_image_args txq_args
= { 0 };
1197 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1198 txq_args
.opcode
= ac_image_get_resinfo
;
1199 txq_args
.dmask
= 0xf;
1200 txq_args
.lod
= ctx
->i32_0
;
1201 txq_args
.resource
= args
->resource
;
1202 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1203 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1205 for (unsigned c
= 0; c
< 2; c
++) {
1206 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1207 LLVMConstInt(ctx
->i32
, c
, false), "");
1208 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1209 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1210 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1211 LLVMConstReal(ctx
->f32
, -0.5), "");
1215 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1217 for (unsigned c
= 0; c
< 2; c
++) {
1219 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1220 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1224 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1225 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1226 * workaround by sampling using a scaled type and converting.
1227 * This is taken from amdgpu-pro shaders.
1229 /* NOTE this produces some ugly code compared to amdgpu-pro,
1230 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1231 * and then reads them back. -pro generates two selects,
1232 * one s_cmp for the descriptor rewriting
1233 * one v_cmp for the coordinate and result changes.
1235 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1236 LLVMValueRef tmp
, tmp2
;
1238 /* workaround 8/8/8/8 uint/sint cube gather bug */
1239 /* first detect it then change to a scaled read and f2i */
1240 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1243 /* extract the DATA_FORMAT */
1244 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1245 LLVMConstInt(ctx
->i32
, 6, false), false);
1247 /* is the DATA_FORMAT == 8_8_8_8 */
1248 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1250 if (stype
== GLSL_TYPE_UINT
)
1251 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1252 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1253 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1255 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1256 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1257 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1259 /* replace the NUM FORMAT in the descriptor */
1260 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1261 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1263 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1265 /* don't modify the coordinates for this case */
1266 for (unsigned c
= 0; c
< 2; ++c
)
1267 args
->coords
[c
] = LLVMBuildSelect(
1268 ctx
->builder
, compare_cube_wa
,
1269 orig_coords
[c
], args
->coords
[c
], "");
1272 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1273 result
= ac_build_image_opcode(ctx
, args
);
1275 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1276 LLVMValueRef tmp
, tmp2
;
1278 /* if the cube workaround is in place, f2i the result. */
1279 for (unsigned c
= 0; c
< 4; c
++) {
1280 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1281 if (stype
== GLSL_TYPE_UINT
)
1282 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1284 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1285 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1286 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1287 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1288 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1289 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1295 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1297 nir_deref_instr
*texture_deref_instr
= NULL
;
1299 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1300 switch (instr
->src
[i
].src_type
) {
1301 case nir_tex_src_texture_deref
:
1302 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1308 return texture_deref_instr
;
1311 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1312 const nir_tex_instr
*instr
,
1313 struct ac_image_args
*args
)
1315 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1316 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1318 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1319 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1323 util_last_bit(mask
),
1326 return ac_build_buffer_load_format(&ctx
->ac
,
1330 util_last_bit(mask
),
1335 args
->opcode
= ac_image_sample
;
1337 switch (instr
->op
) {
1339 case nir_texop_txf_ms
:
1340 case nir_texop_samples_identical
:
1341 args
->opcode
= args
->level_zero
||
1342 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1343 ac_image_load
: ac_image_load_mip
;
1344 args
->level_zero
= false;
1347 case nir_texop_query_levels
:
1348 args
->opcode
= ac_image_get_resinfo
;
1350 args
->lod
= ctx
->ac
.i32_0
;
1351 args
->level_zero
= false;
1354 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1356 args
->level_zero
= true;
1360 args
->opcode
= ac_image_gather4
;
1361 args
->level_zero
= true;
1364 args
->opcode
= ac_image_get_lod
;
1370 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1371 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1372 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1373 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1374 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1375 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1376 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1380 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1381 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1382 if ((args
->dim
== ac_image_2darray
||
1383 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1384 args
->coords
[1] = ctx
->ac
.i32_0
;
1388 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1389 return ac_build_image_opcode(&ctx
->ac
, args
);
1392 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1393 nir_intrinsic_instr
*instr
)
1395 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1396 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1398 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1399 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1403 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1404 nir_intrinsic_instr
*instr
)
1406 LLVMValueRef ptr
, addr
;
1407 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1408 unsigned index
= nir_intrinsic_base(instr
);
1410 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1411 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1413 /* Load constant values from user SGPRS when possible, otherwise
1414 * fallback to the default path that loads directly from memory.
1416 if (LLVMIsConstant(src0
) &&
1417 instr
->dest
.ssa
.bit_size
== 32) {
1418 unsigned count
= instr
->dest
.ssa
.num_components
;
1419 unsigned offset
= index
;
1421 offset
+= LLVMConstIntGetZExtValue(src0
);
1424 offset
-= ctx
->abi
->base_inline_push_consts
;
1426 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1427 return ac_build_gather_values(&ctx
->ac
,
1428 ctx
->abi
->inline_push_consts
+ offset
,
1433 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1435 if (instr
->dest
.ssa
.bit_size
== 8) {
1436 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1437 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1438 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1439 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1441 LLVMValueRef params
[3];
1442 if (load_dwords
> 1) {
1443 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1444 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1445 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1447 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1448 params
[0] = ctx
->ac
.i32_0
;
1452 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1454 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1455 if (instr
->dest
.ssa
.num_components
> 1)
1456 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1458 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1459 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1460 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1461 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1462 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1463 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1464 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1465 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1466 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1467 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1468 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1469 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1470 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1471 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1472 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1473 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1474 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1477 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1479 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1482 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1483 const nir_intrinsic_instr
*instr
)
1485 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1487 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1490 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1492 uint32_t new_mask
= 0;
1493 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1494 if (mask
& (1u << i
))
1495 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1499 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1500 unsigned start
, unsigned count
)
1502 LLVMValueRef mask
[] = {
1503 ctx
->i32_0
, ctx
->i32_1
,
1504 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1506 unsigned src_elements
= ac_get_llvm_num_components(src
);
1508 if (count
== src_elements
) {
1511 } else if (count
== 1) {
1512 assert(start
< src_elements
);
1513 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1515 assert(start
+ count
<= src_elements
);
1517 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1518 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1522 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1523 enum gl_access_qualifier access
,
1524 bool may_store_unaligned
,
1525 bool writeonly_memory
)
1527 unsigned cache_policy
= 0;
1529 /* SI has a TC L1 bug causing corruption of 8bit/16bit stores. All
1530 * store opcodes not aligned to a dword are affected. The only way to
1531 * get unaligned stores is through shader images.
1533 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== SI
) ||
1534 /* If this is write-only, don't keep data in L1 to prevent
1535 * evicting L1 cache lines that may be needed by other
1539 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1540 cache_policy
|= ac_glc
;
1543 return cache_policy
;
1546 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1547 nir_intrinsic_instr
*instr
)
1549 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1550 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1551 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1552 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1553 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1554 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1556 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1557 get_src(ctx
, instr
->src
[1]), true);
1558 LLVMValueRef base_data
= src_data
;
1559 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1560 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1564 LLVMValueRef data
, offset
;
1565 LLVMTypeRef data_type
;
1567 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1569 /* Due to an LLVM limitation, split 3-element writes
1570 * into a 2-element and a 1-element write. */
1572 writemask
|= 1 << (start
+ 2);
1575 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1577 /* we can only store 4 DWords at the same time.
1578 * can only happen for 64 Bit vectors. */
1579 if (num_bytes
> 16) {
1580 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1585 /* check alignment of 16 Bit stores */
1586 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1587 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1591 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1593 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1594 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1596 if (num_bytes
== 1) {
1597 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1598 offset
, ctx
->ac
.i32_0
,
1599 cache_policy
& ac_glc
,
1601 } else if (num_bytes
== 2) {
1602 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1603 offset
, ctx
->ac
.i32_0
,
1604 cache_policy
& ac_glc
,
1607 int num_channels
= num_bytes
/ 4;
1609 switch (num_bytes
) {
1610 case 16: /* v4f32 */
1611 data_type
= ctx
->ac
.v4f32
;
1614 data_type
= ctx
->ac
.v2f32
;
1617 data_type
= ctx
->ac
.f32
;
1620 unreachable("Malformed vector store.");
1622 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1624 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1625 num_channels
, offset
,
1627 cache_policy
& ac_glc
,
1628 false, writeonly_memory
,
1634 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1635 const nir_intrinsic_instr
*instr
)
1637 const char *atomic_name
;
1638 char intrinsic_name
[64];
1639 LLVMValueRef params
[7];
1643 switch (instr
->intrinsic
) {
1644 case nir_intrinsic_ssbo_atomic_add
:
1645 atomic_name
= "add";
1647 case nir_intrinsic_ssbo_atomic_imin
:
1648 atomic_name
= "smin";
1650 case nir_intrinsic_ssbo_atomic_umin
:
1651 atomic_name
= "umin";
1653 case nir_intrinsic_ssbo_atomic_imax
:
1654 atomic_name
= "smax";
1656 case nir_intrinsic_ssbo_atomic_umax
:
1657 atomic_name
= "umax";
1659 case nir_intrinsic_ssbo_atomic_and
:
1660 atomic_name
= "and";
1662 case nir_intrinsic_ssbo_atomic_or
:
1665 case nir_intrinsic_ssbo_atomic_xor
:
1666 atomic_name
= "xor";
1668 case nir_intrinsic_ssbo_atomic_exchange
:
1669 atomic_name
= "swap";
1671 case nir_intrinsic_ssbo_atomic_comp_swap
:
1672 atomic_name
= "cmpswap";
1678 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1679 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1681 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1682 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1683 get_src(ctx
, instr
->src
[0]),
1686 if (HAVE_LLVM
>= 0x0800) {
1687 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1688 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1689 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1691 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
1692 "llvm.amdgcn.raw.buffer.atomic.%s.i32",
1695 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1696 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1697 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1699 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
1700 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
1703 assert(length
< sizeof(intrinsic_name
));
1704 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
1705 params
, arg_count
, 0);
1708 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1709 const nir_intrinsic_instr
*instr
)
1711 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1712 int num_components
= instr
->num_components
;
1713 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1714 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1716 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1717 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1718 get_src(ctx
, instr
->src
[0]), false);
1719 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1721 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1722 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1724 LLVMValueRef results
[4];
1725 for (int i
= 0; i
< num_components
;) {
1726 int num_elems
= num_components
- i
;
1727 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1729 if (num_elems
* elem_size_bytes
> 16)
1730 num_elems
= 16 / elem_size_bytes
;
1731 int load_bytes
= num_elems
* elem_size_bytes
;
1733 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1737 if (load_bytes
== 1) {
1738 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1743 cache_policy
& ac_glc
);
1744 } else if (load_bytes
== 2) {
1745 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1750 cache_policy
& ac_glc
);
1752 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1754 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1755 vindex
, offset
, immoffset
, 0,
1756 cache_policy
& ac_glc
, 0,
1760 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1761 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1762 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1764 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1765 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1767 for (unsigned j
= 0; j
< num_elems
; j
++) {
1768 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1773 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1776 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1777 const nir_intrinsic_instr
*instr
)
1780 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1781 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1782 int num_components
= instr
->num_components
;
1784 if (ctx
->abi
->load_ubo
)
1785 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1787 if (instr
->dest
.ssa
.bit_size
== 64)
1788 num_components
*= 2;
1790 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1791 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1792 LLVMValueRef results
[num_components
];
1793 for (unsigned i
= 0; i
< num_components
; ++i
) {
1794 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1797 if (load_bytes
== 1) {
1798 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1805 assert(load_bytes
== 2);
1806 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1814 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1816 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1817 NULL
, 0, false, false, true, true);
1819 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1822 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1823 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1827 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1828 bool vs_in
, unsigned *vertex_index_out
,
1829 LLVMValueRef
*vertex_index_ref
,
1830 unsigned *const_out
, LLVMValueRef
*indir_out
)
1832 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1833 nir_deref_path path
;
1834 unsigned idx_lvl
= 1;
1836 nir_deref_path_init(&path
, instr
, NULL
);
1838 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1839 if (vertex_index_ref
) {
1840 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1841 if (vertex_index_out
)
1842 *vertex_index_out
= 0;
1844 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1846 *vertex_index_out
= v
->u32
[0];
1851 uint32_t const_offset
= 0;
1852 LLVMValueRef offset
= NULL
;
1854 if (var
->data
.compact
) {
1855 assert(instr
->deref_type
== nir_deref_type_array
);
1856 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1858 const_offset
= v
->u32
[0];
1862 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1863 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1864 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1865 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1867 for (unsigned i
= 0; i
< index
; i
++) {
1868 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1869 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1871 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1872 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1873 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1874 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1876 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1880 unreachable("Uhandled deref type in get_deref_instr_offset");
1884 nir_deref_path_finish(&path
);
1886 if (const_offset
&& offset
)
1887 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1888 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1891 *const_out
= const_offset
;
1892 *indir_out
= offset
;
1895 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1896 nir_intrinsic_instr
*instr
,
1899 LLVMValueRef result
;
1900 LLVMValueRef vertex_index
= NULL
;
1901 LLVMValueRef indir_index
= NULL
;
1902 unsigned const_index
= 0;
1904 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1906 unsigned location
= var
->data
.location
;
1907 unsigned driver_location
= var
->data
.driver_location
;
1908 const bool is_patch
= var
->data
.patch
;
1909 const bool is_compact
= var
->data
.compact
;
1911 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1912 false, NULL
, is_patch
? NULL
: &vertex_index
,
1913 &const_index
, &indir_index
);
1915 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1917 LLVMTypeRef src_component_type
;
1918 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1919 src_component_type
= LLVMGetElementType(dest_type
);
1921 src_component_type
= dest_type
;
1923 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1924 vertex_index
, indir_index
,
1925 const_index
, location
, driver_location
,
1926 var
->data
.location_frac
,
1927 instr
->num_components
,
1928 is_patch
, is_compact
, load_inputs
);
1929 if (instr
->dest
.ssa
.bit_size
== 16) {
1930 result
= ac_to_integer(&ctx
->ac
, result
);
1931 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1933 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1937 type_scalar_size_bytes(const struct glsl_type
*type
)
1939 assert(glsl_type_is_vector_or_scalar(type
) ||
1940 glsl_type_is_matrix(type
));
1941 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1944 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1945 nir_intrinsic_instr
*instr
)
1947 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1948 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1950 LLVMValueRef values
[8];
1952 int ve
= instr
->dest
.ssa
.num_components
;
1954 LLVMValueRef indir_index
;
1956 unsigned const_index
;
1957 unsigned stride
= 4;
1958 int mode
= deref
->mode
;
1961 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1962 var
->data
.mode
== nir_var_shader_in
;
1963 idx
= var
->data
.driver_location
;
1964 comp
= var
->data
.location_frac
;
1965 mode
= var
->data
.mode
;
1967 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1968 &const_index
, &indir_index
);
1970 if (var
->data
.compact
) {
1972 const_index
+= comp
;
1977 if (instr
->dest
.ssa
.bit_size
== 64 &&
1978 (deref
->mode
== nir_var_shader_in
||
1979 deref
->mode
== nir_var_shader_out
||
1980 deref
->mode
== nir_var_function_temp
))
1984 case nir_var_shader_in
:
1985 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1986 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1987 return load_tess_varyings(ctx
, instr
, true);
1990 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1991 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1992 LLVMValueRef indir_index
;
1993 unsigned const_index
, vertex_index
;
1994 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
1995 &const_index
, &indir_index
);
1997 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1998 var
->data
.driver_location
,
1999 var
->data
.location_frac
,
2000 instr
->num_components
, vertex_index
, const_index
, type
);
2003 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2005 unsigned count
= glsl_count_attribute_slots(
2007 ctx
->stage
== MESA_SHADER_VERTEX
);
2009 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2010 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2011 stride
, false, true);
2013 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2017 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2020 case nir_var_function_temp
:
2021 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2023 unsigned count
= glsl_count_attribute_slots(
2026 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2027 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2028 stride
, true, true);
2030 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2034 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2038 case nir_var_mem_shared
: {
2039 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2040 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2041 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2042 get_def_type(ctx
, &instr
->dest
.ssa
),
2045 case nir_var_shader_out
:
2046 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2047 return load_tess_varyings(ctx
, instr
, false);
2050 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2052 unsigned count
= glsl_count_attribute_slots(
2055 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2056 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2057 stride
, true, true);
2059 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2063 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2064 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2069 case nir_var_mem_global
: {
2070 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2071 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2072 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2073 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2075 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2076 if (stride
!= natural_stride
) {
2077 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2078 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2079 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2081 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2082 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2083 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2084 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2086 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2088 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2089 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2090 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2091 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2096 unreachable("unhandle variable mode");
2098 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2099 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2103 visit_store_var(struct ac_nir_context
*ctx
,
2104 nir_intrinsic_instr
*instr
)
2106 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2107 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2109 LLVMValueRef temp_ptr
, value
;
2112 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2113 int writemask
= instr
->const_index
[0];
2114 LLVMValueRef indir_index
;
2115 unsigned const_index
;
2118 get_deref_offset(ctx
, deref
, false,
2119 NULL
, NULL
, &const_index
, &indir_index
);
2120 idx
= var
->data
.driver_location
;
2121 comp
= var
->data
.location_frac
;
2123 if (var
->data
.compact
) {
2124 const_index
+= comp
;
2129 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2130 (deref
->mode
== nir_var_shader_out
||
2131 deref
->mode
== nir_var_function_temp
)) {
2133 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2134 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2137 writemask
= widen_mask(writemask
, 2);
2140 writemask
= writemask
<< comp
;
2142 switch (deref
->mode
) {
2143 case nir_var_shader_out
:
2145 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2146 LLVMValueRef vertex_index
= NULL
;
2147 LLVMValueRef indir_index
= NULL
;
2148 unsigned const_index
= 0;
2149 const bool is_patch
= var
->data
.patch
;
2151 get_deref_offset(ctx
, deref
, false, NULL
,
2152 is_patch
? NULL
: &vertex_index
,
2153 &const_index
, &indir_index
);
2155 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2156 vertex_index
, indir_index
,
2157 const_index
, src
, writemask
);
2161 for (unsigned chan
= 0; chan
< 8; chan
++) {
2163 if (!(writemask
& (1 << chan
)))
2166 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2168 if (var
->data
.compact
)
2171 unsigned count
= glsl_count_attribute_slots(
2174 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2175 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2176 stride
, true, true);
2178 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2179 value
, indir_index
, "");
2180 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2181 count
, stride
, tmp_vec
);
2184 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2186 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2190 case nir_var_function_temp
:
2191 for (unsigned chan
= 0; chan
< 8; chan
++) {
2192 if (!(writemask
& (1 << chan
)))
2195 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2197 unsigned count
= glsl_count_attribute_slots(
2200 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2201 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2204 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2205 value
, indir_index
, "");
2206 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2209 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2211 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2216 case nir_var_mem_global
:
2217 case nir_var_mem_shared
: {
2218 int writemask
= instr
->const_index
[0];
2219 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2220 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2222 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2223 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2224 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2226 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2227 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2228 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2230 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2231 stride
== natural_stride
) {
2232 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2233 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2234 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2236 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2237 LLVMGetElementType(LLVMTypeOf(address
)), "");
2238 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2240 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2241 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2242 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2243 for (unsigned chan
= 0; chan
< 4; chan
++) {
2244 if (!(writemask
& (1 << chan
)))
2247 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2249 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2250 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2252 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2253 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2254 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2265 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2268 case GLSL_SAMPLER_DIM_BUF
:
2270 case GLSL_SAMPLER_DIM_1D
:
2271 return array
? 2 : 1;
2272 case GLSL_SAMPLER_DIM_2D
:
2273 return array
? 3 : 2;
2274 case GLSL_SAMPLER_DIM_MS
:
2275 return array
? 4 : 3;
2276 case GLSL_SAMPLER_DIM_3D
:
2277 case GLSL_SAMPLER_DIM_CUBE
:
2279 case GLSL_SAMPLER_DIM_RECT
:
2280 case GLSL_SAMPLER_DIM_SUBPASS
:
2282 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2291 /* Adjust the sample index according to FMASK.
2293 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2294 * which is the identity mapping. Each nibble says which physical sample
2295 * should be fetched to get that sample.
2297 * For example, 0x11111100 means there are only 2 samples stored and
2298 * the second sample covers 3/4 of the pixel. When reading samples 0
2299 * and 1, return physical sample 0 (determined by the first two 0s
2300 * in FMASK), otherwise return physical sample 1.
2302 * The sample index should be adjusted as follows:
2303 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2305 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2306 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2307 LLVMValueRef coord_z
,
2308 LLVMValueRef sample_index
,
2309 LLVMValueRef fmask_desc_ptr
)
2311 struct ac_image_args args
= {0};
2314 args
.coords
[0] = coord_x
;
2315 args
.coords
[1] = coord_y
;
2317 args
.coords
[2] = coord_z
;
2319 args
.opcode
= ac_image_load
;
2320 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2321 args
.resource
= fmask_desc_ptr
;
2323 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2325 res
= ac_build_image_opcode(ctx
, &args
);
2327 res
= ac_to_integer(ctx
, res
);
2328 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2329 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2331 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2335 LLVMValueRef sample_index4
=
2336 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2337 LLVMValueRef shifted_fmask
=
2338 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2339 LLVMValueRef final_sample
=
2340 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2342 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2343 * resource descriptor is 0 (invalid),
2345 LLVMValueRef fmask_desc
=
2346 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2349 LLVMValueRef fmask_word1
=
2350 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2353 LLVMValueRef word1_is_nonzero
=
2354 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2355 fmask_word1
, ctx
->i32_0
, "");
2357 /* Replace the MSAA sample index. */
2359 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2360 final_sample
, sample_index
, "");
2361 return sample_index
;
2364 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2366 assert(instr
->src
[0].is_ssa
);
2367 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2370 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2371 const nir_intrinsic_instr
*instr
,
2372 enum ac_descriptor_type desc_type
,
2375 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2378 static void get_image_coords(struct ac_nir_context
*ctx
,
2379 const nir_intrinsic_instr
*instr
,
2380 struct ac_image_args
*args
)
2382 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2384 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2385 LLVMValueRef masks
[] = {
2386 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2387 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2389 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2392 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2393 bool is_array
= glsl_sampler_type_is_array(type
);
2394 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2395 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2396 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2397 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2398 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2399 count
= image_type_to_components_count(dim
, is_array
);
2401 if (is_ms
&& instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
2402 LLVMValueRef fmask_load_address
[3];
2405 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2406 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2408 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2410 fmask_load_address
[2] = NULL
;
2412 for (chan
= 0; chan
< 2; ++chan
)
2413 fmask_load_address
[chan
] =
2414 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2415 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2416 ctx
->ac
.i32
, ""), "");
2417 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2419 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2420 fmask_load_address
[0],
2421 fmask_load_address
[1],
2422 fmask_load_address
[2],
2424 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2425 AC_DESC_FMASK
, NULL
, false, false));
2427 if (count
== 1 && !gfx9_1d
) {
2428 if (instr
->src
[1].ssa
->num_components
)
2429 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2431 args
->coords
[0] = src0
;
2436 for (chan
= 0; chan
< count
; ++chan
) {
2437 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2440 for (chan
= 0; chan
< 2; ++chan
) {
2441 args
->coords
[chan
] = LLVMBuildAdd(
2442 ctx
->ac
.builder
, args
->coords
[chan
],
2444 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2445 ctx
->ac
.i32
, ""), "");
2447 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2448 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2454 args
->coords
[2] = args
->coords
[1];
2455 args
->coords
[1] = ctx
->ac
.i32_0
;
2457 args
->coords
[1] = ctx
->ac
.i32_0
;
2462 args
->coords
[count
] = sample_index
;
2468 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2469 const nir_intrinsic_instr
*instr
, bool write
)
2471 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2472 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2473 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2474 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2475 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2477 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2478 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2479 elem_count
, stride
, "");
2481 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2482 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2487 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2488 const nir_intrinsic_instr
*instr
)
2491 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2492 const struct glsl_type
*type
= image_deref
->type
;
2493 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2494 struct ac_image_args args
= {};
2497 get_cache_policy(ctx
, var
->data
.image
.access
, false, false);
2499 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2500 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2501 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2502 unsigned num_channels
= util_last_bit(mask
);
2503 LLVMValueRef rsrc
, vindex
;
2505 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2506 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2509 /* TODO: set "can_speculate" when OpenGL needs it. */
2510 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2511 ctx
->ac
.i32_0
, num_channels
,
2512 !!(args
.cache_policy
& ac_glc
),
2514 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2516 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2517 res
= ac_to_integer(&ctx
->ac
, res
);
2519 args
.opcode
= ac_image_load
;
2520 get_image_coords(ctx
, instr
, &args
);
2521 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2522 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2523 glsl_sampler_type_is_array(type
));
2525 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2527 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2529 return ac_to_integer(&ctx
->ac
, res
);
2532 static void visit_image_store(struct ac_nir_context
*ctx
,
2533 nir_intrinsic_instr
*instr
)
2535 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2536 const struct glsl_type
*type
= image_deref
->type
;
2537 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2538 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2539 bool writeonly_memory
= var
->data
.image
.access
& ACCESS_NON_READABLE
;
2540 struct ac_image_args args
= {};
2542 args
.cache_policy
= get_cache_policy(ctx
, var
->data
.image
.access
, true,
2545 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2546 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2547 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2548 unsigned src_channels
= ac_get_llvm_num_components(src
);
2549 LLVMValueRef vindex
;
2551 if (src_channels
== 3)
2552 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2554 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2555 get_src(ctx
, instr
->src
[1]),
2558 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2559 ctx
->ac
.i32_0
, src_channels
,
2560 args
.cache_policy
& ac_glc
,
2563 args
.opcode
= ac_image_store
;
2564 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2565 get_image_coords(ctx
, instr
, &args
);
2566 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2567 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2568 glsl_sampler_type_is_array(type
));
2571 ac_build_image_opcode(&ctx
->ac
, &args
);
2576 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2577 const nir_intrinsic_instr
*instr
)
2579 LLVMValueRef params
[7];
2580 int param_count
= 0;
2581 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2583 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2584 const char *atomic_name
;
2585 char intrinsic_name
[64];
2586 enum ac_atomic_op atomic_subop
;
2587 MAYBE_UNUSED
int length
;
2589 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2591 switch (instr
->intrinsic
) {
2592 case nir_intrinsic_image_deref_atomic_add
:
2593 atomic_name
= "add";
2594 atomic_subop
= ac_atomic_add
;
2596 case nir_intrinsic_image_deref_atomic_min
:
2597 atomic_name
= is_unsigned
? "umin" : "smin";
2598 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2600 case nir_intrinsic_image_deref_atomic_max
:
2601 atomic_name
= is_unsigned
? "umax" : "smax";
2602 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2604 case nir_intrinsic_image_deref_atomic_and
:
2605 atomic_name
= "and";
2606 atomic_subop
= ac_atomic_and
;
2608 case nir_intrinsic_image_deref_atomic_or
:
2610 atomic_subop
= ac_atomic_or
;
2612 case nir_intrinsic_image_deref_atomic_xor
:
2613 atomic_name
= "xor";
2614 atomic_subop
= ac_atomic_xor
;
2616 case nir_intrinsic_image_deref_atomic_exchange
:
2617 atomic_name
= "swap";
2618 atomic_subop
= ac_atomic_swap
;
2620 case nir_intrinsic_image_deref_atomic_comp_swap
:
2621 atomic_name
= "cmpswap";
2622 atomic_subop
= 0; /* not used */
2629 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2630 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2632 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2633 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2634 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2635 ctx
->ac
.i32_0
, ""); /* vindex */
2636 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2637 if (HAVE_LLVM
>= 0x800) {
2638 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2639 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2641 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2642 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2644 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2646 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2647 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2650 assert(length
< sizeof(intrinsic_name
));
2651 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2652 params
, param_count
, 0);
2654 struct ac_image_args args
= {};
2655 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2656 args
.atomic
= atomic_subop
;
2657 args
.data
[0] = params
[0];
2659 args
.data
[1] = params
[1];
2660 get_image_coords(ctx
, instr
, &args
);
2661 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2662 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2663 glsl_sampler_type_is_array(type
));
2665 return ac_build_image_opcode(&ctx
->ac
, &args
);
2669 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2670 const nir_intrinsic_instr
*instr
)
2672 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2674 struct ac_image_args args
= { 0 };
2675 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2676 glsl_sampler_type_is_array(type
));
2678 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2679 args
.opcode
= ac_image_get_resinfo
;
2680 args
.lod
= ctx
->ac
.i32_0
;
2681 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2683 return ac_build_image_opcode(&ctx
->ac
, &args
);
2686 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2687 const nir_intrinsic_instr
*instr
)
2690 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2692 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2693 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2695 struct ac_image_args args
= { 0 };
2697 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2698 glsl_sampler_type_is_array(type
));
2700 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2701 args
.opcode
= ac_image_get_resinfo
;
2702 args
.lod
= ctx
->ac
.i32_0
;
2703 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2705 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2707 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2709 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2710 glsl_sampler_type_is_array(type
)) {
2711 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2712 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2713 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2714 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2716 if (ctx
->ac
.chip_class
>= GFX9
&&
2717 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2718 glsl_sampler_type_is_array(type
)) {
2719 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2720 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2727 static void emit_membar(struct ac_llvm_context
*ac
,
2728 const nir_intrinsic_instr
*instr
)
2730 unsigned waitcnt
= NOOP_WAITCNT
;
2732 switch (instr
->intrinsic
) {
2733 case nir_intrinsic_memory_barrier
:
2734 case nir_intrinsic_group_memory_barrier
:
2735 waitcnt
&= VM_CNT
& LGKM_CNT
;
2737 case nir_intrinsic_memory_barrier_atomic_counter
:
2738 case nir_intrinsic_memory_barrier_buffer
:
2739 case nir_intrinsic_memory_barrier_image
:
2742 case nir_intrinsic_memory_barrier_shared
:
2743 waitcnt
&= LGKM_CNT
;
2748 if (waitcnt
!= NOOP_WAITCNT
)
2749 ac_build_waitcnt(ac
, waitcnt
);
2752 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2754 /* SI only (thanks to a hw bug workaround):
2755 * The real barrier instruction isn’t needed, because an entire patch
2756 * always fits into a single wave.
2758 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2759 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2762 ac_build_s_barrier(ac
);
2765 static void emit_discard(struct ac_nir_context
*ctx
,
2766 const nir_intrinsic_instr
*instr
)
2770 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2771 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2772 get_src(ctx
, instr
->src
[0]),
2775 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2776 cond
= ctx
->ac
.i1false
;
2779 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2783 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2785 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2786 "llvm.amdgcn.ps.live",
2787 ctx
->ac
.i1
, NULL
, 0,
2788 AC_FUNC_ATTR_READNONE
);
2789 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2790 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2794 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2796 LLVMValueRef result
;
2797 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2798 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2799 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2801 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2805 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2807 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2808 LLVMValueRef result
;
2809 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2810 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2811 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2813 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2818 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2820 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2821 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2822 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2824 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2829 visit_first_invocation(struct ac_nir_context
*ctx
)
2831 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2833 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2834 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2835 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2837 ctx
->ac
.i64
, args
, 2,
2838 AC_FUNC_ATTR_NOUNWIND
|
2839 AC_FUNC_ATTR_READNONE
);
2841 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2845 visit_load_shared(struct ac_nir_context
*ctx
,
2846 const nir_intrinsic_instr
*instr
)
2848 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2850 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2852 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2853 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2854 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2855 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2858 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2859 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2863 visit_store_shared(struct ac_nir_context
*ctx
,
2864 const nir_intrinsic_instr
*instr
)
2866 LLVMValueRef derived_ptr
, data
,index
;
2867 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2869 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2870 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2872 int writemask
= nir_intrinsic_write_mask(instr
);
2873 for (int chan
= 0; chan
< 4; chan
++) {
2874 if (!(writemask
& (1 << chan
))) {
2877 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2878 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2879 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2880 LLVMBuildStore(builder
, data
, derived_ptr
);
2884 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2885 const nir_intrinsic_instr
*instr
,
2886 LLVMValueRef ptr
, int src_idx
)
2888 LLVMValueRef result
;
2889 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2891 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2892 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2893 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2894 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2896 LLVMAtomicOrderingSequentiallyConsistent
,
2897 LLVMAtomicOrderingSequentiallyConsistent
,
2899 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2901 LLVMAtomicRMWBinOp op
;
2902 switch (instr
->intrinsic
) {
2903 case nir_intrinsic_shared_atomic_add
:
2904 case nir_intrinsic_deref_atomic_add
:
2905 op
= LLVMAtomicRMWBinOpAdd
;
2907 case nir_intrinsic_shared_atomic_umin
:
2908 case nir_intrinsic_deref_atomic_umin
:
2909 op
= LLVMAtomicRMWBinOpUMin
;
2911 case nir_intrinsic_shared_atomic_umax
:
2912 case nir_intrinsic_deref_atomic_umax
:
2913 op
= LLVMAtomicRMWBinOpUMax
;
2915 case nir_intrinsic_shared_atomic_imin
:
2916 case nir_intrinsic_deref_atomic_imin
:
2917 op
= LLVMAtomicRMWBinOpMin
;
2919 case nir_intrinsic_shared_atomic_imax
:
2920 case nir_intrinsic_deref_atomic_imax
:
2921 op
= LLVMAtomicRMWBinOpMax
;
2923 case nir_intrinsic_shared_atomic_and
:
2924 case nir_intrinsic_deref_atomic_and
:
2925 op
= LLVMAtomicRMWBinOpAnd
;
2927 case nir_intrinsic_shared_atomic_or
:
2928 case nir_intrinsic_deref_atomic_or
:
2929 op
= LLVMAtomicRMWBinOpOr
;
2931 case nir_intrinsic_shared_atomic_xor
:
2932 case nir_intrinsic_deref_atomic_xor
:
2933 op
= LLVMAtomicRMWBinOpXor
;
2935 case nir_intrinsic_shared_atomic_exchange
:
2936 case nir_intrinsic_deref_atomic_exchange
:
2937 op
= LLVMAtomicRMWBinOpXchg
;
2943 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2944 LLVMAtomicOrderingSequentiallyConsistent
,
2950 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2952 LLVMValueRef values
[2];
2953 LLVMValueRef pos
[2];
2955 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2956 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2958 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2959 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2960 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2963 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2964 const nir_intrinsic_instr
*instr
)
2966 LLVMValueRef result
[4];
2967 LLVMValueRef interp_param
;
2970 LLVMValueRef src_c0
= NULL
;
2971 LLVMValueRef src_c1
= NULL
;
2972 LLVMValueRef src0
= NULL
;
2974 nir_deref_instr
*deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2975 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
2976 int input_base
= ctx
->abi
->fs_input_attr_indices
[var
->data
.location
- VARYING_SLOT_VAR0
];
2977 switch (instr
->intrinsic
) {
2978 case nir_intrinsic_interp_deref_at_centroid
:
2979 location
= INTERP_CENTROID
;
2981 case nir_intrinsic_interp_deref_at_sample
:
2982 case nir_intrinsic_interp_deref_at_offset
:
2983 location
= INTERP_CENTER
;
2984 src0
= get_src(ctx
, instr
->src
[1]);
2990 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2991 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2992 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2993 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2994 LLVMValueRef sample_position
;
2995 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2997 /* fetch sample ID */
2998 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
3000 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
3001 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3002 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
3003 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3005 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
3007 if (location
== INTERP_CENTER
) {
3008 LLVMValueRef ij_out
[2];
3009 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3012 * take the I then J parameters, and the DDX/Y for it, and
3013 * calculate the IJ inputs for the interpolator.
3014 * temp1 = ddx * offset/sample.x + I;
3015 * interp_param.I = ddy * offset/sample.y + temp1;
3016 * temp1 = ddx * offset/sample.x + J;
3017 * interp_param.J = ddy * offset/sample.y + temp1;
3019 for (unsigned i
= 0; i
< 2; i
++) {
3020 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3021 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3022 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3023 ddxy_out
, ix_ll
, "");
3024 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3025 ddxy_out
, iy_ll
, "");
3026 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3027 interp_param
, ix_ll
, "");
3028 LLVMValueRef temp1
, temp2
;
3030 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3033 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3034 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3036 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3037 temp2
, ctx
->ac
.i32
, "");
3039 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3043 LLVMValueRef attrib_idx
= ctx
->ac
.i32_0
;
3044 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3045 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3046 unsigned array_size
= glsl_count_attribute_slots(deref_instr
->type
, false);
3048 LLVMValueRef offset
;
3049 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3051 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* const_value
->u32
[0], false);
3053 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3055 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3056 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3059 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3060 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3061 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3062 LLVMValueRef offset
;
3063 unsigned sidx
= deref_instr
->strct
.index
;
3064 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3065 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_struct_location_offset(deref_instr
->type
, sidx
), false);
3066 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3068 unreachable("Unsupported deref type");
3073 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3074 for (chan
= 0; chan
< 4; chan
++) {
3075 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3076 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3078 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3079 LLVMValueRef v
, attr_number
;
3081 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3083 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3084 interp_param
, ctx
->ac
.v2f32
, "");
3085 LLVMValueRef i
= LLVMBuildExtractElement(
3086 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3087 LLVMValueRef j
= LLVMBuildExtractElement(
3088 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3090 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3091 ctx
->abi
->prim_mask
, i
, j
);
3093 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3094 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3097 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3098 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3101 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3104 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3105 var
->data
.location_frac
);
3108 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3109 nir_intrinsic_instr
*instr
)
3111 LLVMValueRef result
= NULL
;
3113 switch (instr
->intrinsic
) {
3114 case nir_intrinsic_ballot
:
3115 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3117 case nir_intrinsic_read_invocation
:
3118 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3119 get_src(ctx
, instr
->src
[1]));
3121 case nir_intrinsic_read_first_invocation
:
3122 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3124 case nir_intrinsic_load_subgroup_invocation
:
3125 result
= ac_get_thread_id(&ctx
->ac
);
3127 case nir_intrinsic_load_work_group_id
: {
3128 LLVMValueRef values
[3];
3130 for (int i
= 0; i
< 3; i
++) {
3131 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3132 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3135 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3138 case nir_intrinsic_load_base_vertex
:
3139 case nir_intrinsic_load_first_vertex
:
3140 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3142 case nir_intrinsic_load_local_group_size
:
3143 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3145 case nir_intrinsic_load_vertex_id
:
3146 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3147 ctx
->abi
->base_vertex
, "");
3149 case nir_intrinsic_load_vertex_id_zero_base
: {
3150 result
= ctx
->abi
->vertex_id
;
3153 case nir_intrinsic_load_local_invocation_id
: {
3154 result
= ctx
->abi
->local_invocation_ids
;
3157 case nir_intrinsic_load_base_instance
:
3158 result
= ctx
->abi
->start_instance
;
3160 case nir_intrinsic_load_draw_id
:
3161 result
= ctx
->abi
->draw_id
;
3163 case nir_intrinsic_load_view_index
:
3164 result
= ctx
->abi
->view_index
;
3166 case nir_intrinsic_load_invocation_id
:
3167 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3168 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3170 result
= ctx
->abi
->gs_invocation_id
;
3172 case nir_intrinsic_load_primitive_id
:
3173 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3174 result
= ctx
->abi
->gs_prim_id
;
3175 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3176 result
= ctx
->abi
->tcs_patch_id
;
3177 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3178 result
= ctx
->abi
->tes_patch_id
;
3180 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3182 case nir_intrinsic_load_sample_id
:
3183 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3185 case nir_intrinsic_load_sample_pos
:
3186 result
= load_sample_pos(ctx
);
3188 case nir_intrinsic_load_sample_mask_in
:
3189 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3191 case nir_intrinsic_load_frag_coord
: {
3192 LLVMValueRef values
[4] = {
3193 ctx
->abi
->frag_pos
[0],
3194 ctx
->abi
->frag_pos
[1],
3195 ctx
->abi
->frag_pos
[2],
3196 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3198 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
3201 case nir_intrinsic_load_front_face
:
3202 result
= ctx
->abi
->front_face
;
3204 case nir_intrinsic_load_helper_invocation
:
3205 result
= visit_load_helper_invocation(ctx
);
3207 case nir_intrinsic_load_instance_id
:
3208 result
= ctx
->abi
->instance_id
;
3210 case nir_intrinsic_load_num_work_groups
:
3211 result
= ctx
->abi
->num_work_groups
;
3213 case nir_intrinsic_load_local_invocation_index
:
3214 result
= visit_load_local_invocation_index(ctx
);
3216 case nir_intrinsic_load_subgroup_id
:
3217 result
= visit_load_subgroup_id(ctx
);
3219 case nir_intrinsic_load_num_subgroups
:
3220 result
= visit_load_num_subgroups(ctx
);
3222 case nir_intrinsic_first_invocation
:
3223 result
= visit_first_invocation(ctx
);
3225 case nir_intrinsic_load_push_constant
:
3226 result
= visit_load_push_constant(ctx
, instr
);
3228 case nir_intrinsic_vulkan_resource_index
: {
3229 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3230 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3231 unsigned binding
= nir_intrinsic_binding(instr
);
3233 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3237 case nir_intrinsic_vulkan_resource_reindex
:
3238 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3240 case nir_intrinsic_store_ssbo
:
3241 visit_store_ssbo(ctx
, instr
);
3243 case nir_intrinsic_load_ssbo
:
3244 result
= visit_load_buffer(ctx
, instr
);
3246 case nir_intrinsic_ssbo_atomic_add
:
3247 case nir_intrinsic_ssbo_atomic_imin
:
3248 case nir_intrinsic_ssbo_atomic_umin
:
3249 case nir_intrinsic_ssbo_atomic_imax
:
3250 case nir_intrinsic_ssbo_atomic_umax
:
3251 case nir_intrinsic_ssbo_atomic_and
:
3252 case nir_intrinsic_ssbo_atomic_or
:
3253 case nir_intrinsic_ssbo_atomic_xor
:
3254 case nir_intrinsic_ssbo_atomic_exchange
:
3255 case nir_intrinsic_ssbo_atomic_comp_swap
:
3256 result
= visit_atomic_ssbo(ctx
, instr
);
3258 case nir_intrinsic_load_ubo
:
3259 result
= visit_load_ubo_buffer(ctx
, instr
);
3261 case nir_intrinsic_get_buffer_size
:
3262 result
= visit_get_buffer_size(ctx
, instr
);
3264 case nir_intrinsic_load_deref
:
3265 result
= visit_load_var(ctx
, instr
);
3267 case nir_intrinsic_store_deref
:
3268 visit_store_var(ctx
, instr
);
3270 case nir_intrinsic_load_shared
:
3271 result
= visit_load_shared(ctx
, instr
);
3273 case nir_intrinsic_store_shared
:
3274 visit_store_shared(ctx
, instr
);
3276 case nir_intrinsic_image_deref_samples
:
3277 result
= visit_image_samples(ctx
, instr
);
3279 case nir_intrinsic_image_deref_load
:
3280 result
= visit_image_load(ctx
, instr
);
3282 case nir_intrinsic_image_deref_store
:
3283 visit_image_store(ctx
, instr
);
3285 case nir_intrinsic_image_deref_atomic_add
:
3286 case nir_intrinsic_image_deref_atomic_min
:
3287 case nir_intrinsic_image_deref_atomic_max
:
3288 case nir_intrinsic_image_deref_atomic_and
:
3289 case nir_intrinsic_image_deref_atomic_or
:
3290 case nir_intrinsic_image_deref_atomic_xor
:
3291 case nir_intrinsic_image_deref_atomic_exchange
:
3292 case nir_intrinsic_image_deref_atomic_comp_swap
:
3293 result
= visit_image_atomic(ctx
, instr
);
3295 case nir_intrinsic_image_deref_size
:
3296 result
= visit_image_size(ctx
, instr
);
3298 case nir_intrinsic_shader_clock
:
3299 result
= ac_build_shader_clock(&ctx
->ac
);
3301 case nir_intrinsic_discard
:
3302 case nir_intrinsic_discard_if
:
3303 emit_discard(ctx
, instr
);
3305 case nir_intrinsic_memory_barrier
:
3306 case nir_intrinsic_group_memory_barrier
:
3307 case nir_intrinsic_memory_barrier_atomic_counter
:
3308 case nir_intrinsic_memory_barrier_buffer
:
3309 case nir_intrinsic_memory_barrier_image
:
3310 case nir_intrinsic_memory_barrier_shared
:
3311 emit_membar(&ctx
->ac
, instr
);
3313 case nir_intrinsic_barrier
:
3314 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3316 case nir_intrinsic_shared_atomic_add
:
3317 case nir_intrinsic_shared_atomic_imin
:
3318 case nir_intrinsic_shared_atomic_umin
:
3319 case nir_intrinsic_shared_atomic_imax
:
3320 case nir_intrinsic_shared_atomic_umax
:
3321 case nir_intrinsic_shared_atomic_and
:
3322 case nir_intrinsic_shared_atomic_or
:
3323 case nir_intrinsic_shared_atomic_xor
:
3324 case nir_intrinsic_shared_atomic_exchange
:
3325 case nir_intrinsic_shared_atomic_comp_swap
: {
3326 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3327 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3330 case nir_intrinsic_deref_atomic_add
:
3331 case nir_intrinsic_deref_atomic_imin
:
3332 case nir_intrinsic_deref_atomic_umin
:
3333 case nir_intrinsic_deref_atomic_imax
:
3334 case nir_intrinsic_deref_atomic_umax
:
3335 case nir_intrinsic_deref_atomic_and
:
3336 case nir_intrinsic_deref_atomic_or
:
3337 case nir_intrinsic_deref_atomic_xor
:
3338 case nir_intrinsic_deref_atomic_exchange
:
3339 case nir_intrinsic_deref_atomic_comp_swap
: {
3340 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3341 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3344 case nir_intrinsic_interp_deref_at_centroid
:
3345 case nir_intrinsic_interp_deref_at_sample
:
3346 case nir_intrinsic_interp_deref_at_offset
:
3347 result
= visit_interp(ctx
, instr
);
3349 case nir_intrinsic_emit_vertex
:
3350 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3352 case nir_intrinsic_end_primitive
:
3353 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3355 case nir_intrinsic_load_tess_coord
:
3356 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3358 case nir_intrinsic_load_tess_level_outer
:
3359 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3361 case nir_intrinsic_load_tess_level_inner
:
3362 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3364 case nir_intrinsic_load_patch_vertices_in
:
3365 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3367 case nir_intrinsic_vote_all
: {
3368 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3369 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3372 case nir_intrinsic_vote_any
: {
3373 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3374 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3377 case nir_intrinsic_shuffle
:
3378 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3379 get_src(ctx
, instr
->src
[1]));
3381 case nir_intrinsic_reduce
:
3382 result
= ac_build_reduce(&ctx
->ac
,
3383 get_src(ctx
, instr
->src
[0]),
3384 instr
->const_index
[0],
3385 instr
->const_index
[1]);
3387 case nir_intrinsic_inclusive_scan
:
3388 result
= ac_build_inclusive_scan(&ctx
->ac
,
3389 get_src(ctx
, instr
->src
[0]),
3390 instr
->const_index
[0]);
3392 case nir_intrinsic_exclusive_scan
:
3393 result
= ac_build_exclusive_scan(&ctx
->ac
,
3394 get_src(ctx
, instr
->src
[0]),
3395 instr
->const_index
[0]);
3397 case nir_intrinsic_quad_broadcast
: {
3398 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3399 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3400 lane
, lane
, lane
, lane
);
3403 case nir_intrinsic_quad_swap_horizontal
:
3404 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3406 case nir_intrinsic_quad_swap_vertical
:
3407 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3409 case nir_intrinsic_quad_swap_diagonal
:
3410 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3413 fprintf(stderr
, "Unknown intrinsic: ");
3414 nir_print_instr(&instr
->instr
, stderr
);
3415 fprintf(stderr
, "\n");
3419 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3423 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3424 unsigned base_index
,
3425 unsigned constant_index
,
3426 LLVMValueRef dynamic_index
)
3428 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3429 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3430 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3432 /* Bindless uniforms are 64bit so multiple index by 8 */
3433 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3434 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3436 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3438 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3439 NULL
, 0, false, false, true, true);
3441 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3444 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3445 nir_deref_instr
*deref_instr
,
3446 enum ac_descriptor_type desc_type
,
3447 const nir_tex_instr
*tex_instr
,
3448 bool image
, bool write
)
3450 LLVMValueRef index
= NULL
;
3451 unsigned constant_index
= 0;
3452 unsigned descriptor_set
;
3453 unsigned base_index
;
3454 bool bindless
= false;
3457 assert(tex_instr
&& !image
);
3459 base_index
= tex_instr
->sampler_index
;
3461 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3462 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3463 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3467 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3469 constant_index
+= array_size
* const_value
->u32
[0];
3471 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3473 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3474 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3479 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3482 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3483 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3484 unsigned sidx
= deref_instr
->strct
.index
;
3485 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3486 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3488 unreachable("Unsupported deref type");
3491 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3493 if (deref_instr
->var
->data
.bindless
) {
3494 /* For now just assert on unhandled variable types */
3495 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3497 base_index
= deref_instr
->var
->data
.driver_location
;
3500 index
= index
? index
: ctx
->ac
.i32_0
;
3501 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3502 constant_index
, index
);
3504 base_index
= deref_instr
->var
->data
.binding
;
3507 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3510 constant_index
, index
,
3511 desc_type
, image
, write
, bindless
);
3514 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3517 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3518 * filtering manually. The driver sets img7 to a mask clearing
3519 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3520 * s_and_b32 samp0, samp0, img7
3523 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3525 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3526 LLVMValueRef res
, LLVMValueRef samp
)
3528 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3529 LLVMValueRef img7
, samp0
;
3531 if (ctx
->ac
.chip_class
>= VI
)
3534 img7
= LLVMBuildExtractElement(builder
, res
,
3535 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3536 samp0
= LLVMBuildExtractElement(builder
, samp
,
3537 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3538 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3539 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3540 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3543 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3544 nir_tex_instr
*instr
,
3545 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3546 LLVMValueRef
*fmask_ptr
)
3548 nir_deref_instr
*texture_deref_instr
= NULL
;
3549 nir_deref_instr
*sampler_deref_instr
= NULL
;
3551 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3552 switch (instr
->src
[i
].src_type
) {
3553 case nir_tex_src_texture_deref
:
3554 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3556 case nir_tex_src_sampler_deref
:
3557 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3564 if (!sampler_deref_instr
)
3565 sampler_deref_instr
= texture_deref_instr
;
3567 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3568 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3570 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3572 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3573 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3574 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3576 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3577 instr
->op
== nir_texop_samples_identical
))
3578 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3581 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3584 coord
= ac_to_float(ctx
, coord
);
3585 coord
= ac_build_round(ctx
, coord
);
3586 coord
= ac_to_integer(ctx
, coord
);
3590 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3592 LLVMValueRef result
= NULL
;
3593 struct ac_image_args args
= { 0 };
3594 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3595 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3596 unsigned offset_src
= 0;
3598 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3600 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3601 switch (instr
->src
[i
].src_type
) {
3602 case nir_tex_src_coord
: {
3603 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3604 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3605 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3608 case nir_tex_src_projector
:
3610 case nir_tex_src_comparator
:
3611 if (instr
->is_shadow
)
3612 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3614 case nir_tex_src_offset
:
3615 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3618 case nir_tex_src_bias
:
3619 if (instr
->op
== nir_texop_txb
)
3620 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3622 case nir_tex_src_lod
: {
3623 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3625 if (val
&& val
->i32
[0] == 0)
3626 args
.level_zero
= true;
3628 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3631 case nir_tex_src_ms_index
:
3632 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3634 case nir_tex_src_ms_mcs
:
3636 case nir_tex_src_ddx
:
3637 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3639 case nir_tex_src_ddy
:
3640 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3642 case nir_tex_src_texture_offset
:
3643 case nir_tex_src_sampler_offset
:
3644 case nir_tex_src_plane
:
3650 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3651 result
= get_buffer_size(ctx
, args
.resource
, true);
3655 if (instr
->op
== nir_texop_texture_samples
) {
3656 LLVMValueRef res
, samples
, is_msaa
;
3657 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3658 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3659 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3660 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3661 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3662 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3663 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3664 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3665 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3667 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3668 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3669 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3670 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3671 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3673 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3679 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3680 LLVMValueRef offset
[3], pack
;
3681 for (unsigned chan
= 0; chan
< 3; ++chan
)
3682 offset
[chan
] = ctx
->ac
.i32_0
;
3684 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3685 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3686 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3687 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3688 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3690 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3691 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3693 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3694 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3698 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3699 * so the depth comparison value isn't clamped for Z16 and
3700 * Z24 anymore. Do it manually here.
3702 * It's unnecessary if the original texture format was
3703 * Z32_FLOAT, but we don't know that here.
3705 if (args
.compare
&& ctx
->ac
.chip_class
>= VI
&& ctx
->abi
->clamp_shadow_reference
)
3706 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3708 /* pack derivatives */
3710 int num_src_deriv_channels
, num_dest_deriv_channels
;
3711 switch (instr
->sampler_dim
) {
3712 case GLSL_SAMPLER_DIM_3D
:
3713 case GLSL_SAMPLER_DIM_CUBE
:
3714 num_src_deriv_channels
= 3;
3715 num_dest_deriv_channels
= 3;
3717 case GLSL_SAMPLER_DIM_2D
:
3719 num_src_deriv_channels
= 2;
3720 num_dest_deriv_channels
= 2;
3722 case GLSL_SAMPLER_DIM_1D
:
3723 num_src_deriv_channels
= 1;
3724 if (ctx
->ac
.chip_class
>= GFX9
) {
3725 num_dest_deriv_channels
= 2;
3727 num_dest_deriv_channels
= 1;
3732 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3733 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3734 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3735 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3736 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3738 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3739 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3740 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3744 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3745 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3746 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3747 if (instr
->coord_components
== 3)
3748 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3749 ac_prepare_cube_coords(&ctx
->ac
,
3750 instr
->op
== nir_texop_txd
, instr
->is_array
,
3751 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3754 /* Texture coordinates fixups */
3755 if (instr
->coord_components
> 1 &&
3756 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3758 instr
->op
!= nir_texop_txf
) {
3759 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3762 if (instr
->coord_components
> 2 &&
3763 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3764 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3765 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3766 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3768 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3769 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3772 if (ctx
->ac
.chip_class
>= GFX9
&&
3773 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3774 instr
->op
!= nir_texop_lod
) {
3775 LLVMValueRef filler
;
3776 if (instr
->op
== nir_texop_txf
)
3777 filler
= ctx
->ac
.i32_0
;
3779 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3781 if (instr
->is_array
)
3782 args
.coords
[2] = args
.coords
[1];
3783 args
.coords
[1] = filler
;
3786 /* Pack sample index */
3787 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3788 args
.coords
[instr
->coord_components
] = sample_index
;
3790 if (instr
->op
== nir_texop_samples_identical
) {
3791 struct ac_image_args txf_args
= { 0 };
3792 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3794 txf_args
.dmask
= 0xf;
3795 txf_args
.resource
= fmask_ptr
;
3796 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3797 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3799 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3800 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3804 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3805 instr
->op
!= nir_texop_txs
) {
3806 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3807 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3808 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3809 instr
->is_array
? args
.coords
[2] : NULL
,
3810 args
.coords
[sample_chan
], fmask_ptr
);
3813 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3814 nir_const_value
*const_offset
=
3815 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3816 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3817 assert(const_offset
);
3818 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3819 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3820 args
.coords
[i
] = LLVMBuildAdd(
3821 ctx
->ac
.builder
, args
.coords
[i
],
3822 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3827 /* TODO TG4 support */
3829 if (instr
->op
== nir_texop_tg4
) {
3830 if (instr
->is_shadow
)
3833 args
.dmask
= 1 << instr
->component
;
3836 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3837 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3838 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3840 if (instr
->op
== nir_texop_query_levels
)
3841 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3842 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3843 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3844 instr
->op
!= nir_texop_tg4
)
3845 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3846 else if (instr
->op
== nir_texop_txs
&&
3847 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3849 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3850 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3851 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3852 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3853 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3854 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3855 instr
->op
== nir_texop_txs
&&
3856 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3858 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3859 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3860 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3862 } else if (instr
->dest
.ssa
.num_components
!= 4)
3863 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3867 assert(instr
->dest
.is_ssa
);
3868 result
= ac_to_integer(&ctx
->ac
, result
);
3869 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3874 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3876 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3877 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3879 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3880 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3883 static void visit_post_phi(struct ac_nir_context
*ctx
,
3884 nir_phi_instr
*instr
,
3885 LLVMValueRef llvm_phi
)
3887 nir_foreach_phi_src(src
, instr
) {
3888 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3889 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3891 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3895 static void phi_post_pass(struct ac_nir_context
*ctx
)
3897 hash_table_foreach(ctx
->phis
, entry
) {
3898 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3899 (LLVMValueRef
)entry
->data
);
3904 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3905 const nir_ssa_undef_instr
*instr
)
3907 unsigned num_components
= instr
->def
.num_components
;
3908 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3911 if (num_components
== 1)
3912 undef
= LLVMGetUndef(type
);
3914 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3916 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3919 static void visit_jump(struct ac_llvm_context
*ctx
,
3920 const nir_jump_instr
*instr
)
3922 switch (instr
->type
) {
3923 case nir_jump_break
:
3924 ac_build_break(ctx
);
3926 case nir_jump_continue
:
3927 ac_build_continue(ctx
);
3930 fprintf(stderr
, "Unknown NIR jump instr: ");
3931 nir_print_instr(&instr
->instr
, stderr
);
3932 fprintf(stderr
, "\n");
3938 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3939 enum glsl_base_type type
)
3943 case GLSL_TYPE_UINT
:
3944 case GLSL_TYPE_BOOL
:
3945 case GLSL_TYPE_SUBROUTINE
:
3947 case GLSL_TYPE_INT8
:
3948 case GLSL_TYPE_UINT8
:
3950 case GLSL_TYPE_INT16
:
3951 case GLSL_TYPE_UINT16
:
3953 case GLSL_TYPE_FLOAT
:
3955 case GLSL_TYPE_FLOAT16
:
3957 case GLSL_TYPE_INT64
:
3958 case GLSL_TYPE_UINT64
:
3960 case GLSL_TYPE_DOUBLE
:
3963 unreachable("unknown GLSL type");
3968 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3969 const struct glsl_type
*type
)
3971 if (glsl_type_is_scalar(type
)) {
3972 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3975 if (glsl_type_is_vector(type
)) {
3976 return LLVMVectorType(
3977 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3978 glsl_get_vector_elements(type
));
3981 if (glsl_type_is_matrix(type
)) {
3982 return LLVMArrayType(
3983 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3984 glsl_get_matrix_columns(type
));
3987 if (glsl_type_is_array(type
)) {
3988 return LLVMArrayType(
3989 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3990 glsl_get_length(type
));
3993 assert(glsl_type_is_struct_or_ifc(type
));
3995 LLVMTypeRef member_types
[glsl_get_length(type
)];
3997 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3999 glsl_to_llvm_type(ac
,
4000 glsl_get_struct_field(type
, i
));
4003 return LLVMStructTypeInContext(ac
->context
, member_types
,
4004 glsl_get_length(type
), false);
4007 static void visit_deref(struct ac_nir_context
*ctx
,
4008 nir_deref_instr
*instr
)
4010 if (instr
->mode
!= nir_var_mem_shared
&&
4011 instr
->mode
!= nir_var_mem_global
)
4014 LLVMValueRef result
= NULL
;
4015 switch(instr
->deref_type
) {
4016 case nir_deref_type_var
: {
4017 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4018 result
= entry
->data
;
4021 case nir_deref_type_struct
:
4022 if (instr
->mode
== nir_var_mem_global
) {
4023 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4024 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4025 instr
->strct
.index
);
4026 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4027 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4029 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4030 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4033 case nir_deref_type_array
:
4034 if (instr
->mode
== nir_var_mem_global
) {
4035 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4036 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4038 if ((glsl_type_is_matrix(parent
->type
) &&
4039 glsl_matrix_type_is_row_major(parent
->type
)) ||
4040 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4041 stride
= type_scalar_size_bytes(parent
->type
);
4044 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4045 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4046 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4048 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4050 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4052 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4053 get_src(ctx
, instr
->arr
.index
));
4056 case nir_deref_type_ptr_as_array
:
4057 if (instr
->mode
== nir_var_mem_global
) {
4058 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4060 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4061 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4062 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4064 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4066 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4068 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4069 get_src(ctx
, instr
->arr
.index
));
4072 case nir_deref_type_cast
: {
4073 result
= get_src(ctx
, instr
->parent
);
4075 /* We can't use the structs from LLVM because the shader
4076 * specifies its own offsets. */
4077 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4078 if (instr
->mode
== nir_var_mem_shared
)
4079 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4081 unsigned address_space
;
4083 switch(instr
->mode
) {
4084 case nir_var_mem_shared
:
4085 address_space
= AC_ADDR_SPACE_LDS
;
4087 case nir_var_mem_global
:
4088 address_space
= AC_ADDR_SPACE_GLOBAL
;
4091 unreachable("Unhandled address space");
4094 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4096 if (LLVMTypeOf(result
) != type
) {
4097 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4098 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4101 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4108 unreachable("Unhandled deref_instr deref type");
4111 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4114 static void visit_cf_list(struct ac_nir_context
*ctx
,
4115 struct exec_list
*list
);
4117 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4119 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4120 nir_foreach_instr(instr
, block
)
4122 switch (instr
->type
) {
4123 case nir_instr_type_alu
:
4124 visit_alu(ctx
, nir_instr_as_alu(instr
));
4126 case nir_instr_type_load_const
:
4127 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4129 case nir_instr_type_intrinsic
:
4130 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4132 case nir_instr_type_tex
:
4133 visit_tex(ctx
, nir_instr_as_tex(instr
));
4135 case nir_instr_type_phi
:
4136 visit_phi(ctx
, nir_instr_as_phi(instr
));
4138 case nir_instr_type_ssa_undef
:
4139 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4141 case nir_instr_type_jump
:
4142 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4144 case nir_instr_type_deref
:
4145 visit_deref(ctx
, nir_instr_as_deref(instr
));
4148 fprintf(stderr
, "Unknown NIR instr type: ");
4149 nir_print_instr(instr
, stderr
);
4150 fprintf(stderr
, "\n");
4155 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4158 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4160 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4162 nir_block
*then_block
=
4163 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4165 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4167 visit_cf_list(ctx
, &if_stmt
->then_list
);
4169 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4170 nir_block
*else_block
=
4171 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4173 ac_build_else(&ctx
->ac
, else_block
->index
);
4174 visit_cf_list(ctx
, &if_stmt
->else_list
);
4177 ac_build_endif(&ctx
->ac
, then_block
->index
);
4180 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4182 nir_block
*first_loop_block
=
4183 (nir_block
*) exec_list_get_head(&loop
->body
);
4185 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4187 visit_cf_list(ctx
, &loop
->body
);
4189 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4192 static void visit_cf_list(struct ac_nir_context
*ctx
,
4193 struct exec_list
*list
)
4195 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4197 switch (node
->type
) {
4198 case nir_cf_node_block
:
4199 visit_block(ctx
, nir_cf_node_as_block(node
));
4202 case nir_cf_node_if
:
4203 visit_if(ctx
, nir_cf_node_as_if(node
));
4206 case nir_cf_node_loop
:
4207 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4217 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4218 struct ac_shader_abi
*abi
,
4219 struct nir_shader
*nir
,
4220 struct nir_variable
*variable
,
4221 gl_shader_stage stage
)
4223 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4224 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4226 /* tess ctrl has it's own load/store paths for outputs */
4227 if (stage
== MESA_SHADER_TESS_CTRL
)
4230 if (stage
== MESA_SHADER_VERTEX
||
4231 stage
== MESA_SHADER_TESS_EVAL
||
4232 stage
== MESA_SHADER_GEOMETRY
) {
4233 int idx
= variable
->data
.location
+ variable
->data
.index
;
4234 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4235 int length
= nir
->info
.clip_distance_array_size
+
4236 nir
->info
.cull_distance_array_size
;
4245 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4246 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4247 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4248 for (unsigned chan
= 0; chan
< 4; chan
++) {
4249 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4250 ac_build_alloca_undef(ctx
, type
, "");
4256 setup_locals(struct ac_nir_context
*ctx
,
4257 struct nir_function
*func
)
4260 ctx
->num_locals
= 0;
4261 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4262 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4263 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4264 variable
->data
.location_frac
= 0;
4265 ctx
->num_locals
+= attrib_count
;
4267 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4271 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4272 for (j
= 0; j
< 4; j
++) {
4273 ctx
->locals
[i
* 4 + j
] =
4274 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4280 setup_shared(struct ac_nir_context
*ctx
,
4281 struct nir_shader
*nir
)
4283 nir_foreach_variable(variable
, &nir
->shared
) {
4284 LLVMValueRef shared
=
4285 LLVMAddGlobalInAddressSpace(
4286 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4287 variable
->name
? variable
->name
: "",
4289 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4293 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4294 struct nir_shader
*nir
)
4296 struct ac_nir_context ctx
= {};
4297 struct nir_function
*func
;
4302 ctx
.stage
= nir
->info
.stage
;
4304 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4306 nir_foreach_variable(variable
, &nir
->outputs
)
4307 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4310 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4311 _mesa_key_pointer_equal
);
4312 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4313 _mesa_key_pointer_equal
);
4314 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4315 _mesa_key_pointer_equal
);
4317 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4319 nir_index_ssa_defs(func
->impl
);
4320 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4322 setup_locals(&ctx
, func
);
4324 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4325 setup_shared(&ctx
, nir
);
4327 visit_cf_list(&ctx
, &func
->impl
->body
);
4328 phi_post_pass(&ctx
);
4330 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4331 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4336 ralloc_free(ctx
.defs
);
4337 ralloc_free(ctx
.phis
);
4338 ralloc_free(ctx
.vars
);
4342 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4344 /* While it would be nice not to have this flag, we are constrained
4345 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4348 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4350 /* TODO: Indirect indexing of GS inputs is unimplemented.
4352 * TCS and TES load inputs directly from LDS or offchip memory, so
4353 * indirect indexing is trivial.
4355 nir_variable_mode indirect_mask
= 0;
4356 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4357 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4358 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4359 !llvm_has_working_vgpr_indexing
)) {
4360 indirect_mask
|= nir_var_shader_in
;
4362 if (!llvm_has_working_vgpr_indexing
&&
4363 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4364 indirect_mask
|= nir_var_shader_out
;
4366 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4367 * smart enough to handle indirects without causing excess spilling
4368 * causing the gpu to hang.
4370 * See the following thread for more details of the problem:
4371 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4373 indirect_mask
|= nir_var_function_temp
;
4375 nir_lower_indirect_derefs(nir
, indirect_mask
);
4379 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4381 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4385 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4387 if (var
->data
.mode
!= nir_var_shader_out
)
4390 unsigned writemask
= 0;
4391 const int location
= var
->data
.location
;
4392 unsigned first_component
= var
->data
.location_frac
;
4393 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4395 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4396 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4397 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4398 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4404 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4405 unsigned *cond_block_tf_writemask
,
4406 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4408 switch (cf_node
->type
) {
4409 case nir_cf_node_block
: {
4410 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4411 nir_foreach_instr(instr
, block
) {
4412 if (instr
->type
!= nir_instr_type_intrinsic
)
4415 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4416 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4418 /* If we find a barrier in nested control flow put this in the
4419 * too hard basket. In GLSL this is not possible but it is in
4423 *tessfactors_are_def_in_all_invocs
= false;
4427 /* The following case must be prevented:
4428 * gl_TessLevelInner = ...;
4430 * if (gl_InvocationID == 1)
4431 * gl_TessLevelInner = ...;
4433 * If you consider disjoint code segments separated by barriers, each
4434 * such segment that writes tess factor channels should write the same
4435 * channels in all codepaths within that segment.
4437 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4438 /* Accumulate the result: */
4439 *tessfactors_are_def_in_all_invocs
&=
4440 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4442 /* Analyze the next code segment from scratch. */
4443 *upper_block_tf_writemask
= 0;
4444 *cond_block_tf_writemask
= 0;
4447 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4452 case nir_cf_node_if
: {
4453 unsigned then_tessfactor_writemask
= 0;
4454 unsigned else_tessfactor_writemask
= 0;
4456 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4457 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4458 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4459 cond_block_tf_writemask
,
4460 tessfactors_are_def_in_all_invocs
, true);
4463 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4464 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4465 cond_block_tf_writemask
,
4466 tessfactors_are_def_in_all_invocs
, true);
4469 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4470 /* If both statements write the same tess factor channels,
4471 * we can say that the upper block writes them too.
4473 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4474 else_tessfactor_writemask
;
4475 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4476 else_tessfactor_writemask
;
4481 case nir_cf_node_loop
: {
4482 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4483 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4484 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4485 cond_block_tf_writemask
,
4486 tessfactors_are_def_in_all_invocs
, true);
4492 unreachable("unknown cf node type");
4497 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4499 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4501 /* The pass works as follows:
4502 * If all codepaths write tess factors, we can say that all
4503 * invocations define tess factors.
4505 * Each tess factor channel is tracked separately.
4507 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4508 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4510 /* Initial value = true. Here the pass will accumulate results from
4511 * multiple segments surrounded by barriers. If tess factors aren't
4512 * written at all, it's a shader bug and we don't care if this will be
4515 bool tessfactors_are_def_in_all_invocs
= true;
4517 nir_foreach_function(function
, nir
) {
4518 if (function
->impl
) {
4519 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4520 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4521 &cond_block_tf_writemask
,
4522 &tessfactors_are_def_in_all_invocs
,
4528 /* Accumulate the result for the last code segment separated by a
4531 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4532 tessfactors_are_def_in_all_invocs
&=
4533 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4536 return tessfactors_are_def_in_all_invocs
;