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
= ctx
->i32_0
;
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], 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
, "");
498 return ac_build_gather_values(ctx
, temps
, 2);
501 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
509 if (op
== nir_op_fddx_fine
)
510 mask
= AC_TID_MASK_LEFT
;
511 else if (op
== nir_op_fddy_fine
)
512 mask
= AC_TID_MASK_TOP
;
514 mask
= AC_TID_MASK_TOP_LEFT
;
516 /* for DDX we want to next X pixel, DDY next Y pixel. */
517 if (op
== nir_op_fddx_fine
||
518 op
== nir_op_fddx_coarse
||
524 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
529 * this takes an I,J coordinate pair,
530 * and works out the X and Y derivatives.
531 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
533 static LLVMValueRef
emit_ddxy_interp(
534 struct ac_nir_context
*ctx
,
535 LLVMValueRef interp_ij
)
537 LLVMValueRef result
[4], a
;
540 for (i
= 0; i
< 2; i
++) {
541 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
542 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
543 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
544 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
546 return ac_build_gather_values(&ctx
->ac
, result
, 4);
549 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
551 LLVMValueRef src
[4], result
= NULL
;
552 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
553 unsigned src_components
;
554 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
556 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
563 case nir_op_pack_half_2x16
:
566 case nir_op_unpack_half_2x16
:
569 case nir_op_cube_face_coord
:
570 case nir_op_cube_face_index
:
574 src_components
= num_components
;
577 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
578 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
586 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
587 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
590 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
593 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
596 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
599 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
600 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
601 result
= LLVMBuildFAdd(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
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
609 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
612 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
615 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
618 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
621 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
622 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
623 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
624 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
625 ac_to_float_type(&ctx
->ac
, def_type
), result
);
626 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
627 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
630 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
631 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
632 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
635 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
638 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
641 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
644 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
645 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
646 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
649 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
650 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
654 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
657 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
660 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
663 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
664 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
665 LLVMTypeOf(src
[0]), ""),
669 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
670 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
671 LLVMTypeOf(src
[0]), ""),
675 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
676 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
677 LLVMTypeOf(src
[0]), ""),
681 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
684 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
687 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
690 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
693 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
696 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
699 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
702 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
705 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
708 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
711 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
712 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
715 result
= emit_iabs(&ctx
->ac
, src
[0]);
718 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
721 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
724 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
727 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
730 result
= ac_build_isign(&ctx
->ac
, src
[0],
731 instr
->dest
.dest
.ssa
.bit_size
);
734 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
735 result
= ac_build_fsign(&ctx
->ac
, src
[0],
736 instr
->dest
.dest
.ssa
.bit_size
);
739 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
740 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
743 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
744 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
747 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
748 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
750 case nir_op_fround_even
:
751 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
752 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
755 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
756 result
= ac_build_fract(&ctx
->ac
, src
[0],
757 instr
->dest
.dest
.ssa
.bit_size
);
760 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
761 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
764 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
765 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
768 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
769 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
772 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
773 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
776 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
777 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
780 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
781 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
782 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
785 case nir_op_frexp_exp
:
786 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
787 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
788 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
791 case nir_op_frexp_sig
:
792 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
793 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
794 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
797 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
798 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
799 if (ctx
->ac
.chip_class
< GFX9
&&
800 instr
->dest
.dest
.ssa
.bit_size
== 32) {
801 /* Only pre-GFX9 chips do not flush denorms. */
802 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
803 ac_to_float_type(&ctx
->ac
, def_type
),
808 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
809 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
810 if (ctx
->ac
.chip_class
< GFX9
&&
811 instr
->dest
.dest
.ssa
.bit_size
== 32) {
812 /* Only pre-GFX9 chips do not flush denorms. */
813 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
814 ac_to_float_type(&ctx
->ac
, def_type
),
819 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
820 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
823 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
824 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
825 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
827 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
829 case nir_op_ibitfield_extract
:
830 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
832 case nir_op_ubitfield_extract
:
833 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
835 case nir_op_bitfield_insert
:
836 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
838 case nir_op_bitfield_reverse
:
839 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
841 case nir_op_bit_count
:
842 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
847 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
848 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
849 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
854 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
855 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
860 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
861 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
866 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
867 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
872 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
873 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
875 case nir_op_f2f16_rtz
:
876 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
877 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
878 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
879 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
881 case nir_op_f2f16_rtne
:
885 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
886 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
887 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
889 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
894 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
895 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
896 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
898 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
903 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
904 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
905 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
907 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
910 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
912 case nir_op_find_lsb
:
913 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
914 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
916 case nir_op_ufind_msb
:
917 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
918 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
920 case nir_op_ifind_msb
:
921 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
922 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
924 case nir_op_uadd_carry
:
925 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
926 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
927 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
929 case nir_op_usub_borrow
:
930 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
931 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
932 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
935 result
= emit_b2f(&ctx
->ac
, src
[0]);
938 result
= emit_f2b(&ctx
->ac
, src
[0]);
941 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
944 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
945 result
= emit_i2b(&ctx
->ac
, src
[0]);
947 case nir_op_fquantize2f16
:
948 result
= emit_f2f16(&ctx
->ac
, src
[0]);
950 case nir_op_umul_high
:
951 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
952 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
953 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
955 case nir_op_imul_high
:
956 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
957 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
958 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
960 case nir_op_pack_half_2x16
:
961 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
963 case nir_op_unpack_half_2x16
:
964 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
968 case nir_op_fddx_fine
:
969 case nir_op_fddy_fine
:
970 case nir_op_fddx_coarse
:
971 case nir_op_fddy_coarse
:
972 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
975 case nir_op_unpack_64_2x32_split_x
: {
976 assert(ac_get_llvm_num_components(src
[0]) == 1);
977 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
980 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
985 case nir_op_unpack_64_2x32_split_y
: {
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_pack_64_2x32_split
: {
996 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
997 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
998 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1002 case nir_op_cube_face_coord
: {
1003 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1004 LLVMValueRef results
[2];
1006 for (unsigned chan
= 0; chan
< 3; chan
++)
1007 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1008 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1009 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1010 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1011 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1012 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1016 case nir_op_cube_face_index
: {
1017 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1019 for (unsigned chan
= 0; chan
< 3; chan
++)
1020 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1021 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1022 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1027 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1028 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1029 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1030 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1033 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1034 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1037 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1038 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1041 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1042 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1043 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1044 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1047 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1048 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1051 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1052 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1054 case nir_op_fmed3
: {
1055 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1056 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1057 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1058 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1059 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1060 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1061 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1062 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1065 case nir_op_imed3
: {
1066 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1067 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1068 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1069 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1072 case nir_op_umed3
: {
1073 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1074 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1075 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1076 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1081 fprintf(stderr
, "Unknown NIR alu instr: ");
1082 nir_print_instr(&instr
->instr
, stderr
);
1083 fprintf(stderr
, "\n");
1088 assert(instr
->dest
.dest
.is_ssa
);
1089 result
= ac_to_integer(&ctx
->ac
, result
);
1090 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1094 static void visit_load_const(struct ac_nir_context
*ctx
,
1095 const nir_load_const_instr
*instr
)
1097 LLVMValueRef values
[4], value
= NULL
;
1098 LLVMTypeRef element_type
=
1099 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1101 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1102 switch (instr
->def
.bit_size
) {
1104 values
[i
] = LLVMConstInt(element_type
,
1105 instr
->value
.u16
[i
], false);
1108 values
[i
] = LLVMConstInt(element_type
,
1109 instr
->value
.u32
[i
], false);
1112 values
[i
] = LLVMConstInt(element_type
,
1113 instr
->value
.u64
[i
], false);
1117 "unsupported nir load_const bit_size: %d\n",
1118 instr
->def
.bit_size
);
1122 if (instr
->def
.num_components
> 1) {
1123 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1127 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1131 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1134 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1135 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1138 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1139 /* On VI, the descriptor contains the size in bytes,
1140 * but TXQ must return the size in elements.
1141 * The stride is always non-zero for resources using TXQ.
1143 LLVMValueRef stride
=
1144 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1146 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1147 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1148 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1149 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1151 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1156 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1158 struct ac_image_args
*args
,
1159 const nir_tex_instr
*instr
)
1161 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1162 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1163 LLVMValueRef half_texel
[2];
1164 LLVMValueRef compare_cube_wa
= NULL
;
1165 LLVMValueRef result
;
1169 struct ac_image_args txq_args
= { 0 };
1171 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1172 txq_args
.opcode
= ac_image_get_resinfo
;
1173 txq_args
.dmask
= 0xf;
1174 txq_args
.lod
= ctx
->i32_0
;
1175 txq_args
.resource
= args
->resource
;
1176 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1177 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1179 for (unsigned c
= 0; c
< 2; c
++) {
1180 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1181 LLVMConstInt(ctx
->i32
, c
, false), "");
1182 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1183 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1184 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1185 LLVMConstReal(ctx
->f32
, -0.5), "");
1189 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1191 for (unsigned c
= 0; c
< 2; c
++) {
1193 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1194 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1198 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1199 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1200 * workaround by sampling using a scaled type and converting.
1201 * This is taken from amdgpu-pro shaders.
1203 /* NOTE this produces some ugly code compared to amdgpu-pro,
1204 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1205 * and then reads them back. -pro generates two selects,
1206 * one s_cmp for the descriptor rewriting
1207 * one v_cmp for the coordinate and result changes.
1209 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1210 LLVMValueRef tmp
, tmp2
;
1212 /* workaround 8/8/8/8 uint/sint cube gather bug */
1213 /* first detect it then change to a scaled read and f2i */
1214 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1217 /* extract the DATA_FORMAT */
1218 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1219 LLVMConstInt(ctx
->i32
, 6, false), false);
1221 /* is the DATA_FORMAT == 8_8_8_8 */
1222 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1224 if (stype
== GLSL_TYPE_UINT
)
1225 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1226 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1227 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1229 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1230 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1231 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1233 /* replace the NUM FORMAT in the descriptor */
1234 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1235 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1237 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1239 /* don't modify the coordinates for this case */
1240 for (unsigned c
= 0; c
< 2; ++c
)
1241 args
->coords
[c
] = LLVMBuildSelect(
1242 ctx
->builder
, compare_cube_wa
,
1243 orig_coords
[c
], args
->coords
[c
], "");
1246 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1247 result
= ac_build_image_opcode(ctx
, args
);
1249 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1250 LLVMValueRef tmp
, tmp2
;
1252 /* if the cube workaround is in place, f2i the result. */
1253 for (unsigned c
= 0; c
< 4; c
++) {
1254 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1255 if (stype
== GLSL_TYPE_UINT
)
1256 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1258 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1259 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1260 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1261 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1262 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1263 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1269 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1271 nir_deref_instr
*texture_deref_instr
= NULL
;
1273 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1274 switch (instr
->src
[i
].src_type
) {
1275 case nir_tex_src_texture_deref
:
1276 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1282 return texture_deref_instr
;
1285 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1286 const nir_tex_instr
*instr
,
1287 struct ac_image_args
*args
)
1289 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1290 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1292 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1293 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1297 util_last_bit(mask
),
1300 return ac_build_buffer_load_format(&ctx
->ac
,
1304 util_last_bit(mask
),
1309 args
->opcode
= ac_image_sample
;
1311 switch (instr
->op
) {
1313 case nir_texop_txf_ms
:
1314 case nir_texop_samples_identical
:
1315 args
->opcode
= args
->level_zero
||
1316 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1317 ac_image_load
: ac_image_load_mip
;
1318 args
->level_zero
= false;
1321 case nir_texop_query_levels
:
1322 args
->opcode
= ac_image_get_resinfo
;
1324 args
->lod
= ctx
->ac
.i32_0
;
1325 args
->level_zero
= false;
1328 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1330 args
->level_zero
= true;
1334 args
->opcode
= ac_image_gather4
;
1335 args
->level_zero
= true;
1338 args
->opcode
= ac_image_get_lod
;
1344 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1345 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1346 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1347 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1348 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1349 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1350 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1354 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1355 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1356 if ((args
->dim
== ac_image_2darray
||
1357 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1358 args
->coords
[1] = ctx
->ac
.i32_0
;
1362 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1363 return ac_build_image_opcode(&ctx
->ac
, args
);
1366 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1367 nir_intrinsic_instr
*instr
)
1369 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1370 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1372 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1373 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1377 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1378 nir_intrinsic_instr
*instr
)
1380 LLVMValueRef ptr
, addr
;
1382 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1383 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1384 get_src(ctx
, instr
->src
[0]), "");
1386 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1388 if (instr
->dest
.ssa
.bit_size
== 16) {
1389 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1390 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1391 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1392 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1393 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1394 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1395 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1396 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1397 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1398 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1399 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1400 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1401 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1402 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1403 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1404 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1407 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1409 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1412 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1413 const nir_intrinsic_instr
*instr
)
1415 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1417 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1420 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1422 uint32_t new_mask
= 0;
1423 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1424 if (mask
& (1u << i
))
1425 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1429 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1430 unsigned start
, unsigned count
)
1432 LLVMValueRef mask
[] = {
1433 ctx
->i32_0
, ctx
->i32_1
,
1434 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1436 unsigned src_elements
= ac_get_llvm_num_components(src
);
1438 if (count
== src_elements
) {
1441 } else if (count
== 1) {
1442 assert(start
< src_elements
);
1443 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1445 assert(start
+ count
<= src_elements
);
1447 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1448 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1452 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1453 nir_intrinsic_instr
*instr
)
1455 const char *store_name
;
1456 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1457 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1458 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1459 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1460 LLVMValueRef glc
= ctx
->ac
.i1false
;
1462 if (access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
1463 glc
= ctx
->ac
.i1true
;
1465 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1466 get_src(ctx
, instr
->src
[1]), true);
1467 LLVMValueRef base_data
= ac_to_float(&ctx
->ac
, src_data
);
1468 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1469 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1473 LLVMValueRef data
, offset
;
1474 LLVMTypeRef data_type
;
1476 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1478 /* Due to an LLVM limitation, split 3-element writes
1479 * into a 2-element and a 1-element write. */
1481 writemask
|= 1 << (start
+ 2);
1484 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1486 /* we can only store 4 DWords at the same time.
1487 * can only happen for 64 Bit vectors. */
1488 if (num_bytes
> 16) {
1489 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1494 /* check alignment of 16 Bit stores */
1495 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1496 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1500 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1503 offset
= base_offset
;
1505 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1506 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1508 if (num_bytes
== 2) {
1509 store_name
= "llvm.amdgcn.tbuffer.store.i32";
1510 data_type
= ctx
->ac
.i32
;
1511 LLVMValueRef tbuffer_params
[] = {
1514 ctx
->ac
.i32_0
, /* vindex */
1515 offset
, /* voffset */
1518 LLVMConstInt(ctx
->ac
.i32
, 2, false), // dfmt (= 16bit)
1519 LLVMConstInt(ctx
->ac
.i32
, 4, false), // nfmt (= uint)
1523 ac_build_intrinsic(&ctx
->ac
, store_name
,
1524 ctx
->ac
.voidt
, tbuffer_params
, 10, 0);
1526 switch (num_bytes
) {
1527 case 16: /* v4f32 */
1528 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1529 data_type
= ctx
->ac
.v4f32
;
1532 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1533 data_type
= ctx
->ac
.v2f32
;
1536 store_name
= "llvm.amdgcn.buffer.store.f32";
1537 data_type
= ctx
->ac
.f32
;
1540 unreachable("Malformed vector store.");
1542 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1543 LLVMValueRef params
[] = {
1546 ctx
->ac
.i32_0
, /* vindex */
1549 ctx
->ac
.i1false
, /* slc */
1551 ac_build_intrinsic(&ctx
->ac
, store_name
,
1552 ctx
->ac
.voidt
, params
, 6, 0);
1557 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1558 const nir_intrinsic_instr
*instr
)
1561 LLVMValueRef params
[6];
1564 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1565 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1567 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1568 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1569 get_src(ctx
, instr
->src
[0]),
1571 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1572 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1573 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1575 switch (instr
->intrinsic
) {
1576 case nir_intrinsic_ssbo_atomic_add
:
1577 name
= "llvm.amdgcn.buffer.atomic.add";
1579 case nir_intrinsic_ssbo_atomic_imin
:
1580 name
= "llvm.amdgcn.buffer.atomic.smin";
1582 case nir_intrinsic_ssbo_atomic_umin
:
1583 name
= "llvm.amdgcn.buffer.atomic.umin";
1585 case nir_intrinsic_ssbo_atomic_imax
:
1586 name
= "llvm.amdgcn.buffer.atomic.smax";
1588 case nir_intrinsic_ssbo_atomic_umax
:
1589 name
= "llvm.amdgcn.buffer.atomic.umax";
1591 case nir_intrinsic_ssbo_atomic_and
:
1592 name
= "llvm.amdgcn.buffer.atomic.and";
1594 case nir_intrinsic_ssbo_atomic_or
:
1595 name
= "llvm.amdgcn.buffer.atomic.or";
1597 case nir_intrinsic_ssbo_atomic_xor
:
1598 name
= "llvm.amdgcn.buffer.atomic.xor";
1600 case nir_intrinsic_ssbo_atomic_exchange
:
1601 name
= "llvm.amdgcn.buffer.atomic.swap";
1603 case nir_intrinsic_ssbo_atomic_comp_swap
:
1604 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1610 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1613 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1614 const nir_intrinsic_instr
*instr
)
1616 LLVMValueRef results
[2];
1618 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1619 int num_components
= instr
->num_components
;
1620 int num_bytes
= num_components
* elem_size_bytes
;
1621 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1622 LLVMValueRef glc
= ctx
->ac
.i1false
;
1624 if (access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
1625 glc
= ctx
->ac
.i1true
;
1627 for (int i
= 0; i
< num_bytes
; i
+= load_bytes
) {
1628 load_bytes
= MIN2(num_bytes
- i
, 16);
1629 const char *load_name
;
1630 LLVMTypeRef data_type
;
1631 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1632 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
1633 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1634 get_src(ctx
, instr
->src
[0]), false);
1635 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1637 int idx
= i
? 1 : 0;
1638 if (load_bytes
== 2) {
1639 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(LLVMIntTypeInContext(ctx
->ac
.context
, 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 v2i64 and i64 but shuffle fails on that */
1689 results
[1] = ac_build_expand(&ctx
->ac
, results
[1], 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),
1726 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1728 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1729 NULL
, 0, false, false, true, true);
1731 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1734 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1735 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1739 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1740 bool vs_in
, unsigned *vertex_index_out
,
1741 LLVMValueRef
*vertex_index_ref
,
1742 unsigned *const_out
, LLVMValueRef
*indir_out
)
1744 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1745 nir_deref_path path
;
1746 unsigned idx_lvl
= 1;
1748 nir_deref_path_init(&path
, instr
, NULL
);
1750 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1751 if (vertex_index_ref
) {
1752 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1753 if (vertex_index_out
)
1754 *vertex_index_out
= 0;
1756 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1758 *vertex_index_out
= v
->u32
[0];
1763 uint32_t const_offset
= 0;
1764 LLVMValueRef offset
= NULL
;
1766 if (var
->data
.compact
) {
1767 assert(instr
->deref_type
== nir_deref_type_array
);
1768 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1770 const_offset
= v
->u32
[0];
1774 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1775 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1776 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1777 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1779 for (unsigned i
= 0; i
< index
; i
++) {
1780 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1781 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1783 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1784 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1785 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1786 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1788 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1792 unreachable("Uhandled deref type in get_deref_instr_offset");
1796 nir_deref_path_finish(&path
);
1798 if (const_offset
&& offset
)
1799 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1800 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1803 *const_out
= const_offset
;
1804 *indir_out
= offset
;
1807 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1808 nir_intrinsic_instr
*instr
,
1811 LLVMValueRef result
;
1812 LLVMValueRef vertex_index
= NULL
;
1813 LLVMValueRef indir_index
= NULL
;
1814 unsigned const_index
= 0;
1816 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1818 unsigned location
= var
->data
.location
;
1819 unsigned driver_location
= var
->data
.driver_location
;
1820 const bool is_patch
= var
->data
.patch
;
1821 const bool is_compact
= var
->data
.compact
;
1823 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1824 false, NULL
, is_patch
? NULL
: &vertex_index
,
1825 &const_index
, &indir_index
);
1827 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1829 LLVMTypeRef src_component_type
;
1830 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1831 src_component_type
= LLVMGetElementType(dest_type
);
1833 src_component_type
= dest_type
;
1835 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1836 vertex_index
, indir_index
,
1837 const_index
, location
, driver_location
,
1838 var
->data
.location_frac
,
1839 instr
->num_components
,
1840 is_patch
, is_compact
, load_inputs
);
1841 if (instr
->dest
.ssa
.bit_size
== 16) {
1842 result
= ac_to_integer(&ctx
->ac
, result
);
1843 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1845 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1848 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1849 nir_intrinsic_instr
*instr
)
1851 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1853 LLVMValueRef values
[8];
1854 int idx
= var
->data
.driver_location
;
1855 int ve
= instr
->dest
.ssa
.num_components
;
1856 unsigned comp
= var
->data
.location_frac
;
1857 LLVMValueRef indir_index
;
1859 unsigned const_index
;
1860 unsigned stride
= var
->data
.compact
? 1 : 4;
1861 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1862 var
->data
.mode
== nir_var_shader_in
;
1864 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), vs_in
, NULL
, NULL
,
1865 &const_index
, &indir_index
);
1867 if (instr
->dest
.ssa
.bit_size
== 64)
1870 switch (var
->data
.mode
) {
1871 case nir_var_shader_in
:
1872 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1873 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1874 return load_tess_varyings(ctx
, instr
, true);
1877 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1878 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1879 LLVMValueRef indir_index
;
1880 unsigned const_index
, vertex_index
;
1881 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1882 false, &vertex_index
, NULL
, &const_index
, &indir_index
);
1884 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1885 var
->data
.driver_location
,
1886 var
->data
.location_frac
,
1887 instr
->num_components
, vertex_index
, const_index
, type
);
1890 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1892 unsigned count
= glsl_count_attribute_slots(
1894 ctx
->stage
== MESA_SHADER_VERTEX
);
1896 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1897 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1898 stride
, false, true);
1900 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1904 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1908 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1910 unsigned count
= glsl_count_attribute_slots(
1913 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1914 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1915 stride
, true, true);
1917 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1921 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1925 case nir_var_shared
: {
1926 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
1927 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1928 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1929 get_def_type(ctx
, &instr
->dest
.ssa
),
1932 case nir_var_shader_out
:
1933 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1934 return load_tess_varyings(ctx
, instr
, false);
1937 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1939 unsigned count
= glsl_count_attribute_slots(
1942 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1943 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1944 stride
, true, true);
1946 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1950 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1951 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1957 unreachable("unhandle variable mode");
1959 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1960 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1964 visit_store_var(struct ac_nir_context
*ctx
,
1965 nir_intrinsic_instr
*instr
)
1967 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1969 LLVMValueRef temp_ptr
, value
;
1970 int idx
= var
->data
.driver_location
;
1971 unsigned comp
= var
->data
.location_frac
;
1972 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
1973 int writemask
= instr
->const_index
[0];
1974 LLVMValueRef indir_index
;
1975 unsigned const_index
;
1977 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), false,
1978 NULL
, NULL
, &const_index
, &indir_index
);
1980 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1982 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1983 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1986 writemask
= widen_mask(writemask
, 2);
1989 writemask
= writemask
<< comp
;
1991 switch (var
->data
.mode
) {
1992 case nir_var_shader_out
:
1994 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1995 LLVMValueRef vertex_index
= NULL
;
1996 LLVMValueRef indir_index
= NULL
;
1997 unsigned const_index
= 0;
1998 const bool is_patch
= var
->data
.patch
;
2000 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2001 false, NULL
, is_patch
? NULL
: &vertex_index
,
2002 &const_index
, &indir_index
);
2004 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2005 vertex_index
, indir_index
,
2006 const_index
, src
, writemask
);
2010 for (unsigned chan
= 0; chan
< 8; chan
++) {
2012 if (!(writemask
& (1 << chan
)))
2015 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2017 if (var
->data
.compact
)
2020 unsigned count
= glsl_count_attribute_slots(
2023 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2024 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2025 stride
, true, true);
2027 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2028 value
, indir_index
, "");
2029 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2030 count
, stride
, tmp_vec
);
2033 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2035 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2040 for (unsigned chan
= 0; chan
< 8; chan
++) {
2041 if (!(writemask
& (1 << chan
)))
2044 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2046 unsigned count
= glsl_count_attribute_slots(
2049 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2050 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2053 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2054 value
, indir_index
, "");
2055 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2058 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2060 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2064 case nir_var_shared
: {
2065 int writemask
= instr
->const_index
[0];
2066 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2067 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2068 if (util_is_power_of_two_nonzero(writemask
)) {
2069 val
= LLVMBuildBitCast(
2070 ctx
->ac
.builder
, val
,
2071 LLVMGetElementType(LLVMTypeOf(address
)), "");
2072 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2074 for (unsigned chan
= 0; chan
< 4; chan
++) {
2075 if (!(writemask
& (1 << chan
)))
2078 LLVMBuildStructGEP(ctx
->ac
.builder
,
2080 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2082 src
= LLVMBuildBitCast(
2083 ctx
->ac
.builder
, src
,
2084 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2085 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2095 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2098 case GLSL_SAMPLER_DIM_BUF
:
2100 case GLSL_SAMPLER_DIM_1D
:
2101 return array
? 2 : 1;
2102 case GLSL_SAMPLER_DIM_2D
:
2103 return array
? 3 : 2;
2104 case GLSL_SAMPLER_DIM_MS
:
2105 return array
? 4 : 3;
2106 case GLSL_SAMPLER_DIM_3D
:
2107 case GLSL_SAMPLER_DIM_CUBE
:
2109 case GLSL_SAMPLER_DIM_RECT
:
2110 case GLSL_SAMPLER_DIM_SUBPASS
:
2112 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2121 /* Adjust the sample index according to FMASK.
2123 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2124 * which is the identity mapping. Each nibble says which physical sample
2125 * should be fetched to get that sample.
2127 * For example, 0x11111100 means there are only 2 samples stored and
2128 * the second sample covers 3/4 of the pixel. When reading samples 0
2129 * and 1, return physical sample 0 (determined by the first two 0s
2130 * in FMASK), otherwise return physical sample 1.
2132 * The sample index should be adjusted as follows:
2133 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2135 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2136 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2137 LLVMValueRef coord_z
,
2138 LLVMValueRef sample_index
,
2139 LLVMValueRef fmask_desc_ptr
)
2141 struct ac_image_args args
= {0};
2144 args
.coords
[0] = coord_x
;
2145 args
.coords
[1] = coord_y
;
2147 args
.coords
[2] = coord_z
;
2149 args
.opcode
= ac_image_load
;
2150 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2151 args
.resource
= fmask_desc_ptr
;
2153 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2155 res
= ac_build_image_opcode(ctx
, &args
);
2157 res
= ac_to_integer(ctx
, res
);
2158 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2159 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2161 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2165 LLVMValueRef sample_index4
=
2166 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2167 LLVMValueRef shifted_fmask
=
2168 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2169 LLVMValueRef final_sample
=
2170 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2172 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2173 * resource descriptor is 0 (invalid),
2175 LLVMValueRef fmask_desc
=
2176 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2179 LLVMValueRef fmask_word1
=
2180 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2183 LLVMValueRef word1_is_nonzero
=
2184 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2185 fmask_word1
, ctx
->i32_0
, "");
2187 /* Replace the MSAA sample index. */
2189 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2190 final_sample
, sample_index
, "");
2191 return sample_index
;
2194 static nir_variable
*get_image_variable(const nir_intrinsic_instr
*instr
)
2196 assert(instr
->src
[0].is_ssa
);
2197 return nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2200 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2201 const nir_intrinsic_instr
*instr
,
2202 enum ac_descriptor_type desc_type
,
2205 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2208 static void get_image_coords(struct ac_nir_context
*ctx
,
2209 const nir_intrinsic_instr
*instr
,
2210 struct ac_image_args
*args
)
2212 const struct glsl_type
*type
= glsl_without_array(get_image_variable(instr
)->type
);
2214 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2215 LLVMValueRef masks
[] = {
2216 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2217 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2219 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2222 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2223 bool is_array
= glsl_sampler_type_is_array(type
);
2224 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2225 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2226 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2227 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2228 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2229 count
= image_type_to_components_count(dim
, is_array
);
2232 LLVMValueRef fmask_load_address
[3];
2235 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2236 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2238 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2240 fmask_load_address
[2] = NULL
;
2242 for (chan
= 0; chan
< 2; ++chan
)
2243 fmask_load_address
[chan
] =
2244 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2245 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2246 ctx
->ac
.i32
, ""), "");
2247 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2249 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2250 fmask_load_address
[0],
2251 fmask_load_address
[1],
2252 fmask_load_address
[2],
2254 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2255 AC_DESC_FMASK
, NULL
, false, false));
2257 if (count
== 1 && !gfx9_1d
) {
2258 if (instr
->src
[1].ssa
->num_components
)
2259 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2261 args
->coords
[0] = src0
;
2266 for (chan
= 0; chan
< count
; ++chan
) {
2267 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2270 for (chan
= 0; chan
< 2; ++chan
) {
2271 args
->coords
[chan
] = LLVMBuildAdd(
2272 ctx
->ac
.builder
, args
->coords
[chan
],
2274 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2275 ctx
->ac
.i32
, ""), "");
2277 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2278 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2284 args
->coords
[2] = args
->coords
[1];
2285 args
->coords
[1] = ctx
->ac
.i32_0
;
2287 args
->coords
[1] = ctx
->ac
.i32_0
;
2292 args
->coords
[count
] = sample_index
;
2298 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2299 const nir_intrinsic_instr
*instr
, bool write
)
2301 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2302 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2303 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2304 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2305 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2307 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2308 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2309 elem_count
, stride
, "");
2311 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2312 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2317 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2318 const nir_intrinsic_instr
*instr
)
2321 const nir_variable
*var
= get_image_variable(instr
);
2322 const struct glsl_type
*type
= var
->type
;
2324 type
= glsl_without_array(type
);
2326 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2327 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2328 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2329 unsigned num_channels
= util_last_bit(mask
);
2330 LLVMValueRef rsrc
, vindex
;
2332 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2333 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2336 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2337 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2338 ctx
->ac
.i32_0
, num_channels
,
2340 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2342 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2343 res
= ac_to_integer(&ctx
->ac
, res
);
2345 struct ac_image_args args
= {};
2346 args
.opcode
= ac_image_load
;
2347 get_image_coords(ctx
, instr
, &args
);
2348 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2349 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2350 glsl_sampler_type_is_array(type
));
2352 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2353 if (var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
2354 args
.cache_policy
|= ac_glc
;
2356 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2358 return ac_to_integer(&ctx
->ac
, res
);
2361 static void visit_image_store(struct ac_nir_context
*ctx
,
2362 nir_intrinsic_instr
*instr
)
2364 LLVMValueRef params
[8];
2365 const nir_variable
*var
= get_image_variable(instr
);
2366 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2367 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2368 LLVMValueRef glc
= ctx
->ac
.i1false
;
2369 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2371 glc
= ctx
->ac
.i1true
;
2373 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2374 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2376 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3])); /* data */
2378 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2379 ctx
->ac
.i32_0
, ""); /* vindex */
2380 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2381 params
[4] = glc
; /* glc */
2382 params
[5] = ctx
->ac
.i1false
; /* slc */
2383 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2386 struct ac_image_args args
= {};
2387 args
.opcode
= ac_image_store
;
2388 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2389 get_image_coords(ctx
, instr
, &args
);
2390 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2391 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2392 glsl_sampler_type_is_array(type
));
2394 if (force_glc
|| (var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
)))
2395 args
.cache_policy
|= ac_glc
;
2397 ac_build_image_opcode(&ctx
->ac
, &args
);
2402 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2403 const nir_intrinsic_instr
*instr
)
2405 LLVMValueRef params
[7];
2406 int param_count
= 0;
2407 const nir_variable
*var
= get_image_variable(instr
);
2409 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2410 const char *atomic_name
;
2411 char intrinsic_name
[41];
2412 enum ac_atomic_op atomic_subop
;
2413 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2414 MAYBE_UNUSED
int length
;
2416 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2418 switch (instr
->intrinsic
) {
2419 case nir_intrinsic_image_deref_atomic_add
:
2420 atomic_name
= "add";
2421 atomic_subop
= ac_atomic_add
;
2423 case nir_intrinsic_image_deref_atomic_min
:
2424 atomic_name
= is_unsigned
? "umin" : "smin";
2425 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2427 case nir_intrinsic_image_deref_atomic_max
:
2428 atomic_name
= is_unsigned
? "umax" : "smax";
2429 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2431 case nir_intrinsic_image_deref_atomic_and
:
2432 atomic_name
= "and";
2433 atomic_subop
= ac_atomic_and
;
2435 case nir_intrinsic_image_deref_atomic_or
:
2437 atomic_subop
= ac_atomic_or
;
2439 case nir_intrinsic_image_deref_atomic_xor
:
2440 atomic_name
= "xor";
2441 atomic_subop
= ac_atomic_xor
;
2443 case nir_intrinsic_image_deref_atomic_exchange
:
2444 atomic_name
= "swap";
2445 atomic_subop
= ac_atomic_swap
;
2447 case nir_intrinsic_image_deref_atomic_comp_swap
:
2448 atomic_name
= "cmpswap";
2449 atomic_subop
= 0; /* not used */
2456 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2457 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2459 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2460 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2461 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2462 ctx
->ac
.i32_0
, ""); /* vindex */
2463 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2464 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2466 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2467 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2469 assert(length
< sizeof(intrinsic_name
));
2470 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2471 params
, param_count
, 0);
2473 struct ac_image_args args
= {};
2474 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2475 args
.atomic
= atomic_subop
;
2476 args
.data
[0] = params
[0];
2478 args
.data
[1] = params
[1];
2479 get_image_coords(ctx
, instr
, &args
);
2480 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2481 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2482 glsl_sampler_type_is_array(type
));
2484 return ac_build_image_opcode(&ctx
->ac
, &args
);
2488 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2489 const nir_intrinsic_instr
*instr
)
2491 const nir_variable
*var
= get_image_variable(instr
);
2492 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2494 struct ac_image_args args
= { 0 };
2495 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2496 glsl_sampler_type_is_array(type
));
2498 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2499 args
.opcode
= ac_image_get_resinfo
;
2500 args
.lod
= ctx
->ac
.i32_0
;
2501 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2503 return ac_build_image_opcode(&ctx
->ac
, &args
);
2506 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2507 const nir_intrinsic_instr
*instr
)
2510 const nir_variable
*var
= get_image_variable(instr
);
2511 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2513 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2514 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2516 struct ac_image_args args
= { 0 };
2518 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2519 glsl_sampler_type_is_array(type
));
2521 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2522 args
.opcode
= ac_image_get_resinfo
;
2523 args
.lod
= ctx
->ac
.i32_0
;
2524 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2526 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2528 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2530 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2531 glsl_sampler_type_is_array(type
)) {
2532 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2533 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2534 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2535 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2537 if (ctx
->ac
.chip_class
>= GFX9
&&
2538 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2539 glsl_sampler_type_is_array(type
)) {
2540 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2541 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2548 static void emit_membar(struct ac_llvm_context
*ac
,
2549 const nir_intrinsic_instr
*instr
)
2551 unsigned waitcnt
= NOOP_WAITCNT
;
2553 switch (instr
->intrinsic
) {
2554 case nir_intrinsic_memory_barrier
:
2555 case nir_intrinsic_group_memory_barrier
:
2556 waitcnt
&= VM_CNT
& LGKM_CNT
;
2558 case nir_intrinsic_memory_barrier_atomic_counter
:
2559 case nir_intrinsic_memory_barrier_buffer
:
2560 case nir_intrinsic_memory_barrier_image
:
2563 case nir_intrinsic_memory_barrier_shared
:
2564 waitcnt
&= LGKM_CNT
;
2569 if (waitcnt
!= NOOP_WAITCNT
)
2570 ac_build_waitcnt(ac
, waitcnt
);
2573 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2575 /* SI only (thanks to a hw bug workaround):
2576 * The real barrier instruction isn’t needed, because an entire patch
2577 * always fits into a single wave.
2579 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2580 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2583 ac_build_s_barrier(ac
);
2586 static void emit_discard(struct ac_nir_context
*ctx
,
2587 const nir_intrinsic_instr
*instr
)
2591 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2592 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2593 get_src(ctx
, instr
->src
[0]),
2596 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2597 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2600 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2604 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2606 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2607 "llvm.amdgcn.ps.live",
2608 ctx
->ac
.i1
, NULL
, 0,
2609 AC_FUNC_ATTR_READNONE
);
2610 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2611 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2615 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2617 LLVMValueRef result
;
2618 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2619 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2620 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2622 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2626 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2628 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2629 LLVMValueRef result
;
2630 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2631 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2632 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2634 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2639 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2641 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2642 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2643 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2645 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2650 visit_first_invocation(struct ac_nir_context
*ctx
)
2652 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2654 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2655 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2656 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2658 ctx
->ac
.i64
, args
, 2,
2659 AC_FUNC_ATTR_NOUNWIND
|
2660 AC_FUNC_ATTR_READNONE
);
2662 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2666 visit_load_shared(struct ac_nir_context
*ctx
,
2667 const nir_intrinsic_instr
*instr
)
2669 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2671 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2673 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2674 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2675 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2676 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2679 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2680 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2684 visit_store_shared(struct ac_nir_context
*ctx
,
2685 const nir_intrinsic_instr
*instr
)
2687 LLVMValueRef derived_ptr
, data
,index
;
2688 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2690 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2691 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2693 int writemask
= nir_intrinsic_write_mask(instr
);
2694 for (int chan
= 0; chan
< 4; chan
++) {
2695 if (!(writemask
& (1 << chan
))) {
2698 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2699 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2700 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2701 LLVMBuildStore(builder
, data
, derived_ptr
);
2705 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2706 const nir_intrinsic_instr
*instr
,
2707 LLVMValueRef ptr
, int src_idx
)
2709 LLVMValueRef result
;
2710 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2712 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2713 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2714 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2715 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2717 LLVMAtomicOrderingSequentiallyConsistent
,
2718 LLVMAtomicOrderingSequentiallyConsistent
,
2720 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2722 LLVMAtomicRMWBinOp op
;
2723 switch (instr
->intrinsic
) {
2724 case nir_intrinsic_shared_atomic_add
:
2725 case nir_intrinsic_deref_atomic_add
:
2726 op
= LLVMAtomicRMWBinOpAdd
;
2728 case nir_intrinsic_shared_atomic_umin
:
2729 case nir_intrinsic_deref_atomic_umin
:
2730 op
= LLVMAtomicRMWBinOpUMin
;
2732 case nir_intrinsic_shared_atomic_umax
:
2733 case nir_intrinsic_deref_atomic_umax
:
2734 op
= LLVMAtomicRMWBinOpUMax
;
2736 case nir_intrinsic_shared_atomic_imin
:
2737 case nir_intrinsic_deref_atomic_imin
:
2738 op
= LLVMAtomicRMWBinOpMin
;
2740 case nir_intrinsic_shared_atomic_imax
:
2741 case nir_intrinsic_deref_atomic_imax
:
2742 op
= LLVMAtomicRMWBinOpMax
;
2744 case nir_intrinsic_shared_atomic_and
:
2745 case nir_intrinsic_deref_atomic_and
:
2746 op
= LLVMAtomicRMWBinOpAnd
;
2748 case nir_intrinsic_shared_atomic_or
:
2749 case nir_intrinsic_deref_atomic_or
:
2750 op
= LLVMAtomicRMWBinOpOr
;
2752 case nir_intrinsic_shared_atomic_xor
:
2753 case nir_intrinsic_deref_atomic_xor
:
2754 op
= LLVMAtomicRMWBinOpXor
;
2756 case nir_intrinsic_shared_atomic_exchange
:
2757 case nir_intrinsic_deref_atomic_exchange
:
2758 op
= LLVMAtomicRMWBinOpXchg
;
2764 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2765 LLVMAtomicOrderingSequentiallyConsistent
,
2771 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2773 LLVMValueRef values
[2];
2774 LLVMValueRef pos
[2];
2776 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2777 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2779 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2780 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2781 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2784 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2785 const nir_intrinsic_instr
*instr
)
2787 LLVMValueRef result
[4];
2788 LLVMValueRef interp_param
, attr_number
;
2791 LLVMValueRef src_c0
= NULL
;
2792 LLVMValueRef src_c1
= NULL
;
2793 LLVMValueRef src0
= NULL
;
2795 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2796 int input_index
= var
->data
.location
- VARYING_SLOT_VAR0
;
2797 switch (instr
->intrinsic
) {
2798 case nir_intrinsic_interp_deref_at_centroid
:
2799 location
= INTERP_CENTROID
;
2801 case nir_intrinsic_interp_deref_at_sample
:
2802 case nir_intrinsic_interp_deref_at_offset
:
2803 location
= INTERP_CENTER
;
2804 src0
= get_src(ctx
, instr
->src
[1]);
2810 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2811 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2812 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2813 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2814 LLVMValueRef sample_position
;
2815 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2817 /* fetch sample ID */
2818 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2820 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2821 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2822 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2823 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2825 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
2826 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2828 if (location
== INTERP_CENTER
) {
2829 LLVMValueRef ij_out
[2];
2830 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2833 * take the I then J parameters, and the DDX/Y for it, and
2834 * calculate the IJ inputs for the interpolator.
2835 * temp1 = ddx * offset/sample.x + I;
2836 * interp_param.I = ddy * offset/sample.y + temp1;
2837 * temp1 = ddx * offset/sample.x + J;
2838 * interp_param.J = ddy * offset/sample.y + temp1;
2840 for (unsigned i
= 0; i
< 2; i
++) {
2841 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2842 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2843 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2844 ddxy_out
, ix_ll
, "");
2845 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2846 ddxy_out
, iy_ll
, "");
2847 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2848 interp_param
, ix_ll
, "");
2849 LLVMValueRef temp1
, temp2
;
2851 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2854 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
2855 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
2857 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2858 temp2
, ctx
->ac
.i32
, "");
2860 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2864 for (chan
= 0; chan
< 4; chan
++) {
2865 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2868 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2869 interp_param
, ctx
->ac
.v2f32
, "");
2870 LLVMValueRef i
= LLVMBuildExtractElement(
2871 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2872 LLVMValueRef j
= LLVMBuildExtractElement(
2873 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2875 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2876 llvm_chan
, attr_number
,
2877 ctx
->abi
->prim_mask
, i
, j
);
2879 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2880 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2881 llvm_chan
, attr_number
,
2882 ctx
->abi
->prim_mask
);
2885 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2886 var
->data
.location_frac
);
2889 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2890 nir_intrinsic_instr
*instr
)
2892 LLVMValueRef result
= NULL
;
2894 switch (instr
->intrinsic
) {
2895 case nir_intrinsic_ballot
:
2896 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2898 case nir_intrinsic_read_invocation
:
2899 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2900 get_src(ctx
, instr
->src
[1]));
2902 case nir_intrinsic_read_first_invocation
:
2903 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2905 case nir_intrinsic_load_subgroup_invocation
:
2906 result
= ac_get_thread_id(&ctx
->ac
);
2908 case nir_intrinsic_load_work_group_id
: {
2909 LLVMValueRef values
[3];
2911 for (int i
= 0; i
< 3; i
++) {
2912 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2913 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2916 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2919 case nir_intrinsic_load_base_vertex
:
2920 case nir_intrinsic_load_first_vertex
:
2921 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2923 case nir_intrinsic_load_local_group_size
:
2924 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2926 case nir_intrinsic_load_vertex_id
:
2927 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2928 ctx
->abi
->base_vertex
, "");
2930 case nir_intrinsic_load_vertex_id_zero_base
: {
2931 result
= ctx
->abi
->vertex_id
;
2934 case nir_intrinsic_load_local_invocation_id
: {
2935 result
= ctx
->abi
->local_invocation_ids
;
2938 case nir_intrinsic_load_base_instance
:
2939 result
= ctx
->abi
->start_instance
;
2941 case nir_intrinsic_load_draw_id
:
2942 result
= ctx
->abi
->draw_id
;
2944 case nir_intrinsic_load_view_index
:
2945 result
= ctx
->abi
->view_index
;
2947 case nir_intrinsic_load_invocation_id
:
2948 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2949 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2951 result
= ctx
->abi
->gs_invocation_id
;
2953 case nir_intrinsic_load_primitive_id
:
2954 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2955 result
= ctx
->abi
->gs_prim_id
;
2956 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2957 result
= ctx
->abi
->tcs_patch_id
;
2958 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2959 result
= ctx
->abi
->tes_patch_id
;
2961 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2963 case nir_intrinsic_load_sample_id
:
2964 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2966 case nir_intrinsic_load_sample_pos
:
2967 result
= load_sample_pos(ctx
);
2969 case nir_intrinsic_load_sample_mask_in
:
2970 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2972 case nir_intrinsic_load_frag_coord
: {
2973 LLVMValueRef values
[4] = {
2974 ctx
->abi
->frag_pos
[0],
2975 ctx
->abi
->frag_pos
[1],
2976 ctx
->abi
->frag_pos
[2],
2977 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2979 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2982 case nir_intrinsic_load_front_face
:
2983 result
= ctx
->abi
->front_face
;
2985 case nir_intrinsic_load_helper_invocation
:
2986 result
= visit_load_helper_invocation(ctx
);
2988 case nir_intrinsic_load_instance_id
:
2989 result
= ctx
->abi
->instance_id
;
2991 case nir_intrinsic_load_num_work_groups
:
2992 result
= ctx
->abi
->num_work_groups
;
2994 case nir_intrinsic_load_local_invocation_index
:
2995 result
= visit_load_local_invocation_index(ctx
);
2997 case nir_intrinsic_load_subgroup_id
:
2998 result
= visit_load_subgroup_id(ctx
);
3000 case nir_intrinsic_load_num_subgroups
:
3001 result
= visit_load_num_subgroups(ctx
);
3003 case nir_intrinsic_first_invocation
:
3004 result
= visit_first_invocation(ctx
);
3006 case nir_intrinsic_load_push_constant
:
3007 result
= visit_load_push_constant(ctx
, instr
);
3009 case nir_intrinsic_vulkan_resource_index
: {
3010 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3011 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3012 unsigned binding
= nir_intrinsic_binding(instr
);
3014 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3018 case nir_intrinsic_vulkan_resource_reindex
:
3019 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3021 case nir_intrinsic_store_ssbo
:
3022 visit_store_ssbo(ctx
, instr
);
3024 case nir_intrinsic_load_ssbo
:
3025 result
= visit_load_buffer(ctx
, instr
);
3027 case nir_intrinsic_ssbo_atomic_add
:
3028 case nir_intrinsic_ssbo_atomic_imin
:
3029 case nir_intrinsic_ssbo_atomic_umin
:
3030 case nir_intrinsic_ssbo_atomic_imax
:
3031 case nir_intrinsic_ssbo_atomic_umax
:
3032 case nir_intrinsic_ssbo_atomic_and
:
3033 case nir_intrinsic_ssbo_atomic_or
:
3034 case nir_intrinsic_ssbo_atomic_xor
:
3035 case nir_intrinsic_ssbo_atomic_exchange
:
3036 case nir_intrinsic_ssbo_atomic_comp_swap
:
3037 result
= visit_atomic_ssbo(ctx
, instr
);
3039 case nir_intrinsic_load_ubo
:
3040 result
= visit_load_ubo_buffer(ctx
, instr
);
3042 case nir_intrinsic_get_buffer_size
:
3043 result
= visit_get_buffer_size(ctx
, instr
);
3045 case nir_intrinsic_load_deref
:
3046 result
= visit_load_var(ctx
, instr
);
3048 case nir_intrinsic_store_deref
:
3049 visit_store_var(ctx
, instr
);
3051 case nir_intrinsic_load_shared
:
3052 result
= visit_load_shared(ctx
, instr
);
3054 case nir_intrinsic_store_shared
:
3055 visit_store_shared(ctx
, instr
);
3057 case nir_intrinsic_image_deref_samples
:
3058 result
= visit_image_samples(ctx
, instr
);
3060 case nir_intrinsic_image_deref_load
:
3061 result
= visit_image_load(ctx
, instr
);
3063 case nir_intrinsic_image_deref_store
:
3064 visit_image_store(ctx
, instr
);
3066 case nir_intrinsic_image_deref_atomic_add
:
3067 case nir_intrinsic_image_deref_atomic_min
:
3068 case nir_intrinsic_image_deref_atomic_max
:
3069 case nir_intrinsic_image_deref_atomic_and
:
3070 case nir_intrinsic_image_deref_atomic_or
:
3071 case nir_intrinsic_image_deref_atomic_xor
:
3072 case nir_intrinsic_image_deref_atomic_exchange
:
3073 case nir_intrinsic_image_deref_atomic_comp_swap
:
3074 result
= visit_image_atomic(ctx
, instr
);
3076 case nir_intrinsic_image_deref_size
:
3077 result
= visit_image_size(ctx
, instr
);
3079 case nir_intrinsic_shader_clock
:
3080 result
= ac_build_shader_clock(&ctx
->ac
);
3082 case nir_intrinsic_discard
:
3083 case nir_intrinsic_discard_if
:
3084 emit_discard(ctx
, instr
);
3086 case nir_intrinsic_memory_barrier
:
3087 case nir_intrinsic_group_memory_barrier
:
3088 case nir_intrinsic_memory_barrier_atomic_counter
:
3089 case nir_intrinsic_memory_barrier_buffer
:
3090 case nir_intrinsic_memory_barrier_image
:
3091 case nir_intrinsic_memory_barrier_shared
:
3092 emit_membar(&ctx
->ac
, instr
);
3094 case nir_intrinsic_barrier
:
3095 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3097 case nir_intrinsic_shared_atomic_add
:
3098 case nir_intrinsic_shared_atomic_imin
:
3099 case nir_intrinsic_shared_atomic_umin
:
3100 case nir_intrinsic_shared_atomic_imax
:
3101 case nir_intrinsic_shared_atomic_umax
:
3102 case nir_intrinsic_shared_atomic_and
:
3103 case nir_intrinsic_shared_atomic_or
:
3104 case nir_intrinsic_shared_atomic_xor
:
3105 case nir_intrinsic_shared_atomic_exchange
:
3106 case nir_intrinsic_shared_atomic_comp_swap
: {
3107 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3108 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3111 case nir_intrinsic_deref_atomic_add
:
3112 case nir_intrinsic_deref_atomic_imin
:
3113 case nir_intrinsic_deref_atomic_umin
:
3114 case nir_intrinsic_deref_atomic_imax
:
3115 case nir_intrinsic_deref_atomic_umax
:
3116 case nir_intrinsic_deref_atomic_and
:
3117 case nir_intrinsic_deref_atomic_or
:
3118 case nir_intrinsic_deref_atomic_xor
:
3119 case nir_intrinsic_deref_atomic_exchange
:
3120 case nir_intrinsic_deref_atomic_comp_swap
: {
3121 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3122 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3125 case nir_intrinsic_interp_deref_at_centroid
:
3126 case nir_intrinsic_interp_deref_at_sample
:
3127 case nir_intrinsic_interp_deref_at_offset
:
3128 result
= visit_interp(ctx
, instr
);
3130 case nir_intrinsic_emit_vertex
:
3131 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3133 case nir_intrinsic_end_primitive
:
3134 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3136 case nir_intrinsic_load_tess_coord
:
3137 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3139 case nir_intrinsic_load_tess_level_outer
:
3140 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3142 case nir_intrinsic_load_tess_level_inner
:
3143 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3145 case nir_intrinsic_load_patch_vertices_in
:
3146 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3148 case nir_intrinsic_vote_all
: {
3149 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3150 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3153 case nir_intrinsic_vote_any
: {
3154 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3155 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3158 case nir_intrinsic_shuffle
:
3159 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3160 get_src(ctx
, instr
->src
[1]));
3162 case nir_intrinsic_reduce
:
3163 result
= ac_build_reduce(&ctx
->ac
,
3164 get_src(ctx
, instr
->src
[0]),
3165 instr
->const_index
[0],
3166 instr
->const_index
[1]);
3168 case nir_intrinsic_inclusive_scan
:
3169 result
= ac_build_inclusive_scan(&ctx
->ac
,
3170 get_src(ctx
, instr
->src
[0]),
3171 instr
->const_index
[0]);
3173 case nir_intrinsic_exclusive_scan
:
3174 result
= ac_build_exclusive_scan(&ctx
->ac
,
3175 get_src(ctx
, instr
->src
[0]),
3176 instr
->const_index
[0]);
3178 case nir_intrinsic_quad_broadcast
: {
3179 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3180 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3181 lane
, lane
, lane
, lane
);
3184 case nir_intrinsic_quad_swap_horizontal
:
3185 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3187 case nir_intrinsic_quad_swap_vertical
:
3188 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3190 case nir_intrinsic_quad_swap_diagonal
:
3191 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3194 fprintf(stderr
, "Unknown intrinsic: ");
3195 nir_print_instr(&instr
->instr
, stderr
);
3196 fprintf(stderr
, "\n");
3200 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3204 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3205 nir_deref_instr
*deref_instr
,
3206 enum ac_descriptor_type desc_type
,
3207 const nir_tex_instr
*tex_instr
,
3208 bool image
, bool write
)
3210 LLVMValueRef index
= NULL
;
3211 unsigned constant_index
= 0;
3212 unsigned descriptor_set
;
3213 unsigned base_index
;
3214 bool bindless
= false;
3217 assert(tex_instr
&& !image
);
3219 base_index
= tex_instr
->sampler_index
;
3221 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3222 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3226 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3227 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3229 constant_index
+= array_size
* const_value
->u32
[0];
3231 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3233 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3234 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3239 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3242 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3244 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3245 base_index
= deref_instr
->var
->data
.binding
;
3248 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3251 constant_index
, index
,
3252 desc_type
, image
, write
, bindless
);
3255 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3258 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3259 * filtering manually. The driver sets img7 to a mask clearing
3260 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3261 * s_and_b32 samp0, samp0, img7
3264 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3266 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3267 LLVMValueRef res
, LLVMValueRef samp
)
3269 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3270 LLVMValueRef img7
, samp0
;
3272 if (ctx
->ac
.chip_class
>= VI
)
3275 img7
= LLVMBuildExtractElement(builder
, res
,
3276 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3277 samp0
= LLVMBuildExtractElement(builder
, samp
,
3278 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3279 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3280 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3281 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3284 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3285 nir_tex_instr
*instr
,
3286 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3287 LLVMValueRef
*fmask_ptr
)
3289 nir_deref_instr
*texture_deref_instr
= NULL
;
3290 nir_deref_instr
*sampler_deref_instr
= NULL
;
3292 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3293 switch (instr
->src
[i
].src_type
) {
3294 case nir_tex_src_texture_deref
:
3295 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3297 case nir_tex_src_sampler_deref
:
3298 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3305 if (!sampler_deref_instr
)
3306 sampler_deref_instr
= texture_deref_instr
;
3308 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3309 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3311 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3313 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3314 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3315 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3317 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3318 instr
->op
== nir_texop_samples_identical
))
3319 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3322 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3325 coord
= ac_to_float(ctx
, coord
);
3326 coord
= ac_build_round(ctx
, coord
);
3327 coord
= ac_to_integer(ctx
, coord
);
3331 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3333 LLVMValueRef result
= NULL
;
3334 struct ac_image_args args
= { 0 };
3335 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3336 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3337 unsigned offset_src
= 0;
3339 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3341 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3342 switch (instr
->src
[i
].src_type
) {
3343 case nir_tex_src_coord
: {
3344 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3345 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3346 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3349 case nir_tex_src_projector
:
3351 case nir_tex_src_comparator
:
3352 if (instr
->is_shadow
)
3353 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3355 case nir_tex_src_offset
:
3356 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3359 case nir_tex_src_bias
:
3360 if (instr
->op
== nir_texop_txb
)
3361 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3363 case nir_tex_src_lod
: {
3364 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3366 if (val
&& val
->i32
[0] == 0)
3367 args
.level_zero
= true;
3369 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3372 case nir_tex_src_ms_index
:
3373 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3375 case nir_tex_src_ms_mcs
:
3377 case nir_tex_src_ddx
:
3378 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3380 case nir_tex_src_ddy
:
3381 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3383 case nir_tex_src_texture_offset
:
3384 case nir_tex_src_sampler_offset
:
3385 case nir_tex_src_plane
:
3391 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3392 result
= get_buffer_size(ctx
, args
.resource
, true);
3396 if (instr
->op
== nir_texop_texture_samples
) {
3397 LLVMValueRef res
, samples
, is_msaa
;
3398 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3399 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3400 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3401 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3402 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3403 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3404 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3405 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3406 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3408 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3409 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3410 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3411 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3412 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3414 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3420 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3421 LLVMValueRef offset
[3], pack
;
3422 for (unsigned chan
= 0; chan
< 3; ++chan
)
3423 offset
[chan
] = ctx
->ac
.i32_0
;
3425 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3426 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3427 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3428 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3429 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3431 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3432 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3434 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3435 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3439 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3440 * so the depth comparison value isn't clamped for Z16 and
3441 * Z24 anymore. Do it manually here.
3443 * It's unnecessary if the original texture format was
3444 * Z32_FLOAT, but we don't know that here.
3446 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3447 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3449 /* pack derivatives */
3451 int num_src_deriv_channels
, num_dest_deriv_channels
;
3452 switch (instr
->sampler_dim
) {
3453 case GLSL_SAMPLER_DIM_3D
:
3454 case GLSL_SAMPLER_DIM_CUBE
:
3455 num_src_deriv_channels
= 3;
3456 num_dest_deriv_channels
= 3;
3458 case GLSL_SAMPLER_DIM_2D
:
3460 num_src_deriv_channels
= 2;
3461 num_dest_deriv_channels
= 2;
3463 case GLSL_SAMPLER_DIM_1D
:
3464 num_src_deriv_channels
= 1;
3465 if (ctx
->ac
.chip_class
>= GFX9
) {
3466 num_dest_deriv_channels
= 2;
3468 num_dest_deriv_channels
= 1;
3473 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3474 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3475 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3476 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3477 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3479 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3480 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3481 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3485 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3486 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3487 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3488 if (instr
->coord_components
== 3)
3489 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3490 ac_prepare_cube_coords(&ctx
->ac
,
3491 instr
->op
== nir_texop_txd
, instr
->is_array
,
3492 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3495 /* Texture coordinates fixups */
3496 if (instr
->coord_components
> 1 &&
3497 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3499 instr
->op
!= nir_texop_txf
) {
3500 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3503 if (instr
->coord_components
> 2 &&
3504 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3505 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3506 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3507 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3509 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3510 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3513 if (ctx
->ac
.chip_class
>= GFX9
&&
3514 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3515 instr
->op
!= nir_texop_lod
) {
3516 LLVMValueRef filler
;
3517 if (instr
->op
== nir_texop_txf
)
3518 filler
= ctx
->ac
.i32_0
;
3520 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3522 if (instr
->is_array
)
3523 args
.coords
[2] = args
.coords
[1];
3524 args
.coords
[1] = filler
;
3527 /* Pack sample index */
3528 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3529 args
.coords
[instr
->coord_components
] = sample_index
;
3531 if (instr
->op
== nir_texop_samples_identical
) {
3532 struct ac_image_args txf_args
= { 0 };
3533 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3535 txf_args
.dmask
= 0xf;
3536 txf_args
.resource
= fmask_ptr
;
3537 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3538 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3540 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3541 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3545 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3546 instr
->op
!= nir_texop_txs
) {
3547 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3548 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3549 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3550 instr
->is_array
? args
.coords
[2] : NULL
,
3551 args
.coords
[sample_chan
], fmask_ptr
);
3554 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3555 nir_const_value
*const_offset
=
3556 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3557 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3558 assert(const_offset
);
3559 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3560 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3561 args
.coords
[i
] = LLVMBuildAdd(
3562 ctx
->ac
.builder
, args
.coords
[i
],
3563 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3568 /* TODO TG4 support */
3570 if (instr
->op
== nir_texop_tg4
) {
3571 if (instr
->is_shadow
)
3574 args
.dmask
= 1 << instr
->component
;
3577 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3578 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3579 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3581 if (instr
->op
== nir_texop_query_levels
)
3582 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3583 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3584 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3585 instr
->op
!= nir_texop_tg4
)
3586 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3587 else if (instr
->op
== nir_texop_txs
&&
3588 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3590 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3591 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3592 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3593 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3594 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3595 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3596 instr
->op
== nir_texop_txs
&&
3597 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3599 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3600 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3601 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3603 } else if (instr
->dest
.ssa
.num_components
!= 4)
3604 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3608 assert(instr
->dest
.is_ssa
);
3609 result
= ac_to_integer(&ctx
->ac
, result
);
3610 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3615 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3617 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3618 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3620 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3621 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3624 static void visit_post_phi(struct ac_nir_context
*ctx
,
3625 nir_phi_instr
*instr
,
3626 LLVMValueRef llvm_phi
)
3628 nir_foreach_phi_src(src
, instr
) {
3629 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3630 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3632 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3636 static void phi_post_pass(struct ac_nir_context
*ctx
)
3638 hash_table_foreach(ctx
->phis
, entry
) {
3639 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3640 (LLVMValueRef
)entry
->data
);
3645 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3646 const nir_ssa_undef_instr
*instr
)
3648 unsigned num_components
= instr
->def
.num_components
;
3649 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3652 if (num_components
== 1)
3653 undef
= LLVMGetUndef(type
);
3655 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3657 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3660 static void visit_jump(struct ac_llvm_context
*ctx
,
3661 const nir_jump_instr
*instr
)
3663 switch (instr
->type
) {
3664 case nir_jump_break
:
3665 ac_build_break(ctx
);
3667 case nir_jump_continue
:
3668 ac_build_continue(ctx
);
3671 fprintf(stderr
, "Unknown NIR jump instr: ");
3672 nir_print_instr(&instr
->instr
, stderr
);
3673 fprintf(stderr
, "\n");
3678 static void visit_deref(struct ac_nir_context
*ctx
,
3679 nir_deref_instr
*instr
)
3681 if (instr
->mode
!= nir_var_shared
)
3684 LLVMValueRef result
= NULL
;
3685 switch(instr
->deref_type
) {
3686 case nir_deref_type_var
: {
3687 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3688 result
= entry
->data
;
3691 case nir_deref_type_struct
:
3692 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3693 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3695 case nir_deref_type_array
:
3696 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3697 get_src(ctx
, instr
->arr
.index
));
3700 unreachable("Unhandled deref_instr deref type");
3703 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3706 static void visit_cf_list(struct ac_nir_context
*ctx
,
3707 struct exec_list
*list
);
3709 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3711 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3712 nir_foreach_instr(instr
, block
)
3714 switch (instr
->type
) {
3715 case nir_instr_type_alu
:
3716 visit_alu(ctx
, nir_instr_as_alu(instr
));
3718 case nir_instr_type_load_const
:
3719 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3721 case nir_instr_type_intrinsic
:
3722 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3724 case nir_instr_type_tex
:
3725 visit_tex(ctx
, nir_instr_as_tex(instr
));
3727 case nir_instr_type_phi
:
3728 visit_phi(ctx
, nir_instr_as_phi(instr
));
3730 case nir_instr_type_ssa_undef
:
3731 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3733 case nir_instr_type_jump
:
3734 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3736 case nir_instr_type_deref
:
3737 visit_deref(ctx
, nir_instr_as_deref(instr
));
3740 fprintf(stderr
, "Unknown NIR instr type: ");
3741 nir_print_instr(instr
, stderr
);
3742 fprintf(stderr
, "\n");
3747 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3750 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3752 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3754 nir_block
*then_block
=
3755 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3757 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3759 visit_cf_list(ctx
, &if_stmt
->then_list
);
3761 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3762 nir_block
*else_block
=
3763 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3765 ac_build_else(&ctx
->ac
, else_block
->index
);
3766 visit_cf_list(ctx
, &if_stmt
->else_list
);
3769 ac_build_endif(&ctx
->ac
, then_block
->index
);
3772 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3774 nir_block
*first_loop_block
=
3775 (nir_block
*) exec_list_get_head(&loop
->body
);
3777 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3779 visit_cf_list(ctx
, &loop
->body
);
3781 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3784 static void visit_cf_list(struct ac_nir_context
*ctx
,
3785 struct exec_list
*list
)
3787 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3789 switch (node
->type
) {
3790 case nir_cf_node_block
:
3791 visit_block(ctx
, nir_cf_node_as_block(node
));
3794 case nir_cf_node_if
:
3795 visit_if(ctx
, nir_cf_node_as_if(node
));
3798 case nir_cf_node_loop
:
3799 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3809 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3810 struct ac_shader_abi
*abi
,
3811 struct nir_shader
*nir
,
3812 struct nir_variable
*variable
,
3813 gl_shader_stage stage
)
3815 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3816 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3818 /* tess ctrl has it's own load/store paths for outputs */
3819 if (stage
== MESA_SHADER_TESS_CTRL
)
3822 if (stage
== MESA_SHADER_VERTEX
||
3823 stage
== MESA_SHADER_TESS_EVAL
||
3824 stage
== MESA_SHADER_GEOMETRY
) {
3825 int idx
= variable
->data
.location
+ variable
->data
.index
;
3826 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3827 int length
= nir
->info
.clip_distance_array_size
+
3828 nir
->info
.cull_distance_array_size
;
3837 bool is_16bit
= glsl_type_is_16bit(variable
->type
);
3838 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
3839 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3840 for (unsigned chan
= 0; chan
< 4; chan
++) {
3841 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3842 ac_build_alloca_undef(ctx
, type
, "");
3848 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3849 enum glsl_base_type type
)
3853 case GLSL_TYPE_UINT
:
3854 case GLSL_TYPE_BOOL
:
3855 case GLSL_TYPE_SUBROUTINE
:
3857 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3859 case GLSL_TYPE_INT64
:
3860 case GLSL_TYPE_UINT64
:
3862 case GLSL_TYPE_DOUBLE
:
3865 unreachable("unknown GLSL type");
3870 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3871 const struct glsl_type
*type
)
3873 if (glsl_type_is_scalar(type
)) {
3874 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3877 if (glsl_type_is_vector(type
)) {
3878 return LLVMVectorType(
3879 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3880 glsl_get_vector_elements(type
));
3883 if (glsl_type_is_matrix(type
)) {
3884 return LLVMArrayType(
3885 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3886 glsl_get_matrix_columns(type
));
3889 if (glsl_type_is_array(type
)) {
3890 return LLVMArrayType(
3891 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3892 glsl_get_length(type
));
3895 assert(glsl_type_is_struct(type
));
3897 LLVMTypeRef member_types
[glsl_get_length(type
)];
3899 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3901 glsl_to_llvm_type(ac
,
3902 glsl_get_struct_field(type
, i
));
3905 return LLVMStructTypeInContext(ac
->context
, member_types
,
3906 glsl_get_length(type
), false);
3910 setup_locals(struct ac_nir_context
*ctx
,
3911 struct nir_function
*func
)
3914 ctx
->num_locals
= 0;
3915 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3916 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3917 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3918 variable
->data
.location_frac
= 0;
3919 ctx
->num_locals
+= attrib_count
;
3921 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3925 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3926 for (j
= 0; j
< 4; j
++) {
3927 ctx
->locals
[i
* 4 + j
] =
3928 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3934 setup_shared(struct ac_nir_context
*ctx
,
3935 struct nir_shader
*nir
)
3937 nir_foreach_variable(variable
, &nir
->shared
) {
3938 LLVMValueRef shared
=
3939 LLVMAddGlobalInAddressSpace(
3940 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3941 variable
->name
? variable
->name
: "",
3943 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3947 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3948 struct nir_shader
*nir
)
3950 struct ac_nir_context ctx
= {};
3951 struct nir_function
*func
;
3956 ctx
.stage
= nir
->info
.stage
;
3958 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3960 nir_foreach_variable(variable
, &nir
->outputs
)
3961 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3964 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3965 _mesa_key_pointer_equal
);
3966 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3967 _mesa_key_pointer_equal
);
3968 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3969 _mesa_key_pointer_equal
);
3971 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3973 nir_index_ssa_defs(func
->impl
);
3974 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3976 setup_locals(&ctx
, func
);
3978 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3979 setup_shared(&ctx
, nir
);
3981 visit_cf_list(&ctx
, &func
->impl
->body
);
3982 phi_post_pass(&ctx
);
3984 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3985 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3990 ralloc_free(ctx
.defs
);
3991 ralloc_free(ctx
.phis
);
3992 ralloc_free(ctx
.vars
);
3996 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3998 /* While it would be nice not to have this flag, we are constrained
3999 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4002 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4004 /* TODO: Indirect indexing of GS inputs is unimplemented.
4006 * TCS and TES load inputs directly from LDS or offchip memory, so
4007 * indirect indexing is trivial.
4009 nir_variable_mode indirect_mask
= 0;
4010 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4011 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4012 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4013 !llvm_has_working_vgpr_indexing
)) {
4014 indirect_mask
|= nir_var_shader_in
;
4016 if (!llvm_has_working_vgpr_indexing
&&
4017 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4018 indirect_mask
|= nir_var_shader_out
;
4020 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4021 * smart enough to handle indirects without causing excess spilling
4022 * causing the gpu to hang.
4024 * See the following thread for more details of the problem:
4025 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4027 indirect_mask
|= nir_var_local
;
4029 nir_lower_indirect_derefs(nir
, indirect_mask
);