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 "util/bitscan.h"
31 #include "util/u_math.h"
32 #include "ac_shader_abi.h"
33 #include "ac_shader_util.h"
35 struct ac_nir_context
{
36 struct ac_llvm_context ac
;
37 struct ac_shader_abi
*abi
;
39 gl_shader_stage stage
;
41 LLVMValueRef
*ssa_defs
;
43 struct hash_table
*defs
;
44 struct hash_table
*phis
;
45 struct hash_table
*vars
;
47 LLVMValueRef main_function
;
48 LLVMBasicBlockRef continue_block
;
49 LLVMBasicBlockRef break_block
;
55 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
56 const nir_deref_var
*deref
,
57 enum ac_descriptor_type desc_type
,
58 const nir_tex_instr
*instr
,
59 bool image
, bool write
);
62 build_store_values_extended(struct ac_llvm_context
*ac
,
65 unsigned value_stride
,
68 LLVMBuilderRef builder
= ac
->builder
;
71 for (i
= 0; i
< value_count
; i
++) {
72 LLVMValueRef ptr
= values
[i
* value_stride
];
73 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
74 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
75 LLVMBuildStore(builder
, value
, ptr
);
79 static enum ac_image_dim
80 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
84 case GLSL_SAMPLER_DIM_1D
:
85 if (ctx
->chip_class
>= GFX9
)
86 return is_array
? ac_image_2darray
: ac_image_2d
;
87 return is_array
? ac_image_1darray
: ac_image_1d
;
88 case GLSL_SAMPLER_DIM_2D
:
89 case GLSL_SAMPLER_DIM_RECT
:
90 case GLSL_SAMPLER_DIM_EXTERNAL
:
91 return is_array
? ac_image_2darray
: ac_image_2d
;
92 case GLSL_SAMPLER_DIM_3D
:
94 case GLSL_SAMPLER_DIM_CUBE
:
96 case GLSL_SAMPLER_DIM_MS
:
97 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
98 case GLSL_SAMPLER_DIM_SUBPASS
:
99 return ac_image_2darray
;
100 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
101 return ac_image_2darraymsaa
;
103 unreachable("bad sampler dim");
107 static enum ac_image_dim
108 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
111 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
113 if (dim
== ac_image_cube
||
114 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
115 dim
= ac_image_2darray
;
120 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
121 const nir_ssa_def
*def
)
123 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
124 if (def
->num_components
> 1) {
125 type
= LLVMVectorType(type
, def
->num_components
);
130 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
133 return nir
->ssa_defs
[src
.ssa
->index
];
137 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
139 LLVMValueRef ptr
= get_src(ctx
, src
);
140 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
141 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
143 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
144 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
147 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
148 const struct nir_block
*b
)
150 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
151 return (LLVMBasicBlockRef
)entry
->data
;
154 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
156 unsigned num_components
)
158 LLVMValueRef value
= get_src(ctx
, src
.src
);
159 bool need_swizzle
= false;
162 unsigned src_components
= ac_get_llvm_num_components(value
);
163 for (unsigned i
= 0; i
< num_components
; ++i
) {
164 assert(src
.swizzle
[i
] < src_components
);
165 if (src
.swizzle
[i
] != i
)
169 if (need_swizzle
|| num_components
!= src_components
) {
170 LLVMValueRef masks
[] = {
171 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
176 if (src_components
> 1 && num_components
== 1) {
177 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
179 } else if (src_components
== 1 && num_components
> 1) {
180 LLVMValueRef values
[] = {value
, value
, value
, value
};
181 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
183 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
184 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
193 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
194 LLVMIntPredicate pred
, LLVMValueRef src0
,
197 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
198 return LLVMBuildSelect(ctx
->builder
, result
,
199 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
203 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
204 LLVMRealPredicate pred
, LLVMValueRef src0
,
208 src0
= ac_to_float(ctx
, src0
);
209 src1
= ac_to_float(ctx
, src1
);
210 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
211 return LLVMBuildSelect(ctx
->builder
, result
,
212 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
216 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
218 LLVMTypeRef result_type
,
222 LLVMValueRef params
[] = {
223 ac_to_float(ctx
, src0
),
226 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
227 ac_get_elem_bits(ctx
, result_type
));
228 assert(length
< sizeof(name
));
229 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
232 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
234 LLVMTypeRef result_type
,
235 LLVMValueRef src0
, LLVMValueRef src1
)
238 LLVMValueRef params
[] = {
239 ac_to_float(ctx
, src0
),
240 ac_to_float(ctx
, src1
),
243 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
244 ac_get_elem_bits(ctx
, result_type
));
245 assert(length
< sizeof(name
));
246 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
249 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
251 LLVMTypeRef result_type
,
252 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
255 LLVMValueRef params
[] = {
256 ac_to_float(ctx
, src0
),
257 ac_to_float(ctx
, src1
),
258 ac_to_float(ctx
, src2
),
261 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
262 ac_get_elem_bits(ctx
, result_type
));
263 assert(length
< sizeof(name
));
264 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
267 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
268 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
270 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
272 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
273 ac_to_integer(ctx
, src2
), "");
276 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
277 LLVMIntPredicate pred
,
278 LLVMValueRef src0
, LLVMValueRef src1
)
280 return LLVMBuildSelect(ctx
->builder
,
281 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
286 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
289 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
290 LLVMBuildNeg(ctx
->builder
, src0
, ""));
293 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
295 LLVMValueRef src0
, LLVMValueRef src1
)
297 LLVMTypeRef ret_type
;
298 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
300 LLVMValueRef params
[] = { src0
, src1
};
301 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
304 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
305 params
, 2, AC_FUNC_ATTR_READNONE
);
307 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
308 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
312 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
315 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
318 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
321 src0
= ac_to_float(ctx
, src0
);
322 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
323 return LLVMBuildSExt(ctx
->builder
,
324 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
328 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
332 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
337 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
340 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
343 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
344 return LLVMBuildSExt(ctx
->builder
,
345 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
349 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
353 LLVMValueRef cond
= NULL
;
355 src0
= ac_to_float(ctx
, src0
);
356 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
358 if (ctx
->chip_class
>= VI
) {
359 LLVMValueRef args
[2];
360 /* Check if the result is a denormal - and flush to 0 if so. */
362 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
363 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
366 /* need to convert back up to f32 */
367 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
369 if (ctx
->chip_class
>= VI
)
370 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
373 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
374 * so compare the result and flush to 0 if it's smaller.
376 LLVMValueRef temp
, cond2
;
377 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
378 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
379 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
381 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
382 temp
, ctx
->f32_0
, "");
383 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
384 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
389 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
390 LLVMValueRef src0
, LLVMValueRef src1
)
392 LLVMValueRef dst64
, result
;
393 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
394 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
396 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
397 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
398 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
402 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
403 LLVMValueRef src0
, LLVMValueRef src1
)
405 LLVMValueRef dst64
, result
;
406 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
407 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
409 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
410 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
411 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
415 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
417 const LLVMValueRef srcs
[3])
420 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
422 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
423 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
427 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
428 LLVMValueRef src0
, LLVMValueRef src1
,
429 LLVMValueRef src2
, LLVMValueRef src3
)
431 LLVMValueRef bfi_args
[3], result
;
433 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
434 LLVMBuildSub(ctx
->builder
,
435 LLVMBuildShl(ctx
->builder
,
440 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
443 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
446 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
447 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
449 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
450 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
451 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
453 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
457 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
460 LLVMValueRef comp
[2];
462 src0
= ac_to_float(ctx
, src0
);
463 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
464 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
466 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
469 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
472 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
473 LLVMValueRef temps
[2], result
, val
;
476 for (i
= 0; i
< 2; i
++) {
477 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
478 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
479 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
480 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
483 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
485 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
490 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
498 if (op
== nir_op_fddx_fine
)
499 mask
= AC_TID_MASK_LEFT
;
500 else if (op
== nir_op_fddy_fine
)
501 mask
= AC_TID_MASK_TOP
;
503 mask
= AC_TID_MASK_TOP_LEFT
;
505 /* for DDX we want to next X pixel, DDY next Y pixel. */
506 if (op
== nir_op_fddx_fine
||
507 op
== nir_op_fddx_coarse
||
513 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
518 * this takes an I,J coordinate pair,
519 * and works out the X and Y derivatives.
520 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
522 static LLVMValueRef
emit_ddxy_interp(
523 struct ac_nir_context
*ctx
,
524 LLVMValueRef interp_ij
)
526 LLVMValueRef result
[4], a
;
529 for (i
= 0; i
< 2; i
++) {
530 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
531 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
532 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
533 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
535 return ac_build_gather_values(&ctx
->ac
, result
, 4);
538 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
540 LLVMValueRef src
[4], result
= NULL
;
541 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
542 unsigned src_components
;
543 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
545 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
552 case nir_op_pack_half_2x16
:
555 case nir_op_unpack_half_2x16
:
558 case nir_op_cube_face_coord
:
559 case nir_op_cube_face_index
:
563 src_components
= num_components
;
566 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
567 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
575 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
576 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
579 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
582 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
585 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
588 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
589 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
590 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
593 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
594 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
595 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
598 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
601 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
604 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
607 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
610 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
611 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
612 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
613 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
614 ac_to_float_type(&ctx
->ac
, def_type
), result
);
615 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
616 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
619 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
620 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
621 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
624 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
627 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
630 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
633 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
634 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
635 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
638 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
639 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
643 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
646 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
649 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
652 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
653 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
654 LLVMTypeOf(src
[0]), ""),
658 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
659 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
660 LLVMTypeOf(src
[0]), ""),
664 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
665 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
666 LLVMTypeOf(src
[0]), ""),
670 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
673 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
676 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
679 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
682 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
685 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
688 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
691 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
694 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
697 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
700 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
701 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
704 result
= emit_iabs(&ctx
->ac
, src
[0]);
707 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
710 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
713 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
716 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
719 result
= ac_build_isign(&ctx
->ac
, src
[0],
720 instr
->dest
.dest
.ssa
.bit_size
);
723 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
724 result
= ac_build_fsign(&ctx
->ac
, src
[0],
725 instr
->dest
.dest
.ssa
.bit_size
);
728 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
729 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
732 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
733 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
736 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
737 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
739 case nir_op_fround_even
:
740 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
741 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
744 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
745 result
= ac_build_fract(&ctx
->ac
, src
[0],
746 instr
->dest
.dest
.ssa
.bit_size
);
749 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
750 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
753 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
754 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
757 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
758 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
761 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
762 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
765 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
766 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
769 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
771 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
774 case nir_op_frexp_exp
:
775 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
776 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
777 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
780 case nir_op_frexp_sig
:
781 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
782 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
783 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
786 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
787 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
788 if (ctx
->ac
.chip_class
< GFX9
&&
789 instr
->dest
.dest
.ssa
.bit_size
== 32) {
790 /* Only pre-GFX9 chips do not flush denorms. */
791 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
792 ac_to_float_type(&ctx
->ac
, def_type
),
797 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
798 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
799 if (ctx
->ac
.chip_class
< GFX9
&&
800 instr
->dest
.dest
.ssa
.bit_size
== 32) {
801 /* Only pre-GFX9 chips do not flush denorms. */
802 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
803 ac_to_float_type(&ctx
->ac
, def_type
),
808 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
809 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
812 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
813 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
814 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
816 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
818 case nir_op_ibitfield_extract
:
819 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
821 case nir_op_ubitfield_extract
:
822 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
824 case nir_op_bitfield_insert
:
825 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
827 case nir_op_bitfield_reverse
:
828 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
830 case nir_op_bit_count
:
831 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
832 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
834 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
835 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
841 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
842 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
843 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
847 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
848 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
852 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
853 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
857 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
858 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
862 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
863 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
866 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
867 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
875 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
876 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
877 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
879 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
883 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
884 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
885 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
887 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
890 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
892 case nir_op_find_lsb
:
893 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
894 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
896 case nir_op_ufind_msb
:
897 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
898 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
900 case nir_op_ifind_msb
:
901 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
902 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
904 case nir_op_uadd_carry
:
905 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
906 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
907 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
909 case nir_op_usub_borrow
:
910 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
911 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
912 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
915 result
= emit_b2f(&ctx
->ac
, src
[0]);
918 result
= emit_f2b(&ctx
->ac
, src
[0]);
921 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
924 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
925 result
= emit_i2b(&ctx
->ac
, src
[0]);
927 case nir_op_fquantize2f16
:
928 result
= emit_f2f16(&ctx
->ac
, src
[0]);
930 case nir_op_umul_high
:
931 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
932 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
933 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
935 case nir_op_imul_high
:
936 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
937 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
938 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
940 case nir_op_pack_half_2x16
:
941 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
943 case nir_op_unpack_half_2x16
:
944 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
948 case nir_op_fddx_fine
:
949 case nir_op_fddy_fine
:
950 case nir_op_fddx_coarse
:
951 case nir_op_fddy_coarse
:
952 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
955 case nir_op_unpack_64_2x32_split_x
: {
956 assert(ac_get_llvm_num_components(src
[0]) == 1);
957 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
960 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
965 case nir_op_unpack_64_2x32_split_y
: {
966 assert(ac_get_llvm_num_components(src
[0]) == 1);
967 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
970 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
975 case nir_op_pack_64_2x32_split
: {
976 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
977 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
978 src
[0], ctx
->ac
.i32_0
, "");
979 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
980 src
[1], ctx
->ac
.i32_1
, "");
981 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
985 case nir_op_cube_face_coord
: {
986 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
987 LLVMValueRef results
[2];
989 for (unsigned chan
= 0; chan
< 3; chan
++)
990 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
991 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
992 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
993 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
994 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
995 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
999 case nir_op_cube_face_index
: {
1000 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1002 for (unsigned chan
= 0; chan
< 3; chan
++)
1003 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1004 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1005 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1010 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1011 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1012 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1013 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1016 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1017 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1020 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1021 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1024 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1025 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1026 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1027 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1030 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1031 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1034 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1035 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1037 case nir_op_fmed3
: {
1038 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1039 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1040 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1041 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1042 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1043 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1044 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1045 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1048 case nir_op_imed3
: {
1049 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1050 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1051 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1052 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1055 case nir_op_umed3
: {
1056 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1057 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1058 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1059 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1064 fprintf(stderr
, "Unknown NIR alu instr: ");
1065 nir_print_instr(&instr
->instr
, stderr
);
1066 fprintf(stderr
, "\n");
1071 assert(instr
->dest
.dest
.is_ssa
);
1072 result
= ac_to_integer(&ctx
->ac
, result
);
1073 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1077 static void visit_load_const(struct ac_nir_context
*ctx
,
1078 const nir_load_const_instr
*instr
)
1080 LLVMValueRef values
[4], value
= NULL
;
1081 LLVMTypeRef element_type
=
1082 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1084 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1085 switch (instr
->def
.bit_size
) {
1087 values
[i
] = LLVMConstInt(element_type
,
1088 instr
->value
.u32
[i
], false);
1091 values
[i
] = LLVMConstInt(element_type
,
1092 instr
->value
.u64
[i
], false);
1096 "unsupported nir load_const bit_size: %d\n",
1097 instr
->def
.bit_size
);
1101 if (instr
->def
.num_components
> 1) {
1102 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1106 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1110 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1113 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1114 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1117 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1118 /* On VI, the descriptor contains the size in bytes,
1119 * but TXQ must return the size in elements.
1120 * The stride is always non-zero for resources using TXQ.
1122 LLVMValueRef stride
=
1123 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1125 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1126 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1127 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1128 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1130 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1135 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1136 struct ac_image_args
*args
,
1137 const nir_tex_instr
*instr
)
1139 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1140 LLVMValueRef half_texel
[2];
1141 LLVMValueRef compare_cube_wa
= NULL
;
1142 LLVMValueRef result
;
1146 struct ac_image_args txq_args
= { 0 };
1148 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1149 txq_args
.opcode
= ac_image_get_resinfo
;
1150 txq_args
.dmask
= 0xf;
1151 txq_args
.lod
= ctx
->i32_0
;
1152 txq_args
.resource
= args
->resource
;
1153 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1154 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1156 for (unsigned c
= 0; c
< 2; c
++) {
1157 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1158 LLVMConstInt(ctx
->i32
, c
, false), "");
1159 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1160 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1161 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1162 LLVMConstReal(ctx
->f32
, -0.5), "");
1166 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1168 for (unsigned c
= 0; c
< 2; c
++) {
1170 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1171 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1175 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1176 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1177 * workaround by sampling using a scaled type and converting.
1178 * This is taken from amdgpu-pro shaders.
1180 /* NOTE this produces some ugly code compared to amdgpu-pro,
1181 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1182 * and then reads them back. -pro generates two selects,
1183 * one s_cmp for the descriptor rewriting
1184 * one v_cmp for the coordinate and result changes.
1186 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1187 LLVMValueRef tmp
, tmp2
;
1189 /* workaround 8/8/8/8 uint/sint cube gather bug */
1190 /* first detect it then change to a scaled read and f2i */
1191 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1194 /* extract the DATA_FORMAT */
1195 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1196 LLVMConstInt(ctx
->i32
, 6, false), false);
1198 /* is the DATA_FORMAT == 8_8_8_8 */
1199 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1201 if (stype
== GLSL_TYPE_UINT
)
1202 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1203 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1204 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1206 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1207 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1208 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1210 /* replace the NUM FORMAT in the descriptor */
1211 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1212 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1214 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1216 /* don't modify the coordinates for this case */
1217 for (unsigned c
= 0; c
< 2; ++c
)
1218 args
->coords
[c
] = LLVMBuildSelect(
1219 ctx
->builder
, compare_cube_wa
,
1220 orig_coords
[c
], args
->coords
[c
], "");
1223 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1224 result
= ac_build_image_opcode(ctx
, args
);
1226 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1227 LLVMValueRef tmp
, tmp2
;
1229 /* if the cube workaround is in place, f2i the result. */
1230 for (unsigned c
= 0; c
< 4; c
++) {
1231 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1232 if (stype
== GLSL_TYPE_UINT
)
1233 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1235 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1236 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1237 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1238 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1239 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1240 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1246 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1247 const nir_tex_instr
*instr
,
1248 struct ac_image_args
*args
)
1250 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1251 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1253 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1254 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1258 util_last_bit(mask
),
1261 return ac_build_buffer_load_format(&ctx
->ac
,
1265 util_last_bit(mask
),
1270 args
->opcode
= ac_image_sample
;
1272 switch (instr
->op
) {
1274 case nir_texop_txf_ms
:
1275 case nir_texop_samples_identical
:
1276 args
->opcode
= args
->level_zero
||
1277 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1278 ac_image_load
: ac_image_load_mip
;
1279 args
->level_zero
= false;
1282 case nir_texop_query_levels
:
1283 args
->opcode
= ac_image_get_resinfo
;
1285 args
->lod
= ctx
->ac
.i32_0
;
1286 args
->level_zero
= false;
1289 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1291 args
->level_zero
= true;
1295 args
->opcode
= ac_image_gather4
;
1296 args
->level_zero
= true;
1299 args
->opcode
= ac_image_get_lod
;
1305 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1306 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1307 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1308 return lower_gather4_integer(&ctx
->ac
, args
, instr
);
1312 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1313 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1314 if ((args
->dim
== ac_image_2darray
||
1315 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1316 args
->coords
[1] = ctx
->ac
.i32_0
;
1320 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1321 return ac_build_image_opcode(&ctx
->ac
, args
);
1324 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1325 nir_intrinsic_instr
*instr
)
1327 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1328 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1330 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1331 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1335 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1336 nir_intrinsic_instr
*instr
)
1338 LLVMValueRef ptr
, addr
;
1340 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1341 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1342 get_src(ctx
, instr
->src
[0]), "");
1344 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1345 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1347 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1350 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1351 const nir_intrinsic_instr
*instr
)
1353 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1355 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1358 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1360 uint32_t new_mask
= 0;
1361 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1362 if (mask
& (1u << i
))
1363 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1367 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1368 unsigned start
, unsigned count
)
1370 LLVMTypeRef type
= LLVMTypeOf(src
);
1372 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1378 unsigned src_elements
= LLVMGetVectorSize(type
);
1379 assert(start
< src_elements
);
1380 assert(start
+ count
<= src_elements
);
1382 if (start
== 0 && count
== src_elements
)
1386 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1389 LLVMValueRef indices
[8];
1390 for (unsigned i
= 0; i
< count
; ++i
)
1391 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1393 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1394 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1397 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1398 nir_intrinsic_instr
*instr
)
1400 const char *store_name
;
1401 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1402 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1403 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1404 int components_32bit
= elem_size_mult
* instr
->num_components
;
1405 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1406 LLVMValueRef base_data
, base_offset
;
1407 LLVMValueRef params
[6];
1409 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1410 get_src(ctx
, instr
->src
[1]), true);
1411 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1412 params
[4] = ctx
->ac
.i1false
; /* glc */
1413 params
[5] = ctx
->ac
.i1false
; /* slc */
1415 if (components_32bit
> 1)
1416 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1418 writemask
= widen_mask(writemask
, elem_size_mult
);
1420 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1421 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1422 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1424 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1428 LLVMValueRef offset
;
1430 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1432 /* Due to an LLVM limitation, split 3-element writes
1433 * into a 2-element and a 1-element write. */
1435 writemask
|= 1 << (start
+ 2);
1440 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1445 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1446 } else if (count
== 2) {
1447 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1451 store_name
= "llvm.amdgcn.buffer.store.f32";
1453 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1455 offset
= base_offset
;
1457 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1461 ac_build_intrinsic(&ctx
->ac
, store_name
,
1462 ctx
->ac
.voidt
, params
, 6, 0);
1466 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1467 const nir_intrinsic_instr
*instr
)
1470 LLVMValueRef params
[6];
1473 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1474 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1476 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1477 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1478 get_src(ctx
, instr
->src
[0]),
1480 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1481 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1482 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1484 switch (instr
->intrinsic
) {
1485 case nir_intrinsic_ssbo_atomic_add
:
1486 name
= "llvm.amdgcn.buffer.atomic.add";
1488 case nir_intrinsic_ssbo_atomic_imin
:
1489 name
= "llvm.amdgcn.buffer.atomic.smin";
1491 case nir_intrinsic_ssbo_atomic_umin
:
1492 name
= "llvm.amdgcn.buffer.atomic.umin";
1494 case nir_intrinsic_ssbo_atomic_imax
:
1495 name
= "llvm.amdgcn.buffer.atomic.smax";
1497 case nir_intrinsic_ssbo_atomic_umax
:
1498 name
= "llvm.amdgcn.buffer.atomic.umax";
1500 case nir_intrinsic_ssbo_atomic_and
:
1501 name
= "llvm.amdgcn.buffer.atomic.and";
1503 case nir_intrinsic_ssbo_atomic_or
:
1504 name
= "llvm.amdgcn.buffer.atomic.or";
1506 case nir_intrinsic_ssbo_atomic_xor
:
1507 name
= "llvm.amdgcn.buffer.atomic.xor";
1509 case nir_intrinsic_ssbo_atomic_exchange
:
1510 name
= "llvm.amdgcn.buffer.atomic.swap";
1512 case nir_intrinsic_ssbo_atomic_comp_swap
:
1513 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1519 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1522 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1523 const nir_intrinsic_instr
*instr
)
1525 LLVMValueRef results
[2];
1526 int load_components
;
1527 int num_components
= instr
->num_components
;
1528 if (instr
->dest
.ssa
.bit_size
== 64)
1529 num_components
*= 2;
1531 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1532 load_components
= MIN2(num_components
- i
, 4);
1533 const char *load_name
;
1534 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1535 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1536 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1538 if (load_components
== 3)
1539 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1540 else if (load_components
> 1)
1541 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1543 if (load_components
>= 3)
1544 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1545 else if (load_components
== 2)
1546 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1547 else if (load_components
== 1)
1548 load_name
= "llvm.amdgcn.buffer.load.f32";
1550 unreachable("unhandled number of components");
1552 LLVMValueRef params
[] = {
1553 ctx
->abi
->load_ssbo(ctx
->abi
,
1554 get_src(ctx
, instr
->src
[0]),
1562 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1566 LLVMValueRef ret
= results
[0];
1567 if (num_components
> 4 || num_components
== 3) {
1568 LLVMValueRef masks
[] = {
1569 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1570 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1571 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1572 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1575 if (num_components
== 6) {
1576 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1577 results
[1] = ac_build_expand_to_vec4(&ctx
->ac
, results
[1], 4);
1580 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1581 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1582 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1585 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1586 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1589 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1590 const nir_intrinsic_instr
*instr
)
1593 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1594 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1595 int num_components
= instr
->num_components
;
1597 if (ctx
->abi
->load_ubo
)
1598 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1600 if (instr
->dest
.ssa
.bit_size
== 64)
1601 num_components
*= 2;
1603 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1604 NULL
, 0, false, false, true, true);
1605 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1606 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1607 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1611 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1612 bool vs_in
, unsigned *vertex_index_out
,
1613 LLVMValueRef
*vertex_index_ref
,
1614 unsigned *const_out
, LLVMValueRef
*indir_out
)
1616 unsigned const_offset
= 0;
1617 nir_deref
*tail
= &deref
->deref
;
1618 LLVMValueRef offset
= NULL
;
1620 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1622 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1623 if (vertex_index_out
)
1624 *vertex_index_out
= deref_array
->base_offset
;
1626 if (vertex_index_ref
) {
1627 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1628 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1629 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1631 *vertex_index_ref
= vtx
;
1635 if (deref
->var
->data
.compact
) {
1636 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1637 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1638 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1639 /* We always lower indirect dereferences for "compact" array vars. */
1640 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1642 const_offset
= deref_array
->base_offset
;
1646 while (tail
->child
!= NULL
) {
1647 const struct glsl_type
*parent_type
= tail
->type
;
1650 if (tail
->deref_type
== nir_deref_type_array
) {
1651 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1652 LLVMValueRef index
, stride
, local_offset
;
1653 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1655 const_offset
+= size
* deref_array
->base_offset
;
1656 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1659 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1660 index
= get_src(ctx
, deref_array
->indirect
);
1661 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1662 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1665 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1667 offset
= local_offset
;
1668 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1669 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1671 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1672 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1673 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1676 unreachable("unsupported deref type");
1680 if (const_offset
&& offset
)
1681 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1682 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1685 *const_out
= const_offset
;
1686 *indir_out
= offset
;
1690 build_gep_for_deref(struct ac_nir_context
*ctx
,
1691 nir_deref_var
*deref
)
1693 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1694 assert(entry
->data
);
1695 LLVMValueRef val
= entry
->data
;
1696 nir_deref
*tail
= deref
->deref
.child
;
1697 while (tail
!= NULL
) {
1698 LLVMValueRef offset
;
1699 switch (tail
->deref_type
) {
1700 case nir_deref_type_array
: {
1701 nir_deref_array
*array
= nir_deref_as_array(tail
);
1702 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1703 if (array
->deref_array_type
==
1704 nir_deref_array_type_indirect
) {
1705 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1712 case nir_deref_type_struct
: {
1713 nir_deref_struct
*deref_struct
=
1714 nir_deref_as_struct(tail
);
1715 offset
= LLVMConstInt(ctx
->ac
.i32
,
1716 deref_struct
->index
, 0);
1720 unreachable("bad deref type");
1722 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1728 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1729 nir_intrinsic_instr
*instr
,
1732 LLVMValueRef result
;
1733 LLVMValueRef vertex_index
= NULL
;
1734 LLVMValueRef indir_index
= NULL
;
1735 unsigned const_index
= 0;
1736 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1737 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1738 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1739 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1741 get_deref_offset(ctx
, instr
->variables
[0],
1742 false, NULL
, is_patch
? NULL
: &vertex_index
,
1743 &const_index
, &indir_index
);
1745 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1747 LLVMTypeRef src_component_type
;
1748 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1749 src_component_type
= LLVMGetElementType(dest_type
);
1751 src_component_type
= dest_type
;
1753 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1754 vertex_index
, indir_index
,
1755 const_index
, location
, driver_location
,
1756 instr
->variables
[0]->var
->data
.location_frac
,
1757 instr
->num_components
,
1758 is_patch
, is_compact
, load_inputs
);
1759 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1762 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1763 nir_intrinsic_instr
*instr
)
1765 LLVMValueRef values
[8];
1766 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1767 int ve
= instr
->dest
.ssa
.num_components
;
1768 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1769 LLVMValueRef indir_index
;
1771 unsigned const_index
;
1772 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1773 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1774 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1775 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1776 &const_index
, &indir_index
);
1778 if (instr
->dest
.ssa
.bit_size
== 64)
1781 switch (instr
->variables
[0]->var
->data
.mode
) {
1782 case nir_var_shader_in
:
1783 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1784 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1785 return load_tess_varyings(ctx
, instr
, true);
1788 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1789 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1790 LLVMValueRef indir_index
;
1791 unsigned const_index
, vertex_index
;
1792 get_deref_offset(ctx
, instr
->variables
[0],
1793 false, &vertex_index
, NULL
,
1794 &const_index
, &indir_index
);
1796 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1797 instr
->variables
[0]->var
->data
.driver_location
,
1798 instr
->variables
[0]->var
->data
.location_frac
,
1799 instr
->num_components
, vertex_index
, const_index
, type
);
1802 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1804 unsigned count
= glsl_count_attribute_slots(
1805 instr
->variables
[0]->var
->type
,
1806 ctx
->stage
== MESA_SHADER_VERTEX
);
1808 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1809 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1810 stride
, false, true);
1812 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1816 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1820 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1822 unsigned count
= glsl_count_attribute_slots(
1823 instr
->variables
[0]->var
->type
, false);
1825 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1826 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1827 stride
, true, true);
1829 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1833 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1837 case nir_var_shared
: {
1838 LLVMValueRef address
= build_gep_for_deref(ctx
,
1839 instr
->variables
[0]);
1840 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1841 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1842 get_def_type(ctx
, &instr
->dest
.ssa
),
1845 case nir_var_shader_out
:
1846 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1847 return load_tess_varyings(ctx
, instr
, false);
1850 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1852 unsigned count
= glsl_count_attribute_slots(
1853 instr
->variables
[0]->var
->type
, false);
1855 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1856 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1857 stride
, true, true);
1859 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1863 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1864 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1870 unreachable("unhandle variable mode");
1872 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1873 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1877 visit_store_var(struct ac_nir_context
*ctx
,
1878 nir_intrinsic_instr
*instr
)
1880 LLVMValueRef temp_ptr
, value
;
1881 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1882 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1883 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1884 int writemask
= instr
->const_index
[0];
1885 LLVMValueRef indir_index
;
1886 unsigned const_index
;
1887 get_deref_offset(ctx
, instr
->variables
[0], false,
1888 NULL
, NULL
, &const_index
, &indir_index
);
1890 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1892 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1893 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1896 writemask
= widen_mask(writemask
, 2);
1899 writemask
= writemask
<< comp
;
1901 switch (instr
->variables
[0]->var
->data
.mode
) {
1902 case nir_var_shader_out
:
1904 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1905 LLVMValueRef vertex_index
= NULL
;
1906 LLVMValueRef indir_index
= NULL
;
1907 unsigned const_index
= 0;
1908 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1910 get_deref_offset(ctx
, instr
->variables
[0],
1911 false, NULL
, is_patch
? NULL
: &vertex_index
,
1912 &const_index
, &indir_index
);
1914 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1915 vertex_index
, indir_index
,
1916 const_index
, src
, writemask
);
1920 for (unsigned chan
= 0; chan
< 8; chan
++) {
1922 if (!(writemask
& (1 << chan
)))
1925 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1927 if (instr
->variables
[0]->var
->data
.compact
)
1930 unsigned count
= glsl_count_attribute_slots(
1931 instr
->variables
[0]->var
->type
, false);
1933 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1934 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1935 stride
, true, true);
1937 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1938 value
, indir_index
, "");
1939 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1940 count
, stride
, tmp_vec
);
1943 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1945 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1950 for (unsigned chan
= 0; chan
< 8; chan
++) {
1951 if (!(writemask
& (1 << chan
)))
1954 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1956 unsigned count
= glsl_count_attribute_slots(
1957 instr
->variables
[0]->var
->type
, false);
1959 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1960 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1963 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1964 value
, indir_index
, "");
1965 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1968 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1970 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1974 case nir_var_shared
: {
1975 int writemask
= instr
->const_index
[0];
1976 LLVMValueRef address
= build_gep_for_deref(ctx
,
1977 instr
->variables
[0]);
1978 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1979 unsigned components
=
1980 glsl_get_vector_elements(
1981 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1982 if (writemask
== (1 << components
) - 1) {
1983 val
= LLVMBuildBitCast(
1984 ctx
->ac
.builder
, val
,
1985 LLVMGetElementType(LLVMTypeOf(address
)), "");
1986 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1988 for (unsigned chan
= 0; chan
< 4; chan
++) {
1989 if (!(writemask
& (1 << chan
)))
1992 LLVMBuildStructGEP(ctx
->ac
.builder
,
1994 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1996 src
= LLVMBuildBitCast(
1997 ctx
->ac
.builder
, src
,
1998 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
1999 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2009 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2012 case GLSL_SAMPLER_DIM_BUF
:
2014 case GLSL_SAMPLER_DIM_1D
:
2015 return array
? 2 : 1;
2016 case GLSL_SAMPLER_DIM_2D
:
2017 return array
? 3 : 2;
2018 case GLSL_SAMPLER_DIM_MS
:
2019 return array
? 4 : 3;
2020 case GLSL_SAMPLER_DIM_3D
:
2021 case GLSL_SAMPLER_DIM_CUBE
:
2023 case GLSL_SAMPLER_DIM_RECT
:
2024 case GLSL_SAMPLER_DIM_SUBPASS
:
2026 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2035 /* Adjust the sample index according to FMASK.
2037 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2038 * which is the identity mapping. Each nibble says which physical sample
2039 * should be fetched to get that sample.
2041 * For example, 0x11111100 means there are only 2 samples stored and
2042 * the second sample covers 3/4 of the pixel. When reading samples 0
2043 * and 1, return physical sample 0 (determined by the first two 0s
2044 * in FMASK), otherwise return physical sample 1.
2046 * The sample index should be adjusted as follows:
2047 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2049 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2050 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2051 LLVMValueRef coord_z
,
2052 LLVMValueRef sample_index
,
2053 LLVMValueRef fmask_desc_ptr
)
2055 struct ac_image_args args
= {0};
2058 args
.coords
[0] = coord_x
;
2059 args
.coords
[1] = coord_y
;
2061 args
.coords
[2] = coord_z
;
2063 args
.opcode
= ac_image_load
;
2064 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2065 args
.resource
= fmask_desc_ptr
;
2067 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2069 res
= ac_build_image_opcode(ctx
, &args
);
2071 res
= ac_to_integer(ctx
, res
);
2072 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2073 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2075 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2079 LLVMValueRef sample_index4
=
2080 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2081 LLVMValueRef shifted_fmask
=
2082 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2083 LLVMValueRef final_sample
=
2084 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2086 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2087 * resource descriptor is 0 (invalid),
2089 LLVMValueRef fmask_desc
=
2090 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2093 LLVMValueRef fmask_word1
=
2094 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2097 LLVMValueRef word1_is_nonzero
=
2098 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2099 fmask_word1
, ctx
->i32_0
, "");
2101 /* Replace the MSAA sample index. */
2103 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2104 final_sample
, sample_index
, "");
2105 return sample_index
;
2108 static void get_image_coords(struct ac_nir_context
*ctx
,
2109 const nir_intrinsic_instr
*instr
,
2110 struct ac_image_args
*args
)
2112 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2114 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2115 LLVMValueRef masks
[] = {
2116 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2117 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2119 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2122 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2123 bool is_array
= glsl_sampler_type_is_array(type
);
2124 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2125 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2126 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2127 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2128 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2129 count
= image_type_to_components_count(dim
, is_array
);
2132 LLVMValueRef fmask_load_address
[3];
2135 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2136 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2138 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2140 fmask_load_address
[2] = NULL
;
2142 for (chan
= 0; chan
< 2; ++chan
)
2143 fmask_load_address
[chan
] =
2144 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2145 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2146 ctx
->ac
.i32
, ""), "");
2147 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2149 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2150 fmask_load_address
[0],
2151 fmask_load_address
[1],
2152 fmask_load_address
[2],
2154 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
2156 if (count
== 1 && !gfx9_1d
) {
2157 if (instr
->src
[0].ssa
->num_components
)
2158 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2160 args
->coords
[0] = src0
;
2165 for (chan
= 0; chan
< count
; ++chan
) {
2166 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2169 for (chan
= 0; chan
< 2; ++chan
) {
2170 args
->coords
[chan
] = LLVMBuildAdd(
2171 ctx
->ac
.builder
, args
->coords
[chan
],
2173 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2174 ctx
->ac
.i32
, ""), "");
2176 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2177 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2183 args
->coords
[2] = args
->coords
[1];
2184 args
->coords
[1] = ctx
->ac
.i32_0
;
2186 args
->coords
[1] = ctx
->ac
.i32_0
;
2191 args
->coords
[count
] = sample_index
;
2197 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2198 const nir_intrinsic_instr
*instr
, bool write
)
2200 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, write
);
2201 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2202 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2203 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2204 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2206 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2207 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2208 elem_count
, stride
, "");
2210 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2211 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2216 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2217 const nir_intrinsic_instr
*instr
)
2220 const nir_variable
*var
= instr
->variables
[0]->var
;
2221 const struct glsl_type
*type
= var
->type
;
2223 if(instr
->variables
[0]->deref
.child
)
2224 type
= instr
->variables
[0]->deref
.child
->type
;
2226 type
= glsl_without_array(type
);
2228 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2229 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2230 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2231 unsigned num_channels
= util_last_bit(mask
);
2232 LLVMValueRef rsrc
, vindex
;
2234 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2235 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2238 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2239 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2240 ctx
->ac
.i32_0
, num_channels
,
2242 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2244 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2245 res
= ac_to_integer(&ctx
->ac
, res
);
2247 struct ac_image_args args
= {};
2248 args
.opcode
= ac_image_load
;
2249 get_image_coords(ctx
, instr
, &args
);
2250 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2251 AC_DESC_IMAGE
, NULL
, true, false);
2252 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2253 glsl_sampler_type_is_array(type
));
2255 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2256 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2257 args
.cache_policy
|= ac_glc
;
2259 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2261 return ac_to_integer(&ctx
->ac
, res
);
2264 static void visit_image_store(struct ac_nir_context
*ctx
,
2265 nir_intrinsic_instr
*instr
)
2267 LLVMValueRef params
[8];
2268 const nir_variable
*var
= instr
->variables
[0]->var
;
2269 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2270 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2271 LLVMValueRef glc
= ctx
->ac
.i1false
;
2272 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2274 glc
= ctx
->ac
.i1true
;
2276 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2277 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2279 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2281 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2282 ctx
->ac
.i32_0
, ""); /* vindex */
2283 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2284 params
[4] = glc
; /* glc */
2285 params
[5] = ctx
->ac
.i1false
; /* slc */
2286 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2289 struct ac_image_args args
= {};
2290 args
.opcode
= ac_image_store
;
2291 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2292 get_image_coords(ctx
, instr
, &args
);
2293 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2294 AC_DESC_IMAGE
, NULL
, true, false);
2295 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2296 glsl_sampler_type_is_array(type
));
2298 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2299 args
.cache_policy
|= ac_glc
;
2301 ac_build_image_opcode(&ctx
->ac
, &args
);
2306 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2307 const nir_intrinsic_instr
*instr
)
2309 LLVMValueRef params
[7];
2310 int param_count
= 0;
2311 const nir_variable
*var
= instr
->variables
[0]->var
;
2313 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
;
2314 const char *atomic_name
;
2315 char intrinsic_name
[41];
2316 enum ac_atomic_op atomic_subop
;
2317 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2318 MAYBE_UNUSED
int length
;
2320 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2322 switch (instr
->intrinsic
) {
2323 case nir_intrinsic_image_var_atomic_add
:
2324 atomic_name
= "add";
2325 atomic_subop
= ac_atomic_add
;
2327 case nir_intrinsic_image_var_atomic_min
:
2328 atomic_name
= is_unsigned
? "umin" : "smin";
2329 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2331 case nir_intrinsic_image_var_atomic_max
:
2332 atomic_name
= is_unsigned
? "umax" : "smax";
2333 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2335 case nir_intrinsic_image_var_atomic_and
:
2336 atomic_name
= "and";
2337 atomic_subop
= ac_atomic_and
;
2339 case nir_intrinsic_image_var_atomic_or
:
2341 atomic_subop
= ac_atomic_or
;
2343 case nir_intrinsic_image_var_atomic_xor
:
2344 atomic_name
= "xor";
2345 atomic_subop
= ac_atomic_xor
;
2347 case nir_intrinsic_image_var_atomic_exchange
:
2348 atomic_name
= "swap";
2349 atomic_subop
= ac_atomic_swap
;
2351 case nir_intrinsic_image_var_atomic_comp_swap
:
2352 atomic_name
= "cmpswap";
2353 atomic_subop
= 0; /* not used */
2360 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2361 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2363 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2364 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2365 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2366 ctx
->ac
.i32_0
, ""); /* vindex */
2367 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2368 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2370 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2371 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2373 assert(length
< sizeof(intrinsic_name
));
2374 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2375 params
, param_count
, 0);
2377 struct ac_image_args args
= {};
2378 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2379 args
.atomic
= atomic_subop
;
2380 args
.data
[0] = params
[0];
2382 args
.data
[1] = params
[1];
2383 get_image_coords(ctx
, instr
, &args
);
2384 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2385 AC_DESC_IMAGE
, NULL
, true, false);
2386 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2387 glsl_sampler_type_is_array(type
));
2389 return ac_build_image_opcode(&ctx
->ac
, &args
);
2393 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2394 const nir_intrinsic_instr
*instr
)
2396 const nir_variable
*var
= instr
->variables
[0]->var
;
2397 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2399 struct ac_image_args args
= { 0 };
2400 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2401 glsl_sampler_type_is_array(type
));
2403 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2404 AC_DESC_IMAGE
, NULL
, true, false);
2405 args
.opcode
= ac_image_get_resinfo
;
2406 args
.lod
= ctx
->ac
.i32_0
;
2407 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2409 return ac_build_image_opcode(&ctx
->ac
, &args
);
2412 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2413 const nir_intrinsic_instr
*instr
)
2416 const nir_variable
*var
= instr
->variables
[0]->var
;
2417 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2419 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2420 return get_buffer_size(ctx
,
2421 get_sampler_desc(ctx
, instr
->variables
[0],
2422 AC_DESC_BUFFER
, NULL
, true, false), true);
2424 struct ac_image_args args
= { 0 };
2426 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2427 glsl_sampler_type_is_array(type
));
2429 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2430 args
.opcode
= ac_image_get_resinfo
;
2431 args
.lod
= ctx
->ac
.i32_0
;
2432 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2434 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2436 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2438 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2439 glsl_sampler_type_is_array(type
)) {
2440 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2441 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2442 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2443 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2445 if (ctx
->ac
.chip_class
>= GFX9
&&
2446 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2447 glsl_sampler_type_is_array(type
)) {
2448 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2449 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2456 #define NOOP_WAITCNT 0xf7f
2457 #define LGKM_CNT 0x07f
2458 #define VM_CNT 0xf70
2460 static void emit_membar(struct ac_llvm_context
*ac
,
2461 const nir_intrinsic_instr
*instr
)
2463 unsigned waitcnt
= NOOP_WAITCNT
;
2465 switch (instr
->intrinsic
) {
2466 case nir_intrinsic_memory_barrier
:
2467 case nir_intrinsic_group_memory_barrier
:
2468 waitcnt
&= VM_CNT
& LGKM_CNT
;
2470 case nir_intrinsic_memory_barrier_atomic_counter
:
2471 case nir_intrinsic_memory_barrier_buffer
:
2472 case nir_intrinsic_memory_barrier_image
:
2475 case nir_intrinsic_memory_barrier_shared
:
2476 waitcnt
&= LGKM_CNT
;
2481 if (waitcnt
!= NOOP_WAITCNT
)
2482 ac_build_waitcnt(ac
, waitcnt
);
2485 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2487 /* SI only (thanks to a hw bug workaround):
2488 * The real barrier instruction isn’t needed, because an entire patch
2489 * always fits into a single wave.
2491 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2492 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2495 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2496 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2499 static void emit_discard(struct ac_nir_context
*ctx
,
2500 const nir_intrinsic_instr
*instr
)
2504 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2505 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2506 get_src(ctx
, instr
->src
[0]),
2509 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2510 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2513 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2517 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2519 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2520 "llvm.amdgcn.ps.live",
2521 ctx
->ac
.i1
, NULL
, 0,
2522 AC_FUNC_ATTR_READNONE
);
2523 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2524 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2528 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2530 LLVMValueRef result
;
2531 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2532 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2533 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2535 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2539 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2541 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2542 LLVMValueRef result
;
2543 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2544 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2545 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2547 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2552 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2554 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2555 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2556 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2558 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2563 visit_first_invocation(struct ac_nir_context
*ctx
)
2565 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2567 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2568 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2569 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2571 ctx
->ac
.i64
, args
, 2,
2572 AC_FUNC_ATTR_NOUNWIND
|
2573 AC_FUNC_ATTR_READNONE
);
2575 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2579 visit_load_shared(struct ac_nir_context
*ctx
,
2580 const nir_intrinsic_instr
*instr
)
2582 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2584 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2586 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2587 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2588 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2589 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2592 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2593 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2597 visit_store_shared(struct ac_nir_context
*ctx
,
2598 const nir_intrinsic_instr
*instr
)
2600 LLVMValueRef derived_ptr
, data
,index
;
2601 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2603 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2604 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2606 int writemask
= nir_intrinsic_write_mask(instr
);
2607 for (int chan
= 0; chan
< 4; chan
++) {
2608 if (!(writemask
& (1 << chan
))) {
2611 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2612 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2613 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2614 LLVMBuildStore(builder
, data
, derived_ptr
);
2618 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2619 const nir_intrinsic_instr
*instr
,
2620 LLVMValueRef ptr
, int src_idx
)
2622 LLVMValueRef result
;
2623 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2625 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2626 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2627 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2628 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2630 LLVMAtomicOrderingSequentiallyConsistent
,
2631 LLVMAtomicOrderingSequentiallyConsistent
,
2633 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2635 LLVMAtomicRMWBinOp op
;
2636 switch (instr
->intrinsic
) {
2637 case nir_intrinsic_var_atomic_add
:
2638 case nir_intrinsic_shared_atomic_add
:
2639 op
= LLVMAtomicRMWBinOpAdd
;
2641 case nir_intrinsic_var_atomic_umin
:
2642 case nir_intrinsic_shared_atomic_umin
:
2643 op
= LLVMAtomicRMWBinOpUMin
;
2645 case nir_intrinsic_var_atomic_umax
:
2646 case nir_intrinsic_shared_atomic_umax
:
2647 op
= LLVMAtomicRMWBinOpUMax
;
2649 case nir_intrinsic_var_atomic_imin
:
2650 case nir_intrinsic_shared_atomic_imin
:
2651 op
= LLVMAtomicRMWBinOpMin
;
2653 case nir_intrinsic_var_atomic_imax
:
2654 case nir_intrinsic_shared_atomic_imax
:
2655 op
= LLVMAtomicRMWBinOpMax
;
2657 case nir_intrinsic_var_atomic_and
:
2658 case nir_intrinsic_shared_atomic_and
:
2659 op
= LLVMAtomicRMWBinOpAnd
;
2661 case nir_intrinsic_var_atomic_or
:
2662 case nir_intrinsic_shared_atomic_or
:
2663 op
= LLVMAtomicRMWBinOpOr
;
2665 case nir_intrinsic_var_atomic_xor
:
2666 case nir_intrinsic_shared_atomic_xor
:
2667 op
= LLVMAtomicRMWBinOpXor
;
2669 case nir_intrinsic_var_atomic_exchange
:
2670 case nir_intrinsic_shared_atomic_exchange
:
2671 op
= LLVMAtomicRMWBinOpXchg
;
2677 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2678 LLVMAtomicOrderingSequentiallyConsistent
,
2684 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2686 LLVMValueRef values
[2];
2687 LLVMValueRef pos
[2];
2689 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2690 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2692 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2693 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2694 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2697 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2698 const nir_intrinsic_instr
*instr
)
2700 LLVMValueRef result
[4];
2701 LLVMValueRef interp_param
, attr_number
;
2704 LLVMValueRef src_c0
= NULL
;
2705 LLVMValueRef src_c1
= NULL
;
2706 LLVMValueRef src0
= NULL
;
2707 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2708 switch (instr
->intrinsic
) {
2709 case nir_intrinsic_interp_var_at_centroid
:
2710 location
= INTERP_CENTROID
;
2712 case nir_intrinsic_interp_var_at_sample
:
2713 case nir_intrinsic_interp_var_at_offset
:
2714 location
= INTERP_CENTER
;
2715 src0
= get_src(ctx
, instr
->src
[0]);
2721 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2722 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2723 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2724 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2725 LLVMValueRef sample_position
;
2726 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2728 /* fetch sample ID */
2729 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2731 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2732 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2733 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2734 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2736 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2737 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2739 if (location
== INTERP_CENTER
) {
2740 LLVMValueRef ij_out
[2];
2741 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2744 * take the I then J parameters, and the DDX/Y for it, and
2745 * calculate the IJ inputs for the interpolator.
2746 * temp1 = ddx * offset/sample.x + I;
2747 * interp_param.I = ddy * offset/sample.y + temp1;
2748 * temp1 = ddx * offset/sample.x + J;
2749 * interp_param.J = ddy * offset/sample.y + temp1;
2751 for (unsigned i
= 0; i
< 2; i
++) {
2752 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2753 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2754 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2755 ddxy_out
, ix_ll
, "");
2756 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2757 ddxy_out
, iy_ll
, "");
2758 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2759 interp_param
, ix_ll
, "");
2760 LLVMValueRef temp1
, temp2
;
2762 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2765 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2766 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2768 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2769 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2771 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2772 temp2
, ctx
->ac
.i32
, "");
2774 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2778 for (chan
= 0; chan
< 4; chan
++) {
2779 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2782 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2783 interp_param
, ctx
->ac
.v2f32
, "");
2784 LLVMValueRef i
= LLVMBuildExtractElement(
2785 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2786 LLVMValueRef j
= LLVMBuildExtractElement(
2787 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2789 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2790 llvm_chan
, attr_number
,
2791 ctx
->abi
->prim_mask
, i
, j
);
2793 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2794 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2795 llvm_chan
, attr_number
,
2796 ctx
->abi
->prim_mask
);
2799 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2800 instr
->variables
[0]->var
->data
.location_frac
);
2803 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2804 nir_intrinsic_instr
*instr
)
2806 LLVMValueRef result
= NULL
;
2808 switch (instr
->intrinsic
) {
2809 case nir_intrinsic_ballot
:
2810 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2812 case nir_intrinsic_read_invocation
:
2813 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2814 get_src(ctx
, instr
->src
[1]));
2816 case nir_intrinsic_read_first_invocation
:
2817 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2819 case nir_intrinsic_load_subgroup_invocation
:
2820 result
= ac_get_thread_id(&ctx
->ac
);
2822 case nir_intrinsic_load_work_group_id
: {
2823 LLVMValueRef values
[3];
2825 for (int i
= 0; i
< 3; i
++) {
2826 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2827 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2830 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2833 case nir_intrinsic_load_base_vertex
:
2834 case nir_intrinsic_load_first_vertex
:
2835 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2837 case nir_intrinsic_load_local_group_size
:
2838 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2840 case nir_intrinsic_load_vertex_id
:
2841 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2842 ctx
->abi
->base_vertex
, "");
2844 case nir_intrinsic_load_vertex_id_zero_base
: {
2845 result
= ctx
->abi
->vertex_id
;
2848 case nir_intrinsic_load_local_invocation_id
: {
2849 result
= ctx
->abi
->local_invocation_ids
;
2852 case nir_intrinsic_load_base_instance
:
2853 result
= ctx
->abi
->start_instance
;
2855 case nir_intrinsic_load_draw_id
:
2856 result
= ctx
->abi
->draw_id
;
2858 case nir_intrinsic_load_view_index
:
2859 result
= ctx
->abi
->view_index
;
2861 case nir_intrinsic_load_invocation_id
:
2862 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2863 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2865 result
= ctx
->abi
->gs_invocation_id
;
2867 case nir_intrinsic_load_primitive_id
:
2868 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2869 result
= ctx
->abi
->gs_prim_id
;
2870 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2871 result
= ctx
->abi
->tcs_patch_id
;
2872 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2873 result
= ctx
->abi
->tes_patch_id
;
2875 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2877 case nir_intrinsic_load_sample_id
:
2878 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2880 case nir_intrinsic_load_sample_pos
:
2881 result
= load_sample_pos(ctx
);
2883 case nir_intrinsic_load_sample_mask_in
:
2884 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2886 case nir_intrinsic_load_frag_coord
: {
2887 LLVMValueRef values
[4] = {
2888 ctx
->abi
->frag_pos
[0],
2889 ctx
->abi
->frag_pos
[1],
2890 ctx
->abi
->frag_pos
[2],
2891 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2893 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2896 case nir_intrinsic_load_front_face
:
2897 result
= ctx
->abi
->front_face
;
2899 case nir_intrinsic_load_helper_invocation
:
2900 result
= visit_load_helper_invocation(ctx
);
2902 case nir_intrinsic_load_instance_id
:
2903 result
= ctx
->abi
->instance_id
;
2905 case nir_intrinsic_load_num_work_groups
:
2906 result
= ctx
->abi
->num_work_groups
;
2908 case nir_intrinsic_load_local_invocation_index
:
2909 result
= visit_load_local_invocation_index(ctx
);
2911 case nir_intrinsic_load_subgroup_id
:
2912 result
= visit_load_subgroup_id(ctx
);
2914 case nir_intrinsic_load_num_subgroups
:
2915 result
= visit_load_num_subgroups(ctx
);
2917 case nir_intrinsic_first_invocation
:
2918 result
= visit_first_invocation(ctx
);
2920 case nir_intrinsic_load_push_constant
:
2921 result
= visit_load_push_constant(ctx
, instr
);
2923 case nir_intrinsic_vulkan_resource_index
: {
2924 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2925 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2926 unsigned binding
= nir_intrinsic_binding(instr
);
2928 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2932 case nir_intrinsic_vulkan_resource_reindex
:
2933 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2935 case nir_intrinsic_store_ssbo
:
2936 visit_store_ssbo(ctx
, instr
);
2938 case nir_intrinsic_load_ssbo
:
2939 result
= visit_load_buffer(ctx
, instr
);
2941 case nir_intrinsic_ssbo_atomic_add
:
2942 case nir_intrinsic_ssbo_atomic_imin
:
2943 case nir_intrinsic_ssbo_atomic_umin
:
2944 case nir_intrinsic_ssbo_atomic_imax
:
2945 case nir_intrinsic_ssbo_atomic_umax
:
2946 case nir_intrinsic_ssbo_atomic_and
:
2947 case nir_intrinsic_ssbo_atomic_or
:
2948 case nir_intrinsic_ssbo_atomic_xor
:
2949 case nir_intrinsic_ssbo_atomic_exchange
:
2950 case nir_intrinsic_ssbo_atomic_comp_swap
:
2951 result
= visit_atomic_ssbo(ctx
, instr
);
2953 case nir_intrinsic_load_ubo
:
2954 result
= visit_load_ubo_buffer(ctx
, instr
);
2956 case nir_intrinsic_get_buffer_size
:
2957 result
= visit_get_buffer_size(ctx
, instr
);
2959 case nir_intrinsic_load_var
:
2960 result
= visit_load_var(ctx
, instr
);
2962 case nir_intrinsic_store_var
:
2963 visit_store_var(ctx
, instr
);
2965 case nir_intrinsic_load_shared
:
2966 result
= visit_load_shared(ctx
, instr
);
2968 case nir_intrinsic_store_shared
:
2969 visit_store_shared(ctx
, instr
);
2971 case nir_intrinsic_image_var_samples
:
2972 result
= visit_image_samples(ctx
, instr
);
2974 case nir_intrinsic_image_var_load
:
2975 result
= visit_image_load(ctx
, instr
);
2977 case nir_intrinsic_image_var_store
:
2978 visit_image_store(ctx
, instr
);
2980 case nir_intrinsic_image_var_atomic_add
:
2981 case nir_intrinsic_image_var_atomic_min
:
2982 case nir_intrinsic_image_var_atomic_max
:
2983 case nir_intrinsic_image_var_atomic_and
:
2984 case nir_intrinsic_image_var_atomic_or
:
2985 case nir_intrinsic_image_var_atomic_xor
:
2986 case nir_intrinsic_image_var_atomic_exchange
:
2987 case nir_intrinsic_image_var_atomic_comp_swap
:
2988 result
= visit_image_atomic(ctx
, instr
);
2990 case nir_intrinsic_image_var_size
:
2991 result
= visit_image_size(ctx
, instr
);
2993 case nir_intrinsic_shader_clock
:
2994 result
= ac_build_shader_clock(&ctx
->ac
);
2996 case nir_intrinsic_discard
:
2997 case nir_intrinsic_discard_if
:
2998 emit_discard(ctx
, instr
);
3000 case nir_intrinsic_memory_barrier
:
3001 case nir_intrinsic_group_memory_barrier
:
3002 case nir_intrinsic_memory_barrier_atomic_counter
:
3003 case nir_intrinsic_memory_barrier_buffer
:
3004 case nir_intrinsic_memory_barrier_image
:
3005 case nir_intrinsic_memory_barrier_shared
:
3006 emit_membar(&ctx
->ac
, instr
);
3008 case nir_intrinsic_barrier
:
3009 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3011 case nir_intrinsic_shared_atomic_add
:
3012 case nir_intrinsic_shared_atomic_imin
:
3013 case nir_intrinsic_shared_atomic_umin
:
3014 case nir_intrinsic_shared_atomic_imax
:
3015 case nir_intrinsic_shared_atomic_umax
:
3016 case nir_intrinsic_shared_atomic_and
:
3017 case nir_intrinsic_shared_atomic_or
:
3018 case nir_intrinsic_shared_atomic_xor
:
3019 case nir_intrinsic_shared_atomic_exchange
:
3020 case nir_intrinsic_shared_atomic_comp_swap
: {
3021 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3022 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3025 case nir_intrinsic_var_atomic_add
:
3026 case nir_intrinsic_var_atomic_imin
:
3027 case nir_intrinsic_var_atomic_umin
:
3028 case nir_intrinsic_var_atomic_imax
:
3029 case nir_intrinsic_var_atomic_umax
:
3030 case nir_intrinsic_var_atomic_and
:
3031 case nir_intrinsic_var_atomic_or
:
3032 case nir_intrinsic_var_atomic_xor
:
3033 case nir_intrinsic_var_atomic_exchange
:
3034 case nir_intrinsic_var_atomic_comp_swap
: {
3035 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3036 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3039 case nir_intrinsic_interp_var_at_centroid
:
3040 case nir_intrinsic_interp_var_at_sample
:
3041 case nir_intrinsic_interp_var_at_offset
:
3042 result
= visit_interp(ctx
, instr
);
3044 case nir_intrinsic_emit_vertex
:
3045 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3047 case nir_intrinsic_end_primitive
:
3048 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3050 case nir_intrinsic_load_tess_coord
:
3051 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3053 case nir_intrinsic_load_tess_level_outer
:
3054 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3056 case nir_intrinsic_load_tess_level_inner
:
3057 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3059 case nir_intrinsic_load_patch_vertices_in
:
3060 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3062 case nir_intrinsic_vote_all
: {
3063 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3064 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3067 case nir_intrinsic_vote_any
: {
3068 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3069 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3072 case nir_intrinsic_shuffle
:
3073 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3074 get_src(ctx
, instr
->src
[1]));
3076 case nir_intrinsic_reduce
:
3077 result
= ac_build_reduce(&ctx
->ac
,
3078 get_src(ctx
, instr
->src
[0]),
3079 instr
->const_index
[0],
3080 instr
->const_index
[1]);
3082 case nir_intrinsic_inclusive_scan
:
3083 result
= ac_build_inclusive_scan(&ctx
->ac
,
3084 get_src(ctx
, instr
->src
[0]),
3085 instr
->const_index
[0]);
3087 case nir_intrinsic_exclusive_scan
:
3088 result
= ac_build_exclusive_scan(&ctx
->ac
,
3089 get_src(ctx
, instr
->src
[0]),
3090 instr
->const_index
[0]);
3092 case nir_intrinsic_quad_broadcast
: {
3093 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3094 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3095 lane
, lane
, lane
, lane
);
3098 case nir_intrinsic_quad_swap_horizontal
:
3099 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3101 case nir_intrinsic_quad_swap_vertical
:
3102 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3104 case nir_intrinsic_quad_swap_diagonal
:
3105 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3108 fprintf(stderr
, "Unknown intrinsic: ");
3109 nir_print_instr(&instr
->instr
, stderr
);
3110 fprintf(stderr
, "\n");
3114 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3118 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3119 const nir_deref_var
*deref
,
3120 enum ac_descriptor_type desc_type
,
3121 const nir_tex_instr
*tex_instr
,
3122 bool image
, bool write
)
3124 LLVMValueRef index
= NULL
;
3125 unsigned constant_index
= 0;
3126 unsigned descriptor_set
;
3127 unsigned base_index
;
3128 bool bindless
= false;
3131 assert(tex_instr
&& !image
);
3133 base_index
= tex_instr
->sampler_index
;
3135 const nir_deref
*tail
= &deref
->deref
;
3136 while (tail
->child
) {
3137 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3138 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3143 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3145 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3146 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3148 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3149 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3154 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3157 constant_index
+= child
->base_offset
* array_size
;
3159 tail
= &child
->deref
;
3161 descriptor_set
= deref
->var
->data
.descriptor_set
;
3163 if (deref
->var
->data
.bindless
) {
3164 bindless
= deref
->var
->data
.bindless
;
3165 base_index
= deref
->var
->data
.driver_location
;
3167 base_index
= deref
->var
->data
.binding
;
3171 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3174 constant_index
, index
,
3175 desc_type
, image
, write
, bindless
);
3178 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3181 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3182 * filtering manually. The driver sets img7 to a mask clearing
3183 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3184 * s_and_b32 samp0, samp0, img7
3187 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3189 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3190 LLVMValueRef res
, LLVMValueRef samp
)
3192 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3193 LLVMValueRef img7
, samp0
;
3195 if (ctx
->ac
.chip_class
>= VI
)
3198 img7
= LLVMBuildExtractElement(builder
, res
,
3199 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3200 samp0
= LLVMBuildExtractElement(builder
, samp
,
3201 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3202 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3203 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3204 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3207 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3208 nir_tex_instr
*instr
,
3209 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3210 LLVMValueRef
*fmask_ptr
)
3212 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3213 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
3215 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
3218 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
3220 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
3221 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3222 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3224 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3225 instr
->op
== nir_texop_samples_identical
))
3226 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
3229 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3232 coord
= ac_to_float(ctx
, coord
);
3233 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3234 coord
= ac_to_integer(ctx
, coord
);
3238 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3240 LLVMValueRef result
= NULL
;
3241 struct ac_image_args args
= { 0 };
3242 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3243 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3244 unsigned offset_src
= 0;
3246 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3248 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3249 switch (instr
->src
[i
].src_type
) {
3250 case nir_tex_src_coord
: {
3251 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3252 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3253 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3256 case nir_tex_src_projector
:
3258 case nir_tex_src_comparator
:
3259 if (instr
->is_shadow
)
3260 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3262 case nir_tex_src_offset
:
3263 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3266 case nir_tex_src_bias
:
3267 if (instr
->op
== nir_texop_txb
)
3268 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3270 case nir_tex_src_lod
: {
3271 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3273 if (val
&& val
->i32
[0] == 0)
3274 args
.level_zero
= true;
3276 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3279 case nir_tex_src_ms_index
:
3280 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3282 case nir_tex_src_ms_mcs
:
3284 case nir_tex_src_ddx
:
3285 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3287 case nir_tex_src_ddy
:
3288 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3290 case nir_tex_src_texture_offset
:
3291 case nir_tex_src_sampler_offset
:
3292 case nir_tex_src_plane
:
3298 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3299 result
= get_buffer_size(ctx
, args
.resource
, true);
3303 if (instr
->op
== nir_texop_texture_samples
) {
3304 LLVMValueRef res
, samples
, is_msaa
;
3305 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3306 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3307 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3308 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3309 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3310 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3311 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3312 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3313 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3315 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3316 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3317 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3318 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3319 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3321 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3327 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3328 LLVMValueRef offset
[3], pack
;
3329 for (unsigned chan
= 0; chan
< 3; ++chan
)
3330 offset
[chan
] = ctx
->ac
.i32_0
;
3332 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3333 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3334 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3335 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3336 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3338 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3339 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3341 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3342 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3346 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3347 * so the depth comparison value isn't clamped for Z16 and
3348 * Z24 anymore. Do it manually here.
3350 * It's unnecessary if the original texture format was
3351 * Z32_FLOAT, but we don't know that here.
3353 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3354 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3356 /* pack derivatives */
3358 int num_src_deriv_channels
, num_dest_deriv_channels
;
3359 switch (instr
->sampler_dim
) {
3360 case GLSL_SAMPLER_DIM_3D
:
3361 case GLSL_SAMPLER_DIM_CUBE
:
3362 num_src_deriv_channels
= 3;
3363 num_dest_deriv_channels
= 3;
3365 case GLSL_SAMPLER_DIM_2D
:
3367 num_src_deriv_channels
= 2;
3368 num_dest_deriv_channels
= 2;
3370 case GLSL_SAMPLER_DIM_1D
:
3371 num_src_deriv_channels
= 1;
3372 if (ctx
->ac
.chip_class
>= GFX9
) {
3373 num_dest_deriv_channels
= 2;
3375 num_dest_deriv_channels
= 1;
3380 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3381 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3382 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3383 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3384 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3386 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3387 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3388 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3392 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3393 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3394 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3395 if (instr
->coord_components
== 3)
3396 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3397 ac_prepare_cube_coords(&ctx
->ac
,
3398 instr
->op
== nir_texop_txd
, instr
->is_array
,
3399 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3402 /* Texture coordinates fixups */
3403 if (instr
->coord_components
> 1 &&
3404 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3406 instr
->op
!= nir_texop_txf
) {
3407 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3410 if (instr
->coord_components
> 2 &&
3411 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3412 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3413 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3414 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3416 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3417 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3420 if (ctx
->ac
.chip_class
>= GFX9
&&
3421 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3422 instr
->op
!= nir_texop_lod
) {
3423 LLVMValueRef filler
;
3424 if (instr
->op
== nir_texop_txf
)
3425 filler
= ctx
->ac
.i32_0
;
3427 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3429 if (instr
->is_array
)
3430 args
.coords
[2] = args
.coords
[1];
3431 args
.coords
[1] = filler
;
3434 /* Pack sample index */
3435 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3436 args
.coords
[instr
->coord_components
] = sample_index
;
3438 if (instr
->op
== nir_texop_samples_identical
) {
3439 struct ac_image_args txf_args
= { 0 };
3440 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3442 txf_args
.dmask
= 0xf;
3443 txf_args
.resource
= fmask_ptr
;
3444 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3445 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3447 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3448 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3452 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3453 instr
->op
!= nir_texop_txs
) {
3454 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3455 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3456 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3457 instr
->is_array
? args
.coords
[2] : NULL
,
3458 args
.coords
[sample_chan
], fmask_ptr
);
3461 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3462 nir_const_value
*const_offset
=
3463 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3464 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3465 assert(const_offset
);
3466 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3467 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3468 args
.coords
[i
] = LLVMBuildAdd(
3469 ctx
->ac
.builder
, args
.coords
[i
],
3470 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3475 /* TODO TG4 support */
3477 if (instr
->op
== nir_texop_tg4
) {
3478 if (instr
->is_shadow
)
3481 args
.dmask
= 1 << instr
->component
;
3484 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3485 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3486 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3488 if (instr
->op
== nir_texop_query_levels
)
3489 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3490 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3491 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3492 instr
->op
!= nir_texop_tg4
)
3493 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3494 else if (instr
->op
== nir_texop_txs
&&
3495 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3497 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3498 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3499 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3500 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3501 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3502 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3503 instr
->op
== nir_texop_txs
&&
3504 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3506 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3507 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3508 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3510 } else if (instr
->dest
.ssa
.num_components
!= 4)
3511 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3515 assert(instr
->dest
.is_ssa
);
3516 result
= ac_to_integer(&ctx
->ac
, result
);
3517 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3522 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3524 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3525 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3527 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3528 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3531 static void visit_post_phi(struct ac_nir_context
*ctx
,
3532 nir_phi_instr
*instr
,
3533 LLVMValueRef llvm_phi
)
3535 nir_foreach_phi_src(src
, instr
) {
3536 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3537 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3539 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3543 static void phi_post_pass(struct ac_nir_context
*ctx
)
3545 struct hash_entry
*entry
;
3546 hash_table_foreach(ctx
->phis
, entry
) {
3547 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3548 (LLVMValueRef
)entry
->data
);
3553 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3554 const nir_ssa_undef_instr
*instr
)
3556 unsigned num_components
= instr
->def
.num_components
;
3557 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3560 if (num_components
== 1)
3561 undef
= LLVMGetUndef(type
);
3563 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3565 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3568 static void visit_jump(struct ac_llvm_context
*ctx
,
3569 const nir_jump_instr
*instr
)
3571 switch (instr
->type
) {
3572 case nir_jump_break
:
3573 ac_build_break(ctx
);
3575 case nir_jump_continue
:
3576 ac_build_continue(ctx
);
3579 fprintf(stderr
, "Unknown NIR jump instr: ");
3580 nir_print_instr(&instr
->instr
, stderr
);
3581 fprintf(stderr
, "\n");
3586 static void visit_cf_list(struct ac_nir_context
*ctx
,
3587 struct exec_list
*list
);
3589 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3591 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3592 nir_foreach_instr(instr
, block
)
3594 switch (instr
->type
) {
3595 case nir_instr_type_alu
:
3596 visit_alu(ctx
, nir_instr_as_alu(instr
));
3598 case nir_instr_type_load_const
:
3599 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3601 case nir_instr_type_intrinsic
:
3602 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3604 case nir_instr_type_tex
:
3605 visit_tex(ctx
, nir_instr_as_tex(instr
));
3607 case nir_instr_type_phi
:
3608 visit_phi(ctx
, nir_instr_as_phi(instr
));
3610 case nir_instr_type_ssa_undef
:
3611 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3613 case nir_instr_type_jump
:
3614 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3617 fprintf(stderr
, "Unknown NIR instr type: ");
3618 nir_print_instr(instr
, stderr
);
3619 fprintf(stderr
, "\n");
3624 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3627 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3629 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3631 nir_block
*then_block
=
3632 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3634 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3636 visit_cf_list(ctx
, &if_stmt
->then_list
);
3638 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3639 nir_block
*else_block
=
3640 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3642 ac_build_else(&ctx
->ac
, else_block
->index
);
3643 visit_cf_list(ctx
, &if_stmt
->else_list
);
3646 ac_build_endif(&ctx
->ac
, then_block
->index
);
3649 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3651 nir_block
*first_loop_block
=
3652 (nir_block
*) exec_list_get_head(&loop
->body
);
3654 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3656 visit_cf_list(ctx
, &loop
->body
);
3658 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3661 static void visit_cf_list(struct ac_nir_context
*ctx
,
3662 struct exec_list
*list
)
3664 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3666 switch (node
->type
) {
3667 case nir_cf_node_block
:
3668 visit_block(ctx
, nir_cf_node_as_block(node
));
3671 case nir_cf_node_if
:
3672 visit_if(ctx
, nir_cf_node_as_if(node
));
3675 case nir_cf_node_loop
:
3676 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3686 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3687 struct ac_shader_abi
*abi
,
3688 struct nir_shader
*nir
,
3689 struct nir_variable
*variable
,
3690 gl_shader_stage stage
)
3692 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3693 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3695 /* tess ctrl has it's own load/store paths for outputs */
3696 if (stage
== MESA_SHADER_TESS_CTRL
)
3699 if (stage
== MESA_SHADER_VERTEX
||
3700 stage
== MESA_SHADER_TESS_EVAL
||
3701 stage
== MESA_SHADER_GEOMETRY
) {
3702 int idx
= variable
->data
.location
+ variable
->data
.index
;
3703 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3704 int length
= nir
->info
.clip_distance_array_size
+
3705 nir
->info
.cull_distance_array_size
;
3714 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3715 for (unsigned chan
= 0; chan
< 4; chan
++) {
3716 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3717 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3723 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3724 enum glsl_base_type type
)
3728 case GLSL_TYPE_UINT
:
3729 case GLSL_TYPE_BOOL
:
3730 case GLSL_TYPE_SUBROUTINE
:
3732 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3734 case GLSL_TYPE_INT64
:
3735 case GLSL_TYPE_UINT64
:
3737 case GLSL_TYPE_DOUBLE
:
3740 unreachable("unknown GLSL type");
3745 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3746 const struct glsl_type
*type
)
3748 if (glsl_type_is_scalar(type
)) {
3749 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3752 if (glsl_type_is_vector(type
)) {
3753 return LLVMVectorType(
3754 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3755 glsl_get_vector_elements(type
));
3758 if (glsl_type_is_matrix(type
)) {
3759 return LLVMArrayType(
3760 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3761 glsl_get_matrix_columns(type
));
3764 if (glsl_type_is_array(type
)) {
3765 return LLVMArrayType(
3766 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3767 glsl_get_length(type
));
3770 assert(glsl_type_is_struct(type
));
3772 LLVMTypeRef member_types
[glsl_get_length(type
)];
3774 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3776 glsl_to_llvm_type(ac
,
3777 glsl_get_struct_field(type
, i
));
3780 return LLVMStructTypeInContext(ac
->context
, member_types
,
3781 glsl_get_length(type
), false);
3785 setup_locals(struct ac_nir_context
*ctx
,
3786 struct nir_function
*func
)
3789 ctx
->num_locals
= 0;
3790 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3791 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3792 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3793 variable
->data
.location_frac
= 0;
3794 ctx
->num_locals
+= attrib_count
;
3796 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3800 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3801 for (j
= 0; j
< 4; j
++) {
3802 ctx
->locals
[i
* 4 + j
] =
3803 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3809 setup_shared(struct ac_nir_context
*ctx
,
3810 struct nir_shader
*nir
)
3812 nir_foreach_variable(variable
, &nir
->shared
) {
3813 LLVMValueRef shared
=
3814 LLVMAddGlobalInAddressSpace(
3815 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3816 variable
->name
? variable
->name
: "",
3817 AC_LOCAL_ADDR_SPACE
);
3818 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3822 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3823 struct nir_shader
*nir
)
3825 struct ac_nir_context ctx
= {};
3826 struct nir_function
*func
;
3831 ctx
.stage
= nir
->info
.stage
;
3833 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3835 nir_foreach_variable(variable
, &nir
->outputs
)
3836 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3839 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3840 _mesa_key_pointer_equal
);
3841 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3842 _mesa_key_pointer_equal
);
3843 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3844 _mesa_key_pointer_equal
);
3846 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3848 nir_index_ssa_defs(func
->impl
);
3849 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3851 setup_locals(&ctx
, func
);
3853 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3854 setup_shared(&ctx
, nir
);
3856 visit_cf_list(&ctx
, &func
->impl
->body
);
3857 phi_post_pass(&ctx
);
3859 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3860 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3865 ralloc_free(ctx
.defs
);
3866 ralloc_free(ctx
.phis
);
3867 ralloc_free(ctx
.vars
);
3871 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3873 /* While it would be nice not to have this flag, we are constrained
3874 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3877 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3879 /* TODO: Indirect indexing of GS inputs is unimplemented.
3881 * TCS and TES load inputs directly from LDS or offchip memory, so
3882 * indirect indexing is trivial.
3884 nir_variable_mode indirect_mask
= 0;
3885 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3886 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3887 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3888 !llvm_has_working_vgpr_indexing
)) {
3889 indirect_mask
|= nir_var_shader_in
;
3891 if (!llvm_has_working_vgpr_indexing
&&
3892 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3893 indirect_mask
|= nir_var_shader_out
;
3895 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3896 * smart enough to handle indirects without causing excess spilling
3897 * causing the gpu to hang.
3899 * See the following thread for more details of the problem:
3900 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3902 indirect_mask
|= nir_var_local
;
3904 nir_lower_indirect_derefs(nir
, indirect_mask
);