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_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 ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
296 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
298 LLVMValueRef src0
, LLVMValueRef src1
)
300 LLVMTypeRef ret_type
;
301 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
303 LLVMValueRef params
[] = { src0
, src1
};
304 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
307 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
308 params
, 2, AC_FUNC_ATTR_READNONE
);
310 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
311 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
315 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
319 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
320 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
322 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
326 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
330 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
332 unreachable("Unsupported bit size.");
336 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
339 src0
= ac_to_float(ctx
, src0
);
340 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
341 return LLVMBuildSExt(ctx
->builder
,
342 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
346 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
350 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
354 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
356 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
360 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
362 unreachable("Unsupported bit size.");
366 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
369 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
370 return LLVMBuildSExt(ctx
->builder
,
371 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
375 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
379 LLVMValueRef cond
= NULL
;
381 src0
= ac_to_float(ctx
, src0
);
382 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
384 if (ctx
->chip_class
>= VI
) {
385 LLVMValueRef args
[2];
386 /* Check if the result is a denormal - and flush to 0 if so. */
388 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
389 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
392 /* need to convert back up to f32 */
393 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
395 if (ctx
->chip_class
>= VI
)
396 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
399 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
400 * so compare the result and flush to 0 if it's smaller.
402 LLVMValueRef temp
, cond2
;
403 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
404 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
405 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
407 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
408 temp
, ctx
->f32_0
, "");
409 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
410 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
415 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
416 LLVMValueRef src0
, LLVMValueRef src1
)
418 LLVMValueRef dst64
, result
;
419 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
420 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
422 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
423 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
424 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
428 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
429 LLVMValueRef src0
, LLVMValueRef src1
)
431 LLVMValueRef dst64
, result
;
432 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
433 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
435 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
436 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
437 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
441 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
443 const LLVMValueRef srcs
[3])
447 if (HAVE_LLVM
>= 0x0800) {
448 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
449 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
450 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
452 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
453 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
455 LLVMValueRef zero
= ctx
->i32_0
;
456 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
457 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
459 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
460 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
461 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
467 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
468 LLVMValueRef src0
, LLVMValueRef src1
,
469 LLVMValueRef src2
, LLVMValueRef src3
)
471 LLVMValueRef bfi_args
[3], result
;
473 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
474 LLVMBuildSub(ctx
->builder
,
475 LLVMBuildShl(ctx
->builder
,
480 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
483 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
486 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
487 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
489 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
490 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
491 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
493 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
497 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
500 LLVMValueRef comp
[2];
502 src0
= ac_to_float(ctx
, src0
);
503 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
504 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
506 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
510 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
513 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
514 LLVMValueRef temps
[2], val
;
517 for (i
= 0; i
< 2; i
++) {
518 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
519 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
520 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
521 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
523 return ac_build_gather_values(ctx
, temps
, 2);
526 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
534 if (op
== nir_op_fddx_fine
)
535 mask
= AC_TID_MASK_LEFT
;
536 else if (op
== nir_op_fddy_fine
)
537 mask
= AC_TID_MASK_TOP
;
539 mask
= AC_TID_MASK_TOP_LEFT
;
541 /* for DDX we want to next X pixel, DDY next Y pixel. */
542 if (op
== nir_op_fddx_fine
||
543 op
== nir_op_fddx_coarse
||
549 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
554 * this takes an I,J coordinate pair,
555 * and works out the X and Y derivatives.
556 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
558 static LLVMValueRef
emit_ddxy_interp(
559 struct ac_nir_context
*ctx
,
560 LLVMValueRef interp_ij
)
562 LLVMValueRef result
[4], a
;
565 for (i
= 0; i
< 2; i
++) {
566 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
567 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
568 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
569 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
571 return ac_build_gather_values(&ctx
->ac
, result
, 4);
574 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
576 LLVMValueRef src
[4], result
= NULL
;
577 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
578 unsigned src_components
;
579 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
581 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
588 case nir_op_pack_half_2x16
:
591 case nir_op_unpack_half_2x16
:
594 case nir_op_cube_face_coord
:
595 case nir_op_cube_face_index
:
599 src_components
= num_components
;
602 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
603 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
611 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
612 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
615 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
618 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
621 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
624 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
625 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
626 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
629 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
630 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
631 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
634 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
637 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
640 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
643 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
646 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
647 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
648 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
649 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
650 ac_to_float_type(&ctx
->ac
, def_type
), result
);
651 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
652 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
655 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
656 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
657 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
660 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
663 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
666 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
669 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
670 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
671 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
674 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
675 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
678 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
681 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
684 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
687 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
688 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
689 LLVMTypeOf(src
[0]), "");
690 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
691 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
692 LLVMTypeOf(src
[0]), "");
693 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
696 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
697 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
698 LLVMTypeOf(src
[0]), "");
699 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
700 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
701 LLVMTypeOf(src
[0]), "");
702 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
705 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
706 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
707 LLVMTypeOf(src
[0]), "");
708 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
709 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
710 LLVMTypeOf(src
[0]), "");
711 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
714 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
717 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
720 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
723 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
726 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
729 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
732 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
735 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
738 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
741 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
748 result
= emit_iabs(&ctx
->ac
, src
[0]);
751 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
754 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
757 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
760 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
763 result
= ac_build_isign(&ctx
->ac
, src
[0],
764 instr
->dest
.dest
.ssa
.bit_size
);
767 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
768 result
= ac_build_fsign(&ctx
->ac
, src
[0],
769 instr
->dest
.dest
.ssa
.bit_size
);
772 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
773 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
776 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
777 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
780 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
781 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
783 case nir_op_fround_even
:
784 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
785 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
788 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
789 result
= ac_build_fract(&ctx
->ac
, src
[0],
790 instr
->dest
.dest
.ssa
.bit_size
);
793 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
794 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
797 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
798 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
801 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
802 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
805 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
806 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
809 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
810 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
813 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
814 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
815 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
817 case nir_op_frexp_exp
:
818 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
819 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
820 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
821 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
822 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
825 case nir_op_frexp_sig
:
826 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
827 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
828 instr
->dest
.dest
.ssa
.bit_size
);
831 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
832 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
835 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
836 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
837 if (ctx
->ac
.chip_class
< GFX9
&&
838 instr
->dest
.dest
.ssa
.bit_size
== 32) {
839 /* Only pre-GFX9 chips do not flush denorms. */
840 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
841 ac_to_float_type(&ctx
->ac
, def_type
),
846 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
847 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
848 if (ctx
->ac
.chip_class
< GFX9
&&
849 instr
->dest
.dest
.ssa
.bit_size
== 32) {
850 /* Only pre-GFX9 chips do not flush denorms. */
851 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
852 ac_to_float_type(&ctx
->ac
, def_type
),
857 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
858 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
861 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
862 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
863 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
864 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
865 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
867 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
869 case nir_op_ibitfield_extract
:
870 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
872 case nir_op_ubitfield_extract
:
873 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
875 case nir_op_bitfield_insert
:
876 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
878 case nir_op_bitfield_reverse
:
879 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
881 case nir_op_bit_count
:
882 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
887 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
888 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
889 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
895 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
896 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
902 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
903 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
908 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
909 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
914 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
915 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
917 case nir_op_f2f16_rtz
:
918 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
919 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
920 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
921 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
922 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
923 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
925 case nir_op_f2f16_rtne
:
929 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
930 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
931 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
933 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
939 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
940 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
941 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
943 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
949 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
950 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
951 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
953 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
956 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
958 case nir_op_find_lsb
:
959 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
960 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
962 case nir_op_ufind_msb
:
963 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
964 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
966 case nir_op_ifind_msb
:
967 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
968 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
970 case nir_op_uadd_carry
:
971 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
972 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
973 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
975 case nir_op_usub_borrow
:
976 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
977 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
978 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
983 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
986 result
= emit_f2b(&ctx
->ac
, src
[0]);
992 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
995 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
996 result
= emit_i2b(&ctx
->ac
, src
[0]);
998 case nir_op_fquantize2f16
:
999 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1001 case nir_op_umul_high
:
1002 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1003 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1004 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1006 case nir_op_imul_high
:
1007 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
1008 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
1009 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1011 case nir_op_pack_half_2x16
:
1012 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
1014 case nir_op_unpack_half_2x16
:
1015 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1019 case nir_op_fddx_fine
:
1020 case nir_op_fddy_fine
:
1021 case nir_op_fddx_coarse
:
1022 case nir_op_fddy_coarse
:
1023 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1026 case nir_op_unpack_64_2x32_split_x
: {
1027 assert(ac_get_llvm_num_components(src
[0]) == 1);
1028 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1031 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1036 case nir_op_unpack_64_2x32_split_y
: {
1037 assert(ac_get_llvm_num_components(src
[0]) == 1);
1038 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1041 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1046 case nir_op_pack_64_2x32_split
: {
1047 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1048 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1049 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1053 case nir_op_pack_32_2x16_split
: {
1054 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1055 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1059 case nir_op_unpack_32_2x16_split_x
: {
1060 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1063 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1068 case nir_op_unpack_32_2x16_split_y
: {
1069 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1072 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1077 case nir_op_cube_face_coord
: {
1078 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1079 LLVMValueRef results
[2];
1081 for (unsigned chan
= 0; chan
< 3; chan
++)
1082 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1083 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1084 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1085 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1086 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1087 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1091 case nir_op_cube_face_index
: {
1092 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1094 for (unsigned chan
= 0; chan
< 3; chan
++)
1095 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1096 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1097 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1102 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1103 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1104 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1105 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1108 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1109 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1112 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1113 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1116 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1117 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1118 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1119 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1122 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1123 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1126 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1127 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1129 case nir_op_fmed3
: {
1130 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1131 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1132 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1133 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1134 instr
->dest
.dest
.ssa
.bit_size
);
1137 case nir_op_imed3
: {
1138 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1139 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1140 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1141 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1144 case nir_op_umed3
: {
1145 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1146 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1147 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1148 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1153 fprintf(stderr
, "Unknown NIR alu instr: ");
1154 nir_print_instr(&instr
->instr
, stderr
);
1155 fprintf(stderr
, "\n");
1160 assert(instr
->dest
.dest
.is_ssa
);
1161 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1162 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1166 static void visit_load_const(struct ac_nir_context
*ctx
,
1167 const nir_load_const_instr
*instr
)
1169 LLVMValueRef values
[4], value
= NULL
;
1170 LLVMTypeRef element_type
=
1171 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1173 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1174 switch (instr
->def
.bit_size
) {
1176 values
[i
] = LLVMConstInt(element_type
,
1177 instr
->value
.u8
[i
], false);
1180 values
[i
] = LLVMConstInt(element_type
,
1181 instr
->value
.u16
[i
], false);
1184 values
[i
] = LLVMConstInt(element_type
,
1185 instr
->value
.u32
[i
], false);
1188 values
[i
] = LLVMConstInt(element_type
,
1189 instr
->value
.u64
[i
], false);
1193 "unsupported nir load_const bit_size: %d\n",
1194 instr
->def
.bit_size
);
1198 if (instr
->def
.num_components
> 1) {
1199 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1203 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1207 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1210 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1211 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1214 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1215 /* On VI, the descriptor contains the size in bytes,
1216 * but TXQ must return the size in elements.
1217 * The stride is always non-zero for resources using TXQ.
1219 LLVMValueRef stride
=
1220 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1222 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1223 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1224 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1225 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1227 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1232 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1234 struct ac_image_args
*args
,
1235 const nir_tex_instr
*instr
)
1237 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1238 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1239 LLVMValueRef half_texel
[2];
1240 LLVMValueRef compare_cube_wa
= NULL
;
1241 LLVMValueRef result
;
1245 struct ac_image_args txq_args
= { 0 };
1247 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1248 txq_args
.opcode
= ac_image_get_resinfo
;
1249 txq_args
.dmask
= 0xf;
1250 txq_args
.lod
= ctx
->i32_0
;
1251 txq_args
.resource
= args
->resource
;
1252 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1253 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1255 for (unsigned c
= 0; c
< 2; c
++) {
1256 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1257 LLVMConstInt(ctx
->i32
, c
, false), "");
1258 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1259 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1260 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1261 LLVMConstReal(ctx
->f32
, -0.5), "");
1265 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1267 for (unsigned c
= 0; c
< 2; c
++) {
1269 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1270 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1274 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1275 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1276 * workaround by sampling using a scaled type and converting.
1277 * This is taken from amdgpu-pro shaders.
1279 /* NOTE this produces some ugly code compared to amdgpu-pro,
1280 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1281 * and then reads them back. -pro generates two selects,
1282 * one s_cmp for the descriptor rewriting
1283 * one v_cmp for the coordinate and result changes.
1285 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1286 LLVMValueRef tmp
, tmp2
;
1288 /* workaround 8/8/8/8 uint/sint cube gather bug */
1289 /* first detect it then change to a scaled read and f2i */
1290 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1293 /* extract the DATA_FORMAT */
1294 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1295 LLVMConstInt(ctx
->i32
, 6, false), false);
1297 /* is the DATA_FORMAT == 8_8_8_8 */
1298 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1300 if (stype
== GLSL_TYPE_UINT
)
1301 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1302 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1303 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1305 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1306 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1307 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1309 /* replace the NUM FORMAT in the descriptor */
1310 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1311 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1313 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1315 /* don't modify the coordinates for this case */
1316 for (unsigned c
= 0; c
< 2; ++c
)
1317 args
->coords
[c
] = LLVMBuildSelect(
1318 ctx
->builder
, compare_cube_wa
,
1319 orig_coords
[c
], args
->coords
[c
], "");
1322 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1323 result
= ac_build_image_opcode(ctx
, args
);
1325 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1326 LLVMValueRef tmp
, tmp2
;
1328 /* if the cube workaround is in place, f2i the result. */
1329 for (unsigned c
= 0; c
< 4; c
++) {
1330 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1331 if (stype
== GLSL_TYPE_UINT
)
1332 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1334 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1335 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1336 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1337 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1338 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1339 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1345 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1347 nir_deref_instr
*texture_deref_instr
= NULL
;
1349 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1350 switch (instr
->src
[i
].src_type
) {
1351 case nir_tex_src_texture_deref
:
1352 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1358 return texture_deref_instr
;
1361 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1362 const nir_tex_instr
*instr
,
1363 struct ac_image_args
*args
)
1365 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1366 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1368 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1369 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1373 util_last_bit(mask
),
1376 return ac_build_buffer_load_format(&ctx
->ac
,
1380 util_last_bit(mask
),
1385 args
->opcode
= ac_image_sample
;
1387 switch (instr
->op
) {
1389 case nir_texop_txf_ms
:
1390 case nir_texop_samples_identical
:
1391 args
->opcode
= args
->level_zero
||
1392 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1393 ac_image_load
: ac_image_load_mip
;
1394 args
->level_zero
= false;
1397 case nir_texop_query_levels
:
1398 args
->opcode
= ac_image_get_resinfo
;
1400 args
->lod
= ctx
->ac
.i32_0
;
1401 args
->level_zero
= false;
1404 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1406 args
->level_zero
= true;
1410 args
->opcode
= ac_image_gather4
;
1411 args
->level_zero
= true;
1414 args
->opcode
= ac_image_get_lod
;
1420 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1421 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1422 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1423 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1424 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1425 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1426 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1430 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1431 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1432 if ((args
->dim
== ac_image_2darray
||
1433 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1434 args
->coords
[1] = ctx
->ac
.i32_0
;
1438 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1439 return ac_build_image_opcode(&ctx
->ac
, args
);
1442 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1443 nir_intrinsic_instr
*instr
)
1445 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1446 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1448 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1449 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1453 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1454 nir_intrinsic_instr
*instr
)
1456 LLVMValueRef ptr
, addr
;
1457 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1458 unsigned index
= nir_intrinsic_base(instr
);
1460 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1461 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1463 /* Load constant values from user SGPRS when possible, otherwise
1464 * fallback to the default path that loads directly from memory.
1466 if (LLVMIsConstant(src0
) &&
1467 instr
->dest
.ssa
.bit_size
== 32) {
1468 unsigned count
= instr
->dest
.ssa
.num_components
;
1469 unsigned offset
= index
;
1471 offset
+= LLVMConstIntGetZExtValue(src0
);
1474 offset
-= ctx
->abi
->base_inline_push_consts
;
1476 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1477 return ac_build_gather_values(&ctx
->ac
,
1478 ctx
->abi
->inline_push_consts
+ offset
,
1483 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1485 if (instr
->dest
.ssa
.bit_size
== 8) {
1486 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1487 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1488 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1489 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1491 LLVMValueRef params
[3];
1492 if (load_dwords
> 1) {
1493 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1494 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1495 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1497 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1498 params
[0] = ctx
->ac
.i32_0
;
1502 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1504 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1505 if (instr
->dest
.ssa
.num_components
> 1)
1506 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1508 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1509 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1510 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1511 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1512 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1513 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1514 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1515 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1516 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1517 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1518 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1519 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1520 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1521 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1522 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1523 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1524 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1527 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1529 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1532 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1533 const nir_intrinsic_instr
*instr
)
1535 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1537 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1540 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1542 uint32_t new_mask
= 0;
1543 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1544 if (mask
& (1u << i
))
1545 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1549 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1550 unsigned start
, unsigned count
)
1552 LLVMValueRef mask
[] = {
1553 ctx
->i32_0
, ctx
->i32_1
,
1554 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1556 unsigned src_elements
= ac_get_llvm_num_components(src
);
1558 if (count
== src_elements
) {
1561 } else if (count
== 1) {
1562 assert(start
< src_elements
);
1563 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1565 assert(start
+ count
<= src_elements
);
1567 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1568 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1572 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1573 enum gl_access_qualifier access
,
1574 bool may_store_unaligned
,
1575 bool writeonly_memory
)
1577 unsigned cache_policy
= 0;
1579 /* SI has a TC L1 bug causing corruption of 8bit/16bit stores. All
1580 * store opcodes not aligned to a dword are affected. The only way to
1581 * get unaligned stores is through shader images.
1583 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== SI
) ||
1584 /* If this is write-only, don't keep data in L1 to prevent
1585 * evicting L1 cache lines that may be needed by other
1589 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1590 cache_policy
|= ac_glc
;
1593 return cache_policy
;
1596 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1597 nir_intrinsic_instr
*instr
)
1599 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1600 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1601 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1602 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1603 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1604 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1606 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1607 get_src(ctx
, instr
->src
[1]), true);
1608 LLVMValueRef base_data
= src_data
;
1609 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1610 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1614 LLVMValueRef data
, offset
;
1615 LLVMTypeRef data_type
;
1617 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1619 /* Due to an LLVM limitation, split 3-element writes
1620 * into a 2-element and a 1-element write. */
1622 writemask
|= 1 << (start
+ 2);
1625 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1627 /* we can only store 4 DWords at the same time.
1628 * can only happen for 64 Bit vectors. */
1629 if (num_bytes
> 16) {
1630 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1635 /* check alignment of 16 Bit stores */
1636 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1637 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1641 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1643 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1644 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1646 if (num_bytes
== 1) {
1647 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1648 offset
, ctx
->ac
.i32_0
,
1649 cache_policy
& ac_glc
,
1651 } else if (num_bytes
== 2) {
1652 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1653 offset
, ctx
->ac
.i32_0
,
1654 cache_policy
& ac_glc
,
1657 int num_channels
= num_bytes
/ 4;
1659 switch (num_bytes
) {
1660 case 16: /* v4f32 */
1661 data_type
= ctx
->ac
.v4f32
;
1664 data_type
= ctx
->ac
.v2f32
;
1667 data_type
= ctx
->ac
.f32
;
1670 unreachable("Malformed vector store.");
1672 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1674 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1675 num_channels
, offset
,
1677 cache_policy
& ac_glc
,
1678 false, writeonly_memory
,
1684 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1685 const nir_intrinsic_instr
*instr
)
1689 LLVMValueRef params
[6];
1692 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1693 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1695 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1696 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1697 get_src(ctx
, instr
->src
[0]),
1699 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1700 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1701 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1703 switch (instr
->intrinsic
) {
1704 case nir_intrinsic_ssbo_atomic_add
:
1707 case nir_intrinsic_ssbo_atomic_imin
:
1710 case nir_intrinsic_ssbo_atomic_umin
:
1713 case nir_intrinsic_ssbo_atomic_imax
:
1716 case nir_intrinsic_ssbo_atomic_umax
:
1719 case nir_intrinsic_ssbo_atomic_and
:
1722 case nir_intrinsic_ssbo_atomic_or
:
1725 case nir_intrinsic_ssbo_atomic_xor
:
1728 case nir_intrinsic_ssbo_atomic_exchange
:
1731 case nir_intrinsic_ssbo_atomic_comp_swap
:
1738 if (HAVE_LLVM
>= 0x900 &&
1739 instr
->intrinsic
!= nir_intrinsic_ssbo_atomic_comp_swap
) {
1740 snprintf(name
, sizeof(name
),
1741 "llvm.amdgcn.buffer.atomic.%s.i32", op
);
1743 snprintf(name
, sizeof(name
),
1744 "llvm.amdgcn.buffer.atomic.%s", op
);
1747 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1750 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1751 const nir_intrinsic_instr
*instr
)
1753 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1754 int num_components
= instr
->num_components
;
1755 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1756 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1758 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1759 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1760 get_src(ctx
, instr
->src
[0]), false);
1761 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1763 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1764 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1766 LLVMValueRef results
[4];
1767 for (int i
= 0; i
< num_components
;) {
1768 int num_elems
= num_components
- i
;
1769 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1771 if (num_elems
* elem_size_bytes
> 16)
1772 num_elems
= 16 / elem_size_bytes
;
1773 int load_bytes
= num_elems
* elem_size_bytes
;
1775 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1779 if (load_bytes
== 1) {
1780 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1785 cache_policy
& ac_glc
);
1786 } else if (load_bytes
== 2) {
1787 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1792 cache_policy
& ac_glc
);
1794 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1796 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1797 vindex
, offset
, immoffset
, 0,
1798 cache_policy
& ac_glc
, 0,
1802 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1803 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1804 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1806 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1807 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1809 for (unsigned j
= 0; j
< num_elems
; j
++) {
1810 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1815 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1818 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1819 const nir_intrinsic_instr
*instr
)
1822 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1823 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1824 int num_components
= instr
->num_components
;
1826 if (ctx
->abi
->load_ubo
)
1827 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1829 if (instr
->dest
.ssa
.bit_size
== 64)
1830 num_components
*= 2;
1832 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1833 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1834 LLVMValueRef results
[num_components
];
1835 for (unsigned i
= 0; i
< num_components
; ++i
) {
1836 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1839 if (load_bytes
== 1) {
1840 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1847 assert(load_bytes
== 2);
1848 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1856 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1858 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1859 NULL
, 0, false, false, true, true);
1861 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1864 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1865 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1869 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1870 bool vs_in
, unsigned *vertex_index_out
,
1871 LLVMValueRef
*vertex_index_ref
,
1872 unsigned *const_out
, LLVMValueRef
*indir_out
)
1874 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1875 nir_deref_path path
;
1876 unsigned idx_lvl
= 1;
1878 nir_deref_path_init(&path
, instr
, NULL
);
1880 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1881 if (vertex_index_ref
) {
1882 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1883 if (vertex_index_out
)
1884 *vertex_index_out
= 0;
1886 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1888 *vertex_index_out
= v
->u32
[0];
1893 uint32_t const_offset
= 0;
1894 LLVMValueRef offset
= NULL
;
1896 if (var
->data
.compact
) {
1897 assert(instr
->deref_type
== nir_deref_type_array
);
1898 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1900 const_offset
= v
->u32
[0];
1904 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1905 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1906 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1907 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1909 for (unsigned i
= 0; i
< index
; i
++) {
1910 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1911 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1913 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1914 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1915 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1916 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1918 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1922 unreachable("Uhandled deref type in get_deref_instr_offset");
1926 nir_deref_path_finish(&path
);
1928 if (const_offset
&& offset
)
1929 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1930 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1933 *const_out
= const_offset
;
1934 *indir_out
= offset
;
1937 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1938 nir_intrinsic_instr
*instr
,
1941 LLVMValueRef result
;
1942 LLVMValueRef vertex_index
= NULL
;
1943 LLVMValueRef indir_index
= NULL
;
1944 unsigned const_index
= 0;
1946 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1948 unsigned location
= var
->data
.location
;
1949 unsigned driver_location
= var
->data
.driver_location
;
1950 const bool is_patch
= var
->data
.patch
;
1951 const bool is_compact
= var
->data
.compact
;
1953 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1954 false, NULL
, is_patch
? NULL
: &vertex_index
,
1955 &const_index
, &indir_index
);
1957 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1959 LLVMTypeRef src_component_type
;
1960 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1961 src_component_type
= LLVMGetElementType(dest_type
);
1963 src_component_type
= dest_type
;
1965 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1966 vertex_index
, indir_index
,
1967 const_index
, location
, driver_location
,
1968 var
->data
.location_frac
,
1969 instr
->num_components
,
1970 is_patch
, is_compact
, load_inputs
);
1971 if (instr
->dest
.ssa
.bit_size
== 16) {
1972 result
= ac_to_integer(&ctx
->ac
, result
);
1973 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1975 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1979 type_scalar_size_bytes(const struct glsl_type
*type
)
1981 assert(glsl_type_is_vector_or_scalar(type
) ||
1982 glsl_type_is_matrix(type
));
1983 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1986 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1987 nir_intrinsic_instr
*instr
)
1989 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1990 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1992 LLVMValueRef values
[8];
1994 int ve
= instr
->dest
.ssa
.num_components
;
1996 LLVMValueRef indir_index
;
1998 unsigned const_index
;
1999 unsigned stride
= 4;
2000 int mode
= deref
->mode
;
2003 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2004 var
->data
.mode
== nir_var_shader_in
;
2005 idx
= var
->data
.driver_location
;
2006 comp
= var
->data
.location_frac
;
2007 mode
= var
->data
.mode
;
2009 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2010 &const_index
, &indir_index
);
2012 if (var
->data
.compact
) {
2014 const_index
+= comp
;
2019 if (instr
->dest
.ssa
.bit_size
== 64 &&
2020 (deref
->mode
== nir_var_shader_in
||
2021 deref
->mode
== nir_var_shader_out
||
2022 deref
->mode
== nir_var_function_temp
))
2026 case nir_var_shader_in
:
2027 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2028 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2029 return load_tess_varyings(ctx
, instr
, true);
2032 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2033 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2034 LLVMValueRef indir_index
;
2035 unsigned const_index
, vertex_index
;
2036 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2037 &const_index
, &indir_index
);
2039 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2040 var
->data
.driver_location
,
2041 var
->data
.location_frac
,
2042 instr
->num_components
, vertex_index
, const_index
, type
);
2045 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2047 unsigned count
= glsl_count_attribute_slots(
2049 ctx
->stage
== MESA_SHADER_VERTEX
);
2051 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2052 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2053 stride
, false, true);
2055 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2059 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2062 case nir_var_function_temp
:
2063 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2065 unsigned count
= glsl_count_attribute_slots(
2068 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2069 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2070 stride
, true, true);
2072 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2076 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2080 case nir_var_mem_shared
: {
2081 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2082 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2083 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2084 get_def_type(ctx
, &instr
->dest
.ssa
),
2087 case nir_var_shader_out
:
2088 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2089 return load_tess_varyings(ctx
, instr
, false);
2092 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2094 unsigned count
= glsl_count_attribute_slots(
2097 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2098 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2099 stride
, true, true);
2101 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2105 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2106 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2111 case nir_var_mem_global
: {
2112 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2113 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2114 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2115 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2117 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2118 if (stride
!= natural_stride
) {
2119 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2120 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2121 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2123 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2124 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2125 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2126 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2128 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2130 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2131 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2132 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2133 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2138 unreachable("unhandle variable mode");
2140 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2141 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2145 visit_store_var(struct ac_nir_context
*ctx
,
2146 nir_intrinsic_instr
*instr
)
2148 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2149 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2151 LLVMValueRef temp_ptr
, value
;
2154 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2155 int writemask
= instr
->const_index
[0];
2156 LLVMValueRef indir_index
;
2157 unsigned const_index
;
2160 get_deref_offset(ctx
, deref
, false,
2161 NULL
, NULL
, &const_index
, &indir_index
);
2162 idx
= var
->data
.driver_location
;
2163 comp
= var
->data
.location_frac
;
2165 if (var
->data
.compact
) {
2166 const_index
+= comp
;
2171 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2172 (deref
->mode
== nir_var_shader_out
||
2173 deref
->mode
== nir_var_function_temp
)) {
2175 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2176 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2179 writemask
= widen_mask(writemask
, 2);
2182 writemask
= writemask
<< comp
;
2184 switch (deref
->mode
) {
2185 case nir_var_shader_out
:
2187 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2188 LLVMValueRef vertex_index
= NULL
;
2189 LLVMValueRef indir_index
= NULL
;
2190 unsigned const_index
= 0;
2191 const bool is_patch
= var
->data
.patch
;
2193 get_deref_offset(ctx
, deref
, false, NULL
,
2194 is_patch
? NULL
: &vertex_index
,
2195 &const_index
, &indir_index
);
2197 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2198 vertex_index
, indir_index
,
2199 const_index
, src
, writemask
);
2203 for (unsigned chan
= 0; chan
< 8; chan
++) {
2205 if (!(writemask
& (1 << chan
)))
2208 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2210 if (var
->data
.compact
)
2213 unsigned count
= glsl_count_attribute_slots(
2216 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2217 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2218 stride
, true, true);
2220 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2221 value
, indir_index
, "");
2222 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2223 count
, stride
, tmp_vec
);
2226 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2228 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2232 case nir_var_function_temp
:
2233 for (unsigned chan
= 0; chan
< 8; chan
++) {
2234 if (!(writemask
& (1 << chan
)))
2237 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2239 unsigned count
= glsl_count_attribute_slots(
2242 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2243 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2246 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2247 value
, indir_index
, "");
2248 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2251 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2253 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2258 case nir_var_mem_global
:
2259 case nir_var_mem_shared
: {
2260 int writemask
= instr
->const_index
[0];
2261 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2262 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2264 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2265 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2266 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2268 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2269 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2270 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2272 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2273 stride
== natural_stride
) {
2274 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2275 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2276 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2278 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2279 LLVMGetElementType(LLVMTypeOf(address
)), "");
2280 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2282 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2283 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2284 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2285 for (unsigned chan
= 0; chan
< 4; chan
++) {
2286 if (!(writemask
& (1 << chan
)))
2289 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2291 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2292 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2294 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2295 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2296 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2307 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2310 case GLSL_SAMPLER_DIM_BUF
:
2312 case GLSL_SAMPLER_DIM_1D
:
2313 return array
? 2 : 1;
2314 case GLSL_SAMPLER_DIM_2D
:
2315 return array
? 3 : 2;
2316 case GLSL_SAMPLER_DIM_MS
:
2317 return array
? 4 : 3;
2318 case GLSL_SAMPLER_DIM_3D
:
2319 case GLSL_SAMPLER_DIM_CUBE
:
2321 case GLSL_SAMPLER_DIM_RECT
:
2322 case GLSL_SAMPLER_DIM_SUBPASS
:
2324 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2333 /* Adjust the sample index according to FMASK.
2335 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2336 * which is the identity mapping. Each nibble says which physical sample
2337 * should be fetched to get that sample.
2339 * For example, 0x11111100 means there are only 2 samples stored and
2340 * the second sample covers 3/4 of the pixel. When reading samples 0
2341 * and 1, return physical sample 0 (determined by the first two 0s
2342 * in FMASK), otherwise return physical sample 1.
2344 * The sample index should be adjusted as follows:
2345 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2347 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2348 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2349 LLVMValueRef coord_z
,
2350 LLVMValueRef sample_index
,
2351 LLVMValueRef fmask_desc_ptr
)
2353 struct ac_image_args args
= {0};
2356 args
.coords
[0] = coord_x
;
2357 args
.coords
[1] = coord_y
;
2359 args
.coords
[2] = coord_z
;
2361 args
.opcode
= ac_image_load
;
2362 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2363 args
.resource
= fmask_desc_ptr
;
2365 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2367 res
= ac_build_image_opcode(ctx
, &args
);
2369 res
= ac_to_integer(ctx
, res
);
2370 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2371 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2373 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2377 LLVMValueRef sample_index4
=
2378 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2379 LLVMValueRef shifted_fmask
=
2380 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2381 LLVMValueRef final_sample
=
2382 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2384 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2385 * resource descriptor is 0 (invalid),
2387 LLVMValueRef fmask_desc
=
2388 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2391 LLVMValueRef fmask_word1
=
2392 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2395 LLVMValueRef word1_is_nonzero
=
2396 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2397 fmask_word1
, ctx
->i32_0
, "");
2399 /* Replace the MSAA sample index. */
2401 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2402 final_sample
, sample_index
, "");
2403 return sample_index
;
2406 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2408 assert(instr
->src
[0].is_ssa
);
2409 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2412 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2413 const nir_intrinsic_instr
*instr
,
2414 enum ac_descriptor_type desc_type
,
2417 nir_deref_instr
*deref_instr
=
2418 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2419 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2421 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2424 static void get_image_coords(struct ac_nir_context
*ctx
,
2425 const nir_intrinsic_instr
*instr
,
2426 struct ac_image_args
*args
,
2427 enum glsl_sampler_dim dim
,
2430 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2431 LLVMValueRef masks
[] = {
2432 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2433 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2435 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2438 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2439 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2440 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2441 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2442 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2443 count
= image_type_to_components_count(dim
, is_array
);
2445 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2446 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2447 LLVMValueRef fmask_load_address
[3];
2450 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2451 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2453 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2455 fmask_load_address
[2] = NULL
;
2457 for (chan
= 0; chan
< 2; ++chan
)
2458 fmask_load_address
[chan
] =
2459 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2460 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2461 ctx
->ac
.i32
, ""), "");
2462 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2464 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2465 fmask_load_address
[0],
2466 fmask_load_address
[1],
2467 fmask_load_address
[2],
2469 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2470 AC_DESC_FMASK
, &instr
->instr
, false, false));
2472 if (count
== 1 && !gfx9_1d
) {
2473 if (instr
->src
[1].ssa
->num_components
)
2474 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2476 args
->coords
[0] = src0
;
2481 for (chan
= 0; chan
< count
; ++chan
) {
2482 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2485 for (chan
= 0; chan
< 2; ++chan
) {
2486 args
->coords
[chan
] = LLVMBuildAdd(
2487 ctx
->ac
.builder
, args
->coords
[chan
],
2489 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2490 ctx
->ac
.i32
, ""), "");
2492 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2493 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2499 args
->coords
[2] = args
->coords
[1];
2500 args
->coords
[1] = ctx
->ac
.i32_0
;
2502 args
->coords
[1] = ctx
->ac
.i32_0
;
2507 args
->coords
[count
] = sample_index
;
2513 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2514 const nir_intrinsic_instr
*instr
, bool write
)
2516 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2517 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2518 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2519 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2520 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2522 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2523 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2524 elem_count
, stride
, "");
2526 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2527 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2532 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2533 const nir_intrinsic_instr
*instr
,
2538 enum glsl_sampler_dim dim
;
2539 enum gl_access_qualifier access
;
2542 dim
= nir_intrinsic_image_dim(instr
);
2543 access
= nir_intrinsic_access(instr
);
2544 is_array
= nir_intrinsic_image_array(instr
);
2546 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2547 const struct glsl_type
*type
= image_deref
->type
;
2548 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2549 dim
= glsl_get_sampler_dim(type
);
2550 access
= var
->data
.image
.access
;
2551 is_array
= glsl_sampler_type_is_array(type
);
2554 struct ac_image_args args
= {};
2556 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2558 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2559 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2560 unsigned num_channels
= util_last_bit(mask
);
2561 LLVMValueRef rsrc
, vindex
;
2563 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2564 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2567 /* TODO: set "can_speculate" when OpenGL needs it. */
2568 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2569 ctx
->ac
.i32_0
, num_channels
,
2570 !!(args
.cache_policy
& ac_glc
),
2572 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2574 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2575 res
= ac_to_integer(&ctx
->ac
, res
);
2577 args
.opcode
= ac_image_load
;
2578 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2579 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2580 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2582 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2584 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2586 return ac_to_integer(&ctx
->ac
, res
);
2589 static void visit_image_store(struct ac_nir_context
*ctx
,
2590 nir_intrinsic_instr
*instr
,
2595 enum glsl_sampler_dim dim
;
2596 enum gl_access_qualifier access
;
2599 dim
= nir_intrinsic_image_dim(instr
);
2600 access
= nir_intrinsic_access(instr
);
2601 is_array
= nir_intrinsic_image_array(instr
);
2603 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2604 const struct glsl_type
*type
= image_deref
->type
;
2605 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2606 dim
= glsl_get_sampler_dim(type
);
2607 access
= var
->data
.image
.access
;
2608 is_array
= glsl_sampler_type_is_array(type
);
2611 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2612 struct ac_image_args args
= {};
2614 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2616 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2617 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2618 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2619 unsigned src_channels
= ac_get_llvm_num_components(src
);
2620 LLVMValueRef vindex
;
2622 if (src_channels
== 3)
2623 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2625 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2626 get_src(ctx
, instr
->src
[1]),
2629 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2630 ctx
->ac
.i32_0
, src_channels
,
2631 args
.cache_policy
& ac_glc
,
2634 args
.opcode
= ac_image_store
;
2635 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2636 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2637 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2638 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2641 ac_build_image_opcode(&ctx
->ac
, &args
);
2646 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2647 const nir_intrinsic_instr
*instr
,
2650 LLVMValueRef params
[7];
2651 int param_count
= 0;
2653 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2654 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2655 const char *atomic_name
;
2656 char intrinsic_name
[64];
2657 enum ac_atomic_op atomic_subop
;
2658 MAYBE_UNUSED
int length
;
2660 enum glsl_sampler_dim dim
;
2664 if (instr
->intrinsic
== nir_intrinsic_image_atomic_min
||
2665 instr
->intrinsic
== nir_intrinsic_image_atomic_max
) {
2666 const GLenum format
= nir_intrinsic_format(instr
);
2667 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2668 is_unsigned
= format
== GL_R32UI
;
2670 dim
= nir_intrinsic_image_dim(instr
);
2671 is_array
= nir_intrinsic_image_array(instr
);
2673 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2674 is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2675 dim
= glsl_get_sampler_dim(type
);
2676 is_array
= glsl_sampler_type_is_array(type
);
2679 switch (instr
->intrinsic
) {
2680 case nir_intrinsic_bindless_image_atomic_add
:
2681 case nir_intrinsic_image_deref_atomic_add
:
2682 atomic_name
= "add";
2683 atomic_subop
= ac_atomic_add
;
2685 case nir_intrinsic_bindless_image_atomic_min
:
2686 case nir_intrinsic_image_deref_atomic_min
:
2687 atomic_name
= is_unsigned
? "umin" : "smin";
2688 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2690 case nir_intrinsic_bindless_image_atomic_max
:
2691 case nir_intrinsic_image_deref_atomic_max
:
2692 atomic_name
= is_unsigned
? "umax" : "smax";
2693 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2695 case nir_intrinsic_bindless_image_atomic_and
:
2696 case nir_intrinsic_image_deref_atomic_and
:
2697 atomic_name
= "and";
2698 atomic_subop
= ac_atomic_and
;
2700 case nir_intrinsic_bindless_image_atomic_or
:
2701 case nir_intrinsic_image_deref_atomic_or
:
2703 atomic_subop
= ac_atomic_or
;
2705 case nir_intrinsic_bindless_image_atomic_xor
:
2706 case nir_intrinsic_image_deref_atomic_xor
:
2707 atomic_name
= "xor";
2708 atomic_subop
= ac_atomic_xor
;
2710 case nir_intrinsic_bindless_image_atomic_exchange
:
2711 case nir_intrinsic_image_deref_atomic_exchange
:
2712 atomic_name
= "swap";
2713 atomic_subop
= ac_atomic_swap
;
2715 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2716 case nir_intrinsic_image_deref_atomic_comp_swap
:
2717 atomic_name
= "cmpswap";
2718 atomic_subop
= 0; /* not used */
2725 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2726 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2728 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2729 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2730 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2731 ctx
->ac
.i32_0
, ""); /* vindex */
2732 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2733 if (HAVE_LLVM
>= 0x800) {
2734 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2735 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2737 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2738 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2740 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2742 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2743 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2746 assert(length
< sizeof(intrinsic_name
));
2747 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2748 params
, param_count
, 0);
2750 struct ac_image_args args
= {};
2751 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2752 args
.atomic
= atomic_subop
;
2753 args
.data
[0] = params
[0];
2755 args
.data
[1] = params
[1];
2756 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2757 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2758 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2760 return ac_build_image_opcode(&ctx
->ac
, &args
);
2764 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2765 const nir_intrinsic_instr
*instr
,
2768 enum glsl_sampler_dim dim
;
2771 dim
= nir_intrinsic_image_dim(instr
);
2772 is_array
= nir_intrinsic_image_array(instr
);
2774 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2775 dim
= glsl_get_sampler_dim(type
);
2776 is_array
= glsl_sampler_type_is_array(type
);
2779 struct ac_image_args args
= { 0 };
2780 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, dim
, is_array
);
2782 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2783 args
.opcode
= ac_image_get_resinfo
;
2784 args
.lod
= ctx
->ac
.i32_0
;
2785 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2787 return ac_build_image_opcode(&ctx
->ac
, &args
);
2790 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2791 const nir_intrinsic_instr
*instr
,
2796 enum glsl_sampler_dim dim
;
2799 dim
= nir_intrinsic_image_dim(instr
);
2800 is_array
= nir_intrinsic_image_array(instr
);
2802 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2803 dim
= glsl_get_sampler_dim(type
);
2804 is_array
= glsl_sampler_type_is_array(type
);
2807 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2808 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2810 struct ac_image_args args
= { 0 };
2812 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2814 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2815 args
.opcode
= ac_image_get_resinfo
;
2816 args
.lod
= ctx
->ac
.i32_0
;
2817 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2819 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2821 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2823 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2824 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2825 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2826 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2827 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2829 if (ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2830 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2831 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2838 static void emit_membar(struct ac_llvm_context
*ac
,
2839 const nir_intrinsic_instr
*instr
)
2841 unsigned waitcnt
= NOOP_WAITCNT
;
2843 switch (instr
->intrinsic
) {
2844 case nir_intrinsic_memory_barrier
:
2845 case nir_intrinsic_group_memory_barrier
:
2846 waitcnt
&= VM_CNT
& LGKM_CNT
;
2848 case nir_intrinsic_memory_barrier_atomic_counter
:
2849 case nir_intrinsic_memory_barrier_buffer
:
2850 case nir_intrinsic_memory_barrier_image
:
2853 case nir_intrinsic_memory_barrier_shared
:
2854 waitcnt
&= LGKM_CNT
;
2859 if (waitcnt
!= NOOP_WAITCNT
)
2860 ac_build_waitcnt(ac
, waitcnt
);
2863 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2865 /* SI only (thanks to a hw bug workaround):
2866 * The real barrier instruction isn’t needed, because an entire patch
2867 * always fits into a single wave.
2869 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2870 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2873 ac_build_s_barrier(ac
);
2876 static void emit_discard(struct ac_nir_context
*ctx
,
2877 const nir_intrinsic_instr
*instr
)
2881 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2882 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2883 get_src(ctx
, instr
->src
[0]),
2886 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2887 cond
= ctx
->ac
.i1false
;
2890 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2894 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2896 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2897 "llvm.amdgcn.ps.live",
2898 ctx
->ac
.i1
, NULL
, 0,
2899 AC_FUNC_ATTR_READNONE
);
2900 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2901 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2905 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2907 LLVMValueRef result
;
2908 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2909 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2910 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2912 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2916 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2918 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2919 LLVMValueRef result
;
2920 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2921 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2922 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2924 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2929 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2931 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2932 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2933 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2935 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2940 visit_first_invocation(struct ac_nir_context
*ctx
)
2942 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2944 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2945 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2946 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2948 ctx
->ac
.i64
, args
, 2,
2949 AC_FUNC_ATTR_NOUNWIND
|
2950 AC_FUNC_ATTR_READNONE
);
2952 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2956 visit_load_shared(struct ac_nir_context
*ctx
,
2957 const nir_intrinsic_instr
*instr
)
2959 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2961 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2963 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2964 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2965 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2966 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2969 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2970 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2974 visit_store_shared(struct ac_nir_context
*ctx
,
2975 const nir_intrinsic_instr
*instr
)
2977 LLVMValueRef derived_ptr
, data
,index
;
2978 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2980 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2981 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2983 int writemask
= nir_intrinsic_write_mask(instr
);
2984 for (int chan
= 0; chan
< 4; chan
++) {
2985 if (!(writemask
& (1 << chan
))) {
2988 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2989 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2990 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2991 LLVMBuildStore(builder
, data
, derived_ptr
);
2995 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2996 const nir_intrinsic_instr
*instr
,
2997 LLVMValueRef ptr
, int src_idx
)
2999 LLVMValueRef result
;
3000 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3002 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3003 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3004 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3005 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
3007 LLVMAtomicOrderingSequentiallyConsistent
,
3008 LLVMAtomicOrderingSequentiallyConsistent
,
3010 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3012 LLVMAtomicRMWBinOp op
;
3013 switch (instr
->intrinsic
) {
3014 case nir_intrinsic_shared_atomic_add
:
3015 case nir_intrinsic_deref_atomic_add
:
3016 op
= LLVMAtomicRMWBinOpAdd
;
3018 case nir_intrinsic_shared_atomic_umin
:
3019 case nir_intrinsic_deref_atomic_umin
:
3020 op
= LLVMAtomicRMWBinOpUMin
;
3022 case nir_intrinsic_shared_atomic_umax
:
3023 case nir_intrinsic_deref_atomic_umax
:
3024 op
= LLVMAtomicRMWBinOpUMax
;
3026 case nir_intrinsic_shared_atomic_imin
:
3027 case nir_intrinsic_deref_atomic_imin
:
3028 op
= LLVMAtomicRMWBinOpMin
;
3030 case nir_intrinsic_shared_atomic_imax
:
3031 case nir_intrinsic_deref_atomic_imax
:
3032 op
= LLVMAtomicRMWBinOpMax
;
3034 case nir_intrinsic_shared_atomic_and
:
3035 case nir_intrinsic_deref_atomic_and
:
3036 op
= LLVMAtomicRMWBinOpAnd
;
3038 case nir_intrinsic_shared_atomic_or
:
3039 case nir_intrinsic_deref_atomic_or
:
3040 op
= LLVMAtomicRMWBinOpOr
;
3042 case nir_intrinsic_shared_atomic_xor
:
3043 case nir_intrinsic_deref_atomic_xor
:
3044 op
= LLVMAtomicRMWBinOpXor
;
3046 case nir_intrinsic_shared_atomic_exchange
:
3047 case nir_intrinsic_deref_atomic_exchange
:
3048 op
= LLVMAtomicRMWBinOpXchg
;
3054 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
3055 LLVMAtomicOrderingSequentiallyConsistent
,
3061 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3063 LLVMValueRef values
[2];
3064 LLVMValueRef pos
[2];
3066 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
3067 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
3069 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3070 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3071 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3074 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
3075 const nir_intrinsic_instr
*instr
)
3077 LLVMValueRef result
[4];
3078 LLVMValueRef interp_param
;
3081 LLVMValueRef src_c0
= NULL
;
3082 LLVMValueRef src_c1
= NULL
;
3083 LLVMValueRef src0
= NULL
;
3085 nir_deref_instr
*deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3086 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
3087 int input_base
= ctx
->abi
->fs_input_attr_indices
[var
->data
.location
- VARYING_SLOT_VAR0
];
3088 switch (instr
->intrinsic
) {
3089 case nir_intrinsic_interp_deref_at_centroid
:
3090 location
= INTERP_CENTROID
;
3092 case nir_intrinsic_interp_deref_at_sample
:
3093 case nir_intrinsic_interp_deref_at_offset
:
3094 location
= INTERP_CENTER
;
3095 src0
= get_src(ctx
, instr
->src
[1]);
3101 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
3102 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
3103 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
3104 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
3105 LLVMValueRef sample_position
;
3106 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3108 /* fetch sample ID */
3109 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
3111 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
3112 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3113 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
3114 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3116 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
3118 if (location
== INTERP_CENTER
) {
3119 LLVMValueRef ij_out
[2];
3120 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
3123 * take the I then J parameters, and the DDX/Y for it, and
3124 * calculate the IJ inputs for the interpolator.
3125 * temp1 = ddx * offset/sample.x + I;
3126 * interp_param.I = ddy * offset/sample.y + temp1;
3127 * temp1 = ddx * offset/sample.x + J;
3128 * interp_param.J = ddy * offset/sample.y + temp1;
3130 for (unsigned i
= 0; i
< 2; i
++) {
3131 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3132 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3133 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3134 ddxy_out
, ix_ll
, "");
3135 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3136 ddxy_out
, iy_ll
, "");
3137 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3138 interp_param
, ix_ll
, "");
3139 LLVMValueRef temp1
, temp2
;
3141 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3144 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3145 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3147 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3148 temp2
, ctx
->ac
.i32
, "");
3150 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3154 LLVMValueRef attrib_idx
= ctx
->ac
.i32_0
;
3155 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3156 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3157 unsigned array_size
= glsl_count_attribute_slots(deref_instr
->type
, false);
3159 LLVMValueRef offset
;
3160 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3162 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* const_value
->u32
[0], false);
3164 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3166 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3167 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3170 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3171 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3172 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3173 LLVMValueRef offset
;
3174 unsigned sidx
= deref_instr
->strct
.index
;
3175 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3176 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_struct_location_offset(deref_instr
->type
, sidx
), false);
3177 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3179 unreachable("Unsupported deref type");
3184 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3185 for (chan
= 0; chan
< 4; chan
++) {
3186 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3187 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3189 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3190 LLVMValueRef v
, attr_number
;
3192 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3194 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3195 interp_param
, ctx
->ac
.v2f32
, "");
3196 LLVMValueRef i
= LLVMBuildExtractElement(
3197 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3198 LLVMValueRef j
= LLVMBuildExtractElement(
3199 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3201 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3202 ctx
->abi
->prim_mask
, i
, j
);
3204 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3205 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3208 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3209 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3212 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3215 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3216 var
->data
.location_frac
);
3219 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3220 nir_intrinsic_instr
*instr
)
3222 LLVMValueRef result
= NULL
;
3224 switch (instr
->intrinsic
) {
3225 case nir_intrinsic_ballot
:
3226 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3228 case nir_intrinsic_read_invocation
:
3229 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3230 get_src(ctx
, instr
->src
[1]));
3232 case nir_intrinsic_read_first_invocation
:
3233 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3235 case nir_intrinsic_load_subgroup_invocation
:
3236 result
= ac_get_thread_id(&ctx
->ac
);
3238 case nir_intrinsic_load_work_group_id
: {
3239 LLVMValueRef values
[3];
3241 for (int i
= 0; i
< 3; i
++) {
3242 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3243 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3246 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3249 case nir_intrinsic_load_base_vertex
:
3250 case nir_intrinsic_load_first_vertex
:
3251 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3253 case nir_intrinsic_load_local_group_size
:
3254 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3256 case nir_intrinsic_load_vertex_id
:
3257 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3258 ctx
->abi
->base_vertex
, "");
3260 case nir_intrinsic_load_vertex_id_zero_base
: {
3261 result
= ctx
->abi
->vertex_id
;
3264 case nir_intrinsic_load_local_invocation_id
: {
3265 result
= ctx
->abi
->local_invocation_ids
;
3268 case nir_intrinsic_load_base_instance
:
3269 result
= ctx
->abi
->start_instance
;
3271 case nir_intrinsic_load_draw_id
:
3272 result
= ctx
->abi
->draw_id
;
3274 case nir_intrinsic_load_view_index
:
3275 result
= ctx
->abi
->view_index
;
3277 case nir_intrinsic_load_invocation_id
:
3278 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3279 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3281 result
= ctx
->abi
->gs_invocation_id
;
3283 case nir_intrinsic_load_primitive_id
:
3284 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3285 result
= ctx
->abi
->gs_prim_id
;
3286 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3287 result
= ctx
->abi
->tcs_patch_id
;
3288 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3289 result
= ctx
->abi
->tes_patch_id
;
3291 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3293 case nir_intrinsic_load_sample_id
:
3294 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3296 case nir_intrinsic_load_sample_pos
:
3297 result
= load_sample_pos(ctx
);
3299 case nir_intrinsic_load_sample_mask_in
:
3300 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3302 case nir_intrinsic_load_frag_coord
: {
3303 LLVMValueRef values
[4] = {
3304 ctx
->abi
->frag_pos
[0],
3305 ctx
->abi
->frag_pos
[1],
3306 ctx
->abi
->frag_pos
[2],
3307 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3309 result
= ac_to_integer(&ctx
->ac
,
3310 ac_build_gather_values(&ctx
->ac
, values
, 4));
3313 case nir_intrinsic_load_front_face
:
3314 result
= ctx
->abi
->front_face
;
3316 case nir_intrinsic_load_helper_invocation
:
3317 result
= visit_load_helper_invocation(ctx
);
3319 case nir_intrinsic_load_instance_id
:
3320 result
= ctx
->abi
->instance_id
;
3322 case nir_intrinsic_load_num_work_groups
:
3323 result
= ctx
->abi
->num_work_groups
;
3325 case nir_intrinsic_load_local_invocation_index
:
3326 result
= visit_load_local_invocation_index(ctx
);
3328 case nir_intrinsic_load_subgroup_id
:
3329 result
= visit_load_subgroup_id(ctx
);
3331 case nir_intrinsic_load_num_subgroups
:
3332 result
= visit_load_num_subgroups(ctx
);
3334 case nir_intrinsic_first_invocation
:
3335 result
= visit_first_invocation(ctx
);
3337 case nir_intrinsic_load_push_constant
:
3338 result
= visit_load_push_constant(ctx
, instr
);
3340 case nir_intrinsic_vulkan_resource_index
: {
3341 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3342 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3343 unsigned binding
= nir_intrinsic_binding(instr
);
3345 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3349 case nir_intrinsic_vulkan_resource_reindex
:
3350 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3352 case nir_intrinsic_store_ssbo
:
3353 visit_store_ssbo(ctx
, instr
);
3355 case nir_intrinsic_load_ssbo
:
3356 result
= visit_load_buffer(ctx
, instr
);
3358 case nir_intrinsic_ssbo_atomic_add
:
3359 case nir_intrinsic_ssbo_atomic_imin
:
3360 case nir_intrinsic_ssbo_atomic_umin
:
3361 case nir_intrinsic_ssbo_atomic_imax
:
3362 case nir_intrinsic_ssbo_atomic_umax
:
3363 case nir_intrinsic_ssbo_atomic_and
:
3364 case nir_intrinsic_ssbo_atomic_or
:
3365 case nir_intrinsic_ssbo_atomic_xor
:
3366 case nir_intrinsic_ssbo_atomic_exchange
:
3367 case nir_intrinsic_ssbo_atomic_comp_swap
:
3368 result
= visit_atomic_ssbo(ctx
, instr
);
3370 case nir_intrinsic_load_ubo
:
3371 result
= visit_load_ubo_buffer(ctx
, instr
);
3373 case nir_intrinsic_get_buffer_size
:
3374 result
= visit_get_buffer_size(ctx
, instr
);
3376 case nir_intrinsic_load_deref
:
3377 result
= visit_load_var(ctx
, instr
);
3379 case nir_intrinsic_store_deref
:
3380 visit_store_var(ctx
, instr
);
3382 case nir_intrinsic_load_shared
:
3383 result
= visit_load_shared(ctx
, instr
);
3385 case nir_intrinsic_store_shared
:
3386 visit_store_shared(ctx
, instr
);
3388 case nir_intrinsic_bindless_image_samples
:
3389 result
= visit_image_samples(ctx
, instr
, true);
3391 case nir_intrinsic_image_deref_samples
:
3392 result
= visit_image_samples(ctx
, instr
, false);
3394 case nir_intrinsic_bindless_image_load
:
3395 result
= visit_image_load(ctx
, instr
, true);
3397 case nir_intrinsic_image_deref_load
:
3398 result
= visit_image_load(ctx
, instr
, false);
3400 case nir_intrinsic_bindless_image_store
:
3401 visit_image_store(ctx
, instr
, true);
3403 case nir_intrinsic_image_deref_store
:
3404 visit_image_store(ctx
, instr
, false);
3406 case nir_intrinsic_bindless_image_atomic_add
:
3407 case nir_intrinsic_bindless_image_atomic_min
:
3408 case nir_intrinsic_bindless_image_atomic_max
:
3409 case nir_intrinsic_bindless_image_atomic_and
:
3410 case nir_intrinsic_bindless_image_atomic_or
:
3411 case nir_intrinsic_bindless_image_atomic_xor
:
3412 case nir_intrinsic_bindless_image_atomic_exchange
:
3413 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3414 result
= visit_image_atomic(ctx
, instr
, true);
3416 case nir_intrinsic_image_deref_atomic_add
:
3417 case nir_intrinsic_image_deref_atomic_min
:
3418 case nir_intrinsic_image_deref_atomic_max
:
3419 case nir_intrinsic_image_deref_atomic_and
:
3420 case nir_intrinsic_image_deref_atomic_or
:
3421 case nir_intrinsic_image_deref_atomic_xor
:
3422 case nir_intrinsic_image_deref_atomic_exchange
:
3423 case nir_intrinsic_image_deref_atomic_comp_swap
:
3424 result
= visit_image_atomic(ctx
, instr
, false);
3426 case nir_intrinsic_bindless_image_size
:
3427 result
= visit_image_size(ctx
, instr
, true);
3429 case nir_intrinsic_image_deref_size
:
3430 result
= visit_image_size(ctx
, instr
, false);
3432 case nir_intrinsic_shader_clock
:
3433 result
= ac_build_shader_clock(&ctx
->ac
);
3435 case nir_intrinsic_discard
:
3436 case nir_intrinsic_discard_if
:
3437 emit_discard(ctx
, instr
);
3439 case nir_intrinsic_memory_barrier
:
3440 case nir_intrinsic_group_memory_barrier
:
3441 case nir_intrinsic_memory_barrier_atomic_counter
:
3442 case nir_intrinsic_memory_barrier_buffer
:
3443 case nir_intrinsic_memory_barrier_image
:
3444 case nir_intrinsic_memory_barrier_shared
:
3445 emit_membar(&ctx
->ac
, instr
);
3447 case nir_intrinsic_barrier
:
3448 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3450 case nir_intrinsic_shared_atomic_add
:
3451 case nir_intrinsic_shared_atomic_imin
:
3452 case nir_intrinsic_shared_atomic_umin
:
3453 case nir_intrinsic_shared_atomic_imax
:
3454 case nir_intrinsic_shared_atomic_umax
:
3455 case nir_intrinsic_shared_atomic_and
:
3456 case nir_intrinsic_shared_atomic_or
:
3457 case nir_intrinsic_shared_atomic_xor
:
3458 case nir_intrinsic_shared_atomic_exchange
:
3459 case nir_intrinsic_shared_atomic_comp_swap
: {
3460 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3461 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3464 case nir_intrinsic_deref_atomic_add
:
3465 case nir_intrinsic_deref_atomic_imin
:
3466 case nir_intrinsic_deref_atomic_umin
:
3467 case nir_intrinsic_deref_atomic_imax
:
3468 case nir_intrinsic_deref_atomic_umax
:
3469 case nir_intrinsic_deref_atomic_and
:
3470 case nir_intrinsic_deref_atomic_or
:
3471 case nir_intrinsic_deref_atomic_xor
:
3472 case nir_intrinsic_deref_atomic_exchange
:
3473 case nir_intrinsic_deref_atomic_comp_swap
: {
3474 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3475 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3478 case nir_intrinsic_interp_deref_at_centroid
:
3479 case nir_intrinsic_interp_deref_at_sample
:
3480 case nir_intrinsic_interp_deref_at_offset
:
3481 result
= visit_interp(ctx
, instr
);
3483 case nir_intrinsic_emit_vertex
:
3484 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3486 case nir_intrinsic_end_primitive
:
3487 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3489 case nir_intrinsic_load_tess_coord
:
3490 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3492 case nir_intrinsic_load_tess_level_outer
:
3493 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3495 case nir_intrinsic_load_tess_level_inner
:
3496 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3498 case nir_intrinsic_load_patch_vertices_in
:
3499 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3501 case nir_intrinsic_vote_all
: {
3502 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3503 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3506 case nir_intrinsic_vote_any
: {
3507 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3508 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3511 case nir_intrinsic_shuffle
:
3512 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3513 get_src(ctx
, instr
->src
[1]));
3515 case nir_intrinsic_reduce
:
3516 result
= ac_build_reduce(&ctx
->ac
,
3517 get_src(ctx
, instr
->src
[0]),
3518 instr
->const_index
[0],
3519 instr
->const_index
[1]);
3521 case nir_intrinsic_inclusive_scan
:
3522 result
= ac_build_inclusive_scan(&ctx
->ac
,
3523 get_src(ctx
, instr
->src
[0]),
3524 instr
->const_index
[0]);
3526 case nir_intrinsic_exclusive_scan
:
3527 result
= ac_build_exclusive_scan(&ctx
->ac
,
3528 get_src(ctx
, instr
->src
[0]),
3529 instr
->const_index
[0]);
3531 case nir_intrinsic_quad_broadcast
: {
3532 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3533 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3534 lane
, lane
, lane
, lane
);
3537 case nir_intrinsic_quad_swap_horizontal
:
3538 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3540 case nir_intrinsic_quad_swap_vertical
:
3541 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3543 case nir_intrinsic_quad_swap_diagonal
:
3544 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3547 fprintf(stderr
, "Unknown intrinsic: ");
3548 nir_print_instr(&instr
->instr
, stderr
);
3549 fprintf(stderr
, "\n");
3553 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3557 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3558 unsigned base_index
,
3559 unsigned constant_index
,
3560 LLVMValueRef dynamic_index
)
3562 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3563 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3564 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3566 /* Bindless uniforms are 64bit so multiple index by 8 */
3567 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3568 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3570 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3572 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3573 NULL
, 0, false, false, true, true);
3575 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3578 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3579 nir_deref_instr
*deref_instr
,
3580 enum ac_descriptor_type desc_type
,
3581 const nir_instr
*instr
,
3582 bool image
, bool write
)
3584 LLVMValueRef index
= NULL
;
3585 unsigned constant_index
= 0;
3586 unsigned descriptor_set
;
3587 unsigned base_index
;
3588 bool bindless
= false;
3593 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3596 index
= get_src(ctx
, img_instr
->src
[0]);
3598 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3599 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3600 nir_tex_src_sampler_handle
);
3601 if (sampSrcIdx
!= -1) {
3604 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3606 assert(tex_instr
&& !image
);
3607 base_index
= tex_instr
->sampler_index
;
3611 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3612 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3613 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3617 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3619 constant_index
+= array_size
* const_value
->u32
[0];
3621 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3623 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3624 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3629 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3632 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3633 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3634 unsigned sidx
= deref_instr
->strct
.index
;
3635 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3636 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3638 unreachable("Unsupported deref type");
3641 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3643 if (deref_instr
->var
->data
.bindless
) {
3644 /* For now just assert on unhandled variable types */
3645 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3647 base_index
= deref_instr
->var
->data
.driver_location
;
3650 index
= index
? index
: ctx
->ac
.i32_0
;
3651 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3652 constant_index
, index
);
3654 base_index
= deref_instr
->var
->data
.binding
;
3657 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3660 constant_index
, index
,
3661 desc_type
, image
, write
, bindless
);
3664 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3667 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3668 * filtering manually. The driver sets img7 to a mask clearing
3669 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3670 * s_and_b32 samp0, samp0, img7
3673 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3675 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3676 LLVMValueRef res
, LLVMValueRef samp
)
3678 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3679 LLVMValueRef img7
, samp0
;
3681 if (ctx
->ac
.chip_class
>= VI
)
3684 img7
= LLVMBuildExtractElement(builder
, res
,
3685 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3686 samp0
= LLVMBuildExtractElement(builder
, samp
,
3687 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3688 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3689 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3690 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3693 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3694 nir_tex_instr
*instr
,
3695 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3696 LLVMValueRef
*fmask_ptr
)
3698 nir_deref_instr
*texture_deref_instr
= NULL
;
3699 nir_deref_instr
*sampler_deref_instr
= NULL
;
3701 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3702 switch (instr
->src
[i
].src_type
) {
3703 case nir_tex_src_texture_deref
:
3704 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3706 case nir_tex_src_sampler_deref
:
3707 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3714 if (!sampler_deref_instr
)
3715 sampler_deref_instr
= texture_deref_instr
;
3717 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3718 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, &instr
->instr
, false, false);
3720 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, &instr
->instr
, false, false);
3722 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3723 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3724 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3726 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3727 instr
->op
== nir_texop_samples_identical
))
3728 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3731 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3734 coord
= ac_to_float(ctx
, coord
);
3735 coord
= ac_build_round(ctx
, coord
);
3736 coord
= ac_to_integer(ctx
, coord
);
3740 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3742 LLVMValueRef result
= NULL
;
3743 struct ac_image_args args
= { 0 };
3744 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3745 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3746 unsigned offset_src
= 0;
3748 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3750 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3751 switch (instr
->src
[i
].src_type
) {
3752 case nir_tex_src_coord
: {
3753 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3754 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3755 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3758 case nir_tex_src_projector
:
3760 case nir_tex_src_comparator
:
3761 if (instr
->is_shadow
)
3762 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3764 case nir_tex_src_offset
:
3765 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3768 case nir_tex_src_bias
:
3769 if (instr
->op
== nir_texop_txb
)
3770 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3772 case nir_tex_src_lod
: {
3773 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3775 if (val
&& val
->i32
[0] == 0)
3776 args
.level_zero
= true;
3778 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3781 case nir_tex_src_ms_index
:
3782 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3784 case nir_tex_src_ms_mcs
:
3786 case nir_tex_src_ddx
:
3787 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3789 case nir_tex_src_ddy
:
3790 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3792 case nir_tex_src_texture_offset
:
3793 case nir_tex_src_sampler_offset
:
3794 case nir_tex_src_plane
:
3800 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3801 result
= get_buffer_size(ctx
, args
.resource
, true);
3805 if (instr
->op
== nir_texop_texture_samples
) {
3806 LLVMValueRef res
, samples
, is_msaa
;
3807 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3808 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3809 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3810 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3811 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3812 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3813 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3814 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3815 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3817 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3818 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3819 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3820 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3821 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3823 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3829 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3830 LLVMValueRef offset
[3], pack
;
3831 for (unsigned chan
= 0; chan
< 3; ++chan
)
3832 offset
[chan
] = ctx
->ac
.i32_0
;
3834 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3835 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3836 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3837 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3838 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3840 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3841 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3843 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3844 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3848 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3849 * so the depth comparison value isn't clamped for Z16 and
3850 * Z24 anymore. Do it manually here.
3852 * It's unnecessary if the original texture format was
3853 * Z32_FLOAT, but we don't know that here.
3855 if (args
.compare
&& ctx
->ac
.chip_class
>= VI
&& ctx
->abi
->clamp_shadow_reference
)
3856 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3858 /* pack derivatives */
3860 int num_src_deriv_channels
, num_dest_deriv_channels
;
3861 switch (instr
->sampler_dim
) {
3862 case GLSL_SAMPLER_DIM_3D
:
3863 case GLSL_SAMPLER_DIM_CUBE
:
3864 num_src_deriv_channels
= 3;
3865 num_dest_deriv_channels
= 3;
3867 case GLSL_SAMPLER_DIM_2D
:
3869 num_src_deriv_channels
= 2;
3870 num_dest_deriv_channels
= 2;
3872 case GLSL_SAMPLER_DIM_1D
:
3873 num_src_deriv_channels
= 1;
3874 if (ctx
->ac
.chip_class
>= GFX9
) {
3875 num_dest_deriv_channels
= 2;
3877 num_dest_deriv_channels
= 1;
3882 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3883 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3884 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3885 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3886 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3888 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3889 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3890 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3894 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3895 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3896 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3897 if (instr
->coord_components
== 3)
3898 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3899 ac_prepare_cube_coords(&ctx
->ac
,
3900 instr
->op
== nir_texop_txd
, instr
->is_array
,
3901 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3904 /* Texture coordinates fixups */
3905 if (instr
->coord_components
> 1 &&
3906 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3908 instr
->op
!= nir_texop_txf
) {
3909 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3912 if (instr
->coord_components
> 2 &&
3913 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3914 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3915 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3916 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3918 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3919 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3922 if (ctx
->ac
.chip_class
>= GFX9
&&
3923 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3924 instr
->op
!= nir_texop_lod
) {
3925 LLVMValueRef filler
;
3926 if (instr
->op
== nir_texop_txf
)
3927 filler
= ctx
->ac
.i32_0
;
3929 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3931 if (instr
->is_array
)
3932 args
.coords
[2] = args
.coords
[1];
3933 args
.coords
[1] = filler
;
3936 /* Pack sample index */
3937 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3938 args
.coords
[instr
->coord_components
] = sample_index
;
3940 if (instr
->op
== nir_texop_samples_identical
) {
3941 struct ac_image_args txf_args
= { 0 };
3942 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3944 txf_args
.dmask
= 0xf;
3945 txf_args
.resource
= fmask_ptr
;
3946 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3947 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3949 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3950 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3954 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3955 instr
->op
!= nir_texop_txs
) {
3956 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3957 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3958 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3959 instr
->is_array
? args
.coords
[2] : NULL
,
3960 args
.coords
[sample_chan
], fmask_ptr
);
3963 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3964 nir_const_value
*const_offset
=
3965 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3966 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3967 assert(const_offset
);
3968 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3969 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3970 args
.coords
[i
] = LLVMBuildAdd(
3971 ctx
->ac
.builder
, args
.coords
[i
],
3972 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3977 /* TODO TG4 support */
3979 if (instr
->op
== nir_texop_tg4
) {
3980 if (instr
->is_shadow
)
3983 args
.dmask
= 1 << instr
->component
;
3986 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3987 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3988 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3990 if (instr
->op
== nir_texop_query_levels
)
3991 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3992 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3993 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3994 instr
->op
!= nir_texop_tg4
)
3995 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3996 else if (instr
->op
== nir_texop_txs
&&
3997 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3999 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4000 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4001 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4002 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4003 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4004 } else if (ctx
->ac
.chip_class
>= GFX9
&&
4005 instr
->op
== nir_texop_txs
&&
4006 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4008 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4009 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4010 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4012 } else if (instr
->dest
.ssa
.num_components
!= 4)
4013 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4017 assert(instr
->dest
.is_ssa
);
4018 result
= ac_to_integer(&ctx
->ac
, result
);
4019 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4024 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4026 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4027 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4029 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4030 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4033 static void visit_post_phi(struct ac_nir_context
*ctx
,
4034 nir_phi_instr
*instr
,
4035 LLVMValueRef llvm_phi
)
4037 nir_foreach_phi_src(src
, instr
) {
4038 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4039 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4041 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4045 static void phi_post_pass(struct ac_nir_context
*ctx
)
4047 hash_table_foreach(ctx
->phis
, entry
) {
4048 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4049 (LLVMValueRef
)entry
->data
);
4054 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4055 const nir_ssa_undef_instr
*instr
)
4057 unsigned num_components
= instr
->def
.num_components
;
4058 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4061 if (num_components
== 1)
4062 undef
= LLVMGetUndef(type
);
4064 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4066 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4069 static void visit_jump(struct ac_llvm_context
*ctx
,
4070 const nir_jump_instr
*instr
)
4072 switch (instr
->type
) {
4073 case nir_jump_break
:
4074 ac_build_break(ctx
);
4076 case nir_jump_continue
:
4077 ac_build_continue(ctx
);
4080 fprintf(stderr
, "Unknown NIR jump instr: ");
4081 nir_print_instr(&instr
->instr
, stderr
);
4082 fprintf(stderr
, "\n");
4088 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4089 enum glsl_base_type type
)
4093 case GLSL_TYPE_UINT
:
4094 case GLSL_TYPE_BOOL
:
4095 case GLSL_TYPE_SUBROUTINE
:
4097 case GLSL_TYPE_INT8
:
4098 case GLSL_TYPE_UINT8
:
4100 case GLSL_TYPE_INT16
:
4101 case GLSL_TYPE_UINT16
:
4103 case GLSL_TYPE_FLOAT
:
4105 case GLSL_TYPE_FLOAT16
:
4107 case GLSL_TYPE_INT64
:
4108 case GLSL_TYPE_UINT64
:
4110 case GLSL_TYPE_DOUBLE
:
4113 unreachable("unknown GLSL type");
4118 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4119 const struct glsl_type
*type
)
4121 if (glsl_type_is_scalar(type
)) {
4122 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4125 if (glsl_type_is_vector(type
)) {
4126 return LLVMVectorType(
4127 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4128 glsl_get_vector_elements(type
));
4131 if (glsl_type_is_matrix(type
)) {
4132 return LLVMArrayType(
4133 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4134 glsl_get_matrix_columns(type
));
4137 if (glsl_type_is_array(type
)) {
4138 return LLVMArrayType(
4139 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4140 glsl_get_length(type
));
4143 assert(glsl_type_is_struct_or_ifc(type
));
4145 LLVMTypeRef member_types
[glsl_get_length(type
)];
4147 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4149 glsl_to_llvm_type(ac
,
4150 glsl_get_struct_field(type
, i
));
4153 return LLVMStructTypeInContext(ac
->context
, member_types
,
4154 glsl_get_length(type
), false);
4157 static void visit_deref(struct ac_nir_context
*ctx
,
4158 nir_deref_instr
*instr
)
4160 if (instr
->mode
!= nir_var_mem_shared
&&
4161 instr
->mode
!= nir_var_mem_global
)
4164 LLVMValueRef result
= NULL
;
4165 switch(instr
->deref_type
) {
4166 case nir_deref_type_var
: {
4167 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4168 result
= entry
->data
;
4171 case nir_deref_type_struct
:
4172 if (instr
->mode
== nir_var_mem_global
) {
4173 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4174 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4175 instr
->strct
.index
);
4176 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4177 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4179 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4180 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4183 case nir_deref_type_array
:
4184 if (instr
->mode
== nir_var_mem_global
) {
4185 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4186 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4188 if ((glsl_type_is_matrix(parent
->type
) &&
4189 glsl_matrix_type_is_row_major(parent
->type
)) ||
4190 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4191 stride
= type_scalar_size_bytes(parent
->type
);
4194 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4195 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4196 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4198 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4200 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4202 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4203 get_src(ctx
, instr
->arr
.index
));
4206 case nir_deref_type_ptr_as_array
:
4207 if (instr
->mode
== nir_var_mem_global
) {
4208 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4210 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4211 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4212 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4214 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4216 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4218 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4219 get_src(ctx
, instr
->arr
.index
));
4222 case nir_deref_type_cast
: {
4223 result
= get_src(ctx
, instr
->parent
);
4225 /* We can't use the structs from LLVM because the shader
4226 * specifies its own offsets. */
4227 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4228 if (instr
->mode
== nir_var_mem_shared
)
4229 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4231 unsigned address_space
;
4233 switch(instr
->mode
) {
4234 case nir_var_mem_shared
:
4235 address_space
= AC_ADDR_SPACE_LDS
;
4237 case nir_var_mem_global
:
4238 address_space
= AC_ADDR_SPACE_GLOBAL
;
4241 unreachable("Unhandled address space");
4244 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4246 if (LLVMTypeOf(result
) != type
) {
4247 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4248 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4251 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4258 unreachable("Unhandled deref_instr deref type");
4261 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4264 static void visit_cf_list(struct ac_nir_context
*ctx
,
4265 struct exec_list
*list
);
4267 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4269 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4270 nir_foreach_instr(instr
, block
)
4272 switch (instr
->type
) {
4273 case nir_instr_type_alu
:
4274 visit_alu(ctx
, nir_instr_as_alu(instr
));
4276 case nir_instr_type_load_const
:
4277 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4279 case nir_instr_type_intrinsic
:
4280 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4282 case nir_instr_type_tex
:
4283 visit_tex(ctx
, nir_instr_as_tex(instr
));
4285 case nir_instr_type_phi
:
4286 visit_phi(ctx
, nir_instr_as_phi(instr
));
4288 case nir_instr_type_ssa_undef
:
4289 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4291 case nir_instr_type_jump
:
4292 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4294 case nir_instr_type_deref
:
4295 visit_deref(ctx
, nir_instr_as_deref(instr
));
4298 fprintf(stderr
, "Unknown NIR instr type: ");
4299 nir_print_instr(instr
, stderr
);
4300 fprintf(stderr
, "\n");
4305 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4308 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4310 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4312 nir_block
*then_block
=
4313 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4315 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4317 visit_cf_list(ctx
, &if_stmt
->then_list
);
4319 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4320 nir_block
*else_block
=
4321 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4323 ac_build_else(&ctx
->ac
, else_block
->index
);
4324 visit_cf_list(ctx
, &if_stmt
->else_list
);
4327 ac_build_endif(&ctx
->ac
, then_block
->index
);
4330 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4332 nir_block
*first_loop_block
=
4333 (nir_block
*) exec_list_get_head(&loop
->body
);
4335 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4337 visit_cf_list(ctx
, &loop
->body
);
4339 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4342 static void visit_cf_list(struct ac_nir_context
*ctx
,
4343 struct exec_list
*list
)
4345 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4347 switch (node
->type
) {
4348 case nir_cf_node_block
:
4349 visit_block(ctx
, nir_cf_node_as_block(node
));
4352 case nir_cf_node_if
:
4353 visit_if(ctx
, nir_cf_node_as_if(node
));
4356 case nir_cf_node_loop
:
4357 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4367 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4368 struct ac_shader_abi
*abi
,
4369 struct nir_shader
*nir
,
4370 struct nir_variable
*variable
,
4371 gl_shader_stage stage
)
4373 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4374 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4376 /* tess ctrl has it's own load/store paths for outputs */
4377 if (stage
== MESA_SHADER_TESS_CTRL
)
4380 if (stage
== MESA_SHADER_VERTEX
||
4381 stage
== MESA_SHADER_TESS_EVAL
||
4382 stage
== MESA_SHADER_GEOMETRY
) {
4383 int idx
= variable
->data
.location
+ variable
->data
.index
;
4384 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4385 int length
= nir
->info
.clip_distance_array_size
+
4386 nir
->info
.cull_distance_array_size
;
4395 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4396 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4397 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4398 for (unsigned chan
= 0; chan
< 4; chan
++) {
4399 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4400 ac_build_alloca_undef(ctx
, type
, "");
4406 setup_locals(struct ac_nir_context
*ctx
,
4407 struct nir_function
*func
)
4410 ctx
->num_locals
= 0;
4411 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4412 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4413 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4414 variable
->data
.location_frac
= 0;
4415 ctx
->num_locals
+= attrib_count
;
4417 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4421 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4422 for (j
= 0; j
< 4; j
++) {
4423 ctx
->locals
[i
* 4 + j
] =
4424 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4430 setup_shared(struct ac_nir_context
*ctx
,
4431 struct nir_shader
*nir
)
4433 nir_foreach_variable(variable
, &nir
->shared
) {
4434 LLVMValueRef shared
=
4435 LLVMAddGlobalInAddressSpace(
4436 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4437 variable
->name
? variable
->name
: "",
4439 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4443 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4444 struct nir_shader
*nir
)
4446 struct ac_nir_context ctx
= {};
4447 struct nir_function
*func
;
4452 ctx
.stage
= nir
->info
.stage
;
4454 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4456 nir_foreach_variable(variable
, &nir
->outputs
)
4457 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4460 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4461 _mesa_key_pointer_equal
);
4462 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4463 _mesa_key_pointer_equal
);
4464 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4465 _mesa_key_pointer_equal
);
4467 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4469 nir_index_ssa_defs(func
->impl
);
4470 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4472 setup_locals(&ctx
, func
);
4474 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4475 setup_shared(&ctx
, nir
);
4477 visit_cf_list(&ctx
, &func
->impl
->body
);
4478 phi_post_pass(&ctx
);
4480 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4481 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4486 ralloc_free(ctx
.defs
);
4487 ralloc_free(ctx
.phis
);
4488 ralloc_free(ctx
.vars
);
4492 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4494 /* While it would be nice not to have this flag, we are constrained
4495 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4498 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4500 /* TODO: Indirect indexing of GS inputs is unimplemented.
4502 * TCS and TES load inputs directly from LDS or offchip memory, so
4503 * indirect indexing is trivial.
4505 nir_variable_mode indirect_mask
= 0;
4506 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4507 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4508 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4509 !llvm_has_working_vgpr_indexing
)) {
4510 indirect_mask
|= nir_var_shader_in
;
4512 if (!llvm_has_working_vgpr_indexing
&&
4513 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4514 indirect_mask
|= nir_var_shader_out
;
4516 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4517 * smart enough to handle indirects without causing excess spilling
4518 * causing the gpu to hang.
4520 * See the following thread for more details of the problem:
4521 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4523 indirect_mask
|= nir_var_function_temp
;
4525 nir_lower_indirect_derefs(nir
, indirect_mask
);
4529 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4531 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4535 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4537 if (var
->data
.mode
!= nir_var_shader_out
)
4540 unsigned writemask
= 0;
4541 const int location
= var
->data
.location
;
4542 unsigned first_component
= var
->data
.location_frac
;
4543 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4545 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4546 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4547 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4548 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4554 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4555 unsigned *cond_block_tf_writemask
,
4556 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4558 switch (cf_node
->type
) {
4559 case nir_cf_node_block
: {
4560 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4561 nir_foreach_instr(instr
, block
) {
4562 if (instr
->type
!= nir_instr_type_intrinsic
)
4565 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4566 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4568 /* If we find a barrier in nested control flow put this in the
4569 * too hard basket. In GLSL this is not possible but it is in
4573 *tessfactors_are_def_in_all_invocs
= false;
4577 /* The following case must be prevented:
4578 * gl_TessLevelInner = ...;
4580 * if (gl_InvocationID == 1)
4581 * gl_TessLevelInner = ...;
4583 * If you consider disjoint code segments separated by barriers, each
4584 * such segment that writes tess factor channels should write the same
4585 * channels in all codepaths within that segment.
4587 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4588 /* Accumulate the result: */
4589 *tessfactors_are_def_in_all_invocs
&=
4590 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4592 /* Analyze the next code segment from scratch. */
4593 *upper_block_tf_writemask
= 0;
4594 *cond_block_tf_writemask
= 0;
4597 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4602 case nir_cf_node_if
: {
4603 unsigned then_tessfactor_writemask
= 0;
4604 unsigned else_tessfactor_writemask
= 0;
4606 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4607 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4608 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4609 cond_block_tf_writemask
,
4610 tessfactors_are_def_in_all_invocs
, true);
4613 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4614 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4615 cond_block_tf_writemask
,
4616 tessfactors_are_def_in_all_invocs
, true);
4619 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4620 /* If both statements write the same tess factor channels,
4621 * we can say that the upper block writes them too.
4623 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4624 else_tessfactor_writemask
;
4625 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4626 else_tessfactor_writemask
;
4631 case nir_cf_node_loop
: {
4632 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4633 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4634 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4635 cond_block_tf_writemask
,
4636 tessfactors_are_def_in_all_invocs
, true);
4642 unreachable("unknown cf node type");
4647 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4649 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4651 /* The pass works as follows:
4652 * If all codepaths write tess factors, we can say that all
4653 * invocations define tess factors.
4655 * Each tess factor channel is tracked separately.
4657 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4658 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4660 /* Initial value = true. Here the pass will accumulate results from
4661 * multiple segments surrounded by barriers. If tess factors aren't
4662 * written at all, it's a shader bug and we don't care if this will be
4665 bool tessfactors_are_def_in_all_invocs
= true;
4667 nir_foreach_function(function
, nir
) {
4668 if (function
->impl
) {
4669 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4670 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4671 &cond_block_tf_writemask
,
4672 &tessfactors_are_def_in_all_invocs
,
4678 /* Accumulate the result for the last code segment separated by a
4681 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4682 tessfactors_are_def_in_all_invocs
&=
4683 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4686 return tessfactors_are_def_in_all_invocs
;