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 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1313 return ac_build_image_opcode(&ctx
->ac
, args
);
1316 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1317 nir_intrinsic_instr
*instr
)
1319 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1320 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1322 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1323 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1327 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1328 nir_intrinsic_instr
*instr
)
1330 LLVMValueRef ptr
, addr
;
1332 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1333 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1334 get_src(ctx
, instr
->src
[0]), "");
1336 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1337 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1339 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1342 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1343 const nir_intrinsic_instr
*instr
)
1345 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1347 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1350 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1352 uint32_t new_mask
= 0;
1353 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1354 if (mask
& (1u << i
))
1355 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1359 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1360 unsigned start
, unsigned count
)
1362 LLVMTypeRef type
= LLVMTypeOf(src
);
1364 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1370 unsigned src_elements
= LLVMGetVectorSize(type
);
1371 assert(start
< src_elements
);
1372 assert(start
+ count
<= src_elements
);
1374 if (start
== 0 && count
== src_elements
)
1378 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1381 LLVMValueRef indices
[8];
1382 for (unsigned i
= 0; i
< count
; ++i
)
1383 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1385 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1386 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1389 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1390 nir_intrinsic_instr
*instr
)
1392 const char *store_name
;
1393 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1394 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1395 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1396 int components_32bit
= elem_size_mult
* instr
->num_components
;
1397 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1398 LLVMValueRef base_data
, base_offset
;
1399 LLVMValueRef params
[6];
1401 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1402 get_src(ctx
, instr
->src
[1]), true);
1403 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1404 params
[4] = ctx
->ac
.i1false
; /* glc */
1405 params
[5] = ctx
->ac
.i1false
; /* slc */
1407 if (components_32bit
> 1)
1408 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1410 writemask
= widen_mask(writemask
, elem_size_mult
);
1412 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1413 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1414 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1416 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1420 LLVMValueRef offset
;
1422 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1424 /* Due to an LLVM limitation, split 3-element writes
1425 * into a 2-element and a 1-element write. */
1427 writemask
|= 1 << (start
+ 2);
1432 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1437 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1438 } else if (count
== 2) {
1439 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1443 store_name
= "llvm.amdgcn.buffer.store.f32";
1445 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1447 offset
= base_offset
;
1449 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1453 ac_build_intrinsic(&ctx
->ac
, store_name
,
1454 ctx
->ac
.voidt
, params
, 6, 0);
1458 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1459 const nir_intrinsic_instr
*instr
)
1462 LLVMValueRef params
[6];
1465 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1466 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1468 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1469 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1470 get_src(ctx
, instr
->src
[0]),
1472 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1473 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1474 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1476 switch (instr
->intrinsic
) {
1477 case nir_intrinsic_ssbo_atomic_add
:
1478 name
= "llvm.amdgcn.buffer.atomic.add";
1480 case nir_intrinsic_ssbo_atomic_imin
:
1481 name
= "llvm.amdgcn.buffer.atomic.smin";
1483 case nir_intrinsic_ssbo_atomic_umin
:
1484 name
= "llvm.amdgcn.buffer.atomic.umin";
1486 case nir_intrinsic_ssbo_atomic_imax
:
1487 name
= "llvm.amdgcn.buffer.atomic.smax";
1489 case nir_intrinsic_ssbo_atomic_umax
:
1490 name
= "llvm.amdgcn.buffer.atomic.umax";
1492 case nir_intrinsic_ssbo_atomic_and
:
1493 name
= "llvm.amdgcn.buffer.atomic.and";
1495 case nir_intrinsic_ssbo_atomic_or
:
1496 name
= "llvm.amdgcn.buffer.atomic.or";
1498 case nir_intrinsic_ssbo_atomic_xor
:
1499 name
= "llvm.amdgcn.buffer.atomic.xor";
1501 case nir_intrinsic_ssbo_atomic_exchange
:
1502 name
= "llvm.amdgcn.buffer.atomic.swap";
1504 case nir_intrinsic_ssbo_atomic_comp_swap
:
1505 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1511 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1514 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1515 const nir_intrinsic_instr
*instr
)
1517 LLVMValueRef results
[2];
1518 int load_components
;
1519 int num_components
= instr
->num_components
;
1520 if (instr
->dest
.ssa
.bit_size
== 64)
1521 num_components
*= 2;
1523 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1524 load_components
= MIN2(num_components
- i
, 4);
1525 const char *load_name
;
1526 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1527 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1528 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1530 if (load_components
== 3)
1531 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1532 else if (load_components
> 1)
1533 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1535 if (load_components
>= 3)
1536 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1537 else if (load_components
== 2)
1538 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1539 else if (load_components
== 1)
1540 load_name
= "llvm.amdgcn.buffer.load.f32";
1542 unreachable("unhandled number of components");
1544 LLVMValueRef params
[] = {
1545 ctx
->abi
->load_ssbo(ctx
->abi
,
1546 get_src(ctx
, instr
->src
[0]),
1554 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1558 LLVMValueRef ret
= results
[0];
1559 if (num_components
> 4 || num_components
== 3) {
1560 LLVMValueRef masks
[] = {
1561 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1562 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1563 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1564 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1567 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1568 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1569 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1572 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1573 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1576 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1577 const nir_intrinsic_instr
*instr
)
1580 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1581 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1582 int num_components
= instr
->num_components
;
1584 if (ctx
->abi
->load_ubo
)
1585 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1587 if (instr
->dest
.ssa
.bit_size
== 64)
1588 num_components
*= 2;
1590 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1591 NULL
, 0, false, false, true, true);
1592 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1593 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1594 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1598 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1599 bool vs_in
, unsigned *vertex_index_out
,
1600 LLVMValueRef
*vertex_index_ref
,
1601 unsigned *const_out
, LLVMValueRef
*indir_out
)
1603 unsigned const_offset
= 0;
1604 nir_deref
*tail
= &deref
->deref
;
1605 LLVMValueRef offset
= NULL
;
1607 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1609 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1610 if (vertex_index_out
)
1611 *vertex_index_out
= deref_array
->base_offset
;
1613 if (vertex_index_ref
) {
1614 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1615 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1616 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1618 *vertex_index_ref
= vtx
;
1622 if (deref
->var
->data
.compact
) {
1623 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1624 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1625 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1626 /* We always lower indirect dereferences for "compact" array vars. */
1627 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1629 const_offset
= deref_array
->base_offset
;
1633 while (tail
->child
!= NULL
) {
1634 const struct glsl_type
*parent_type
= tail
->type
;
1637 if (tail
->deref_type
== nir_deref_type_array
) {
1638 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1639 LLVMValueRef index
, stride
, local_offset
;
1640 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1642 const_offset
+= size
* deref_array
->base_offset
;
1643 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1646 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1647 index
= get_src(ctx
, deref_array
->indirect
);
1648 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1649 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1652 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1654 offset
= local_offset
;
1655 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1656 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1658 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1659 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1660 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1663 unreachable("unsupported deref type");
1667 if (const_offset
&& offset
)
1668 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1669 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1672 *const_out
= const_offset
;
1673 *indir_out
= offset
;
1677 build_gep_for_deref(struct ac_nir_context
*ctx
,
1678 nir_deref_var
*deref
)
1680 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1681 assert(entry
->data
);
1682 LLVMValueRef val
= entry
->data
;
1683 nir_deref
*tail
= deref
->deref
.child
;
1684 while (tail
!= NULL
) {
1685 LLVMValueRef offset
;
1686 switch (tail
->deref_type
) {
1687 case nir_deref_type_array
: {
1688 nir_deref_array
*array
= nir_deref_as_array(tail
);
1689 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1690 if (array
->deref_array_type
==
1691 nir_deref_array_type_indirect
) {
1692 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1699 case nir_deref_type_struct
: {
1700 nir_deref_struct
*deref_struct
=
1701 nir_deref_as_struct(tail
);
1702 offset
= LLVMConstInt(ctx
->ac
.i32
,
1703 deref_struct
->index
, 0);
1707 unreachable("bad deref type");
1709 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1715 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1716 nir_intrinsic_instr
*instr
,
1719 LLVMValueRef result
;
1720 LLVMValueRef vertex_index
= NULL
;
1721 LLVMValueRef indir_index
= NULL
;
1722 unsigned const_index
= 0;
1723 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1724 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1725 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1726 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1728 get_deref_offset(ctx
, instr
->variables
[0],
1729 false, NULL
, is_patch
? NULL
: &vertex_index
,
1730 &const_index
, &indir_index
);
1732 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1734 LLVMTypeRef src_component_type
;
1735 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1736 src_component_type
= LLVMGetElementType(dest_type
);
1738 src_component_type
= dest_type
;
1740 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1741 vertex_index
, indir_index
,
1742 const_index
, location
, driver_location
,
1743 instr
->variables
[0]->var
->data
.location_frac
,
1744 instr
->num_components
,
1745 is_patch
, is_compact
, load_inputs
);
1746 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1749 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1750 nir_intrinsic_instr
*instr
)
1752 LLVMValueRef values
[8];
1753 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1754 int ve
= instr
->dest
.ssa
.num_components
;
1755 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1756 LLVMValueRef indir_index
;
1758 unsigned const_index
;
1759 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1760 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1761 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1762 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1763 &const_index
, &indir_index
);
1765 if (instr
->dest
.ssa
.bit_size
== 64)
1768 switch (instr
->variables
[0]->var
->data
.mode
) {
1769 case nir_var_shader_in
:
1770 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1771 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1772 return load_tess_varyings(ctx
, instr
, true);
1775 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1776 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1777 LLVMValueRef indir_index
;
1778 unsigned const_index
, vertex_index
;
1779 get_deref_offset(ctx
, instr
->variables
[0],
1780 false, &vertex_index
, NULL
,
1781 &const_index
, &indir_index
);
1783 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1784 instr
->variables
[0]->var
->data
.driver_location
,
1785 instr
->variables
[0]->var
->data
.location_frac
,
1786 instr
->num_components
, vertex_index
, const_index
, type
);
1789 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1791 unsigned count
= glsl_count_attribute_slots(
1792 instr
->variables
[0]->var
->type
,
1793 ctx
->stage
== MESA_SHADER_VERTEX
);
1795 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1796 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1797 stride
, false, true);
1799 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1803 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1807 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1809 unsigned count
= glsl_count_attribute_slots(
1810 instr
->variables
[0]->var
->type
, false);
1812 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1813 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1814 stride
, true, true);
1816 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1820 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1824 case nir_var_shared
: {
1825 LLVMValueRef address
= build_gep_for_deref(ctx
,
1826 instr
->variables
[0]);
1827 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1828 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1829 get_def_type(ctx
, &instr
->dest
.ssa
),
1832 case nir_var_shader_out
:
1833 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1834 return load_tess_varyings(ctx
, instr
, false);
1837 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1839 unsigned count
= glsl_count_attribute_slots(
1840 instr
->variables
[0]->var
->type
, false);
1842 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1843 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1844 stride
, true, true);
1846 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1850 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1851 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1857 unreachable("unhandle variable mode");
1859 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1860 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1864 visit_store_var(struct ac_nir_context
*ctx
,
1865 nir_intrinsic_instr
*instr
)
1867 LLVMValueRef temp_ptr
, value
;
1868 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1869 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1870 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1871 int writemask
= instr
->const_index
[0];
1872 LLVMValueRef indir_index
;
1873 unsigned const_index
;
1874 get_deref_offset(ctx
, instr
->variables
[0], false,
1875 NULL
, NULL
, &const_index
, &indir_index
);
1877 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1879 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1880 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1883 writemask
= widen_mask(writemask
, 2);
1886 writemask
= writemask
<< comp
;
1888 switch (instr
->variables
[0]->var
->data
.mode
) {
1889 case nir_var_shader_out
:
1891 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1892 LLVMValueRef vertex_index
= NULL
;
1893 LLVMValueRef indir_index
= NULL
;
1894 unsigned const_index
= 0;
1895 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1897 get_deref_offset(ctx
, instr
->variables
[0],
1898 false, NULL
, is_patch
? NULL
: &vertex_index
,
1899 &const_index
, &indir_index
);
1901 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1902 vertex_index
, indir_index
,
1903 const_index
, src
, writemask
);
1907 for (unsigned chan
= 0; chan
< 8; chan
++) {
1909 if (!(writemask
& (1 << chan
)))
1912 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1914 if (instr
->variables
[0]->var
->data
.compact
)
1917 unsigned count
= glsl_count_attribute_slots(
1918 instr
->variables
[0]->var
->type
, false);
1920 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1921 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1922 stride
, true, true);
1924 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1925 value
, indir_index
, "");
1926 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1927 count
, stride
, tmp_vec
);
1930 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1932 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1937 for (unsigned chan
= 0; chan
< 8; chan
++) {
1938 if (!(writemask
& (1 << chan
)))
1941 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1943 unsigned count
= glsl_count_attribute_slots(
1944 instr
->variables
[0]->var
->type
, false);
1946 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1947 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1950 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1951 value
, indir_index
, "");
1952 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1955 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1957 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1961 case nir_var_shared
: {
1962 int writemask
= instr
->const_index
[0];
1963 LLVMValueRef address
= build_gep_for_deref(ctx
,
1964 instr
->variables
[0]);
1965 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1966 unsigned components
=
1967 glsl_get_vector_elements(
1968 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1969 if (writemask
== (1 << components
) - 1) {
1970 val
= LLVMBuildBitCast(
1971 ctx
->ac
.builder
, val
,
1972 LLVMGetElementType(LLVMTypeOf(address
)), "");
1973 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1975 for (unsigned chan
= 0; chan
< 4; chan
++) {
1976 if (!(writemask
& (1 << chan
)))
1979 LLVMBuildStructGEP(ctx
->ac
.builder
,
1981 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1983 src
= LLVMBuildBitCast(
1984 ctx
->ac
.builder
, src
,
1985 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
1986 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
1996 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
1999 case GLSL_SAMPLER_DIM_BUF
:
2001 case GLSL_SAMPLER_DIM_1D
:
2002 return array
? 2 : 1;
2003 case GLSL_SAMPLER_DIM_2D
:
2004 return array
? 3 : 2;
2005 case GLSL_SAMPLER_DIM_MS
:
2006 return array
? 4 : 3;
2007 case GLSL_SAMPLER_DIM_3D
:
2008 case GLSL_SAMPLER_DIM_CUBE
:
2010 case GLSL_SAMPLER_DIM_RECT
:
2011 case GLSL_SAMPLER_DIM_SUBPASS
:
2013 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2022 /* Adjust the sample index according to FMASK.
2024 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2025 * which is the identity mapping. Each nibble says which physical sample
2026 * should be fetched to get that sample.
2028 * For example, 0x11111100 means there are only 2 samples stored and
2029 * the second sample covers 3/4 of the pixel. When reading samples 0
2030 * and 1, return physical sample 0 (determined by the first two 0s
2031 * in FMASK), otherwise return physical sample 1.
2033 * The sample index should be adjusted as follows:
2034 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2036 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2037 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2038 LLVMValueRef coord_z
,
2039 LLVMValueRef sample_index
,
2040 LLVMValueRef fmask_desc_ptr
)
2042 struct ac_image_args args
= {0};
2045 args
.coords
[0] = coord_x
;
2046 args
.coords
[1] = coord_y
;
2048 args
.coords
[2] = coord_z
;
2050 args
.opcode
= ac_image_load
;
2051 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2052 args
.resource
= fmask_desc_ptr
;
2054 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2056 res
= ac_build_image_opcode(ctx
, &args
);
2058 res
= ac_to_integer(ctx
, res
);
2059 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2060 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2062 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2066 LLVMValueRef sample_index4
=
2067 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2068 LLVMValueRef shifted_fmask
=
2069 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2070 LLVMValueRef final_sample
=
2071 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2073 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2074 * resource descriptor is 0 (invalid),
2076 LLVMValueRef fmask_desc
=
2077 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2080 LLVMValueRef fmask_word1
=
2081 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2084 LLVMValueRef word1_is_nonzero
=
2085 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2086 fmask_word1
, ctx
->i32_0
, "");
2088 /* Replace the MSAA sample index. */
2090 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2091 final_sample
, sample_index
, "");
2092 return sample_index
;
2095 static void get_image_coords(struct ac_nir_context
*ctx
,
2096 const nir_intrinsic_instr
*instr
,
2097 struct ac_image_args
*args
)
2099 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2101 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2102 LLVMValueRef masks
[] = {
2103 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2104 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2106 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2109 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2110 bool is_array
= glsl_sampler_type_is_array(type
);
2111 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2112 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2113 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2114 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2115 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2116 count
= image_type_to_components_count(dim
, is_array
);
2119 LLVMValueRef fmask_load_address
[3];
2122 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2123 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2125 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2127 fmask_load_address
[2] = NULL
;
2129 for (chan
= 0; chan
< 2; ++chan
)
2130 fmask_load_address
[chan
] =
2131 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2132 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2133 ctx
->ac
.i32
, ""), "");
2134 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2136 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2137 fmask_load_address
[0],
2138 fmask_load_address
[1],
2139 fmask_load_address
[2],
2141 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
2143 if (count
== 1 && !gfx9_1d
) {
2144 if (instr
->src
[0].ssa
->num_components
)
2145 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2147 args
->coords
[0] = src0
;
2152 for (chan
= 0; chan
< count
; ++chan
) {
2153 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2156 for (chan
= 0; chan
< 2; ++chan
) {
2157 args
->coords
[chan
] = LLVMBuildAdd(
2158 ctx
->ac
.builder
, args
->coords
[chan
],
2160 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2161 ctx
->ac
.i32
, ""), "");
2163 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2164 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2170 args
->coords
[2] = args
->coords
[1];
2171 args
->coords
[1] = ctx
->ac
.i32_0
;
2173 args
->coords
[1] = ctx
->ac
.i32_0
;
2178 args
->coords
[count
] = sample_index
;
2184 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2185 const nir_intrinsic_instr
*instr
, bool write
)
2187 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, write
);
2188 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2189 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2190 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2191 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2193 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2194 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2195 elem_count
, stride
, "");
2197 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2198 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2203 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2204 const nir_intrinsic_instr
*instr
)
2207 const nir_variable
*var
= instr
->variables
[0]->var
;
2208 const struct glsl_type
*type
= var
->type
;
2210 if(instr
->variables
[0]->deref
.child
)
2211 type
= instr
->variables
[0]->deref
.child
->type
;
2213 type
= glsl_without_array(type
);
2215 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2216 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2217 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2218 unsigned num_channels
= util_last_bit(mask
);
2219 LLVMValueRef rsrc
, vindex
;
2221 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2222 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2225 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2226 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2227 ctx
->ac
.i32_0
, num_channels
,
2229 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2231 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2232 res
= ac_to_integer(&ctx
->ac
, res
);
2234 struct ac_image_args args
= {};
2235 args
.opcode
= ac_image_load
;
2236 get_image_coords(ctx
, instr
, &args
);
2237 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2238 AC_DESC_IMAGE
, NULL
, true, false);
2239 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2240 glsl_sampler_type_is_array(type
));
2242 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2243 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2244 args
.cache_policy
|= ac_glc
;
2246 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2248 return ac_to_integer(&ctx
->ac
, res
);
2251 static void visit_image_store(struct ac_nir_context
*ctx
,
2252 nir_intrinsic_instr
*instr
)
2254 LLVMValueRef params
[8];
2255 const nir_variable
*var
= instr
->variables
[0]->var
;
2256 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2257 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2258 LLVMValueRef glc
= ctx
->ac
.i1false
;
2259 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2261 glc
= ctx
->ac
.i1true
;
2263 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2264 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2266 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2268 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2269 ctx
->ac
.i32_0
, ""); /* vindex */
2270 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2271 params
[4] = glc
; /* glc */
2272 params
[5] = ctx
->ac
.i1false
; /* slc */
2273 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2276 struct ac_image_args args
= {};
2277 args
.opcode
= ac_image_store
;
2278 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2279 get_image_coords(ctx
, instr
, &args
);
2280 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2281 AC_DESC_IMAGE
, NULL
, true, false);
2282 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2283 glsl_sampler_type_is_array(type
));
2285 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2286 args
.cache_policy
|= ac_glc
;
2288 ac_build_image_opcode(&ctx
->ac
, &args
);
2293 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2294 const nir_intrinsic_instr
*instr
)
2296 LLVMValueRef params
[7];
2297 int param_count
= 0;
2298 const nir_variable
*var
= instr
->variables
[0]->var
;
2300 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
;
2301 const char *atomic_name
;
2302 char intrinsic_name
[41];
2303 enum ac_atomic_op atomic_subop
;
2304 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2305 MAYBE_UNUSED
int length
;
2307 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2309 switch (instr
->intrinsic
) {
2310 case nir_intrinsic_image_var_atomic_add
:
2311 atomic_name
= "add";
2312 atomic_subop
= ac_atomic_add
;
2314 case nir_intrinsic_image_var_atomic_min
:
2315 atomic_name
= is_unsigned
? "umin" : "smin";
2316 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2318 case nir_intrinsic_image_var_atomic_max
:
2319 atomic_name
= is_unsigned
? "umax" : "smax";
2320 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2322 case nir_intrinsic_image_var_atomic_and
:
2323 atomic_name
= "and";
2324 atomic_subop
= ac_atomic_and
;
2326 case nir_intrinsic_image_var_atomic_or
:
2328 atomic_subop
= ac_atomic_or
;
2330 case nir_intrinsic_image_var_atomic_xor
:
2331 atomic_name
= "xor";
2332 atomic_subop
= ac_atomic_xor
;
2334 case nir_intrinsic_image_var_atomic_exchange
:
2335 atomic_name
= "swap";
2336 atomic_subop
= ac_atomic_swap
;
2338 case nir_intrinsic_image_var_atomic_comp_swap
:
2339 atomic_name
= "cmpswap";
2340 atomic_subop
= 0; /* not used */
2347 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2348 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2350 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2351 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2352 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2353 ctx
->ac
.i32_0
, ""); /* vindex */
2354 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2355 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2357 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2358 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2360 assert(length
< sizeof(intrinsic_name
));
2361 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2362 params
, param_count
, 0);
2364 struct ac_image_args args
= {};
2365 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2366 args
.atomic
= atomic_subop
;
2367 args
.data
[0] = params
[0];
2369 args
.data
[1] = params
[1];
2370 get_image_coords(ctx
, instr
, &args
);
2371 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2372 AC_DESC_IMAGE
, NULL
, true, false);
2373 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2374 glsl_sampler_type_is_array(type
));
2376 return ac_build_image_opcode(&ctx
->ac
, &args
);
2380 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2381 const nir_intrinsic_instr
*instr
)
2383 const nir_variable
*var
= instr
->variables
[0]->var
;
2384 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2386 struct ac_image_args args
= { 0 };
2387 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2388 glsl_sampler_type_is_array(type
));
2390 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2391 AC_DESC_IMAGE
, NULL
, true, false);
2392 args
.opcode
= ac_image_get_resinfo
;
2393 args
.lod
= ctx
->ac
.i32_0
;
2394 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2396 return ac_build_image_opcode(&ctx
->ac
, &args
);
2399 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2400 const nir_intrinsic_instr
*instr
)
2403 const nir_variable
*var
= instr
->variables
[0]->var
;
2404 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2406 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2407 return get_buffer_size(ctx
,
2408 get_sampler_desc(ctx
, instr
->variables
[0],
2409 AC_DESC_BUFFER
, NULL
, true, false), true);
2411 struct ac_image_args args
= { 0 };
2413 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2414 glsl_sampler_type_is_array(type
));
2416 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2417 args
.opcode
= ac_image_get_resinfo
;
2418 args
.lod
= ctx
->ac
.i32_0
;
2419 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2421 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2423 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2425 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2426 glsl_sampler_type_is_array(type
)) {
2427 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2428 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2429 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2430 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2432 if (ctx
->ac
.chip_class
>= GFX9
&&
2433 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2434 glsl_sampler_type_is_array(type
)) {
2435 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2436 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2443 #define NOOP_WAITCNT 0xf7f
2444 #define LGKM_CNT 0x07f
2445 #define VM_CNT 0xf70
2447 static void emit_membar(struct ac_llvm_context
*ac
,
2448 const nir_intrinsic_instr
*instr
)
2450 unsigned waitcnt
= NOOP_WAITCNT
;
2452 switch (instr
->intrinsic
) {
2453 case nir_intrinsic_memory_barrier
:
2454 case nir_intrinsic_group_memory_barrier
:
2455 waitcnt
&= VM_CNT
& LGKM_CNT
;
2457 case nir_intrinsic_memory_barrier_atomic_counter
:
2458 case nir_intrinsic_memory_barrier_buffer
:
2459 case nir_intrinsic_memory_barrier_image
:
2462 case nir_intrinsic_memory_barrier_shared
:
2463 waitcnt
&= LGKM_CNT
;
2468 if (waitcnt
!= NOOP_WAITCNT
)
2469 ac_build_waitcnt(ac
, waitcnt
);
2472 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2474 /* SI only (thanks to a hw bug workaround):
2475 * The real barrier instruction isn’t needed, because an entire patch
2476 * always fits into a single wave.
2478 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2479 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2482 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2483 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2486 static void emit_discard(struct ac_nir_context
*ctx
,
2487 const nir_intrinsic_instr
*instr
)
2491 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2492 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2493 get_src(ctx
, instr
->src
[0]),
2496 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2497 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2500 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2504 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2506 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2507 "llvm.amdgcn.ps.live",
2508 ctx
->ac
.i1
, NULL
, 0,
2509 AC_FUNC_ATTR_READNONE
);
2510 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2511 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2515 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2517 LLVMValueRef result
;
2518 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2519 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2520 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2522 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2526 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2528 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2529 LLVMValueRef result
;
2530 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2531 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2532 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2534 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2539 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2541 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2542 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2543 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2545 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2550 visit_first_invocation(struct ac_nir_context
*ctx
)
2552 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2554 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2555 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2556 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2558 ctx
->ac
.i64
, args
, 2,
2559 AC_FUNC_ATTR_NOUNWIND
|
2560 AC_FUNC_ATTR_READNONE
);
2562 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2566 visit_load_shared(struct ac_nir_context
*ctx
,
2567 const nir_intrinsic_instr
*instr
)
2569 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2571 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2573 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2574 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2575 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2576 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2579 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2580 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2584 visit_store_shared(struct ac_nir_context
*ctx
,
2585 const nir_intrinsic_instr
*instr
)
2587 LLVMValueRef derived_ptr
, data
,index
;
2588 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2590 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2591 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2593 int writemask
= nir_intrinsic_write_mask(instr
);
2594 for (int chan
= 0; chan
< 4; chan
++) {
2595 if (!(writemask
& (1 << chan
))) {
2598 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2599 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2600 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2601 LLVMBuildStore(builder
, data
, derived_ptr
);
2605 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2606 const nir_intrinsic_instr
*instr
,
2607 LLVMValueRef ptr
, int src_idx
)
2609 LLVMValueRef result
;
2610 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2612 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2613 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2614 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2615 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2617 LLVMAtomicOrderingSequentiallyConsistent
,
2618 LLVMAtomicOrderingSequentiallyConsistent
,
2620 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2622 LLVMAtomicRMWBinOp op
;
2623 switch (instr
->intrinsic
) {
2624 case nir_intrinsic_var_atomic_add
:
2625 case nir_intrinsic_shared_atomic_add
:
2626 op
= LLVMAtomicRMWBinOpAdd
;
2628 case nir_intrinsic_var_atomic_umin
:
2629 case nir_intrinsic_shared_atomic_umin
:
2630 op
= LLVMAtomicRMWBinOpUMin
;
2632 case nir_intrinsic_var_atomic_umax
:
2633 case nir_intrinsic_shared_atomic_umax
:
2634 op
= LLVMAtomicRMWBinOpUMax
;
2636 case nir_intrinsic_var_atomic_imin
:
2637 case nir_intrinsic_shared_atomic_imin
:
2638 op
= LLVMAtomicRMWBinOpMin
;
2640 case nir_intrinsic_var_atomic_imax
:
2641 case nir_intrinsic_shared_atomic_imax
:
2642 op
= LLVMAtomicRMWBinOpMax
;
2644 case nir_intrinsic_var_atomic_and
:
2645 case nir_intrinsic_shared_atomic_and
:
2646 op
= LLVMAtomicRMWBinOpAnd
;
2648 case nir_intrinsic_var_atomic_or
:
2649 case nir_intrinsic_shared_atomic_or
:
2650 op
= LLVMAtomicRMWBinOpOr
;
2652 case nir_intrinsic_var_atomic_xor
:
2653 case nir_intrinsic_shared_atomic_xor
:
2654 op
= LLVMAtomicRMWBinOpXor
;
2656 case nir_intrinsic_var_atomic_exchange
:
2657 case nir_intrinsic_shared_atomic_exchange
:
2658 op
= LLVMAtomicRMWBinOpXchg
;
2664 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2665 LLVMAtomicOrderingSequentiallyConsistent
,
2671 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2673 LLVMValueRef values
[2];
2674 LLVMValueRef pos
[2];
2676 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2677 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2679 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2680 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2681 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2684 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2685 const nir_intrinsic_instr
*instr
)
2687 LLVMValueRef result
[4];
2688 LLVMValueRef interp_param
, attr_number
;
2691 LLVMValueRef src_c0
= NULL
;
2692 LLVMValueRef src_c1
= NULL
;
2693 LLVMValueRef src0
= NULL
;
2694 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2695 switch (instr
->intrinsic
) {
2696 case nir_intrinsic_interp_var_at_centroid
:
2697 location
= INTERP_CENTROID
;
2699 case nir_intrinsic_interp_var_at_sample
:
2700 case nir_intrinsic_interp_var_at_offset
:
2701 location
= INTERP_CENTER
;
2702 src0
= get_src(ctx
, instr
->src
[0]);
2708 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2709 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2710 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2711 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2712 LLVMValueRef sample_position
;
2713 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2715 /* fetch sample ID */
2716 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2718 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2719 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2720 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2721 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2723 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2724 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2726 if (location
== INTERP_CENTER
) {
2727 LLVMValueRef ij_out
[2];
2728 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2731 * take the I then J parameters, and the DDX/Y for it, and
2732 * calculate the IJ inputs for the interpolator.
2733 * temp1 = ddx * offset/sample.x + I;
2734 * interp_param.I = ddy * offset/sample.y + temp1;
2735 * temp1 = ddx * offset/sample.x + J;
2736 * interp_param.J = ddy * offset/sample.y + temp1;
2738 for (unsigned i
= 0; i
< 2; i
++) {
2739 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2740 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2741 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2742 ddxy_out
, ix_ll
, "");
2743 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2744 ddxy_out
, iy_ll
, "");
2745 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2746 interp_param
, ix_ll
, "");
2747 LLVMValueRef temp1
, temp2
;
2749 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2752 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2753 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2755 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2756 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2758 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2759 temp2
, ctx
->ac
.i32
, "");
2761 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2765 for (chan
= 0; chan
< 4; chan
++) {
2766 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2769 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2770 interp_param
, ctx
->ac
.v2f32
, "");
2771 LLVMValueRef i
= LLVMBuildExtractElement(
2772 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2773 LLVMValueRef j
= LLVMBuildExtractElement(
2774 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2776 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2777 llvm_chan
, attr_number
,
2778 ctx
->abi
->prim_mask
, i
, j
);
2780 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2781 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2782 llvm_chan
, attr_number
,
2783 ctx
->abi
->prim_mask
);
2786 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2787 instr
->variables
[0]->var
->data
.location_frac
);
2790 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2791 nir_intrinsic_instr
*instr
)
2793 LLVMValueRef result
= NULL
;
2795 switch (instr
->intrinsic
) {
2796 case nir_intrinsic_ballot
:
2797 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2799 case nir_intrinsic_read_invocation
:
2800 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2801 get_src(ctx
, instr
->src
[1]));
2803 case nir_intrinsic_read_first_invocation
:
2804 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2806 case nir_intrinsic_load_subgroup_invocation
:
2807 result
= ac_get_thread_id(&ctx
->ac
);
2809 case nir_intrinsic_load_work_group_id
: {
2810 LLVMValueRef values
[3];
2812 for (int i
= 0; i
< 3; i
++) {
2813 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2814 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2817 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2820 case nir_intrinsic_load_base_vertex
:
2821 case nir_intrinsic_load_first_vertex
:
2822 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2824 case nir_intrinsic_load_local_group_size
:
2825 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2827 case nir_intrinsic_load_vertex_id
:
2828 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2829 ctx
->abi
->base_vertex
, "");
2831 case nir_intrinsic_load_vertex_id_zero_base
: {
2832 result
= ctx
->abi
->vertex_id
;
2835 case nir_intrinsic_load_local_invocation_id
: {
2836 result
= ctx
->abi
->local_invocation_ids
;
2839 case nir_intrinsic_load_base_instance
:
2840 result
= ctx
->abi
->start_instance
;
2842 case nir_intrinsic_load_draw_id
:
2843 result
= ctx
->abi
->draw_id
;
2845 case nir_intrinsic_load_view_index
:
2846 result
= ctx
->abi
->view_index
;
2848 case nir_intrinsic_load_invocation_id
:
2849 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2850 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2852 result
= ctx
->abi
->gs_invocation_id
;
2854 case nir_intrinsic_load_primitive_id
:
2855 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2856 result
= ctx
->abi
->gs_prim_id
;
2857 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2858 result
= ctx
->abi
->tcs_patch_id
;
2859 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2860 result
= ctx
->abi
->tes_patch_id
;
2862 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2864 case nir_intrinsic_load_sample_id
:
2865 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2867 case nir_intrinsic_load_sample_pos
:
2868 result
= load_sample_pos(ctx
);
2870 case nir_intrinsic_load_sample_mask_in
:
2871 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2873 case nir_intrinsic_load_frag_coord
: {
2874 LLVMValueRef values
[4] = {
2875 ctx
->abi
->frag_pos
[0],
2876 ctx
->abi
->frag_pos
[1],
2877 ctx
->abi
->frag_pos
[2],
2878 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2880 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2883 case nir_intrinsic_load_front_face
:
2884 result
= ctx
->abi
->front_face
;
2886 case nir_intrinsic_load_helper_invocation
:
2887 result
= visit_load_helper_invocation(ctx
);
2889 case nir_intrinsic_load_instance_id
:
2890 result
= ctx
->abi
->instance_id
;
2892 case nir_intrinsic_load_num_work_groups
:
2893 result
= ctx
->abi
->num_work_groups
;
2895 case nir_intrinsic_load_local_invocation_index
:
2896 result
= visit_load_local_invocation_index(ctx
);
2898 case nir_intrinsic_load_subgroup_id
:
2899 result
= visit_load_subgroup_id(ctx
);
2901 case nir_intrinsic_load_num_subgroups
:
2902 result
= visit_load_num_subgroups(ctx
);
2904 case nir_intrinsic_first_invocation
:
2905 result
= visit_first_invocation(ctx
);
2907 case nir_intrinsic_load_push_constant
:
2908 result
= visit_load_push_constant(ctx
, instr
);
2910 case nir_intrinsic_vulkan_resource_index
: {
2911 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2912 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2913 unsigned binding
= nir_intrinsic_binding(instr
);
2915 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2919 case nir_intrinsic_vulkan_resource_reindex
:
2920 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2922 case nir_intrinsic_store_ssbo
:
2923 visit_store_ssbo(ctx
, instr
);
2925 case nir_intrinsic_load_ssbo
:
2926 result
= visit_load_buffer(ctx
, instr
);
2928 case nir_intrinsic_ssbo_atomic_add
:
2929 case nir_intrinsic_ssbo_atomic_imin
:
2930 case nir_intrinsic_ssbo_atomic_umin
:
2931 case nir_intrinsic_ssbo_atomic_imax
:
2932 case nir_intrinsic_ssbo_atomic_umax
:
2933 case nir_intrinsic_ssbo_atomic_and
:
2934 case nir_intrinsic_ssbo_atomic_or
:
2935 case nir_intrinsic_ssbo_atomic_xor
:
2936 case nir_intrinsic_ssbo_atomic_exchange
:
2937 case nir_intrinsic_ssbo_atomic_comp_swap
:
2938 result
= visit_atomic_ssbo(ctx
, instr
);
2940 case nir_intrinsic_load_ubo
:
2941 result
= visit_load_ubo_buffer(ctx
, instr
);
2943 case nir_intrinsic_get_buffer_size
:
2944 result
= visit_get_buffer_size(ctx
, instr
);
2946 case nir_intrinsic_load_var
:
2947 result
= visit_load_var(ctx
, instr
);
2949 case nir_intrinsic_store_var
:
2950 visit_store_var(ctx
, instr
);
2952 case nir_intrinsic_load_shared
:
2953 result
= visit_load_shared(ctx
, instr
);
2955 case nir_intrinsic_store_shared
:
2956 visit_store_shared(ctx
, instr
);
2958 case nir_intrinsic_image_var_samples
:
2959 result
= visit_image_samples(ctx
, instr
);
2961 case nir_intrinsic_image_var_load
:
2962 result
= visit_image_load(ctx
, instr
);
2964 case nir_intrinsic_image_var_store
:
2965 visit_image_store(ctx
, instr
);
2967 case nir_intrinsic_image_var_atomic_add
:
2968 case nir_intrinsic_image_var_atomic_min
:
2969 case nir_intrinsic_image_var_atomic_max
:
2970 case nir_intrinsic_image_var_atomic_and
:
2971 case nir_intrinsic_image_var_atomic_or
:
2972 case nir_intrinsic_image_var_atomic_xor
:
2973 case nir_intrinsic_image_var_atomic_exchange
:
2974 case nir_intrinsic_image_var_atomic_comp_swap
:
2975 result
= visit_image_atomic(ctx
, instr
);
2977 case nir_intrinsic_image_var_size
:
2978 result
= visit_image_size(ctx
, instr
);
2980 case nir_intrinsic_shader_clock
:
2981 result
= ac_build_shader_clock(&ctx
->ac
);
2983 case nir_intrinsic_discard
:
2984 case nir_intrinsic_discard_if
:
2985 emit_discard(ctx
, instr
);
2987 case nir_intrinsic_memory_barrier
:
2988 case nir_intrinsic_group_memory_barrier
:
2989 case nir_intrinsic_memory_barrier_atomic_counter
:
2990 case nir_intrinsic_memory_barrier_buffer
:
2991 case nir_intrinsic_memory_barrier_image
:
2992 case nir_intrinsic_memory_barrier_shared
:
2993 emit_membar(&ctx
->ac
, instr
);
2995 case nir_intrinsic_barrier
:
2996 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
2998 case nir_intrinsic_shared_atomic_add
:
2999 case nir_intrinsic_shared_atomic_imin
:
3000 case nir_intrinsic_shared_atomic_umin
:
3001 case nir_intrinsic_shared_atomic_imax
:
3002 case nir_intrinsic_shared_atomic_umax
:
3003 case nir_intrinsic_shared_atomic_and
:
3004 case nir_intrinsic_shared_atomic_or
:
3005 case nir_intrinsic_shared_atomic_xor
:
3006 case nir_intrinsic_shared_atomic_exchange
:
3007 case nir_intrinsic_shared_atomic_comp_swap
: {
3008 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3009 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3012 case nir_intrinsic_var_atomic_add
:
3013 case nir_intrinsic_var_atomic_imin
:
3014 case nir_intrinsic_var_atomic_umin
:
3015 case nir_intrinsic_var_atomic_imax
:
3016 case nir_intrinsic_var_atomic_umax
:
3017 case nir_intrinsic_var_atomic_and
:
3018 case nir_intrinsic_var_atomic_or
:
3019 case nir_intrinsic_var_atomic_xor
:
3020 case nir_intrinsic_var_atomic_exchange
:
3021 case nir_intrinsic_var_atomic_comp_swap
: {
3022 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3023 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3026 case nir_intrinsic_interp_var_at_centroid
:
3027 case nir_intrinsic_interp_var_at_sample
:
3028 case nir_intrinsic_interp_var_at_offset
:
3029 result
= visit_interp(ctx
, instr
);
3031 case nir_intrinsic_emit_vertex
:
3032 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3034 case nir_intrinsic_end_primitive
:
3035 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3037 case nir_intrinsic_load_tess_coord
:
3038 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3040 case nir_intrinsic_load_tess_level_outer
:
3041 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3043 case nir_intrinsic_load_tess_level_inner
:
3044 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3046 case nir_intrinsic_load_patch_vertices_in
:
3047 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3049 case nir_intrinsic_vote_all
: {
3050 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3051 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3054 case nir_intrinsic_vote_any
: {
3055 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3056 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3059 case nir_intrinsic_shuffle
:
3060 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3061 get_src(ctx
, instr
->src
[1]));
3063 case nir_intrinsic_reduce
:
3064 result
= ac_build_reduce(&ctx
->ac
,
3065 get_src(ctx
, instr
->src
[0]),
3066 instr
->const_index
[0],
3067 instr
->const_index
[1]);
3069 case nir_intrinsic_inclusive_scan
:
3070 result
= ac_build_inclusive_scan(&ctx
->ac
,
3071 get_src(ctx
, instr
->src
[0]),
3072 instr
->const_index
[0]);
3074 case nir_intrinsic_exclusive_scan
:
3075 result
= ac_build_exclusive_scan(&ctx
->ac
,
3076 get_src(ctx
, instr
->src
[0]),
3077 instr
->const_index
[0]);
3079 case nir_intrinsic_quad_broadcast
: {
3080 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3081 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3082 lane
, lane
, lane
, lane
);
3085 case nir_intrinsic_quad_swap_horizontal
:
3086 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3088 case nir_intrinsic_quad_swap_vertical
:
3089 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3091 case nir_intrinsic_quad_swap_diagonal
:
3092 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3095 fprintf(stderr
, "Unknown intrinsic: ");
3096 nir_print_instr(&instr
->instr
, stderr
);
3097 fprintf(stderr
, "\n");
3101 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3105 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3106 const nir_deref_var
*deref
,
3107 enum ac_descriptor_type desc_type
,
3108 const nir_tex_instr
*tex_instr
,
3109 bool image
, bool write
)
3111 LLVMValueRef index
= NULL
;
3112 unsigned constant_index
= 0;
3113 unsigned descriptor_set
;
3114 unsigned base_index
;
3115 bool bindless
= false;
3118 assert(tex_instr
&& !image
);
3120 base_index
= tex_instr
->sampler_index
;
3122 const nir_deref
*tail
= &deref
->deref
;
3123 while (tail
->child
) {
3124 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3125 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3130 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3132 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3133 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3135 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3136 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3141 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3144 constant_index
+= child
->base_offset
* array_size
;
3146 tail
= &child
->deref
;
3148 descriptor_set
= deref
->var
->data
.descriptor_set
;
3150 if (deref
->var
->data
.bindless
) {
3151 bindless
= deref
->var
->data
.bindless
;
3152 base_index
= deref
->var
->data
.driver_location
;
3154 base_index
= deref
->var
->data
.binding
;
3158 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3161 constant_index
, index
,
3162 desc_type
, image
, write
, bindless
);
3165 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3168 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3169 * filtering manually. The driver sets img7 to a mask clearing
3170 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3171 * s_and_b32 samp0, samp0, img7
3174 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3176 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3177 LLVMValueRef res
, LLVMValueRef samp
)
3179 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3180 LLVMValueRef img7
, samp0
;
3182 if (ctx
->ac
.chip_class
>= VI
)
3185 img7
= LLVMBuildExtractElement(builder
, res
,
3186 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3187 samp0
= LLVMBuildExtractElement(builder
, samp
,
3188 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3189 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3190 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3191 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3194 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3195 nir_tex_instr
*instr
,
3196 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3197 LLVMValueRef
*fmask_ptr
)
3199 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3200 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
3202 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
3205 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
3207 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
3208 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3209 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3211 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3212 instr
->op
== nir_texop_samples_identical
))
3213 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
3216 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3219 coord
= ac_to_float(ctx
, coord
);
3220 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3221 coord
= ac_to_integer(ctx
, coord
);
3225 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3227 LLVMValueRef result
= NULL
;
3228 struct ac_image_args args
= { 0 };
3229 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3230 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3231 unsigned offset_src
= 0;
3233 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3235 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3236 switch (instr
->src
[i
].src_type
) {
3237 case nir_tex_src_coord
: {
3238 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3239 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3240 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3243 case nir_tex_src_projector
:
3245 case nir_tex_src_comparator
:
3246 if (instr
->is_shadow
)
3247 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3249 case nir_tex_src_offset
:
3250 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3253 case nir_tex_src_bias
:
3254 if (instr
->op
== nir_texop_txb
)
3255 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3257 case nir_tex_src_lod
: {
3258 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3260 if (val
&& val
->i32
[0] == 0)
3261 args
.level_zero
= true;
3263 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3266 case nir_tex_src_ms_index
:
3267 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3269 case nir_tex_src_ms_mcs
:
3271 case nir_tex_src_ddx
:
3272 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3274 case nir_tex_src_ddy
:
3275 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3277 case nir_tex_src_texture_offset
:
3278 case nir_tex_src_sampler_offset
:
3279 case nir_tex_src_plane
:
3285 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3286 result
= get_buffer_size(ctx
, args
.resource
, true);
3290 if (instr
->op
== nir_texop_texture_samples
) {
3291 LLVMValueRef res
, samples
, is_msaa
;
3292 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3293 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3294 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3295 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3296 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3297 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3298 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3299 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3300 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3302 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3303 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3304 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3305 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3306 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3308 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3314 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3315 LLVMValueRef offset
[3], pack
;
3316 for (unsigned chan
= 0; chan
< 3; ++chan
)
3317 offset
[chan
] = ctx
->ac
.i32_0
;
3319 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3320 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3321 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3322 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3323 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3325 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3326 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3328 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3329 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3333 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3334 * so the depth comparison value isn't clamped for Z16 and
3335 * Z24 anymore. Do it manually here.
3337 * It's unnecessary if the original texture format was
3338 * Z32_FLOAT, but we don't know that here.
3340 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3341 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3343 /* pack derivatives */
3345 int num_src_deriv_channels
, num_dest_deriv_channels
;
3346 switch (instr
->sampler_dim
) {
3347 case GLSL_SAMPLER_DIM_3D
:
3348 case GLSL_SAMPLER_DIM_CUBE
:
3349 num_src_deriv_channels
= 3;
3350 num_dest_deriv_channels
= 3;
3352 case GLSL_SAMPLER_DIM_2D
:
3354 num_src_deriv_channels
= 2;
3355 num_dest_deriv_channels
= 2;
3357 case GLSL_SAMPLER_DIM_1D
:
3358 num_src_deriv_channels
= 1;
3359 if (ctx
->ac
.chip_class
>= GFX9
) {
3360 num_dest_deriv_channels
= 2;
3362 num_dest_deriv_channels
= 1;
3367 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3368 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3369 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3370 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3371 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3373 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3374 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3375 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3379 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3380 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3381 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3382 if (instr
->coord_components
== 3)
3383 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3384 ac_prepare_cube_coords(&ctx
->ac
,
3385 instr
->op
== nir_texop_txd
, instr
->is_array
,
3386 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3389 /* Texture coordinates fixups */
3390 if (instr
->coord_components
> 1 &&
3391 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3393 instr
->op
!= nir_texop_txf
) {
3394 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3397 if (instr
->coord_components
> 2 &&
3398 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3399 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3400 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3401 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3403 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3404 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3407 if (ctx
->ac
.chip_class
>= GFX9
&&
3408 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3409 instr
->op
!= nir_texop_lod
) {
3410 LLVMValueRef filler
;
3411 if (instr
->op
== nir_texop_txf
)
3412 filler
= ctx
->ac
.i32_0
;
3414 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3416 if (instr
->is_array
)
3417 args
.coords
[2] = args
.coords
[1];
3418 args
.coords
[1] = filler
;
3421 /* Pack sample index */
3422 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3423 args
.coords
[instr
->coord_components
] = sample_index
;
3425 if (instr
->op
== nir_texop_samples_identical
) {
3426 struct ac_image_args txf_args
= { 0 };
3427 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3429 txf_args
.dmask
= 0xf;
3430 txf_args
.resource
= fmask_ptr
;
3431 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3432 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3434 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3435 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3439 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3440 instr
->op
!= nir_texop_txs
) {
3441 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3442 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3443 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3444 instr
->is_array
? args
.coords
[2] : NULL
,
3445 args
.coords
[sample_chan
], fmask_ptr
);
3448 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3449 nir_const_value
*const_offset
=
3450 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3451 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3452 assert(const_offset
);
3453 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3454 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3455 args
.coords
[i
] = LLVMBuildAdd(
3456 ctx
->ac
.builder
, args
.coords
[i
],
3457 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3462 /* TODO TG4 support */
3464 if (instr
->op
== nir_texop_tg4
) {
3465 if (instr
->is_shadow
)
3468 args
.dmask
= 1 << instr
->component
;
3471 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3472 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3473 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3475 if (instr
->op
== nir_texop_query_levels
)
3476 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3477 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3478 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3479 instr
->op
!= nir_texop_tg4
)
3480 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3481 else if (instr
->op
== nir_texop_txs
&&
3482 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3484 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3485 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3486 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3487 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3488 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3489 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3490 instr
->op
== nir_texop_txs
&&
3491 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3493 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3494 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3495 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3497 } else if (instr
->dest
.ssa
.num_components
!= 4)
3498 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3502 assert(instr
->dest
.is_ssa
);
3503 result
= ac_to_integer(&ctx
->ac
, result
);
3504 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3509 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3511 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3512 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3514 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3515 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3518 static void visit_post_phi(struct ac_nir_context
*ctx
,
3519 nir_phi_instr
*instr
,
3520 LLVMValueRef llvm_phi
)
3522 nir_foreach_phi_src(src
, instr
) {
3523 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3524 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3526 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3530 static void phi_post_pass(struct ac_nir_context
*ctx
)
3532 struct hash_entry
*entry
;
3533 hash_table_foreach(ctx
->phis
, entry
) {
3534 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3535 (LLVMValueRef
)entry
->data
);
3540 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3541 const nir_ssa_undef_instr
*instr
)
3543 unsigned num_components
= instr
->def
.num_components
;
3544 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3547 if (num_components
== 1)
3548 undef
= LLVMGetUndef(type
);
3550 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3552 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3555 static void visit_jump(struct ac_llvm_context
*ctx
,
3556 const nir_jump_instr
*instr
)
3558 switch (instr
->type
) {
3559 case nir_jump_break
:
3560 ac_build_break(ctx
);
3562 case nir_jump_continue
:
3563 ac_build_continue(ctx
);
3566 fprintf(stderr
, "Unknown NIR jump instr: ");
3567 nir_print_instr(&instr
->instr
, stderr
);
3568 fprintf(stderr
, "\n");
3573 static void visit_cf_list(struct ac_nir_context
*ctx
,
3574 struct exec_list
*list
);
3576 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3578 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3579 nir_foreach_instr(instr
, block
)
3581 switch (instr
->type
) {
3582 case nir_instr_type_alu
:
3583 visit_alu(ctx
, nir_instr_as_alu(instr
));
3585 case nir_instr_type_load_const
:
3586 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3588 case nir_instr_type_intrinsic
:
3589 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3591 case nir_instr_type_tex
:
3592 visit_tex(ctx
, nir_instr_as_tex(instr
));
3594 case nir_instr_type_phi
:
3595 visit_phi(ctx
, nir_instr_as_phi(instr
));
3597 case nir_instr_type_ssa_undef
:
3598 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3600 case nir_instr_type_jump
:
3601 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3604 fprintf(stderr
, "Unknown NIR instr type: ");
3605 nir_print_instr(instr
, stderr
);
3606 fprintf(stderr
, "\n");
3611 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3614 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3616 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3618 nir_block
*then_block
=
3619 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3621 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3623 visit_cf_list(ctx
, &if_stmt
->then_list
);
3625 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3626 nir_block
*else_block
=
3627 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3629 ac_build_else(&ctx
->ac
, else_block
->index
);
3630 visit_cf_list(ctx
, &if_stmt
->else_list
);
3633 ac_build_endif(&ctx
->ac
, then_block
->index
);
3636 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3638 nir_block
*first_loop_block
=
3639 (nir_block
*) exec_list_get_head(&loop
->body
);
3641 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3643 visit_cf_list(ctx
, &loop
->body
);
3645 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3648 static void visit_cf_list(struct ac_nir_context
*ctx
,
3649 struct exec_list
*list
)
3651 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3653 switch (node
->type
) {
3654 case nir_cf_node_block
:
3655 visit_block(ctx
, nir_cf_node_as_block(node
));
3658 case nir_cf_node_if
:
3659 visit_if(ctx
, nir_cf_node_as_if(node
));
3662 case nir_cf_node_loop
:
3663 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3673 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3674 struct ac_shader_abi
*abi
,
3675 struct nir_shader
*nir
,
3676 struct nir_variable
*variable
,
3677 gl_shader_stage stage
)
3679 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3680 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3682 /* tess ctrl has it's own load/store paths for outputs */
3683 if (stage
== MESA_SHADER_TESS_CTRL
)
3686 if (stage
== MESA_SHADER_VERTEX
||
3687 stage
== MESA_SHADER_TESS_EVAL
||
3688 stage
== MESA_SHADER_GEOMETRY
) {
3689 int idx
= variable
->data
.location
+ variable
->data
.index
;
3690 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3691 int length
= nir
->info
.clip_distance_array_size
+
3692 nir
->info
.cull_distance_array_size
;
3701 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3702 for (unsigned chan
= 0; chan
< 4; chan
++) {
3703 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3704 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3710 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3711 enum glsl_base_type type
)
3715 case GLSL_TYPE_UINT
:
3716 case GLSL_TYPE_BOOL
:
3717 case GLSL_TYPE_SUBROUTINE
:
3719 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3721 case GLSL_TYPE_INT64
:
3722 case GLSL_TYPE_UINT64
:
3724 case GLSL_TYPE_DOUBLE
:
3727 unreachable("unknown GLSL type");
3732 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3733 const struct glsl_type
*type
)
3735 if (glsl_type_is_scalar(type
)) {
3736 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3739 if (glsl_type_is_vector(type
)) {
3740 return LLVMVectorType(
3741 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3742 glsl_get_vector_elements(type
));
3745 if (glsl_type_is_matrix(type
)) {
3746 return LLVMArrayType(
3747 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3748 glsl_get_matrix_columns(type
));
3751 if (glsl_type_is_array(type
)) {
3752 return LLVMArrayType(
3753 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3754 glsl_get_length(type
));
3757 assert(glsl_type_is_struct(type
));
3759 LLVMTypeRef member_types
[glsl_get_length(type
)];
3761 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3763 glsl_to_llvm_type(ac
,
3764 glsl_get_struct_field(type
, i
));
3767 return LLVMStructTypeInContext(ac
->context
, member_types
,
3768 glsl_get_length(type
), false);
3772 setup_locals(struct ac_nir_context
*ctx
,
3773 struct nir_function
*func
)
3776 ctx
->num_locals
= 0;
3777 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3778 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3779 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3780 variable
->data
.location_frac
= 0;
3781 ctx
->num_locals
+= attrib_count
;
3783 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3787 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3788 for (j
= 0; j
< 4; j
++) {
3789 ctx
->locals
[i
* 4 + j
] =
3790 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3796 setup_shared(struct ac_nir_context
*ctx
,
3797 struct nir_shader
*nir
)
3799 nir_foreach_variable(variable
, &nir
->shared
) {
3800 LLVMValueRef shared
=
3801 LLVMAddGlobalInAddressSpace(
3802 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3803 variable
->name
? variable
->name
: "",
3804 AC_LOCAL_ADDR_SPACE
);
3805 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3809 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3810 struct nir_shader
*nir
)
3812 struct ac_nir_context ctx
= {};
3813 struct nir_function
*func
;
3818 ctx
.stage
= nir
->info
.stage
;
3820 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3822 nir_foreach_variable(variable
, &nir
->outputs
)
3823 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3826 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3827 _mesa_key_pointer_equal
);
3828 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3829 _mesa_key_pointer_equal
);
3830 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3831 _mesa_key_pointer_equal
);
3833 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3835 nir_index_ssa_defs(func
->impl
);
3836 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3838 setup_locals(&ctx
, func
);
3840 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3841 setup_shared(&ctx
, nir
);
3843 visit_cf_list(&ctx
, &func
->impl
->body
);
3844 phi_post_pass(&ctx
);
3846 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3847 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3852 ralloc_free(ctx
.defs
);
3853 ralloc_free(ctx
.phis
);
3854 ralloc_free(ctx
.vars
);
3858 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3860 /* While it would be nice not to have this flag, we are constrained
3861 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3864 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3866 /* TODO: Indirect indexing of GS inputs is unimplemented.
3868 * TCS and TES load inputs directly from LDS or offchip memory, so
3869 * indirect indexing is trivial.
3871 nir_variable_mode indirect_mask
= 0;
3872 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3873 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3874 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3875 !llvm_has_working_vgpr_indexing
)) {
3876 indirect_mask
|= nir_var_shader_in
;
3878 if (!llvm_has_working_vgpr_indexing
&&
3879 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3880 indirect_mask
|= nir_var_shader_out
;
3882 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3883 * smart enough to handle indirects without causing excess spilling
3884 * causing the gpu to hang.
3886 * See the following thread for more details of the problem:
3887 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3889 indirect_mask
|= nir_var_local
;
3891 nir_lower_indirect_derefs(nir
, indirect_mask
);