2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
36 struct ac_nir_context
{
37 struct ac_llvm_context ac
;
38 struct ac_shader_abi
*abi
;
40 gl_shader_stage stage
;
42 LLVMValueRef
*ssa_defs
;
44 struct hash_table
*defs
;
45 struct hash_table
*phis
;
46 struct hash_table
*vars
;
48 LLVMValueRef main_function
;
49 LLVMBasicBlockRef continue_block
;
50 LLVMBasicBlockRef break_block
;
56 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
273 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
274 ac_to_integer(ctx
, src2
), "");
277 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
278 LLVMIntPredicate pred
,
279 LLVMValueRef src0
, LLVMValueRef src1
)
281 return LLVMBuildSelect(ctx
->builder
,
282 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
287 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
290 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
291 LLVMBuildNeg(ctx
->builder
, src0
, ""));
294 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
296 LLVMValueRef src0
, LLVMValueRef src1
)
298 LLVMTypeRef ret_type
;
299 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
301 LLVMValueRef params
[] = { src0
, src1
};
302 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
305 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
306 params
, 2, AC_FUNC_ATTR_READNONE
);
308 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
309 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
313 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
316 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
319 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
322 src0
= ac_to_float(ctx
, src0
);
323 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
324 return LLVMBuildSExt(ctx
->builder
,
325 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
329 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
333 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
338 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
341 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
344 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
345 return LLVMBuildSExt(ctx
->builder
,
346 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
350 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
354 LLVMValueRef cond
= NULL
;
356 src0
= ac_to_float(ctx
, src0
);
357 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
359 if (ctx
->chip_class
>= VI
) {
360 LLVMValueRef args
[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
363 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
364 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
367 /* need to convert back up to f32 */
368 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
370 if (ctx
->chip_class
>= VI
)
371 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
377 LLVMValueRef temp
, cond2
;
378 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
379 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
380 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
382 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
383 temp
, ctx
->f32_0
, "");
384 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
385 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
391 LLVMValueRef src0
, LLVMValueRef src1
)
393 LLVMValueRef dst64
, result
;
394 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
395 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
397 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
398 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
399 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
403 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
404 LLVMValueRef src0
, LLVMValueRef src1
)
406 LLVMValueRef dst64
, result
;
407 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
408 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
410 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
411 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
412 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
416 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
418 const LLVMValueRef srcs
[3])
422 if (HAVE_LLVM
< 0x0700) {
423 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
424 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
425 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
427 /* FIXME: LLVM 7 returns incorrect result when count is 0.
428 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
430 LLVMValueRef zero
= LLVMConstInt(ctx
->i32
, 0, false);
431 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
432 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
434 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
435 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
436 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
442 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
443 LLVMValueRef src0
, LLVMValueRef src1
,
444 LLVMValueRef src2
, LLVMValueRef src3
)
446 LLVMValueRef bfi_args
[3], result
;
448 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
449 LLVMBuildSub(ctx
->builder
,
450 LLVMBuildShl(ctx
->builder
,
455 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
458 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
461 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
462 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
464 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
465 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
466 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
468 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
472 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
475 LLVMValueRef comp
[2];
477 src0
= ac_to_float(ctx
, src0
);
478 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
479 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
481 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
485 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
488 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
489 LLVMValueRef temps
[2], result
, val
;
492 for (i
= 0; i
< 2; i
++) {
493 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
494 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
495 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
496 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
499 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
501 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
506 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
514 if (op
== nir_op_fddx_fine
)
515 mask
= AC_TID_MASK_LEFT
;
516 else if (op
== nir_op_fddy_fine
)
517 mask
= AC_TID_MASK_TOP
;
519 mask
= AC_TID_MASK_TOP_LEFT
;
521 /* for DDX we want to next X pixel, DDY next Y pixel. */
522 if (op
== nir_op_fddx_fine
||
523 op
== nir_op_fddx_coarse
||
529 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
534 * this takes an I,J coordinate pair,
535 * and works out the X and Y derivatives.
536 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
538 static LLVMValueRef
emit_ddxy_interp(
539 struct ac_nir_context
*ctx
,
540 LLVMValueRef interp_ij
)
542 LLVMValueRef result
[4], a
;
545 for (i
= 0; i
< 2; i
++) {
546 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
547 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
548 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
549 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
551 return ac_build_gather_values(&ctx
->ac
, result
, 4);
554 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
556 LLVMValueRef src
[4], result
= NULL
;
557 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
558 unsigned src_components
;
559 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
561 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
568 case nir_op_pack_half_2x16
:
571 case nir_op_unpack_half_2x16
:
574 case nir_op_cube_face_coord
:
575 case nir_op_cube_face_index
:
579 src_components
= num_components
;
582 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
583 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
591 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
592 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
595 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
598 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
601 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
604 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
605 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
606 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
609 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
610 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
611 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
614 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
617 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
620 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
623 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
626 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
627 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
628 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
629 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
630 ac_to_float_type(&ctx
->ac
, def_type
), result
);
631 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
632 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
635 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
636 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
637 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
640 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
643 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
646 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
649 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
650 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
651 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
654 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
655 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
659 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
662 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
665 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
668 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
669 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
670 LLVMTypeOf(src
[0]), ""),
674 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
675 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
676 LLVMTypeOf(src
[0]), ""),
680 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
681 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
682 LLVMTypeOf(src
[0]), ""),
686 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
689 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
692 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
701 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
704 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
707 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
710 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
713 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
716 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
717 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
720 result
= emit_iabs(&ctx
->ac
, src
[0]);
723 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
726 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
729 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
732 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
735 result
= ac_build_isign(&ctx
->ac
, src
[0],
736 instr
->dest
.dest
.ssa
.bit_size
);
739 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
740 result
= ac_build_fsign(&ctx
->ac
, src
[0],
741 instr
->dest
.dest
.ssa
.bit_size
);
744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
749 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
752 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
753 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
755 case nir_op_fround_even
:
756 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
757 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
760 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
761 result
= ac_build_fract(&ctx
->ac
, src
[0],
762 instr
->dest
.dest
.ssa
.bit_size
);
765 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
766 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
769 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
773 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
774 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
777 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
781 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
782 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
785 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
786 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
787 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
790 case nir_op_frexp_exp
:
791 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
792 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
793 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
796 case nir_op_frexp_sig
:
797 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
798 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
799 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
802 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
803 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
804 if (ctx
->ac
.chip_class
< GFX9
&&
805 instr
->dest
.dest
.ssa
.bit_size
== 32) {
806 /* Only pre-GFX9 chips do not flush denorms. */
807 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
808 ac_to_float_type(&ctx
->ac
, def_type
),
813 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
814 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
815 if (ctx
->ac
.chip_class
< GFX9
&&
816 instr
->dest
.dest
.ssa
.bit_size
== 32) {
817 /* Only pre-GFX9 chips do not flush denorms. */
818 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
819 ac_to_float_type(&ctx
->ac
, def_type
),
824 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
825 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
828 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
829 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
830 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
832 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
834 case nir_op_ibitfield_extract
:
835 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
837 case nir_op_ubitfield_extract
:
838 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
840 case nir_op_bitfield_insert
:
841 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
843 case nir_op_bitfield_reverse
:
844 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
846 case nir_op_bit_count
:
847 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
848 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
850 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
851 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
857 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
858 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
859 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
877 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
885 case nir_op_f2f16_rtz
:
886 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
887 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
888 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
889 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
891 case nir_op_f2f16_rtne
:
895 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
896 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
897 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
899 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
904 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
905 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
906 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
908 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
913 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
914 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
915 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
917 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
920 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
922 case nir_op_find_lsb
:
923 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
924 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
926 case nir_op_ufind_msb
:
927 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
928 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
930 case nir_op_ifind_msb
:
931 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
932 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
934 case nir_op_uadd_carry
:
935 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
936 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
937 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
939 case nir_op_usub_borrow
:
940 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
941 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
942 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
945 result
= emit_b2f(&ctx
->ac
, src
[0]);
948 result
= emit_f2b(&ctx
->ac
, src
[0]);
951 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
954 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
955 result
= emit_i2b(&ctx
->ac
, src
[0]);
957 case nir_op_fquantize2f16
:
958 result
= emit_f2f16(&ctx
->ac
, src
[0]);
960 case nir_op_umul_high
:
961 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
962 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
963 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
965 case nir_op_imul_high
:
966 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
967 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
968 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
970 case nir_op_pack_half_2x16
:
971 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
973 case nir_op_unpack_half_2x16
:
974 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
978 case nir_op_fddx_fine
:
979 case nir_op_fddy_fine
:
980 case nir_op_fddx_coarse
:
981 case nir_op_fddy_coarse
:
982 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
985 case nir_op_unpack_64_2x32_split_x
: {
986 assert(ac_get_llvm_num_components(src
[0]) == 1);
987 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
990 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
995 case nir_op_unpack_64_2x32_split_y
: {
996 assert(ac_get_llvm_num_components(src
[0]) == 1);
997 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1000 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1005 case nir_op_pack_64_2x32_split
: {
1006 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1007 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1008 src
[0], ctx
->ac
.i32_0
, "");
1009 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1010 src
[1], ctx
->ac
.i32_1
, "");
1011 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1015 case nir_op_cube_face_coord
: {
1016 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1017 LLVMValueRef results
[2];
1019 for (unsigned chan
= 0; chan
< 3; chan
++)
1020 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1021 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1022 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1023 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1024 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1025 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1029 case nir_op_cube_face_index
: {
1030 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1032 for (unsigned chan
= 0; chan
< 3; chan
++)
1033 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1034 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1035 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1040 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1041 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1042 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1043 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1046 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1047 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1050 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1051 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1054 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1055 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1056 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1057 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1060 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1061 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1064 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1065 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1067 case nir_op_fmed3
: {
1068 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1069 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1070 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1071 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1072 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1073 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1074 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1075 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1078 case nir_op_imed3
: {
1079 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1080 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1081 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1082 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1085 case nir_op_umed3
: {
1086 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1087 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1088 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1089 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1094 fprintf(stderr
, "Unknown NIR alu instr: ");
1095 nir_print_instr(&instr
->instr
, stderr
);
1096 fprintf(stderr
, "\n");
1101 assert(instr
->dest
.dest
.is_ssa
);
1102 result
= ac_to_integer(&ctx
->ac
, result
);
1103 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1107 static void visit_load_const(struct ac_nir_context
*ctx
,
1108 const nir_load_const_instr
*instr
)
1110 LLVMValueRef values
[4], value
= NULL
;
1111 LLVMTypeRef element_type
=
1112 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1114 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1115 switch (instr
->def
.bit_size
) {
1117 values
[i
] = LLVMConstInt(element_type
,
1118 instr
->value
.u16
[i
], false);
1121 values
[i
] = LLVMConstInt(element_type
,
1122 instr
->value
.u32
[i
], false);
1125 values
[i
] = LLVMConstInt(element_type
,
1126 instr
->value
.u64
[i
], false);
1130 "unsupported nir load_const bit_size: %d\n",
1131 instr
->def
.bit_size
);
1135 if (instr
->def
.num_components
> 1) {
1136 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1140 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1144 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1147 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1148 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1151 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1152 /* On VI, the descriptor contains the size in bytes,
1153 * but TXQ must return the size in elements.
1154 * The stride is always non-zero for resources using TXQ.
1156 LLVMValueRef stride
=
1157 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1159 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1160 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1161 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1162 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1164 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1169 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1171 struct ac_image_args
*args
,
1172 const nir_tex_instr
*instr
)
1174 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1175 LLVMValueRef half_texel
[2];
1176 LLVMValueRef compare_cube_wa
= NULL
;
1177 LLVMValueRef result
;
1181 struct ac_image_args txq_args
= { 0 };
1183 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1184 txq_args
.opcode
= ac_image_get_resinfo
;
1185 txq_args
.dmask
= 0xf;
1186 txq_args
.lod
= ctx
->i32_0
;
1187 txq_args
.resource
= args
->resource
;
1188 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1189 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1191 for (unsigned c
= 0; c
< 2; c
++) {
1192 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1193 LLVMConstInt(ctx
->i32
, c
, false), "");
1194 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1195 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1196 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1197 LLVMConstReal(ctx
->f32
, -0.5), "");
1201 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1203 for (unsigned c
= 0; c
< 2; c
++) {
1205 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1206 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1210 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1211 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1212 * workaround by sampling using a scaled type and converting.
1213 * This is taken from amdgpu-pro shaders.
1215 /* NOTE this produces some ugly code compared to amdgpu-pro,
1216 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1217 * and then reads them back. -pro generates two selects,
1218 * one s_cmp for the descriptor rewriting
1219 * one v_cmp for the coordinate and result changes.
1221 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1222 LLVMValueRef tmp
, tmp2
;
1224 /* workaround 8/8/8/8 uint/sint cube gather bug */
1225 /* first detect it then change to a scaled read and f2i */
1226 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1229 /* extract the DATA_FORMAT */
1230 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1231 LLVMConstInt(ctx
->i32
, 6, false), false);
1233 /* is the DATA_FORMAT == 8_8_8_8 */
1234 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1236 if (stype
== GLSL_TYPE_UINT
)
1237 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1238 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1239 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1241 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1242 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1243 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1245 /* replace the NUM FORMAT in the descriptor */
1246 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1247 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1249 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1251 /* don't modify the coordinates for this case */
1252 for (unsigned c
= 0; c
< 2; ++c
)
1253 args
->coords
[c
] = LLVMBuildSelect(
1254 ctx
->builder
, compare_cube_wa
,
1255 orig_coords
[c
], args
->coords
[c
], "");
1258 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1259 result
= ac_build_image_opcode(ctx
, args
);
1261 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1262 LLVMValueRef tmp
, tmp2
;
1264 /* if the cube workaround is in place, f2i the result. */
1265 for (unsigned c
= 0; c
< 4; c
++) {
1266 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1267 if (stype
== GLSL_TYPE_UINT
)
1268 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1270 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1271 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1272 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1273 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1274 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1275 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1281 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1283 nir_deref_instr
*texture_deref_instr
= NULL
;
1285 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1286 switch (instr
->src
[i
].src_type
) {
1287 case nir_tex_src_texture_deref
:
1288 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1294 return texture_deref_instr
;
1297 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1298 const nir_tex_instr
*instr
,
1299 struct ac_image_args
*args
)
1301 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1302 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1304 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1305 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1309 util_last_bit(mask
),
1312 return ac_build_buffer_load_format(&ctx
->ac
,
1316 util_last_bit(mask
),
1321 args
->opcode
= ac_image_sample
;
1323 switch (instr
->op
) {
1325 case nir_texop_txf_ms
:
1326 case nir_texop_samples_identical
:
1327 args
->opcode
= args
->level_zero
||
1328 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1329 ac_image_load
: ac_image_load_mip
;
1330 args
->level_zero
= false;
1333 case nir_texop_query_levels
:
1334 args
->opcode
= ac_image_get_resinfo
;
1336 args
->lod
= ctx
->ac
.i32_0
;
1337 args
->level_zero
= false;
1340 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1342 args
->level_zero
= true;
1346 args
->opcode
= ac_image_gather4
;
1347 args
->level_zero
= true;
1350 args
->opcode
= ac_image_get_lod
;
1356 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1357 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1358 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1359 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1360 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1361 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1365 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1366 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1367 if ((args
->dim
== ac_image_2darray
||
1368 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1369 args
->coords
[1] = ctx
->ac
.i32_0
;
1373 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1374 return ac_build_image_opcode(&ctx
->ac
, args
);
1377 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1378 nir_intrinsic_instr
*instr
)
1380 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1381 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1383 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1384 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1388 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1389 nir_intrinsic_instr
*instr
)
1391 LLVMValueRef ptr
, addr
;
1393 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1394 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1395 get_src(ctx
, instr
->src
[0]), "");
1397 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1399 if (instr
->dest
.ssa
.bit_size
== 16) {
1400 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1401 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16Type(), 2 * load_dwords
);
1402 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1403 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1404 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1405 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1406 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, LLVMInt1Type(), "");
1407 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1408 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1409 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1410 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1411 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1412 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1413 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1414 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1415 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1418 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1420 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1423 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1424 const nir_intrinsic_instr
*instr
)
1426 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1428 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1431 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1433 uint32_t new_mask
= 0;
1434 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1435 if (mask
& (1u << i
))
1436 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1440 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1441 unsigned start
, unsigned count
)
1443 LLVMValueRef mask
[] = {
1444 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1445 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1447 unsigned src_elements
= ac_get_llvm_num_components(src
);
1449 if (count
== src_elements
) {
1452 } else if (count
== 1) {
1453 assert(start
< src_elements
);
1454 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1456 assert(start
+ count
<= src_elements
);
1458 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1459 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1463 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1464 nir_intrinsic_instr
*instr
)
1466 const char *store_name
;
1467 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1468 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1469 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1471 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1472 get_src(ctx
, instr
->src
[1]), true);
1473 LLVMValueRef base_data
= ac_to_float(&ctx
->ac
, src_data
);
1474 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1475 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1479 LLVMValueRef data
, offset
;
1480 LLVMTypeRef data_type
;
1482 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1484 /* Due to an LLVM limitation, split 3-element writes
1485 * into a 2-element and a 1-element write. */
1487 writemask
|= 1 << (start
+ 2);
1490 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1492 /* we can only store 4 DWords at the same time.
1493 * can only happen for 64 Bit vectors. */
1494 if (num_bytes
> 16) {
1495 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1500 /* check alignment of 16 Bit stores */
1501 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1502 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1506 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1509 offset
= base_offset
;
1511 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1512 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1514 if (num_bytes
== 2) {
1515 store_name
= "llvm.amdgcn.tbuffer.store.i32";
1516 data_type
= ctx
->ac
.i32
;
1517 LLVMValueRef tbuffer_params
[] = {
1520 ctx
->ac
.i32_0
, /* vindex */
1521 offset
, /* voffset */
1524 LLVMConstInt(ctx
->ac
.i32
, 2, false), // dfmt (= 16bit)
1525 LLVMConstInt(ctx
->ac
.i32
, 4, false), // nfmt (= uint)
1529 ac_build_intrinsic(&ctx
->ac
, store_name
,
1530 ctx
->ac
.voidt
, tbuffer_params
, 10, 0);
1532 switch (num_bytes
) {
1533 case 16: /* v4f32 */
1534 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1535 data_type
= ctx
->ac
.v4f32
;
1538 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1539 data_type
= ctx
->ac
.v2f32
;
1542 store_name
= "llvm.amdgcn.buffer.store.f32";
1543 data_type
= ctx
->ac
.f32
;
1546 unreachable("Malformed vector store.");
1548 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1549 LLVMValueRef params
[] = {
1552 ctx
->ac
.i32_0
, /* vindex */
1554 ctx
->ac
.i1false
, /* glc */
1555 ctx
->ac
.i1false
, /* slc */
1557 ac_build_intrinsic(&ctx
->ac
, store_name
,
1558 ctx
->ac
.voidt
, params
, 6, 0);
1563 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1564 const nir_intrinsic_instr
*instr
)
1567 LLVMValueRef params
[6];
1570 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1571 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1573 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1574 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1575 get_src(ctx
, instr
->src
[0]),
1577 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1578 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1579 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1581 switch (instr
->intrinsic
) {
1582 case nir_intrinsic_ssbo_atomic_add
:
1583 name
= "llvm.amdgcn.buffer.atomic.add";
1585 case nir_intrinsic_ssbo_atomic_imin
:
1586 name
= "llvm.amdgcn.buffer.atomic.smin";
1588 case nir_intrinsic_ssbo_atomic_umin
:
1589 name
= "llvm.amdgcn.buffer.atomic.umin";
1591 case nir_intrinsic_ssbo_atomic_imax
:
1592 name
= "llvm.amdgcn.buffer.atomic.smax";
1594 case nir_intrinsic_ssbo_atomic_umax
:
1595 name
= "llvm.amdgcn.buffer.atomic.umax";
1597 case nir_intrinsic_ssbo_atomic_and
:
1598 name
= "llvm.amdgcn.buffer.atomic.and";
1600 case nir_intrinsic_ssbo_atomic_or
:
1601 name
= "llvm.amdgcn.buffer.atomic.or";
1603 case nir_intrinsic_ssbo_atomic_xor
:
1604 name
= "llvm.amdgcn.buffer.atomic.xor";
1606 case nir_intrinsic_ssbo_atomic_exchange
:
1607 name
= "llvm.amdgcn.buffer.atomic.swap";
1609 case nir_intrinsic_ssbo_atomic_comp_swap
:
1610 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1616 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1619 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1620 const nir_intrinsic_instr
*instr
)
1622 LLVMValueRef results
[2];
1624 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1625 int num_components
= instr
->num_components
;
1626 int num_bytes
= num_components
* elem_size_bytes
;
1628 for (int i
= 0; i
< num_bytes
; i
+= load_bytes
) {
1629 load_bytes
= MIN2(num_bytes
- i
, 16);
1630 const char *load_name
;
1631 LLVMTypeRef data_type
;
1632 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1633 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
1634 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1635 get_src(ctx
, instr
->src
[0]), false);
1636 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1638 int idx
= i
? 1 : 0;
1639 if (load_bytes
== 2) {
1640 results
[idx
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1647 switch (load_bytes
) {
1650 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1651 data_type
= ctx
->ac
.v4f32
;
1655 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1656 data_type
= ctx
->ac
.v2f32
;
1659 load_name
= "llvm.amdgcn.buffer.load.f32";
1660 data_type
= ctx
->ac
.f32
;
1663 unreachable("Malformed load buffer.");
1665 LLVMValueRef params
[] = {
1668 LLVMBuildAdd(ctx
->ac
.builder
, offset
, immoffset
, ""),
1672 results
[idx
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1673 unsigned num_elems
= ac_get_type_size(data_type
) / elem_size_bytes
;
1674 LLVMTypeRef resTy
= LLVMVectorType(LLVMIntType(instr
->dest
.ssa
.bit_size
), num_elems
);
1675 results
[idx
] = LLVMBuildBitCast(ctx
->ac
.builder
, results
[idx
], resTy
, "");
1680 LLVMValueRef ret
= results
[0];
1681 if (num_bytes
> 16 || num_components
== 3) {
1682 LLVMValueRef masks
[] = {
1683 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1684 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1687 if (num_bytes
> 16 && num_components
== 3) {
1688 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1689 results
[1] = ac_build_expand_to_vec4(&ctx
->ac
, results
[1], 2);
1692 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1693 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1694 results
[num_bytes
> 16 ? 1 : 0], swizzle
, "");
1697 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1698 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1701 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1702 const nir_intrinsic_instr
*instr
)
1705 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1706 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1707 int num_components
= instr
->num_components
;
1709 if (ctx
->abi
->load_ubo
)
1710 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1712 if (instr
->dest
.ssa
.bit_size
== 64)
1713 num_components
*= 2;
1715 if (instr
->dest
.ssa
.bit_size
== 16) {
1716 LLVMValueRef results
[num_components
];
1717 for (unsigned i
= 0; i
< num_components
; ++i
) {
1718 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1723 LLVMConstInt(ctx
->ac
.i32
, 2 * i
, 0));
1725 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1727 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1728 NULL
, 0, false, false, true, true);
1730 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1733 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1734 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1738 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1739 bool vs_in
, unsigned *vertex_index_out
,
1740 LLVMValueRef
*vertex_index_ref
,
1741 unsigned *const_out
, LLVMValueRef
*indir_out
)
1743 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1744 nir_deref_path path
;
1745 unsigned idx_lvl
= 1;
1747 nir_deref_path_init(&path
, instr
, NULL
);
1749 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1750 if (vertex_index_ref
) {
1751 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1752 if (vertex_index_out
)
1753 *vertex_index_out
= 0;
1755 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1757 *vertex_index_out
= v
->u32
[0];
1762 uint32_t const_offset
= 0;
1763 LLVMValueRef offset
= NULL
;
1765 if (var
->data
.compact
) {
1766 assert(instr
->deref_type
== nir_deref_type_array
);
1767 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1769 const_offset
= v
->u32
[0];
1773 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1774 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1775 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1776 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1778 for (unsigned i
= 0; i
< index
; i
++) {
1779 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1780 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1782 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1783 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1784 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1785 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1787 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1791 unreachable("Uhandled deref type in get_deref_instr_offset");
1795 nir_deref_path_finish(&path
);
1797 if (const_offset
&& offset
)
1798 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1799 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1802 *const_out
= const_offset
;
1803 *indir_out
= offset
;
1806 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1807 nir_intrinsic_instr
*instr
,
1810 LLVMValueRef result
;
1811 LLVMValueRef vertex_index
= NULL
;
1812 LLVMValueRef indir_index
= NULL
;
1813 unsigned const_index
= 0;
1815 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1817 unsigned location
= var
->data
.location
;
1818 unsigned driver_location
= var
->data
.driver_location
;
1819 const bool is_patch
= var
->data
.patch
;
1820 const bool is_compact
= var
->data
.compact
;
1822 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1823 false, NULL
, is_patch
? NULL
: &vertex_index
,
1824 &const_index
, &indir_index
);
1826 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1828 LLVMTypeRef src_component_type
;
1829 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1830 src_component_type
= LLVMGetElementType(dest_type
);
1832 src_component_type
= dest_type
;
1834 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1835 vertex_index
, indir_index
,
1836 const_index
, location
, driver_location
,
1837 var
->data
.location_frac
,
1838 instr
->num_components
,
1839 is_patch
, is_compact
, load_inputs
);
1840 if (instr
->dest
.ssa
.bit_size
== 16) {
1841 result
= ac_to_integer(&ctx
->ac
, result
);
1842 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1844 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1847 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1848 nir_intrinsic_instr
*instr
)
1850 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1852 LLVMValueRef values
[8];
1853 int idx
= var
->data
.driver_location
;
1854 int ve
= instr
->dest
.ssa
.num_components
;
1855 unsigned comp
= var
->data
.location_frac
;
1856 LLVMValueRef indir_index
;
1858 unsigned const_index
;
1859 unsigned stride
= var
->data
.compact
? 1 : 4;
1860 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1861 var
->data
.mode
== nir_var_shader_in
;
1863 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), vs_in
, NULL
, NULL
,
1864 &const_index
, &indir_index
);
1866 if (instr
->dest
.ssa
.bit_size
== 64)
1869 switch (var
->data
.mode
) {
1870 case nir_var_shader_in
:
1871 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1872 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1873 return load_tess_varyings(ctx
, instr
, true);
1876 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1877 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1878 LLVMValueRef indir_index
;
1879 unsigned const_index
, vertex_index
;
1880 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1881 false, &vertex_index
, NULL
, &const_index
, &indir_index
);
1883 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1884 var
->data
.driver_location
,
1885 var
->data
.location_frac
,
1886 instr
->num_components
, vertex_index
, const_index
, type
);
1889 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1891 unsigned count
= glsl_count_attribute_slots(
1893 ctx
->stage
== MESA_SHADER_VERTEX
);
1895 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1896 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1897 stride
, false, true);
1899 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1903 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1907 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1909 unsigned count
= glsl_count_attribute_slots(
1912 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1913 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1914 stride
, true, true);
1916 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1920 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1924 case nir_var_shared
: {
1925 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
1926 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1927 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1928 get_def_type(ctx
, &instr
->dest
.ssa
),
1931 case nir_var_shader_out
:
1932 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1933 return load_tess_varyings(ctx
, instr
, false);
1936 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1938 unsigned count
= glsl_count_attribute_slots(
1941 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1942 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1943 stride
, true, true);
1945 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1949 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1950 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1956 unreachable("unhandle variable mode");
1958 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1959 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1963 visit_store_var(struct ac_nir_context
*ctx
,
1964 nir_intrinsic_instr
*instr
)
1966 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1968 LLVMValueRef temp_ptr
, value
;
1969 int idx
= var
->data
.driver_location
;
1970 unsigned comp
= var
->data
.location_frac
;
1971 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
1972 int writemask
= instr
->const_index
[0];
1973 LLVMValueRef indir_index
;
1974 unsigned const_index
;
1976 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), false,
1977 NULL
, NULL
, &const_index
, &indir_index
);
1979 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1981 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1982 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1985 writemask
= widen_mask(writemask
, 2);
1988 writemask
= writemask
<< comp
;
1990 switch (var
->data
.mode
) {
1991 case nir_var_shader_out
:
1993 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1994 LLVMValueRef vertex_index
= NULL
;
1995 LLVMValueRef indir_index
= NULL
;
1996 unsigned const_index
= 0;
1997 const bool is_patch
= var
->data
.patch
;
1999 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2000 false, NULL
, is_patch
? NULL
: &vertex_index
,
2001 &const_index
, &indir_index
);
2003 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2004 vertex_index
, indir_index
,
2005 const_index
, src
, writemask
);
2009 for (unsigned chan
= 0; chan
< 8; chan
++) {
2011 if (!(writemask
& (1 << chan
)))
2014 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2016 if (var
->data
.compact
)
2019 unsigned count
= glsl_count_attribute_slots(
2022 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2023 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2024 stride
, true, true);
2026 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2027 value
, indir_index
, "");
2028 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2029 count
, stride
, tmp_vec
);
2032 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2034 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2039 for (unsigned chan
= 0; chan
< 8; chan
++) {
2040 if (!(writemask
& (1 << chan
)))
2043 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2045 unsigned count
= glsl_count_attribute_slots(
2048 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2049 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2052 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2053 value
, indir_index
, "");
2054 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2057 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2059 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2063 case nir_var_shared
: {
2064 int writemask
= instr
->const_index
[0];
2065 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2066 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2067 if (util_is_power_of_two_nonzero(writemask
)) {
2068 val
= LLVMBuildBitCast(
2069 ctx
->ac
.builder
, val
,
2070 LLVMGetElementType(LLVMTypeOf(address
)), "");
2071 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2073 for (unsigned chan
= 0; chan
< 4; chan
++) {
2074 if (!(writemask
& (1 << chan
)))
2077 LLVMBuildStructGEP(ctx
->ac
.builder
,
2079 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2081 src
= LLVMBuildBitCast(
2082 ctx
->ac
.builder
, src
,
2083 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2084 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2094 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2097 case GLSL_SAMPLER_DIM_BUF
:
2099 case GLSL_SAMPLER_DIM_1D
:
2100 return array
? 2 : 1;
2101 case GLSL_SAMPLER_DIM_2D
:
2102 return array
? 3 : 2;
2103 case GLSL_SAMPLER_DIM_MS
:
2104 return array
? 4 : 3;
2105 case GLSL_SAMPLER_DIM_3D
:
2106 case GLSL_SAMPLER_DIM_CUBE
:
2108 case GLSL_SAMPLER_DIM_RECT
:
2109 case GLSL_SAMPLER_DIM_SUBPASS
:
2111 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2120 /* Adjust the sample index according to FMASK.
2122 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2123 * which is the identity mapping. Each nibble says which physical sample
2124 * should be fetched to get that sample.
2126 * For example, 0x11111100 means there are only 2 samples stored and
2127 * the second sample covers 3/4 of the pixel. When reading samples 0
2128 * and 1, return physical sample 0 (determined by the first two 0s
2129 * in FMASK), otherwise return physical sample 1.
2131 * The sample index should be adjusted as follows:
2132 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2134 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2135 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2136 LLVMValueRef coord_z
,
2137 LLVMValueRef sample_index
,
2138 LLVMValueRef fmask_desc_ptr
)
2140 struct ac_image_args args
= {0};
2143 args
.coords
[0] = coord_x
;
2144 args
.coords
[1] = coord_y
;
2146 args
.coords
[2] = coord_z
;
2148 args
.opcode
= ac_image_load
;
2149 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2150 args
.resource
= fmask_desc_ptr
;
2152 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2154 res
= ac_build_image_opcode(ctx
, &args
);
2156 res
= ac_to_integer(ctx
, res
);
2157 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2158 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2160 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2164 LLVMValueRef sample_index4
=
2165 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2166 LLVMValueRef shifted_fmask
=
2167 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2168 LLVMValueRef final_sample
=
2169 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2171 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2172 * resource descriptor is 0 (invalid),
2174 LLVMValueRef fmask_desc
=
2175 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2178 LLVMValueRef fmask_word1
=
2179 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2182 LLVMValueRef word1_is_nonzero
=
2183 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2184 fmask_word1
, ctx
->i32_0
, "");
2186 /* Replace the MSAA sample index. */
2188 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2189 final_sample
, sample_index
, "");
2190 return sample_index
;
2193 static nir_variable
*get_image_variable(const nir_intrinsic_instr
*instr
)
2195 assert(instr
->src
[0].is_ssa
);
2196 return nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2199 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2200 const nir_intrinsic_instr
*instr
,
2201 enum ac_descriptor_type desc_type
,
2204 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2207 static void get_image_coords(struct ac_nir_context
*ctx
,
2208 const nir_intrinsic_instr
*instr
,
2209 struct ac_image_args
*args
)
2211 const struct glsl_type
*type
= glsl_without_array(get_image_variable(instr
)->type
);
2213 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2214 LLVMValueRef masks
[] = {
2215 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2216 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2218 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2221 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2222 bool is_array
= glsl_sampler_type_is_array(type
);
2223 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2224 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2225 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2226 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2227 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2228 count
= image_type_to_components_count(dim
, is_array
);
2231 LLVMValueRef fmask_load_address
[3];
2234 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2235 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2237 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2239 fmask_load_address
[2] = NULL
;
2241 for (chan
= 0; chan
< 2; ++chan
)
2242 fmask_load_address
[chan
] =
2243 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2244 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2245 ctx
->ac
.i32
, ""), "");
2246 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2248 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2249 fmask_load_address
[0],
2250 fmask_load_address
[1],
2251 fmask_load_address
[2],
2253 get_image_descriptor(ctx
, instr
, AC_DESC_FMASK
, false));
2255 if (count
== 1 && !gfx9_1d
) {
2256 if (instr
->src
[1].ssa
->num_components
)
2257 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2259 args
->coords
[0] = src0
;
2264 for (chan
= 0; chan
< count
; ++chan
) {
2265 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2268 for (chan
= 0; chan
< 2; ++chan
) {
2269 args
->coords
[chan
] = LLVMBuildAdd(
2270 ctx
->ac
.builder
, args
->coords
[chan
],
2272 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2273 ctx
->ac
.i32
, ""), "");
2275 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2276 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2282 args
->coords
[2] = args
->coords
[1];
2283 args
->coords
[1] = ctx
->ac
.i32_0
;
2285 args
->coords
[1] = ctx
->ac
.i32_0
;
2290 args
->coords
[count
] = sample_index
;
2296 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2297 const nir_intrinsic_instr
*instr
, bool write
)
2299 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2300 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2301 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2302 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2303 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2305 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2306 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2307 elem_count
, stride
, "");
2309 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2310 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2315 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2316 const nir_intrinsic_instr
*instr
)
2319 const nir_variable
*var
= get_image_variable(instr
);
2320 const struct glsl_type
*type
= var
->type
;
2322 type
= glsl_without_array(type
);
2324 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2325 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2326 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2327 unsigned num_channels
= util_last_bit(mask
);
2328 LLVMValueRef rsrc
, vindex
;
2330 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2331 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2334 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2335 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2336 ctx
->ac
.i32_0
, num_channels
,
2338 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2340 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2341 res
= ac_to_integer(&ctx
->ac
, res
);
2343 struct ac_image_args args
= {};
2344 args
.opcode
= ac_image_load
;
2345 get_image_coords(ctx
, instr
, &args
);
2346 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2347 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2348 glsl_sampler_type_is_array(type
));
2350 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2351 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2352 args
.cache_policy
|= ac_glc
;
2354 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2356 return ac_to_integer(&ctx
->ac
, res
);
2359 static void visit_image_store(struct ac_nir_context
*ctx
,
2360 nir_intrinsic_instr
*instr
)
2362 LLVMValueRef params
[8];
2363 const nir_variable
*var
= get_image_variable(instr
);
2364 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2365 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2366 LLVMValueRef glc
= ctx
->ac
.i1false
;
2367 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2369 glc
= ctx
->ac
.i1true
;
2371 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2372 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2374 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3])); /* data */
2376 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2377 ctx
->ac
.i32_0
, ""); /* vindex */
2378 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2379 params
[4] = glc
; /* glc */
2380 params
[5] = ctx
->ac
.i1false
; /* slc */
2381 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2384 struct ac_image_args args
= {};
2385 args
.opcode
= ac_image_store
;
2386 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2387 get_image_coords(ctx
, instr
, &args
);
2388 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2389 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2390 glsl_sampler_type_is_array(type
));
2392 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2393 args
.cache_policy
|= ac_glc
;
2395 ac_build_image_opcode(&ctx
->ac
, &args
);
2400 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2401 const nir_intrinsic_instr
*instr
)
2403 LLVMValueRef params
[7];
2404 int param_count
= 0;
2405 const nir_variable
*var
= get_image_variable(instr
);
2407 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2408 const char *atomic_name
;
2409 char intrinsic_name
[41];
2410 enum ac_atomic_op atomic_subop
;
2411 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2412 MAYBE_UNUSED
int length
;
2414 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2416 switch (instr
->intrinsic
) {
2417 case nir_intrinsic_image_deref_atomic_add
:
2418 atomic_name
= "add";
2419 atomic_subop
= ac_atomic_add
;
2421 case nir_intrinsic_image_deref_atomic_min
:
2422 atomic_name
= is_unsigned
? "umin" : "smin";
2423 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2425 case nir_intrinsic_image_deref_atomic_max
:
2426 atomic_name
= is_unsigned
? "umax" : "smax";
2427 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2429 case nir_intrinsic_image_deref_atomic_and
:
2430 atomic_name
= "and";
2431 atomic_subop
= ac_atomic_and
;
2433 case nir_intrinsic_image_deref_atomic_or
:
2435 atomic_subop
= ac_atomic_or
;
2437 case nir_intrinsic_image_deref_atomic_xor
:
2438 atomic_name
= "xor";
2439 atomic_subop
= ac_atomic_xor
;
2441 case nir_intrinsic_image_deref_atomic_exchange
:
2442 atomic_name
= "swap";
2443 atomic_subop
= ac_atomic_swap
;
2445 case nir_intrinsic_image_deref_atomic_comp_swap
:
2446 atomic_name
= "cmpswap";
2447 atomic_subop
= 0; /* not used */
2454 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2455 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2457 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2458 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2459 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2460 ctx
->ac
.i32_0
, ""); /* vindex */
2461 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2462 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2464 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2465 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2467 assert(length
< sizeof(intrinsic_name
));
2468 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2469 params
, param_count
, 0);
2471 struct ac_image_args args
= {};
2472 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2473 args
.atomic
= atomic_subop
;
2474 args
.data
[0] = params
[0];
2476 args
.data
[1] = params
[1];
2477 get_image_coords(ctx
, instr
, &args
);
2478 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2479 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2480 glsl_sampler_type_is_array(type
));
2482 return ac_build_image_opcode(&ctx
->ac
, &args
);
2486 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2487 const nir_intrinsic_instr
*instr
)
2489 const nir_variable
*var
= get_image_variable(instr
);
2490 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2492 struct ac_image_args args
= { 0 };
2493 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2494 glsl_sampler_type_is_array(type
));
2496 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2497 args
.opcode
= ac_image_get_resinfo
;
2498 args
.lod
= ctx
->ac
.i32_0
;
2499 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2501 return ac_build_image_opcode(&ctx
->ac
, &args
);
2504 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2505 const nir_intrinsic_instr
*instr
)
2508 const nir_variable
*var
= get_image_variable(instr
);
2509 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2511 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2512 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2514 struct ac_image_args args
= { 0 };
2516 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2517 glsl_sampler_type_is_array(type
));
2519 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2520 args
.opcode
= ac_image_get_resinfo
;
2521 args
.lod
= ctx
->ac
.i32_0
;
2522 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2524 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2526 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2528 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2529 glsl_sampler_type_is_array(type
)) {
2530 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2531 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2532 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2533 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2535 if (ctx
->ac
.chip_class
>= GFX9
&&
2536 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2537 glsl_sampler_type_is_array(type
)) {
2538 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2539 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2546 #define NOOP_WAITCNT 0xf7f
2547 #define LGKM_CNT 0x07f
2548 #define VM_CNT 0xf70
2550 static void emit_membar(struct ac_llvm_context
*ac
,
2551 const nir_intrinsic_instr
*instr
)
2553 unsigned waitcnt
= NOOP_WAITCNT
;
2555 switch (instr
->intrinsic
) {
2556 case nir_intrinsic_memory_barrier
:
2557 case nir_intrinsic_group_memory_barrier
:
2558 waitcnt
&= VM_CNT
& LGKM_CNT
;
2560 case nir_intrinsic_memory_barrier_atomic_counter
:
2561 case nir_intrinsic_memory_barrier_buffer
:
2562 case nir_intrinsic_memory_barrier_image
:
2565 case nir_intrinsic_memory_barrier_shared
:
2566 waitcnt
&= LGKM_CNT
;
2571 if (waitcnt
!= NOOP_WAITCNT
)
2572 ac_build_waitcnt(ac
, waitcnt
);
2575 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2577 /* SI only (thanks to a hw bug workaround):
2578 * The real barrier instruction isn’t needed, because an entire patch
2579 * always fits into a single wave.
2581 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2582 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2585 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2586 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2589 static void emit_discard(struct ac_nir_context
*ctx
,
2590 const nir_intrinsic_instr
*instr
)
2594 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2595 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2596 get_src(ctx
, instr
->src
[0]),
2599 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2600 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2603 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2607 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2609 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2610 "llvm.amdgcn.ps.live",
2611 ctx
->ac
.i1
, NULL
, 0,
2612 AC_FUNC_ATTR_READNONE
);
2613 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2614 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2618 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2620 LLVMValueRef result
;
2621 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2622 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2623 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2625 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2629 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2631 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2632 LLVMValueRef result
;
2633 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2634 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2635 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2637 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2642 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2644 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2645 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2646 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2648 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2653 visit_first_invocation(struct ac_nir_context
*ctx
)
2655 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2657 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2658 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2659 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2661 ctx
->ac
.i64
, args
, 2,
2662 AC_FUNC_ATTR_NOUNWIND
|
2663 AC_FUNC_ATTR_READNONE
);
2665 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2669 visit_load_shared(struct ac_nir_context
*ctx
,
2670 const nir_intrinsic_instr
*instr
)
2672 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2674 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2676 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2677 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2678 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2679 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2682 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2683 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2687 visit_store_shared(struct ac_nir_context
*ctx
,
2688 const nir_intrinsic_instr
*instr
)
2690 LLVMValueRef derived_ptr
, data
,index
;
2691 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2693 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2694 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2696 int writemask
= nir_intrinsic_write_mask(instr
);
2697 for (int chan
= 0; chan
< 4; chan
++) {
2698 if (!(writemask
& (1 << chan
))) {
2701 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2702 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2703 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2704 LLVMBuildStore(builder
, data
, derived_ptr
);
2708 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2709 const nir_intrinsic_instr
*instr
,
2710 LLVMValueRef ptr
, int src_idx
)
2712 LLVMValueRef result
;
2713 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2715 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2716 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2717 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2718 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2720 LLVMAtomicOrderingSequentiallyConsistent
,
2721 LLVMAtomicOrderingSequentiallyConsistent
,
2723 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2725 LLVMAtomicRMWBinOp op
;
2726 switch (instr
->intrinsic
) {
2727 case nir_intrinsic_shared_atomic_add
:
2728 case nir_intrinsic_deref_atomic_add
:
2729 op
= LLVMAtomicRMWBinOpAdd
;
2731 case nir_intrinsic_shared_atomic_umin
:
2732 case nir_intrinsic_deref_atomic_umin
:
2733 op
= LLVMAtomicRMWBinOpUMin
;
2735 case nir_intrinsic_shared_atomic_umax
:
2736 case nir_intrinsic_deref_atomic_umax
:
2737 op
= LLVMAtomicRMWBinOpUMax
;
2739 case nir_intrinsic_shared_atomic_imin
:
2740 case nir_intrinsic_deref_atomic_imin
:
2741 op
= LLVMAtomicRMWBinOpMin
;
2743 case nir_intrinsic_shared_atomic_imax
:
2744 case nir_intrinsic_deref_atomic_imax
:
2745 op
= LLVMAtomicRMWBinOpMax
;
2747 case nir_intrinsic_shared_atomic_and
:
2748 case nir_intrinsic_deref_atomic_and
:
2749 op
= LLVMAtomicRMWBinOpAnd
;
2751 case nir_intrinsic_shared_atomic_or
:
2752 case nir_intrinsic_deref_atomic_or
:
2753 op
= LLVMAtomicRMWBinOpOr
;
2755 case nir_intrinsic_shared_atomic_xor
:
2756 case nir_intrinsic_deref_atomic_xor
:
2757 op
= LLVMAtomicRMWBinOpXor
;
2759 case nir_intrinsic_shared_atomic_exchange
:
2760 case nir_intrinsic_deref_atomic_exchange
:
2761 op
= LLVMAtomicRMWBinOpXchg
;
2767 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2768 LLVMAtomicOrderingSequentiallyConsistent
,
2774 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2776 LLVMValueRef values
[2];
2777 LLVMValueRef pos
[2];
2779 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2780 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2782 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2783 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2784 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2787 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2788 const nir_intrinsic_instr
*instr
)
2790 LLVMValueRef result
[4];
2791 LLVMValueRef interp_param
, attr_number
;
2794 LLVMValueRef src_c0
= NULL
;
2795 LLVMValueRef src_c1
= NULL
;
2796 LLVMValueRef src0
= NULL
;
2798 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2799 int input_index
= var
->data
.location
- VARYING_SLOT_VAR0
;
2800 switch (instr
->intrinsic
) {
2801 case nir_intrinsic_interp_deref_at_centroid
:
2802 location
= INTERP_CENTROID
;
2804 case nir_intrinsic_interp_deref_at_sample
:
2805 case nir_intrinsic_interp_deref_at_offset
:
2806 location
= INTERP_CENTER
;
2807 src0
= get_src(ctx
, instr
->src
[1]);
2813 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2814 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2815 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2816 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2817 LLVMValueRef sample_position
;
2818 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2820 /* fetch sample ID */
2821 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2823 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2824 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2825 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2826 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2828 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
2829 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2831 if (location
== INTERP_CENTER
) {
2832 LLVMValueRef ij_out
[2];
2833 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2836 * take the I then J parameters, and the DDX/Y for it, and
2837 * calculate the IJ inputs for the interpolator.
2838 * temp1 = ddx * offset/sample.x + I;
2839 * interp_param.I = ddy * offset/sample.y + temp1;
2840 * temp1 = ddx * offset/sample.x + J;
2841 * interp_param.J = ddy * offset/sample.y + temp1;
2843 for (unsigned i
= 0; i
< 2; i
++) {
2844 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2845 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2846 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2847 ddxy_out
, ix_ll
, "");
2848 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2849 ddxy_out
, iy_ll
, "");
2850 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2851 interp_param
, ix_ll
, "");
2852 LLVMValueRef temp1
, temp2
;
2854 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2857 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2858 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2860 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2861 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2863 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2864 temp2
, ctx
->ac
.i32
, "");
2866 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2870 for (chan
= 0; chan
< 4; chan
++) {
2871 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2874 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2875 interp_param
, ctx
->ac
.v2f32
, "");
2876 LLVMValueRef i
= LLVMBuildExtractElement(
2877 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2878 LLVMValueRef j
= LLVMBuildExtractElement(
2879 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2881 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2882 llvm_chan
, attr_number
,
2883 ctx
->abi
->prim_mask
, i
, j
);
2885 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2886 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2887 llvm_chan
, attr_number
,
2888 ctx
->abi
->prim_mask
);
2891 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2892 var
->data
.location_frac
);
2895 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2896 nir_intrinsic_instr
*instr
)
2898 LLVMValueRef result
= NULL
;
2900 switch (instr
->intrinsic
) {
2901 case nir_intrinsic_ballot
:
2902 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2904 case nir_intrinsic_read_invocation
:
2905 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2906 get_src(ctx
, instr
->src
[1]));
2908 case nir_intrinsic_read_first_invocation
:
2909 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2911 case nir_intrinsic_load_subgroup_invocation
:
2912 result
= ac_get_thread_id(&ctx
->ac
);
2914 case nir_intrinsic_load_work_group_id
: {
2915 LLVMValueRef values
[3];
2917 for (int i
= 0; i
< 3; i
++) {
2918 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2919 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2922 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2925 case nir_intrinsic_load_base_vertex
:
2926 case nir_intrinsic_load_first_vertex
:
2927 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2929 case nir_intrinsic_load_local_group_size
:
2930 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2932 case nir_intrinsic_load_vertex_id
:
2933 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2934 ctx
->abi
->base_vertex
, "");
2936 case nir_intrinsic_load_vertex_id_zero_base
: {
2937 result
= ctx
->abi
->vertex_id
;
2940 case nir_intrinsic_load_local_invocation_id
: {
2941 result
= ctx
->abi
->local_invocation_ids
;
2944 case nir_intrinsic_load_base_instance
:
2945 result
= ctx
->abi
->start_instance
;
2947 case nir_intrinsic_load_draw_id
:
2948 result
= ctx
->abi
->draw_id
;
2950 case nir_intrinsic_load_view_index
:
2951 result
= ctx
->abi
->view_index
;
2953 case nir_intrinsic_load_invocation_id
:
2954 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2955 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2957 result
= ctx
->abi
->gs_invocation_id
;
2959 case nir_intrinsic_load_primitive_id
:
2960 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2961 result
= ctx
->abi
->gs_prim_id
;
2962 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2963 result
= ctx
->abi
->tcs_patch_id
;
2964 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2965 result
= ctx
->abi
->tes_patch_id
;
2967 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2969 case nir_intrinsic_load_sample_id
:
2970 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2972 case nir_intrinsic_load_sample_pos
:
2973 result
= load_sample_pos(ctx
);
2975 case nir_intrinsic_load_sample_mask_in
:
2976 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2978 case nir_intrinsic_load_frag_coord
: {
2979 LLVMValueRef values
[4] = {
2980 ctx
->abi
->frag_pos
[0],
2981 ctx
->abi
->frag_pos
[1],
2982 ctx
->abi
->frag_pos
[2],
2983 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2985 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2988 case nir_intrinsic_load_front_face
:
2989 result
= ctx
->abi
->front_face
;
2991 case nir_intrinsic_load_helper_invocation
:
2992 result
= visit_load_helper_invocation(ctx
);
2994 case nir_intrinsic_load_instance_id
:
2995 result
= ctx
->abi
->instance_id
;
2997 case nir_intrinsic_load_num_work_groups
:
2998 result
= ctx
->abi
->num_work_groups
;
3000 case nir_intrinsic_load_local_invocation_index
:
3001 result
= visit_load_local_invocation_index(ctx
);
3003 case nir_intrinsic_load_subgroup_id
:
3004 result
= visit_load_subgroup_id(ctx
);
3006 case nir_intrinsic_load_num_subgroups
:
3007 result
= visit_load_num_subgroups(ctx
);
3009 case nir_intrinsic_first_invocation
:
3010 result
= visit_first_invocation(ctx
);
3012 case nir_intrinsic_load_push_constant
:
3013 result
= visit_load_push_constant(ctx
, instr
);
3015 case nir_intrinsic_vulkan_resource_index
: {
3016 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3017 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3018 unsigned binding
= nir_intrinsic_binding(instr
);
3020 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3024 case nir_intrinsic_vulkan_resource_reindex
:
3025 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3027 case nir_intrinsic_store_ssbo
:
3028 visit_store_ssbo(ctx
, instr
);
3030 case nir_intrinsic_load_ssbo
:
3031 result
= visit_load_buffer(ctx
, instr
);
3033 case nir_intrinsic_ssbo_atomic_add
:
3034 case nir_intrinsic_ssbo_atomic_imin
:
3035 case nir_intrinsic_ssbo_atomic_umin
:
3036 case nir_intrinsic_ssbo_atomic_imax
:
3037 case nir_intrinsic_ssbo_atomic_umax
:
3038 case nir_intrinsic_ssbo_atomic_and
:
3039 case nir_intrinsic_ssbo_atomic_or
:
3040 case nir_intrinsic_ssbo_atomic_xor
:
3041 case nir_intrinsic_ssbo_atomic_exchange
:
3042 case nir_intrinsic_ssbo_atomic_comp_swap
:
3043 result
= visit_atomic_ssbo(ctx
, instr
);
3045 case nir_intrinsic_load_ubo
:
3046 result
= visit_load_ubo_buffer(ctx
, instr
);
3048 case nir_intrinsic_get_buffer_size
:
3049 result
= visit_get_buffer_size(ctx
, instr
);
3051 case nir_intrinsic_load_deref
:
3052 result
= visit_load_var(ctx
, instr
);
3054 case nir_intrinsic_store_deref
:
3055 visit_store_var(ctx
, instr
);
3057 case nir_intrinsic_load_shared
:
3058 result
= visit_load_shared(ctx
, instr
);
3060 case nir_intrinsic_store_shared
:
3061 visit_store_shared(ctx
, instr
);
3063 case nir_intrinsic_image_deref_samples
:
3064 result
= visit_image_samples(ctx
, instr
);
3066 case nir_intrinsic_image_deref_load
:
3067 result
= visit_image_load(ctx
, instr
);
3069 case nir_intrinsic_image_deref_store
:
3070 visit_image_store(ctx
, instr
);
3072 case nir_intrinsic_image_deref_atomic_add
:
3073 case nir_intrinsic_image_deref_atomic_min
:
3074 case nir_intrinsic_image_deref_atomic_max
:
3075 case nir_intrinsic_image_deref_atomic_and
:
3076 case nir_intrinsic_image_deref_atomic_or
:
3077 case nir_intrinsic_image_deref_atomic_xor
:
3078 case nir_intrinsic_image_deref_atomic_exchange
:
3079 case nir_intrinsic_image_deref_atomic_comp_swap
:
3080 result
= visit_image_atomic(ctx
, instr
);
3082 case nir_intrinsic_image_deref_size
:
3083 result
= visit_image_size(ctx
, instr
);
3085 case nir_intrinsic_shader_clock
:
3086 result
= ac_build_shader_clock(&ctx
->ac
);
3088 case nir_intrinsic_discard
:
3089 case nir_intrinsic_discard_if
:
3090 emit_discard(ctx
, instr
);
3092 case nir_intrinsic_memory_barrier
:
3093 case nir_intrinsic_group_memory_barrier
:
3094 case nir_intrinsic_memory_barrier_atomic_counter
:
3095 case nir_intrinsic_memory_barrier_buffer
:
3096 case nir_intrinsic_memory_barrier_image
:
3097 case nir_intrinsic_memory_barrier_shared
:
3098 emit_membar(&ctx
->ac
, instr
);
3100 case nir_intrinsic_barrier
:
3101 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3103 case nir_intrinsic_shared_atomic_add
:
3104 case nir_intrinsic_shared_atomic_imin
:
3105 case nir_intrinsic_shared_atomic_umin
:
3106 case nir_intrinsic_shared_atomic_imax
:
3107 case nir_intrinsic_shared_atomic_umax
:
3108 case nir_intrinsic_shared_atomic_and
:
3109 case nir_intrinsic_shared_atomic_or
:
3110 case nir_intrinsic_shared_atomic_xor
:
3111 case nir_intrinsic_shared_atomic_exchange
:
3112 case nir_intrinsic_shared_atomic_comp_swap
: {
3113 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3114 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3117 case nir_intrinsic_deref_atomic_add
:
3118 case nir_intrinsic_deref_atomic_imin
:
3119 case nir_intrinsic_deref_atomic_umin
:
3120 case nir_intrinsic_deref_atomic_imax
:
3121 case nir_intrinsic_deref_atomic_umax
:
3122 case nir_intrinsic_deref_atomic_and
:
3123 case nir_intrinsic_deref_atomic_or
:
3124 case nir_intrinsic_deref_atomic_xor
:
3125 case nir_intrinsic_deref_atomic_exchange
:
3126 case nir_intrinsic_deref_atomic_comp_swap
: {
3127 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3128 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3131 case nir_intrinsic_interp_deref_at_centroid
:
3132 case nir_intrinsic_interp_deref_at_sample
:
3133 case nir_intrinsic_interp_deref_at_offset
:
3134 result
= visit_interp(ctx
, instr
);
3136 case nir_intrinsic_emit_vertex
:
3137 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3139 case nir_intrinsic_end_primitive
:
3140 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3142 case nir_intrinsic_load_tess_coord
:
3143 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3145 case nir_intrinsic_load_tess_level_outer
:
3146 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3148 case nir_intrinsic_load_tess_level_inner
:
3149 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3151 case nir_intrinsic_load_patch_vertices_in
:
3152 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3154 case nir_intrinsic_vote_all
: {
3155 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3156 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3159 case nir_intrinsic_vote_any
: {
3160 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3161 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3164 case nir_intrinsic_shuffle
:
3165 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3166 get_src(ctx
, instr
->src
[1]));
3168 case nir_intrinsic_reduce
:
3169 result
= ac_build_reduce(&ctx
->ac
,
3170 get_src(ctx
, instr
->src
[0]),
3171 instr
->const_index
[0],
3172 instr
->const_index
[1]);
3174 case nir_intrinsic_inclusive_scan
:
3175 result
= ac_build_inclusive_scan(&ctx
->ac
,
3176 get_src(ctx
, instr
->src
[0]),
3177 instr
->const_index
[0]);
3179 case nir_intrinsic_exclusive_scan
:
3180 result
= ac_build_exclusive_scan(&ctx
->ac
,
3181 get_src(ctx
, instr
->src
[0]),
3182 instr
->const_index
[0]);
3184 case nir_intrinsic_quad_broadcast
: {
3185 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3186 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3187 lane
, lane
, lane
, lane
);
3190 case nir_intrinsic_quad_swap_horizontal
:
3191 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3193 case nir_intrinsic_quad_swap_vertical
:
3194 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3196 case nir_intrinsic_quad_swap_diagonal
:
3197 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3200 fprintf(stderr
, "Unknown intrinsic: ");
3201 nir_print_instr(&instr
->instr
, stderr
);
3202 fprintf(stderr
, "\n");
3206 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3210 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3211 nir_deref_instr
*deref_instr
,
3212 enum ac_descriptor_type desc_type
,
3213 const nir_tex_instr
*tex_instr
,
3214 bool image
, bool write
)
3216 LLVMValueRef index
= NULL
;
3217 unsigned constant_index
= 0;
3218 unsigned descriptor_set
;
3219 unsigned base_index
;
3220 bool bindless
= false;
3223 assert(tex_instr
&& !image
);
3225 base_index
= tex_instr
->sampler_index
;
3227 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3228 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3232 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3233 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3235 constant_index
+= array_size
* const_value
->u32
[0];
3237 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3239 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3240 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3245 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3248 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3250 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3251 base_index
= deref_instr
->var
->data
.binding
;
3254 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3257 constant_index
, index
,
3258 desc_type
, image
, write
, bindless
);
3261 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3264 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3265 * filtering manually. The driver sets img7 to a mask clearing
3266 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3267 * s_and_b32 samp0, samp0, img7
3270 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3272 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3273 LLVMValueRef res
, LLVMValueRef samp
)
3275 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3276 LLVMValueRef img7
, samp0
;
3278 if (ctx
->ac
.chip_class
>= VI
)
3281 img7
= LLVMBuildExtractElement(builder
, res
,
3282 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3283 samp0
= LLVMBuildExtractElement(builder
, samp
,
3284 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3285 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3286 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3287 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3290 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3291 nir_tex_instr
*instr
,
3292 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3293 LLVMValueRef
*fmask_ptr
)
3295 nir_deref_instr
*texture_deref_instr
= NULL
;
3296 nir_deref_instr
*sampler_deref_instr
= NULL
;
3298 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3299 switch (instr
->src
[i
].src_type
) {
3300 case nir_tex_src_texture_deref
:
3301 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3303 case nir_tex_src_sampler_deref
:
3304 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3311 if (!sampler_deref_instr
)
3312 sampler_deref_instr
= texture_deref_instr
;
3314 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3315 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3317 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3319 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3320 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3321 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3323 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3324 instr
->op
== nir_texop_samples_identical
))
3325 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3328 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3331 coord
= ac_to_float(ctx
, coord
);
3332 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3333 coord
= ac_to_integer(ctx
, coord
);
3337 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3339 LLVMValueRef result
= NULL
;
3340 struct ac_image_args args
= { 0 };
3341 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3342 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3343 unsigned offset_src
= 0;
3345 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3347 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3348 switch (instr
->src
[i
].src_type
) {
3349 case nir_tex_src_coord
: {
3350 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3351 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3352 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3355 case nir_tex_src_projector
:
3357 case nir_tex_src_comparator
:
3358 if (instr
->is_shadow
)
3359 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3361 case nir_tex_src_offset
:
3362 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3365 case nir_tex_src_bias
:
3366 if (instr
->op
== nir_texop_txb
)
3367 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3369 case nir_tex_src_lod
: {
3370 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3372 if (val
&& val
->i32
[0] == 0)
3373 args
.level_zero
= true;
3375 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3378 case nir_tex_src_ms_index
:
3379 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3381 case nir_tex_src_ms_mcs
:
3383 case nir_tex_src_ddx
:
3384 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3386 case nir_tex_src_ddy
:
3387 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3389 case nir_tex_src_texture_offset
:
3390 case nir_tex_src_sampler_offset
:
3391 case nir_tex_src_plane
:
3397 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3398 result
= get_buffer_size(ctx
, args
.resource
, true);
3402 if (instr
->op
== nir_texop_texture_samples
) {
3403 LLVMValueRef res
, samples
, is_msaa
;
3404 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3405 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3406 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3407 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3408 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3409 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3410 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3411 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3412 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3414 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3415 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3416 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3417 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3418 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3420 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3426 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3427 LLVMValueRef offset
[3], pack
;
3428 for (unsigned chan
= 0; chan
< 3; ++chan
)
3429 offset
[chan
] = ctx
->ac
.i32_0
;
3431 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3432 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3433 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3434 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3435 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3437 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3438 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3440 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3441 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3445 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3446 * so the depth comparison value isn't clamped for Z16 and
3447 * Z24 anymore. Do it manually here.
3449 * It's unnecessary if the original texture format was
3450 * Z32_FLOAT, but we don't know that here.
3452 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3453 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3455 /* pack derivatives */
3457 int num_src_deriv_channels
, num_dest_deriv_channels
;
3458 switch (instr
->sampler_dim
) {
3459 case GLSL_SAMPLER_DIM_3D
:
3460 case GLSL_SAMPLER_DIM_CUBE
:
3461 num_src_deriv_channels
= 3;
3462 num_dest_deriv_channels
= 3;
3464 case GLSL_SAMPLER_DIM_2D
:
3466 num_src_deriv_channels
= 2;
3467 num_dest_deriv_channels
= 2;
3469 case GLSL_SAMPLER_DIM_1D
:
3470 num_src_deriv_channels
= 1;
3471 if (ctx
->ac
.chip_class
>= GFX9
) {
3472 num_dest_deriv_channels
= 2;
3474 num_dest_deriv_channels
= 1;
3479 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3480 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3481 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3482 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3483 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3485 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3486 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3487 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3491 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3492 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3493 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3494 if (instr
->coord_components
== 3)
3495 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3496 ac_prepare_cube_coords(&ctx
->ac
,
3497 instr
->op
== nir_texop_txd
, instr
->is_array
,
3498 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3501 /* Texture coordinates fixups */
3502 if (instr
->coord_components
> 1 &&
3503 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3505 instr
->op
!= nir_texop_txf
) {
3506 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3509 if (instr
->coord_components
> 2 &&
3510 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3511 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3512 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3513 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3515 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3516 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3519 if (ctx
->ac
.chip_class
>= GFX9
&&
3520 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3521 instr
->op
!= nir_texop_lod
) {
3522 LLVMValueRef filler
;
3523 if (instr
->op
== nir_texop_txf
)
3524 filler
= ctx
->ac
.i32_0
;
3526 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3528 if (instr
->is_array
)
3529 args
.coords
[2] = args
.coords
[1];
3530 args
.coords
[1] = filler
;
3533 /* Pack sample index */
3534 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3535 args
.coords
[instr
->coord_components
] = sample_index
;
3537 if (instr
->op
== nir_texop_samples_identical
) {
3538 struct ac_image_args txf_args
= { 0 };
3539 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3541 txf_args
.dmask
= 0xf;
3542 txf_args
.resource
= fmask_ptr
;
3543 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3544 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3546 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3547 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3551 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3552 instr
->op
!= nir_texop_txs
) {
3553 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3554 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3555 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3556 instr
->is_array
? args
.coords
[2] : NULL
,
3557 args
.coords
[sample_chan
], fmask_ptr
);
3560 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3561 nir_const_value
*const_offset
=
3562 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3563 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3564 assert(const_offset
);
3565 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3566 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3567 args
.coords
[i
] = LLVMBuildAdd(
3568 ctx
->ac
.builder
, args
.coords
[i
],
3569 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3574 /* TODO TG4 support */
3576 if (instr
->op
== nir_texop_tg4
) {
3577 if (instr
->is_shadow
)
3580 args
.dmask
= 1 << instr
->component
;
3583 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3584 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3585 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3587 if (instr
->op
== nir_texop_query_levels
)
3588 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3589 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3590 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3591 instr
->op
!= nir_texop_tg4
)
3592 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3593 else if (instr
->op
== nir_texop_txs
&&
3594 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3596 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3597 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3598 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3599 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3600 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3601 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3602 instr
->op
== nir_texop_txs
&&
3603 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3605 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3606 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3607 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3609 } else if (instr
->dest
.ssa
.num_components
!= 4)
3610 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3614 assert(instr
->dest
.is_ssa
);
3615 result
= ac_to_integer(&ctx
->ac
, result
);
3616 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3621 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3623 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3624 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3626 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3627 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3630 static void visit_post_phi(struct ac_nir_context
*ctx
,
3631 nir_phi_instr
*instr
,
3632 LLVMValueRef llvm_phi
)
3634 nir_foreach_phi_src(src
, instr
) {
3635 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3636 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3638 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3642 static void phi_post_pass(struct ac_nir_context
*ctx
)
3644 struct hash_entry
*entry
;
3645 hash_table_foreach(ctx
->phis
, entry
) {
3646 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3647 (LLVMValueRef
)entry
->data
);
3652 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3653 const nir_ssa_undef_instr
*instr
)
3655 unsigned num_components
= instr
->def
.num_components
;
3656 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3659 if (num_components
== 1)
3660 undef
= LLVMGetUndef(type
);
3662 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3664 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3667 static void visit_jump(struct ac_llvm_context
*ctx
,
3668 const nir_jump_instr
*instr
)
3670 switch (instr
->type
) {
3671 case nir_jump_break
:
3672 ac_build_break(ctx
);
3674 case nir_jump_continue
:
3675 ac_build_continue(ctx
);
3678 fprintf(stderr
, "Unknown NIR jump instr: ");
3679 nir_print_instr(&instr
->instr
, stderr
);
3680 fprintf(stderr
, "\n");
3685 static void visit_deref(struct ac_nir_context
*ctx
,
3686 nir_deref_instr
*instr
)
3688 if (instr
->mode
!= nir_var_shared
)
3691 LLVMValueRef result
= NULL
;
3692 switch(instr
->deref_type
) {
3693 case nir_deref_type_var
: {
3694 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3695 result
= entry
->data
;
3698 case nir_deref_type_struct
:
3699 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3700 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3702 case nir_deref_type_array
:
3703 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3704 get_src(ctx
, instr
->arr
.index
));
3707 unreachable("Unhandled deref_instr deref type");
3710 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3713 static void visit_cf_list(struct ac_nir_context
*ctx
,
3714 struct exec_list
*list
);
3716 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3718 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3719 nir_foreach_instr(instr
, block
)
3721 switch (instr
->type
) {
3722 case nir_instr_type_alu
:
3723 visit_alu(ctx
, nir_instr_as_alu(instr
));
3725 case nir_instr_type_load_const
:
3726 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3728 case nir_instr_type_intrinsic
:
3729 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3731 case nir_instr_type_tex
:
3732 visit_tex(ctx
, nir_instr_as_tex(instr
));
3734 case nir_instr_type_phi
:
3735 visit_phi(ctx
, nir_instr_as_phi(instr
));
3737 case nir_instr_type_ssa_undef
:
3738 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3740 case nir_instr_type_jump
:
3741 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3743 case nir_instr_type_deref
:
3744 visit_deref(ctx
, nir_instr_as_deref(instr
));
3747 fprintf(stderr
, "Unknown NIR instr type: ");
3748 nir_print_instr(instr
, stderr
);
3749 fprintf(stderr
, "\n");
3754 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3757 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3759 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3761 nir_block
*then_block
=
3762 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3764 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3766 visit_cf_list(ctx
, &if_stmt
->then_list
);
3768 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3769 nir_block
*else_block
=
3770 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3772 ac_build_else(&ctx
->ac
, else_block
->index
);
3773 visit_cf_list(ctx
, &if_stmt
->else_list
);
3776 ac_build_endif(&ctx
->ac
, then_block
->index
);
3779 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3781 nir_block
*first_loop_block
=
3782 (nir_block
*) exec_list_get_head(&loop
->body
);
3784 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3786 visit_cf_list(ctx
, &loop
->body
);
3788 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3791 static void visit_cf_list(struct ac_nir_context
*ctx
,
3792 struct exec_list
*list
)
3794 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3796 switch (node
->type
) {
3797 case nir_cf_node_block
:
3798 visit_block(ctx
, nir_cf_node_as_block(node
));
3801 case nir_cf_node_if
:
3802 visit_if(ctx
, nir_cf_node_as_if(node
));
3805 case nir_cf_node_loop
:
3806 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3816 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3817 struct ac_shader_abi
*abi
,
3818 struct nir_shader
*nir
,
3819 struct nir_variable
*variable
,
3820 gl_shader_stage stage
)
3822 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3823 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3825 /* tess ctrl has it's own load/store paths for outputs */
3826 if (stage
== MESA_SHADER_TESS_CTRL
)
3829 if (stage
== MESA_SHADER_VERTEX
||
3830 stage
== MESA_SHADER_TESS_EVAL
||
3831 stage
== MESA_SHADER_GEOMETRY
) {
3832 int idx
= variable
->data
.location
+ variable
->data
.index
;
3833 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3834 int length
= nir
->info
.clip_distance_array_size
+
3835 nir
->info
.cull_distance_array_size
;
3844 bool is_16bit
= glsl_type_is_16bit(variable
->type
);
3845 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
3846 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3847 for (unsigned chan
= 0; chan
< 4; chan
++) {
3848 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3849 ac_build_alloca_undef(ctx
, type
, "");
3855 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3856 enum glsl_base_type type
)
3860 case GLSL_TYPE_UINT
:
3861 case GLSL_TYPE_BOOL
:
3862 case GLSL_TYPE_SUBROUTINE
:
3864 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3866 case GLSL_TYPE_INT64
:
3867 case GLSL_TYPE_UINT64
:
3869 case GLSL_TYPE_DOUBLE
:
3872 unreachable("unknown GLSL type");
3877 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3878 const struct glsl_type
*type
)
3880 if (glsl_type_is_scalar(type
)) {
3881 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3884 if (glsl_type_is_vector(type
)) {
3885 return LLVMVectorType(
3886 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3887 glsl_get_vector_elements(type
));
3890 if (glsl_type_is_matrix(type
)) {
3891 return LLVMArrayType(
3892 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3893 glsl_get_matrix_columns(type
));
3896 if (glsl_type_is_array(type
)) {
3897 return LLVMArrayType(
3898 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3899 glsl_get_length(type
));
3902 assert(glsl_type_is_struct(type
));
3904 LLVMTypeRef member_types
[glsl_get_length(type
)];
3906 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3908 glsl_to_llvm_type(ac
,
3909 glsl_get_struct_field(type
, i
));
3912 return LLVMStructTypeInContext(ac
->context
, member_types
,
3913 glsl_get_length(type
), false);
3917 setup_locals(struct ac_nir_context
*ctx
,
3918 struct nir_function
*func
)
3921 ctx
->num_locals
= 0;
3922 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3923 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3924 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3925 variable
->data
.location_frac
= 0;
3926 ctx
->num_locals
+= attrib_count
;
3928 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3932 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3933 for (j
= 0; j
< 4; j
++) {
3934 ctx
->locals
[i
* 4 + j
] =
3935 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3941 setup_shared(struct ac_nir_context
*ctx
,
3942 struct nir_shader
*nir
)
3944 nir_foreach_variable(variable
, &nir
->shared
) {
3945 LLVMValueRef shared
=
3946 LLVMAddGlobalInAddressSpace(
3947 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3948 variable
->name
? variable
->name
: "",
3949 AC_LOCAL_ADDR_SPACE
);
3950 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3954 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3955 struct nir_shader
*nir
)
3957 struct ac_nir_context ctx
= {};
3958 struct nir_function
*func
;
3963 ctx
.stage
= nir
->info
.stage
;
3965 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3967 nir_foreach_variable(variable
, &nir
->outputs
)
3968 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3971 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3972 _mesa_key_pointer_equal
);
3973 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3974 _mesa_key_pointer_equal
);
3975 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3976 _mesa_key_pointer_equal
);
3978 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3980 nir_index_ssa_defs(func
->impl
);
3981 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3983 setup_locals(&ctx
, func
);
3985 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3986 setup_shared(&ctx
, nir
);
3988 visit_cf_list(&ctx
, &func
->impl
->body
);
3989 phi_post_pass(&ctx
);
3991 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3992 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3997 ralloc_free(ctx
.defs
);
3998 ralloc_free(ctx
.phis
);
3999 ralloc_free(ctx
.vars
);
4003 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4005 /* While it would be nice not to have this flag, we are constrained
4006 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4009 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4011 /* TODO: Indirect indexing of GS inputs is unimplemented.
4013 * TCS and TES load inputs directly from LDS or offchip memory, so
4014 * indirect indexing is trivial.
4016 nir_variable_mode indirect_mask
= 0;
4017 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4018 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4019 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4020 !llvm_has_working_vgpr_indexing
)) {
4021 indirect_mask
|= nir_var_shader_in
;
4023 if (!llvm_has_working_vgpr_indexing
&&
4024 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4025 indirect_mask
|= nir_var_shader_out
;
4027 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4028 * smart enough to handle indirects without causing excess spilling
4029 * causing the gpu to hang.
4031 * See the following thread for more details of the problem:
4032 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4034 indirect_mask
|= nir_var_local
;
4036 nir_lower_indirect_derefs(nir
, indirect_mask
);