2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
36 struct ac_nir_context
{
37 struct ac_llvm_context ac
;
38 struct ac_shader_abi
*abi
;
40 gl_shader_stage stage
;
42 LLVMValueRef
*ssa_defs
;
44 struct hash_table
*defs
;
45 struct hash_table
*phis
;
46 struct hash_table
*vars
;
48 LLVMValueRef main_function
;
49 LLVMBasicBlockRef continue_block
;
50 LLVMBasicBlockRef break_block
;
56 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
273 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
274 ac_to_integer(ctx
, src2
), "");
277 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
278 LLVMIntPredicate pred
,
279 LLVMValueRef src0
, LLVMValueRef src1
)
281 return LLVMBuildSelect(ctx
->builder
,
282 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
287 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
290 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
291 LLVMBuildNeg(ctx
->builder
, src0
, ""));
294 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
296 LLVMValueRef src0
, LLVMValueRef src1
)
298 LLVMTypeRef ret_type
;
299 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
301 LLVMValueRef params
[] = { src0
, src1
};
302 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
305 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
306 params
, 2, AC_FUNC_ATTR_READNONE
);
308 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
309 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
313 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
316 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
319 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
322 src0
= ac_to_float(ctx
, src0
);
323 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
324 return LLVMBuildSExt(ctx
->builder
,
325 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
329 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
333 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
338 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
341 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
344 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
345 return LLVMBuildSExt(ctx
->builder
,
346 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
350 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
354 LLVMValueRef cond
= NULL
;
356 src0
= ac_to_float(ctx
, src0
);
357 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
359 if (ctx
->chip_class
>= VI
) {
360 LLVMValueRef args
[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
363 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
364 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
367 /* need to convert back up to f32 */
368 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
370 if (ctx
->chip_class
>= VI
)
371 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
377 LLVMValueRef temp
, cond2
;
378 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
379 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
380 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
382 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
383 temp
, ctx
->f32_0
, "");
384 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
385 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
391 LLVMValueRef src0
, LLVMValueRef src1
)
393 LLVMValueRef dst64
, result
;
394 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
395 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
397 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
398 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
399 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
403 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
404 LLVMValueRef src0
, LLVMValueRef src1
)
406 LLVMValueRef dst64
, result
;
407 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
408 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
410 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
411 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
412 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
416 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
418 const LLVMValueRef srcs
[3])
422 if (HAVE_LLVM
< 0x0700) {
423 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
424 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
425 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
427 /* FIXME: LLVM 7 returns incorrect result when count is 0.
428 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
430 LLVMValueRef zero
= LLVMConstInt(ctx
->i32
, 0, false);
431 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
432 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
434 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
435 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
436 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
442 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
443 LLVMValueRef src0
, LLVMValueRef src1
,
444 LLVMValueRef src2
, LLVMValueRef src3
)
446 LLVMValueRef bfi_args
[3], result
;
448 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
449 LLVMBuildSub(ctx
->builder
,
450 LLVMBuildShl(ctx
->builder
,
455 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
458 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
461 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
462 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
464 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
465 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
466 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
468 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
472 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
475 LLVMValueRef comp
[2];
477 src0
= ac_to_float(ctx
, src0
);
478 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
479 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
481 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
485 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
488 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
489 LLVMValueRef temps
[2], result
, val
;
492 for (i
= 0; i
< 2; i
++) {
493 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
494 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
495 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
496 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
499 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
501 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
506 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
514 if (op
== nir_op_fddx_fine
)
515 mask
= AC_TID_MASK_LEFT
;
516 else if (op
== nir_op_fddy_fine
)
517 mask
= AC_TID_MASK_TOP
;
519 mask
= AC_TID_MASK_TOP_LEFT
;
521 /* for DDX we want to next X pixel, DDY next Y pixel. */
522 if (op
== nir_op_fddx_fine
||
523 op
== nir_op_fddx_coarse
||
529 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
534 * this takes an I,J coordinate pair,
535 * and works out the X and Y derivatives.
536 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
538 static LLVMValueRef
emit_ddxy_interp(
539 struct ac_nir_context
*ctx
,
540 LLVMValueRef interp_ij
)
542 LLVMValueRef result
[4], a
;
545 for (i
= 0; i
< 2; i
++) {
546 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
547 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
548 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
549 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
551 return ac_build_gather_values(&ctx
->ac
, result
, 4);
554 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
556 LLVMValueRef src
[4], result
= NULL
;
557 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
558 unsigned src_components
;
559 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
561 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
568 case nir_op_pack_half_2x16
:
571 case nir_op_unpack_half_2x16
:
574 case nir_op_cube_face_coord
:
575 case nir_op_cube_face_index
:
579 src_components
= num_components
;
582 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
583 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
591 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
592 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
595 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
598 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
601 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
604 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
605 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
606 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
609 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
610 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
611 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
614 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
617 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
620 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
623 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
626 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
627 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
628 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
629 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
630 ac_to_float_type(&ctx
->ac
, def_type
), result
);
631 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
632 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
635 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
636 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
637 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
640 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
643 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
646 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
649 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
650 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
651 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
654 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
655 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
659 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
662 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
665 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
668 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
669 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
670 LLVMTypeOf(src
[0]), ""),
674 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
675 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
676 LLVMTypeOf(src
[0]), ""),
680 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
681 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
682 LLVMTypeOf(src
[0]), ""),
686 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
689 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
692 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
701 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
704 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
707 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
710 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
713 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
716 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
717 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
720 result
= emit_iabs(&ctx
->ac
, src
[0]);
723 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
726 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
729 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
732 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
735 result
= ac_build_isign(&ctx
->ac
, src
[0],
736 instr
->dest
.dest
.ssa
.bit_size
);
739 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
740 result
= ac_build_fsign(&ctx
->ac
, src
[0],
741 instr
->dest
.dest
.ssa
.bit_size
);
744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
749 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
752 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
753 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
755 case nir_op_fround_even
:
756 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
757 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
760 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
761 result
= ac_build_fract(&ctx
->ac
, src
[0],
762 instr
->dest
.dest
.ssa
.bit_size
);
765 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
766 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
769 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
773 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
774 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
777 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
781 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
782 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
785 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
786 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
787 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
790 case nir_op_frexp_exp
:
791 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
792 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
793 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
796 case nir_op_frexp_sig
:
797 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
798 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
799 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
802 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
803 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
804 if (ctx
->ac
.chip_class
< GFX9
&&
805 instr
->dest
.dest
.ssa
.bit_size
== 32) {
806 /* Only pre-GFX9 chips do not flush denorms. */
807 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
808 ac_to_float_type(&ctx
->ac
, def_type
),
813 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
814 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
815 if (ctx
->ac
.chip_class
< GFX9
&&
816 instr
->dest
.dest
.ssa
.bit_size
== 32) {
817 /* Only pre-GFX9 chips do not flush denorms. */
818 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
819 ac_to_float_type(&ctx
->ac
, def_type
),
824 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
825 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
828 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
829 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
830 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
832 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
834 case nir_op_ibitfield_extract
:
835 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
837 case nir_op_ubitfield_extract
:
838 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
840 case nir_op_bitfield_insert
:
841 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
843 case nir_op_bitfield_reverse
:
844 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
846 case nir_op_bit_count
:
847 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
848 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
850 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
851 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
857 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
858 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
859 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
877 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
885 case nir_op_f2f16_rtz
:
886 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
887 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
888 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
889 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
891 case nir_op_f2f16_rtne
:
892 case nir_op_f2f16_undef
:
895 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
896 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
897 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
899 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
904 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
905 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
906 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
908 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
913 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
914 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
915 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
917 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
920 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
922 case nir_op_find_lsb
:
923 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
924 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
926 case nir_op_ufind_msb
:
927 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
928 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
930 case nir_op_ifind_msb
:
931 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
932 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
934 case nir_op_uadd_carry
:
935 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
936 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
937 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
939 case nir_op_usub_borrow
:
940 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
941 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
942 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
945 result
= emit_b2f(&ctx
->ac
, src
[0]);
948 result
= emit_f2b(&ctx
->ac
, src
[0]);
951 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
954 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
955 result
= emit_i2b(&ctx
->ac
, src
[0]);
957 case nir_op_fquantize2f16
:
958 result
= emit_f2f16(&ctx
->ac
, src
[0]);
960 case nir_op_umul_high
:
961 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
962 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
963 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
965 case nir_op_imul_high
:
966 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
967 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
968 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
970 case nir_op_pack_half_2x16
:
971 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
973 case nir_op_unpack_half_2x16
:
974 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
978 case nir_op_fddx_fine
:
979 case nir_op_fddy_fine
:
980 case nir_op_fddx_coarse
:
981 case nir_op_fddy_coarse
:
982 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
985 case nir_op_unpack_64_2x32_split_x
: {
986 assert(ac_get_llvm_num_components(src
[0]) == 1);
987 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
990 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
995 case nir_op_unpack_64_2x32_split_y
: {
996 assert(ac_get_llvm_num_components(src
[0]) == 1);
997 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1000 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1005 case nir_op_pack_64_2x32_split
: {
1006 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1007 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1008 src
[0], ctx
->ac
.i32_0
, "");
1009 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
1010 src
[1], ctx
->ac
.i32_1
, "");
1011 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1015 case nir_op_cube_face_coord
: {
1016 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1017 LLVMValueRef results
[2];
1019 for (unsigned chan
= 0; chan
< 3; chan
++)
1020 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1021 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1022 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1023 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1024 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1025 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1029 case nir_op_cube_face_index
: {
1030 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1032 for (unsigned chan
= 0; chan
< 3; chan
++)
1033 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1034 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1035 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1040 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1041 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1042 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1043 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1046 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1047 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1050 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1051 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1054 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1055 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1056 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1057 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1060 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1061 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1064 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1065 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1067 case nir_op_fmed3
: {
1068 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1069 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1070 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1071 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1072 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1073 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1074 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1075 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1078 case nir_op_imed3
: {
1079 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1080 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1081 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1082 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1085 case nir_op_umed3
: {
1086 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1087 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1088 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1089 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1094 fprintf(stderr
, "Unknown NIR alu instr: ");
1095 nir_print_instr(&instr
->instr
, stderr
);
1096 fprintf(stderr
, "\n");
1101 assert(instr
->dest
.dest
.is_ssa
);
1102 result
= ac_to_integer(&ctx
->ac
, result
);
1103 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1107 static void visit_load_const(struct ac_nir_context
*ctx
,
1108 const nir_load_const_instr
*instr
)
1110 LLVMValueRef values
[4], value
= NULL
;
1111 LLVMTypeRef element_type
=
1112 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1114 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1115 switch (instr
->def
.bit_size
) {
1117 values
[i
] = LLVMConstInt(element_type
,
1118 instr
->value
.u16
[i
], false);
1121 values
[i
] = LLVMConstInt(element_type
,
1122 instr
->value
.u32
[i
], false);
1125 values
[i
] = LLVMConstInt(element_type
,
1126 instr
->value
.u64
[i
], false);
1130 "unsupported nir load_const bit_size: %d\n",
1131 instr
->def
.bit_size
);
1135 if (instr
->def
.num_components
> 1) {
1136 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1140 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1144 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1147 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1148 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1151 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1152 /* On VI, the descriptor contains the size in bytes,
1153 * but TXQ must return the size in elements.
1154 * The stride is always non-zero for resources using TXQ.
1156 LLVMValueRef stride
=
1157 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1159 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1160 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1161 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1162 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1164 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1169 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1171 struct ac_image_args
*args
,
1172 const nir_tex_instr
*instr
)
1174 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1175 LLVMValueRef half_texel
[2];
1176 LLVMValueRef compare_cube_wa
= NULL
;
1177 LLVMValueRef result
;
1181 struct ac_image_args txq_args
= { 0 };
1183 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1184 txq_args
.opcode
= ac_image_get_resinfo
;
1185 txq_args
.dmask
= 0xf;
1186 txq_args
.lod
= ctx
->i32_0
;
1187 txq_args
.resource
= args
->resource
;
1188 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1189 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1191 for (unsigned c
= 0; c
< 2; c
++) {
1192 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1193 LLVMConstInt(ctx
->i32
, c
, false), "");
1194 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1195 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1196 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1197 LLVMConstReal(ctx
->f32
, -0.5), "");
1201 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1203 for (unsigned c
= 0; c
< 2; c
++) {
1205 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1206 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1210 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1211 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1212 * workaround by sampling using a scaled type and converting.
1213 * This is taken from amdgpu-pro shaders.
1215 /* NOTE this produces some ugly code compared to amdgpu-pro,
1216 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1217 * and then reads them back. -pro generates two selects,
1218 * one s_cmp for the descriptor rewriting
1219 * one v_cmp for the coordinate and result changes.
1221 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1222 LLVMValueRef tmp
, tmp2
;
1224 /* workaround 8/8/8/8 uint/sint cube gather bug */
1225 /* first detect it then change to a scaled read and f2i */
1226 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1229 /* extract the DATA_FORMAT */
1230 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1231 LLVMConstInt(ctx
->i32
, 6, false), false);
1233 /* is the DATA_FORMAT == 8_8_8_8 */
1234 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1236 if (stype
== GLSL_TYPE_UINT
)
1237 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1238 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1239 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1241 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1242 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1243 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1245 /* replace the NUM FORMAT in the descriptor */
1246 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1247 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1249 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1251 /* don't modify the coordinates for this case */
1252 for (unsigned c
= 0; c
< 2; ++c
)
1253 args
->coords
[c
] = LLVMBuildSelect(
1254 ctx
->builder
, compare_cube_wa
,
1255 orig_coords
[c
], args
->coords
[c
], "");
1258 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1259 result
= ac_build_image_opcode(ctx
, args
);
1261 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1262 LLVMValueRef tmp
, tmp2
;
1264 /* if the cube workaround is in place, f2i the result. */
1265 for (unsigned c
= 0; c
< 4; c
++) {
1266 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1267 if (stype
== GLSL_TYPE_UINT
)
1268 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1270 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1271 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1272 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1273 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1274 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1275 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1281 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1283 nir_deref_instr
*texture_deref_instr
= NULL
;
1285 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1286 switch (instr
->src
[i
].src_type
) {
1287 case nir_tex_src_texture_deref
:
1288 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1294 return texture_deref_instr
;
1297 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1298 const nir_tex_instr
*instr
,
1299 struct ac_image_args
*args
)
1301 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1302 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1304 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1305 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1309 util_last_bit(mask
),
1312 return ac_build_buffer_load_format(&ctx
->ac
,
1316 util_last_bit(mask
),
1321 args
->opcode
= ac_image_sample
;
1323 switch (instr
->op
) {
1325 case nir_texop_txf_ms
:
1326 case nir_texop_samples_identical
:
1327 args
->opcode
= args
->level_zero
||
1328 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1329 ac_image_load
: ac_image_load_mip
;
1330 args
->level_zero
= false;
1333 case nir_texop_query_levels
:
1334 args
->opcode
= ac_image_get_resinfo
;
1336 args
->lod
= ctx
->ac
.i32_0
;
1337 args
->level_zero
= false;
1340 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1342 args
->level_zero
= true;
1346 args
->opcode
= ac_image_gather4
;
1347 args
->level_zero
= true;
1350 args
->opcode
= ac_image_get_lod
;
1356 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1357 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1358 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1359 enum glsl_base_type stype
= glsl_get_sampler_result_type(var
->type
);
1360 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1361 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1365 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1366 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1367 if ((args
->dim
== ac_image_2darray
||
1368 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1369 args
->coords
[1] = ctx
->ac
.i32_0
;
1373 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1374 return ac_build_image_opcode(&ctx
->ac
, args
);
1377 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1378 nir_intrinsic_instr
*instr
)
1380 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1381 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1383 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1384 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1388 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1389 nir_intrinsic_instr
*instr
)
1391 LLVMValueRef ptr
, addr
;
1393 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1394 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1395 get_src(ctx
, instr
->src
[0]), "");
1397 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1398 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1400 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1403 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1404 const nir_intrinsic_instr
*instr
)
1406 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1408 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1411 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1413 uint32_t new_mask
= 0;
1414 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1415 if (mask
& (1u << i
))
1416 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1420 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1421 unsigned start
, unsigned count
)
1423 LLVMValueRef mask
[] = {
1424 LLVMConstInt(ctx
->i32
, 0, false), LLVMConstInt(ctx
->i32
, 1, false),
1425 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1427 unsigned src_elements
= ac_get_llvm_num_components(src
);
1429 if (count
== src_elements
) {
1432 } else if (count
== 1) {
1433 assert(start
< src_elements
);
1434 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1436 assert(start
+ count
<= src_elements
);
1438 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1439 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1443 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1444 nir_intrinsic_instr
*instr
)
1446 const char *store_name
;
1447 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1448 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1449 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1451 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1452 get_src(ctx
, instr
->src
[1]), true);
1453 LLVMValueRef base_data
= ac_to_float(&ctx
->ac
, src_data
);
1454 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1455 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1459 LLVMValueRef data
, offset
;
1460 LLVMTypeRef data_type
;
1462 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1464 /* Due to an LLVM limitation, split 3-element writes
1465 * into a 2-element and a 1-element write. */
1467 writemask
|= 1 << (start
+ 2);
1470 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1472 /* we can only store 4 DWords at the same time.
1473 * can only happen for 64 Bit vectors. */
1474 if (num_bytes
> 16) {
1475 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1480 /* check alignment of 16 Bit stores */
1481 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1482 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1486 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1489 offset
= base_offset
;
1491 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1492 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1494 if (num_bytes
== 2) {
1495 store_name
= "llvm.amdgcn.tbuffer.store.i32";
1496 data_type
= ctx
->ac
.i32
;
1497 LLVMValueRef tbuffer_params
[] = {
1500 ctx
->ac
.i32_0
, /* vindex */
1501 offset
, /* voffset */
1504 LLVMConstInt(ctx
->ac
.i32
, 2, false), // dfmt (= 16bit)
1505 LLVMConstInt(ctx
->ac
.i32
, 4, false), // nfmt (= uint)
1509 ac_build_intrinsic(&ctx
->ac
, store_name
,
1510 ctx
->ac
.voidt
, tbuffer_params
, 10, 0);
1512 switch (num_bytes
) {
1513 case 16: /* v4f32 */
1514 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1515 data_type
= ctx
->ac
.v4f32
;
1518 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1519 data_type
= ctx
->ac
.v2f32
;
1522 store_name
= "llvm.amdgcn.buffer.store.f32";
1523 data_type
= ctx
->ac
.f32
;
1526 unreachable("Malformed vector store.");
1528 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1529 LLVMValueRef params
[] = {
1532 ctx
->ac
.i32_0
, /* vindex */
1534 ctx
->ac
.i1false
, /* glc */
1535 ctx
->ac
.i1false
, /* slc */
1537 ac_build_intrinsic(&ctx
->ac
, store_name
,
1538 ctx
->ac
.voidt
, params
, 6, 0);
1543 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1544 const nir_intrinsic_instr
*instr
)
1547 LLVMValueRef params
[6];
1550 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1551 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1553 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1554 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1555 get_src(ctx
, instr
->src
[0]),
1557 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1558 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1559 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1561 switch (instr
->intrinsic
) {
1562 case nir_intrinsic_ssbo_atomic_add
:
1563 name
= "llvm.amdgcn.buffer.atomic.add";
1565 case nir_intrinsic_ssbo_atomic_imin
:
1566 name
= "llvm.amdgcn.buffer.atomic.smin";
1568 case nir_intrinsic_ssbo_atomic_umin
:
1569 name
= "llvm.amdgcn.buffer.atomic.umin";
1571 case nir_intrinsic_ssbo_atomic_imax
:
1572 name
= "llvm.amdgcn.buffer.atomic.smax";
1574 case nir_intrinsic_ssbo_atomic_umax
:
1575 name
= "llvm.amdgcn.buffer.atomic.umax";
1577 case nir_intrinsic_ssbo_atomic_and
:
1578 name
= "llvm.amdgcn.buffer.atomic.and";
1580 case nir_intrinsic_ssbo_atomic_or
:
1581 name
= "llvm.amdgcn.buffer.atomic.or";
1583 case nir_intrinsic_ssbo_atomic_xor
:
1584 name
= "llvm.amdgcn.buffer.atomic.xor";
1586 case nir_intrinsic_ssbo_atomic_exchange
:
1587 name
= "llvm.amdgcn.buffer.atomic.swap";
1589 case nir_intrinsic_ssbo_atomic_comp_swap
:
1590 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1596 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1599 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1600 const nir_intrinsic_instr
*instr
)
1602 LLVMValueRef results
[2];
1603 int load_components
;
1604 int num_components
= instr
->num_components
;
1605 if (instr
->dest
.ssa
.bit_size
== 64)
1606 num_components
*= 2;
1608 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1609 load_components
= MIN2(num_components
- i
, 4);
1610 const char *load_name
;
1611 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1612 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1613 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1615 if (load_components
== 3)
1616 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1617 else if (load_components
> 1)
1618 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1620 if (load_components
>= 3)
1621 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1622 else if (load_components
== 2)
1623 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1624 else if (load_components
== 1)
1625 load_name
= "llvm.amdgcn.buffer.load.f32";
1627 unreachable("unhandled number of components");
1629 LLVMValueRef params
[] = {
1630 ctx
->abi
->load_ssbo(ctx
->abi
,
1631 get_src(ctx
, instr
->src
[0]),
1639 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1643 LLVMValueRef ret
= results
[0];
1644 if (num_components
> 4 || num_components
== 3) {
1645 LLVMValueRef masks
[] = {
1646 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1647 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1648 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1649 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1652 if (num_components
== 6) {
1653 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1654 results
[1] = ac_build_expand_to_vec4(&ctx
->ac
, results
[1], 4);
1657 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1658 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1659 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1662 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1663 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1666 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1667 const nir_intrinsic_instr
*instr
)
1670 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1671 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1672 int num_components
= instr
->num_components
;
1674 if (ctx
->abi
->load_ubo
)
1675 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1677 if (instr
->dest
.ssa
.bit_size
== 64)
1678 num_components
*= 2;
1680 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1681 NULL
, 0, false, false, true, true);
1682 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1683 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1684 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1688 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1689 bool vs_in
, unsigned *vertex_index_out
,
1690 LLVMValueRef
*vertex_index_ref
,
1691 unsigned *const_out
, LLVMValueRef
*indir_out
)
1693 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1694 nir_deref_path path
;
1695 unsigned idx_lvl
= 1;
1697 nir_deref_path_init(&path
, instr
, NULL
);
1699 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1700 if (vertex_index_ref
) {
1701 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1702 if (vertex_index_out
)
1703 *vertex_index_out
= 0;
1705 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1707 *vertex_index_out
= v
->u32
[0];
1712 uint32_t const_offset
= 0;
1713 LLVMValueRef offset
= NULL
;
1715 if (var
->data
.compact
) {
1716 assert(instr
->deref_type
== nir_deref_type_array
);
1717 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1719 const_offset
= v
->u32
[0];
1723 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1724 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1725 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1726 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1728 for (unsigned i
= 0; i
< index
; i
++) {
1729 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1730 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1732 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1733 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1734 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1735 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1737 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1741 unreachable("Uhandled deref type in get_deref_instr_offset");
1745 nir_deref_path_finish(&path
);
1747 if (const_offset
&& offset
)
1748 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1749 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1752 *const_out
= const_offset
;
1753 *indir_out
= offset
;
1756 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1757 nir_intrinsic_instr
*instr
,
1760 LLVMValueRef result
;
1761 LLVMValueRef vertex_index
= NULL
;
1762 LLVMValueRef indir_index
= NULL
;
1763 unsigned const_index
= 0;
1765 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1767 unsigned location
= var
->data
.location
;
1768 unsigned driver_location
= var
->data
.driver_location
;
1769 const bool is_patch
= var
->data
.patch
;
1770 const bool is_compact
= var
->data
.compact
;
1772 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1773 false, NULL
, is_patch
? NULL
: &vertex_index
,
1774 &const_index
, &indir_index
);
1776 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1778 LLVMTypeRef src_component_type
;
1779 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1780 src_component_type
= LLVMGetElementType(dest_type
);
1782 src_component_type
= dest_type
;
1784 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1785 vertex_index
, indir_index
,
1786 const_index
, location
, driver_location
,
1787 var
->data
.location_frac
,
1788 instr
->num_components
,
1789 is_patch
, is_compact
, load_inputs
);
1790 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1793 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1794 nir_intrinsic_instr
*instr
)
1796 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1798 LLVMValueRef values
[8];
1799 int idx
= var
->data
.driver_location
;
1800 int ve
= instr
->dest
.ssa
.num_components
;
1801 unsigned comp
= var
->data
.location_frac
;
1802 LLVMValueRef indir_index
;
1804 unsigned const_index
;
1805 unsigned stride
= var
->data
.compact
? 1 : 4;
1806 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1807 var
->data
.mode
== nir_var_shader_in
;
1809 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), vs_in
, NULL
, NULL
,
1810 &const_index
, &indir_index
);
1812 if (instr
->dest
.ssa
.bit_size
== 64)
1815 switch (var
->data
.mode
) {
1816 case nir_var_shader_in
:
1817 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1818 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1819 return load_tess_varyings(ctx
, instr
, true);
1822 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1823 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1824 LLVMValueRef indir_index
;
1825 unsigned const_index
, vertex_index
;
1826 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1827 false, &vertex_index
, NULL
, &const_index
, &indir_index
);
1829 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1830 var
->data
.driver_location
,
1831 var
->data
.location_frac
,
1832 instr
->num_components
, vertex_index
, const_index
, type
);
1835 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1837 unsigned count
= glsl_count_attribute_slots(
1839 ctx
->stage
== MESA_SHADER_VERTEX
);
1841 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1842 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1843 stride
, false, true);
1845 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1849 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1853 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1855 unsigned count
= glsl_count_attribute_slots(
1858 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1859 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1860 stride
, true, true);
1862 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1866 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1870 case nir_var_shared
: {
1871 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
1872 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1873 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1874 get_def_type(ctx
, &instr
->dest
.ssa
),
1877 case nir_var_shader_out
:
1878 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1879 return load_tess_varyings(ctx
, instr
, false);
1882 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1884 unsigned count
= glsl_count_attribute_slots(
1887 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1888 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1889 stride
, true, true);
1891 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1895 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1896 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1902 unreachable("unhandle variable mode");
1904 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1905 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1909 visit_store_var(struct ac_nir_context
*ctx
,
1910 nir_intrinsic_instr
*instr
)
1912 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1914 LLVMValueRef temp_ptr
, value
;
1915 int idx
= var
->data
.driver_location
;
1916 unsigned comp
= var
->data
.location_frac
;
1917 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
1918 int writemask
= instr
->const_index
[0];
1919 LLVMValueRef indir_index
;
1920 unsigned const_index
;
1922 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), false,
1923 NULL
, NULL
, &const_index
, &indir_index
);
1925 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1927 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1928 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1931 writemask
= widen_mask(writemask
, 2);
1934 writemask
= writemask
<< comp
;
1936 switch (var
->data
.mode
) {
1937 case nir_var_shader_out
:
1939 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1940 LLVMValueRef vertex_index
= NULL
;
1941 LLVMValueRef indir_index
= NULL
;
1942 unsigned const_index
= 0;
1943 const bool is_patch
= var
->data
.patch
;
1945 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1946 false, NULL
, is_patch
? NULL
: &vertex_index
,
1947 &const_index
, &indir_index
);
1949 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
1950 vertex_index
, indir_index
,
1951 const_index
, src
, writemask
);
1955 for (unsigned chan
= 0; chan
< 8; chan
++) {
1957 if (!(writemask
& (1 << chan
)))
1960 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1962 if (var
->data
.compact
)
1965 unsigned count
= glsl_count_attribute_slots(
1968 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1969 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1970 stride
, true, true);
1972 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1973 value
, indir_index
, "");
1974 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1975 count
, stride
, tmp_vec
);
1978 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1980 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1985 for (unsigned chan
= 0; chan
< 8; chan
++) {
1986 if (!(writemask
& (1 << chan
)))
1989 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1991 unsigned count
= glsl_count_attribute_slots(
1994 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1995 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1998 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1999 value
, indir_index
, "");
2000 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2003 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2005 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2009 case nir_var_shared
: {
2010 int writemask
= instr
->const_index
[0];
2011 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2012 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2013 if (util_is_power_of_two_nonzero(writemask
)) {
2014 val
= LLVMBuildBitCast(
2015 ctx
->ac
.builder
, val
,
2016 LLVMGetElementType(LLVMTypeOf(address
)), "");
2017 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2019 for (unsigned chan
= 0; chan
< 4; chan
++) {
2020 if (!(writemask
& (1 << chan
)))
2023 LLVMBuildStructGEP(ctx
->ac
.builder
,
2025 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2027 src
= LLVMBuildBitCast(
2028 ctx
->ac
.builder
, src
,
2029 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2030 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2040 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2043 case GLSL_SAMPLER_DIM_BUF
:
2045 case GLSL_SAMPLER_DIM_1D
:
2046 return array
? 2 : 1;
2047 case GLSL_SAMPLER_DIM_2D
:
2048 return array
? 3 : 2;
2049 case GLSL_SAMPLER_DIM_MS
:
2050 return array
? 4 : 3;
2051 case GLSL_SAMPLER_DIM_3D
:
2052 case GLSL_SAMPLER_DIM_CUBE
:
2054 case GLSL_SAMPLER_DIM_RECT
:
2055 case GLSL_SAMPLER_DIM_SUBPASS
:
2057 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2066 /* Adjust the sample index according to FMASK.
2068 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2069 * which is the identity mapping. Each nibble says which physical sample
2070 * should be fetched to get that sample.
2072 * For example, 0x11111100 means there are only 2 samples stored and
2073 * the second sample covers 3/4 of the pixel. When reading samples 0
2074 * and 1, return physical sample 0 (determined by the first two 0s
2075 * in FMASK), otherwise return physical sample 1.
2077 * The sample index should be adjusted as follows:
2078 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2080 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2081 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2082 LLVMValueRef coord_z
,
2083 LLVMValueRef sample_index
,
2084 LLVMValueRef fmask_desc_ptr
)
2086 struct ac_image_args args
= {0};
2089 args
.coords
[0] = coord_x
;
2090 args
.coords
[1] = coord_y
;
2092 args
.coords
[2] = coord_z
;
2094 args
.opcode
= ac_image_load
;
2095 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2096 args
.resource
= fmask_desc_ptr
;
2098 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2100 res
= ac_build_image_opcode(ctx
, &args
);
2102 res
= ac_to_integer(ctx
, res
);
2103 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2104 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2106 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2110 LLVMValueRef sample_index4
=
2111 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2112 LLVMValueRef shifted_fmask
=
2113 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2114 LLVMValueRef final_sample
=
2115 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2117 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2118 * resource descriptor is 0 (invalid),
2120 LLVMValueRef fmask_desc
=
2121 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2124 LLVMValueRef fmask_word1
=
2125 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2128 LLVMValueRef word1_is_nonzero
=
2129 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2130 fmask_word1
, ctx
->i32_0
, "");
2132 /* Replace the MSAA sample index. */
2134 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2135 final_sample
, sample_index
, "");
2136 return sample_index
;
2139 static nir_variable
*get_image_variable(const nir_intrinsic_instr
*instr
)
2141 assert(instr
->src
[0].is_ssa
);
2142 return nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2145 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2146 const nir_intrinsic_instr
*instr
,
2147 enum ac_descriptor_type desc_type
,
2150 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, true);
2153 static void get_image_coords(struct ac_nir_context
*ctx
,
2154 const nir_intrinsic_instr
*instr
,
2155 struct ac_image_args
*args
)
2157 const struct glsl_type
*type
= glsl_without_array(get_image_variable(instr
)->type
);
2159 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2160 LLVMValueRef masks
[] = {
2161 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2162 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2164 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2167 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2168 bool is_array
= glsl_sampler_type_is_array(type
);
2169 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2170 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2171 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2172 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2173 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2174 count
= image_type_to_components_count(dim
, is_array
);
2177 LLVMValueRef fmask_load_address
[3];
2180 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2181 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2183 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2185 fmask_load_address
[2] = NULL
;
2187 for (chan
= 0; chan
< 2; ++chan
)
2188 fmask_load_address
[chan
] =
2189 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2190 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2191 ctx
->ac
.i32
, ""), "");
2192 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2194 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2195 fmask_load_address
[0],
2196 fmask_load_address
[1],
2197 fmask_load_address
[2],
2199 get_image_descriptor(ctx
, instr
, AC_DESC_FMASK
, false));
2201 if (count
== 1 && !gfx9_1d
) {
2202 if (instr
->src
[1].ssa
->num_components
)
2203 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2205 args
->coords
[0] = src0
;
2210 for (chan
= 0; chan
< count
; ++chan
) {
2211 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2214 for (chan
= 0; chan
< 2; ++chan
) {
2215 args
->coords
[chan
] = LLVMBuildAdd(
2216 ctx
->ac
.builder
, args
->coords
[chan
],
2218 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2219 ctx
->ac
.i32
, ""), "");
2221 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2222 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2228 args
->coords
[2] = args
->coords
[1];
2229 args
->coords
[1] = ctx
->ac
.i32_0
;
2231 args
->coords
[1] = ctx
->ac
.i32_0
;
2236 args
->coords
[count
] = sample_index
;
2242 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2243 const nir_intrinsic_instr
*instr
, bool write
)
2245 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2246 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2247 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2248 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2249 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2251 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2252 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2253 elem_count
, stride
, "");
2255 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2256 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2261 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2262 const nir_intrinsic_instr
*instr
)
2265 const nir_variable
*var
= get_image_variable(instr
);
2266 const struct glsl_type
*type
= var
->type
;
2268 type
= glsl_without_array(type
);
2270 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2271 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2272 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2273 unsigned num_channels
= util_last_bit(mask
);
2274 LLVMValueRef rsrc
, vindex
;
2276 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2277 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2280 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2281 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2282 ctx
->ac
.i32_0
, num_channels
,
2284 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2286 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2287 res
= ac_to_integer(&ctx
->ac
, res
);
2289 struct ac_image_args args
= {};
2290 args
.opcode
= ac_image_load
;
2291 get_image_coords(ctx
, instr
, &args
);
2292 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2293 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2294 glsl_sampler_type_is_array(type
));
2296 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2297 if (var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2298 args
.cache_policy
|= ac_glc
;
2300 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2302 return ac_to_integer(&ctx
->ac
, res
);
2305 static void visit_image_store(struct ac_nir_context
*ctx
,
2306 nir_intrinsic_instr
*instr
)
2308 LLVMValueRef params
[8];
2309 const nir_variable
*var
= get_image_variable(instr
);
2310 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2311 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2312 LLVMValueRef glc
= ctx
->ac
.i1false
;
2313 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2315 glc
= ctx
->ac
.i1true
;
2317 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2318 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2320 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3])); /* data */
2322 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2323 ctx
->ac
.i32_0
, ""); /* vindex */
2324 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2325 params
[4] = glc
; /* glc */
2326 params
[5] = ctx
->ac
.i1false
; /* slc */
2327 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2330 struct ac_image_args args
= {};
2331 args
.opcode
= ac_image_store
;
2332 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2333 get_image_coords(ctx
, instr
, &args
);
2334 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2335 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2336 glsl_sampler_type_is_array(type
));
2338 if (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
)
2339 args
.cache_policy
|= ac_glc
;
2341 ac_build_image_opcode(&ctx
->ac
, &args
);
2346 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2347 const nir_intrinsic_instr
*instr
)
2349 LLVMValueRef params
[7];
2350 int param_count
= 0;
2351 const nir_variable
*var
= get_image_variable(instr
);
2353 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2354 const char *atomic_name
;
2355 char intrinsic_name
[41];
2356 enum ac_atomic_op atomic_subop
;
2357 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2358 MAYBE_UNUSED
int length
;
2360 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2362 switch (instr
->intrinsic
) {
2363 case nir_intrinsic_image_deref_atomic_add
:
2364 atomic_name
= "add";
2365 atomic_subop
= ac_atomic_add
;
2367 case nir_intrinsic_image_deref_atomic_min
:
2368 atomic_name
= is_unsigned
? "umin" : "smin";
2369 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2371 case nir_intrinsic_image_deref_atomic_max
:
2372 atomic_name
= is_unsigned
? "umax" : "smax";
2373 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2375 case nir_intrinsic_image_deref_atomic_and
:
2376 atomic_name
= "and";
2377 atomic_subop
= ac_atomic_and
;
2379 case nir_intrinsic_image_deref_atomic_or
:
2381 atomic_subop
= ac_atomic_or
;
2383 case nir_intrinsic_image_deref_atomic_xor
:
2384 atomic_name
= "xor";
2385 atomic_subop
= ac_atomic_xor
;
2387 case nir_intrinsic_image_deref_atomic_exchange
:
2388 atomic_name
= "swap";
2389 atomic_subop
= ac_atomic_swap
;
2391 case nir_intrinsic_image_deref_atomic_comp_swap
:
2392 atomic_name
= "cmpswap";
2393 atomic_subop
= 0; /* not used */
2400 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2401 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2403 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2404 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2405 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2406 ctx
->ac
.i32_0
, ""); /* vindex */
2407 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2408 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2410 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2411 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2413 assert(length
< sizeof(intrinsic_name
));
2414 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2415 params
, param_count
, 0);
2417 struct ac_image_args args
= {};
2418 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2419 args
.atomic
= atomic_subop
;
2420 args
.data
[0] = params
[0];
2422 args
.data
[1] = params
[1];
2423 get_image_coords(ctx
, instr
, &args
);
2424 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2425 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2426 glsl_sampler_type_is_array(type
));
2428 return ac_build_image_opcode(&ctx
->ac
, &args
);
2432 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2433 const nir_intrinsic_instr
*instr
)
2435 const nir_variable
*var
= get_image_variable(instr
);
2436 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2438 struct ac_image_args args
= { 0 };
2439 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2440 glsl_sampler_type_is_array(type
));
2442 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2443 args
.opcode
= ac_image_get_resinfo
;
2444 args
.lod
= ctx
->ac
.i32_0
;
2445 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2447 return ac_build_image_opcode(&ctx
->ac
, &args
);
2450 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2451 const nir_intrinsic_instr
*instr
)
2454 const nir_variable
*var
= get_image_variable(instr
);
2455 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2457 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2458 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2460 struct ac_image_args args
= { 0 };
2462 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2463 glsl_sampler_type_is_array(type
));
2465 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2466 args
.opcode
= ac_image_get_resinfo
;
2467 args
.lod
= ctx
->ac
.i32_0
;
2468 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2470 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2472 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2474 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2475 glsl_sampler_type_is_array(type
)) {
2476 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2477 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2478 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2479 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2481 if (ctx
->ac
.chip_class
>= GFX9
&&
2482 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2483 glsl_sampler_type_is_array(type
)) {
2484 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2485 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2492 #define NOOP_WAITCNT 0xf7f
2493 #define LGKM_CNT 0x07f
2494 #define VM_CNT 0xf70
2496 static void emit_membar(struct ac_llvm_context
*ac
,
2497 const nir_intrinsic_instr
*instr
)
2499 unsigned waitcnt
= NOOP_WAITCNT
;
2501 switch (instr
->intrinsic
) {
2502 case nir_intrinsic_memory_barrier
:
2503 case nir_intrinsic_group_memory_barrier
:
2504 waitcnt
&= VM_CNT
& LGKM_CNT
;
2506 case nir_intrinsic_memory_barrier_atomic_counter
:
2507 case nir_intrinsic_memory_barrier_buffer
:
2508 case nir_intrinsic_memory_barrier_image
:
2511 case nir_intrinsic_memory_barrier_shared
:
2512 waitcnt
&= LGKM_CNT
;
2517 if (waitcnt
!= NOOP_WAITCNT
)
2518 ac_build_waitcnt(ac
, waitcnt
);
2521 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2523 /* SI only (thanks to a hw bug workaround):
2524 * The real barrier instruction isn’t needed, because an entire patch
2525 * always fits into a single wave.
2527 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2528 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2531 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2532 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2535 static void emit_discard(struct ac_nir_context
*ctx
,
2536 const nir_intrinsic_instr
*instr
)
2540 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2541 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2542 get_src(ctx
, instr
->src
[0]),
2545 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2546 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2549 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2553 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2555 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2556 "llvm.amdgcn.ps.live",
2557 ctx
->ac
.i1
, NULL
, 0,
2558 AC_FUNC_ATTR_READNONE
);
2559 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2560 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2564 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2566 LLVMValueRef result
;
2567 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2568 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2569 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2571 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2575 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2577 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2578 LLVMValueRef result
;
2579 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2580 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2581 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2583 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2588 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2590 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2591 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2592 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2594 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2599 visit_first_invocation(struct ac_nir_context
*ctx
)
2601 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2603 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2604 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2605 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2607 ctx
->ac
.i64
, args
, 2,
2608 AC_FUNC_ATTR_NOUNWIND
|
2609 AC_FUNC_ATTR_READNONE
);
2611 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2615 visit_load_shared(struct ac_nir_context
*ctx
,
2616 const nir_intrinsic_instr
*instr
)
2618 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2620 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2622 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2623 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2624 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2625 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2628 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2629 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2633 visit_store_shared(struct ac_nir_context
*ctx
,
2634 const nir_intrinsic_instr
*instr
)
2636 LLVMValueRef derived_ptr
, data
,index
;
2637 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2639 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2640 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2642 int writemask
= nir_intrinsic_write_mask(instr
);
2643 for (int chan
= 0; chan
< 4; chan
++) {
2644 if (!(writemask
& (1 << chan
))) {
2647 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2648 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2649 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2650 LLVMBuildStore(builder
, data
, derived_ptr
);
2654 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2655 const nir_intrinsic_instr
*instr
,
2656 LLVMValueRef ptr
, int src_idx
)
2658 LLVMValueRef result
;
2659 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2661 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2662 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2663 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2664 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2666 LLVMAtomicOrderingSequentiallyConsistent
,
2667 LLVMAtomicOrderingSequentiallyConsistent
,
2669 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2671 LLVMAtomicRMWBinOp op
;
2672 switch (instr
->intrinsic
) {
2673 case nir_intrinsic_shared_atomic_add
:
2674 case nir_intrinsic_deref_atomic_add
:
2675 op
= LLVMAtomicRMWBinOpAdd
;
2677 case nir_intrinsic_shared_atomic_umin
:
2678 case nir_intrinsic_deref_atomic_umin
:
2679 op
= LLVMAtomicRMWBinOpUMin
;
2681 case nir_intrinsic_shared_atomic_umax
:
2682 case nir_intrinsic_deref_atomic_umax
:
2683 op
= LLVMAtomicRMWBinOpUMax
;
2685 case nir_intrinsic_shared_atomic_imin
:
2686 case nir_intrinsic_deref_atomic_imin
:
2687 op
= LLVMAtomicRMWBinOpMin
;
2689 case nir_intrinsic_shared_atomic_imax
:
2690 case nir_intrinsic_deref_atomic_imax
:
2691 op
= LLVMAtomicRMWBinOpMax
;
2693 case nir_intrinsic_shared_atomic_and
:
2694 case nir_intrinsic_deref_atomic_and
:
2695 op
= LLVMAtomicRMWBinOpAnd
;
2697 case nir_intrinsic_shared_atomic_or
:
2698 case nir_intrinsic_deref_atomic_or
:
2699 op
= LLVMAtomicRMWBinOpOr
;
2701 case nir_intrinsic_shared_atomic_xor
:
2702 case nir_intrinsic_deref_atomic_xor
:
2703 op
= LLVMAtomicRMWBinOpXor
;
2705 case nir_intrinsic_shared_atomic_exchange
:
2706 case nir_intrinsic_deref_atomic_exchange
:
2707 op
= LLVMAtomicRMWBinOpXchg
;
2713 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2714 LLVMAtomicOrderingSequentiallyConsistent
,
2720 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2722 LLVMValueRef values
[2];
2723 LLVMValueRef pos
[2];
2725 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2726 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2728 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2729 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2730 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2733 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2734 const nir_intrinsic_instr
*instr
)
2736 LLVMValueRef result
[4];
2737 LLVMValueRef interp_param
, attr_number
;
2740 LLVMValueRef src_c0
= NULL
;
2741 LLVMValueRef src_c1
= NULL
;
2742 LLVMValueRef src0
= NULL
;
2744 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2745 int input_index
= var
->data
.location
- VARYING_SLOT_VAR0
;
2746 switch (instr
->intrinsic
) {
2747 case nir_intrinsic_interp_deref_at_centroid
:
2748 location
= INTERP_CENTROID
;
2750 case nir_intrinsic_interp_deref_at_sample
:
2751 case nir_intrinsic_interp_deref_at_offset
:
2752 location
= INTERP_CENTER
;
2753 src0
= get_src(ctx
, instr
->src
[1]);
2759 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2760 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2761 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2762 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2763 LLVMValueRef sample_position
;
2764 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2766 /* fetch sample ID */
2767 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2769 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2770 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2771 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2772 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2774 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
2775 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2777 if (location
== INTERP_CENTER
) {
2778 LLVMValueRef ij_out
[2];
2779 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2782 * take the I then J parameters, and the DDX/Y for it, and
2783 * calculate the IJ inputs for the interpolator.
2784 * temp1 = ddx * offset/sample.x + I;
2785 * interp_param.I = ddy * offset/sample.y + temp1;
2786 * temp1 = ddx * offset/sample.x + J;
2787 * interp_param.J = ddy * offset/sample.y + temp1;
2789 for (unsigned i
= 0; i
< 2; i
++) {
2790 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2791 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2792 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2793 ddxy_out
, ix_ll
, "");
2794 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2795 ddxy_out
, iy_ll
, "");
2796 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2797 interp_param
, ix_ll
, "");
2798 LLVMValueRef temp1
, temp2
;
2800 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2803 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2804 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2806 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2807 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2809 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2810 temp2
, ctx
->ac
.i32
, "");
2812 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2816 for (chan
= 0; chan
< 4; chan
++) {
2817 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2820 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2821 interp_param
, ctx
->ac
.v2f32
, "");
2822 LLVMValueRef i
= LLVMBuildExtractElement(
2823 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2824 LLVMValueRef j
= LLVMBuildExtractElement(
2825 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2827 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2828 llvm_chan
, attr_number
,
2829 ctx
->abi
->prim_mask
, i
, j
);
2831 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2832 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2833 llvm_chan
, attr_number
,
2834 ctx
->abi
->prim_mask
);
2837 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2838 var
->data
.location_frac
);
2841 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2842 nir_intrinsic_instr
*instr
)
2844 LLVMValueRef result
= NULL
;
2846 switch (instr
->intrinsic
) {
2847 case nir_intrinsic_ballot
:
2848 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2850 case nir_intrinsic_read_invocation
:
2851 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2852 get_src(ctx
, instr
->src
[1]));
2854 case nir_intrinsic_read_first_invocation
:
2855 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2857 case nir_intrinsic_load_subgroup_invocation
:
2858 result
= ac_get_thread_id(&ctx
->ac
);
2860 case nir_intrinsic_load_work_group_id
: {
2861 LLVMValueRef values
[3];
2863 for (int i
= 0; i
< 3; i
++) {
2864 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2865 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2868 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2871 case nir_intrinsic_load_base_vertex
:
2872 case nir_intrinsic_load_first_vertex
:
2873 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2875 case nir_intrinsic_load_local_group_size
:
2876 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2878 case nir_intrinsic_load_vertex_id
:
2879 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2880 ctx
->abi
->base_vertex
, "");
2882 case nir_intrinsic_load_vertex_id_zero_base
: {
2883 result
= ctx
->abi
->vertex_id
;
2886 case nir_intrinsic_load_local_invocation_id
: {
2887 result
= ctx
->abi
->local_invocation_ids
;
2890 case nir_intrinsic_load_base_instance
:
2891 result
= ctx
->abi
->start_instance
;
2893 case nir_intrinsic_load_draw_id
:
2894 result
= ctx
->abi
->draw_id
;
2896 case nir_intrinsic_load_view_index
:
2897 result
= ctx
->abi
->view_index
;
2899 case nir_intrinsic_load_invocation_id
:
2900 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2901 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2903 result
= ctx
->abi
->gs_invocation_id
;
2905 case nir_intrinsic_load_primitive_id
:
2906 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2907 result
= ctx
->abi
->gs_prim_id
;
2908 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2909 result
= ctx
->abi
->tcs_patch_id
;
2910 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2911 result
= ctx
->abi
->tes_patch_id
;
2913 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2915 case nir_intrinsic_load_sample_id
:
2916 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2918 case nir_intrinsic_load_sample_pos
:
2919 result
= load_sample_pos(ctx
);
2921 case nir_intrinsic_load_sample_mask_in
:
2922 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2924 case nir_intrinsic_load_frag_coord
: {
2925 LLVMValueRef values
[4] = {
2926 ctx
->abi
->frag_pos
[0],
2927 ctx
->abi
->frag_pos
[1],
2928 ctx
->abi
->frag_pos
[2],
2929 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2931 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2934 case nir_intrinsic_load_front_face
:
2935 result
= ctx
->abi
->front_face
;
2937 case nir_intrinsic_load_helper_invocation
:
2938 result
= visit_load_helper_invocation(ctx
);
2940 case nir_intrinsic_load_instance_id
:
2941 result
= ctx
->abi
->instance_id
;
2943 case nir_intrinsic_load_num_work_groups
:
2944 result
= ctx
->abi
->num_work_groups
;
2946 case nir_intrinsic_load_local_invocation_index
:
2947 result
= visit_load_local_invocation_index(ctx
);
2949 case nir_intrinsic_load_subgroup_id
:
2950 result
= visit_load_subgroup_id(ctx
);
2952 case nir_intrinsic_load_num_subgroups
:
2953 result
= visit_load_num_subgroups(ctx
);
2955 case nir_intrinsic_first_invocation
:
2956 result
= visit_first_invocation(ctx
);
2958 case nir_intrinsic_load_push_constant
:
2959 result
= visit_load_push_constant(ctx
, instr
);
2961 case nir_intrinsic_vulkan_resource_index
: {
2962 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2963 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2964 unsigned binding
= nir_intrinsic_binding(instr
);
2966 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2970 case nir_intrinsic_vulkan_resource_reindex
:
2971 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2973 case nir_intrinsic_store_ssbo
:
2974 visit_store_ssbo(ctx
, instr
);
2976 case nir_intrinsic_load_ssbo
:
2977 result
= visit_load_buffer(ctx
, instr
);
2979 case nir_intrinsic_ssbo_atomic_add
:
2980 case nir_intrinsic_ssbo_atomic_imin
:
2981 case nir_intrinsic_ssbo_atomic_umin
:
2982 case nir_intrinsic_ssbo_atomic_imax
:
2983 case nir_intrinsic_ssbo_atomic_umax
:
2984 case nir_intrinsic_ssbo_atomic_and
:
2985 case nir_intrinsic_ssbo_atomic_or
:
2986 case nir_intrinsic_ssbo_atomic_xor
:
2987 case nir_intrinsic_ssbo_atomic_exchange
:
2988 case nir_intrinsic_ssbo_atomic_comp_swap
:
2989 result
= visit_atomic_ssbo(ctx
, instr
);
2991 case nir_intrinsic_load_ubo
:
2992 result
= visit_load_ubo_buffer(ctx
, instr
);
2994 case nir_intrinsic_get_buffer_size
:
2995 result
= visit_get_buffer_size(ctx
, instr
);
2997 case nir_intrinsic_load_deref
:
2998 result
= visit_load_var(ctx
, instr
);
3000 case nir_intrinsic_store_deref
:
3001 visit_store_var(ctx
, instr
);
3003 case nir_intrinsic_load_shared
:
3004 result
= visit_load_shared(ctx
, instr
);
3006 case nir_intrinsic_store_shared
:
3007 visit_store_shared(ctx
, instr
);
3009 case nir_intrinsic_image_deref_samples
:
3010 result
= visit_image_samples(ctx
, instr
);
3012 case nir_intrinsic_image_deref_load
:
3013 result
= visit_image_load(ctx
, instr
);
3015 case nir_intrinsic_image_deref_store
:
3016 visit_image_store(ctx
, instr
);
3018 case nir_intrinsic_image_deref_atomic_add
:
3019 case nir_intrinsic_image_deref_atomic_min
:
3020 case nir_intrinsic_image_deref_atomic_max
:
3021 case nir_intrinsic_image_deref_atomic_and
:
3022 case nir_intrinsic_image_deref_atomic_or
:
3023 case nir_intrinsic_image_deref_atomic_xor
:
3024 case nir_intrinsic_image_deref_atomic_exchange
:
3025 case nir_intrinsic_image_deref_atomic_comp_swap
:
3026 result
= visit_image_atomic(ctx
, instr
);
3028 case nir_intrinsic_image_deref_size
:
3029 result
= visit_image_size(ctx
, instr
);
3031 case nir_intrinsic_shader_clock
:
3032 result
= ac_build_shader_clock(&ctx
->ac
);
3034 case nir_intrinsic_discard
:
3035 case nir_intrinsic_discard_if
:
3036 emit_discard(ctx
, instr
);
3038 case nir_intrinsic_memory_barrier
:
3039 case nir_intrinsic_group_memory_barrier
:
3040 case nir_intrinsic_memory_barrier_atomic_counter
:
3041 case nir_intrinsic_memory_barrier_buffer
:
3042 case nir_intrinsic_memory_barrier_image
:
3043 case nir_intrinsic_memory_barrier_shared
:
3044 emit_membar(&ctx
->ac
, instr
);
3046 case nir_intrinsic_barrier
:
3047 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3049 case nir_intrinsic_shared_atomic_add
:
3050 case nir_intrinsic_shared_atomic_imin
:
3051 case nir_intrinsic_shared_atomic_umin
:
3052 case nir_intrinsic_shared_atomic_imax
:
3053 case nir_intrinsic_shared_atomic_umax
:
3054 case nir_intrinsic_shared_atomic_and
:
3055 case nir_intrinsic_shared_atomic_or
:
3056 case nir_intrinsic_shared_atomic_xor
:
3057 case nir_intrinsic_shared_atomic_exchange
:
3058 case nir_intrinsic_shared_atomic_comp_swap
: {
3059 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3060 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3063 case nir_intrinsic_deref_atomic_add
:
3064 case nir_intrinsic_deref_atomic_imin
:
3065 case nir_intrinsic_deref_atomic_umin
:
3066 case nir_intrinsic_deref_atomic_imax
:
3067 case nir_intrinsic_deref_atomic_umax
:
3068 case nir_intrinsic_deref_atomic_and
:
3069 case nir_intrinsic_deref_atomic_or
:
3070 case nir_intrinsic_deref_atomic_xor
:
3071 case nir_intrinsic_deref_atomic_exchange
:
3072 case nir_intrinsic_deref_atomic_comp_swap
: {
3073 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3074 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3077 case nir_intrinsic_interp_deref_at_centroid
:
3078 case nir_intrinsic_interp_deref_at_sample
:
3079 case nir_intrinsic_interp_deref_at_offset
:
3080 result
= visit_interp(ctx
, instr
);
3082 case nir_intrinsic_emit_vertex
:
3083 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3085 case nir_intrinsic_end_primitive
:
3086 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3088 case nir_intrinsic_load_tess_coord
:
3089 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3091 case nir_intrinsic_load_tess_level_outer
:
3092 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3094 case nir_intrinsic_load_tess_level_inner
:
3095 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3097 case nir_intrinsic_load_patch_vertices_in
:
3098 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3100 case nir_intrinsic_vote_all
: {
3101 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3102 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3105 case nir_intrinsic_vote_any
: {
3106 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3107 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3110 case nir_intrinsic_shuffle
:
3111 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3112 get_src(ctx
, instr
->src
[1]));
3114 case nir_intrinsic_reduce
:
3115 result
= ac_build_reduce(&ctx
->ac
,
3116 get_src(ctx
, instr
->src
[0]),
3117 instr
->const_index
[0],
3118 instr
->const_index
[1]);
3120 case nir_intrinsic_inclusive_scan
:
3121 result
= ac_build_inclusive_scan(&ctx
->ac
,
3122 get_src(ctx
, instr
->src
[0]),
3123 instr
->const_index
[0]);
3125 case nir_intrinsic_exclusive_scan
:
3126 result
= ac_build_exclusive_scan(&ctx
->ac
,
3127 get_src(ctx
, instr
->src
[0]),
3128 instr
->const_index
[0]);
3130 case nir_intrinsic_quad_broadcast
: {
3131 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3132 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3133 lane
, lane
, lane
, lane
);
3136 case nir_intrinsic_quad_swap_horizontal
:
3137 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3139 case nir_intrinsic_quad_swap_vertical
:
3140 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3142 case nir_intrinsic_quad_swap_diagonal
:
3143 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3146 fprintf(stderr
, "Unknown intrinsic: ");
3147 nir_print_instr(&instr
->instr
, stderr
);
3148 fprintf(stderr
, "\n");
3152 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3156 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3157 nir_deref_instr
*deref_instr
,
3158 enum ac_descriptor_type desc_type
,
3159 const nir_tex_instr
*tex_instr
,
3160 bool image
, bool write
)
3162 LLVMValueRef index
= NULL
;
3163 unsigned constant_index
= 0;
3164 unsigned descriptor_set
;
3165 unsigned base_index
;
3166 bool bindless
= false;
3169 assert(tex_instr
&& !image
);
3171 base_index
= tex_instr
->sampler_index
;
3173 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3174 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3178 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3179 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3181 constant_index
+= array_size
* const_value
->u32
[0];
3183 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3185 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3186 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3191 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3194 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3196 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3197 base_index
= deref_instr
->var
->data
.binding
;
3200 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3203 constant_index
, index
,
3204 desc_type
, image
, write
, bindless
);
3207 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3210 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3211 * filtering manually. The driver sets img7 to a mask clearing
3212 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3213 * s_and_b32 samp0, samp0, img7
3216 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3218 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3219 LLVMValueRef res
, LLVMValueRef samp
)
3221 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3222 LLVMValueRef img7
, samp0
;
3224 if (ctx
->ac
.chip_class
>= VI
)
3227 img7
= LLVMBuildExtractElement(builder
, res
,
3228 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3229 samp0
= LLVMBuildExtractElement(builder
, samp
,
3230 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3231 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3232 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3233 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3236 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3237 nir_tex_instr
*instr
,
3238 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3239 LLVMValueRef
*fmask_ptr
)
3241 nir_deref_instr
*texture_deref_instr
= NULL
;
3242 nir_deref_instr
*sampler_deref_instr
= NULL
;
3244 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3245 switch (instr
->src
[i
].src_type
) {
3246 case nir_tex_src_texture_deref
:
3247 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3249 case nir_tex_src_sampler_deref
:
3250 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3257 if (!sampler_deref_instr
)
3258 sampler_deref_instr
= texture_deref_instr
;
3260 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3261 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3263 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3265 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3266 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3267 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3269 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3270 instr
->op
== nir_texop_samples_identical
))
3271 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3274 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3277 coord
= ac_to_float(ctx
, coord
);
3278 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3279 coord
= ac_to_integer(ctx
, coord
);
3283 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3285 LLVMValueRef result
= NULL
;
3286 struct ac_image_args args
= { 0 };
3287 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3288 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3289 unsigned offset_src
= 0;
3291 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3293 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3294 switch (instr
->src
[i
].src_type
) {
3295 case nir_tex_src_coord
: {
3296 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3297 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3298 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3301 case nir_tex_src_projector
:
3303 case nir_tex_src_comparator
:
3304 if (instr
->is_shadow
)
3305 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3307 case nir_tex_src_offset
:
3308 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3311 case nir_tex_src_bias
:
3312 if (instr
->op
== nir_texop_txb
)
3313 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3315 case nir_tex_src_lod
: {
3316 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3318 if (val
&& val
->i32
[0] == 0)
3319 args
.level_zero
= true;
3321 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3324 case nir_tex_src_ms_index
:
3325 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3327 case nir_tex_src_ms_mcs
:
3329 case nir_tex_src_ddx
:
3330 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3332 case nir_tex_src_ddy
:
3333 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3335 case nir_tex_src_texture_offset
:
3336 case nir_tex_src_sampler_offset
:
3337 case nir_tex_src_plane
:
3343 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3344 result
= get_buffer_size(ctx
, args
.resource
, true);
3348 if (instr
->op
== nir_texop_texture_samples
) {
3349 LLVMValueRef res
, samples
, is_msaa
;
3350 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3351 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3352 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3353 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3354 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3355 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3356 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3357 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3358 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3360 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3361 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3362 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3363 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3364 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3366 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3372 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3373 LLVMValueRef offset
[3], pack
;
3374 for (unsigned chan
= 0; chan
< 3; ++chan
)
3375 offset
[chan
] = ctx
->ac
.i32_0
;
3377 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3378 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3379 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3380 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3381 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3383 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3384 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3386 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3387 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3391 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3392 * so the depth comparison value isn't clamped for Z16 and
3393 * Z24 anymore. Do it manually here.
3395 * It's unnecessary if the original texture format was
3396 * Z32_FLOAT, but we don't know that here.
3398 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3399 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3401 /* pack derivatives */
3403 int num_src_deriv_channels
, num_dest_deriv_channels
;
3404 switch (instr
->sampler_dim
) {
3405 case GLSL_SAMPLER_DIM_3D
:
3406 case GLSL_SAMPLER_DIM_CUBE
:
3407 num_src_deriv_channels
= 3;
3408 num_dest_deriv_channels
= 3;
3410 case GLSL_SAMPLER_DIM_2D
:
3412 num_src_deriv_channels
= 2;
3413 num_dest_deriv_channels
= 2;
3415 case GLSL_SAMPLER_DIM_1D
:
3416 num_src_deriv_channels
= 1;
3417 if (ctx
->ac
.chip_class
>= GFX9
) {
3418 num_dest_deriv_channels
= 2;
3420 num_dest_deriv_channels
= 1;
3425 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3426 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3427 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3428 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3429 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3431 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3432 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3433 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3437 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3438 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3439 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3440 if (instr
->coord_components
== 3)
3441 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3442 ac_prepare_cube_coords(&ctx
->ac
,
3443 instr
->op
== nir_texop_txd
, instr
->is_array
,
3444 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3447 /* Texture coordinates fixups */
3448 if (instr
->coord_components
> 1 &&
3449 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3451 instr
->op
!= nir_texop_txf
) {
3452 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3455 if (instr
->coord_components
> 2 &&
3456 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3457 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3458 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3459 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3461 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3462 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3465 if (ctx
->ac
.chip_class
>= GFX9
&&
3466 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3467 instr
->op
!= nir_texop_lod
) {
3468 LLVMValueRef filler
;
3469 if (instr
->op
== nir_texop_txf
)
3470 filler
= ctx
->ac
.i32_0
;
3472 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3474 if (instr
->is_array
)
3475 args
.coords
[2] = args
.coords
[1];
3476 args
.coords
[1] = filler
;
3479 /* Pack sample index */
3480 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3481 args
.coords
[instr
->coord_components
] = sample_index
;
3483 if (instr
->op
== nir_texop_samples_identical
) {
3484 struct ac_image_args txf_args
= { 0 };
3485 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3487 txf_args
.dmask
= 0xf;
3488 txf_args
.resource
= fmask_ptr
;
3489 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3490 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3492 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3493 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3497 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3498 instr
->op
!= nir_texop_txs
) {
3499 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3500 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3501 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3502 instr
->is_array
? args
.coords
[2] : NULL
,
3503 args
.coords
[sample_chan
], fmask_ptr
);
3506 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3507 nir_const_value
*const_offset
=
3508 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3509 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3510 assert(const_offset
);
3511 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3512 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3513 args
.coords
[i
] = LLVMBuildAdd(
3514 ctx
->ac
.builder
, args
.coords
[i
],
3515 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3520 /* TODO TG4 support */
3522 if (instr
->op
== nir_texop_tg4
) {
3523 if (instr
->is_shadow
)
3526 args
.dmask
= 1 << instr
->component
;
3529 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3530 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3531 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3533 if (instr
->op
== nir_texop_query_levels
)
3534 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3535 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3536 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3537 instr
->op
!= nir_texop_tg4
)
3538 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3539 else if (instr
->op
== nir_texop_txs
&&
3540 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3542 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3543 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3544 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3545 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3546 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3547 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3548 instr
->op
== nir_texop_txs
&&
3549 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3551 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3552 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3553 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3555 } else if (instr
->dest
.ssa
.num_components
!= 4)
3556 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3560 assert(instr
->dest
.is_ssa
);
3561 result
= ac_to_integer(&ctx
->ac
, result
);
3562 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3567 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3569 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3570 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3572 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3573 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3576 static void visit_post_phi(struct ac_nir_context
*ctx
,
3577 nir_phi_instr
*instr
,
3578 LLVMValueRef llvm_phi
)
3580 nir_foreach_phi_src(src
, instr
) {
3581 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3582 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3584 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3588 static void phi_post_pass(struct ac_nir_context
*ctx
)
3590 struct hash_entry
*entry
;
3591 hash_table_foreach(ctx
->phis
, entry
) {
3592 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3593 (LLVMValueRef
)entry
->data
);
3598 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3599 const nir_ssa_undef_instr
*instr
)
3601 unsigned num_components
= instr
->def
.num_components
;
3602 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3605 if (num_components
== 1)
3606 undef
= LLVMGetUndef(type
);
3608 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3610 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3613 static void visit_jump(struct ac_llvm_context
*ctx
,
3614 const nir_jump_instr
*instr
)
3616 switch (instr
->type
) {
3617 case nir_jump_break
:
3618 ac_build_break(ctx
);
3620 case nir_jump_continue
:
3621 ac_build_continue(ctx
);
3624 fprintf(stderr
, "Unknown NIR jump instr: ");
3625 nir_print_instr(&instr
->instr
, stderr
);
3626 fprintf(stderr
, "\n");
3631 static void visit_deref(struct ac_nir_context
*ctx
,
3632 nir_deref_instr
*instr
)
3634 if (instr
->mode
!= nir_var_shared
)
3637 LLVMValueRef result
= NULL
;
3638 switch(instr
->deref_type
) {
3639 case nir_deref_type_var
: {
3640 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3641 result
= entry
->data
;
3644 case nir_deref_type_struct
:
3645 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3646 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3648 case nir_deref_type_array
:
3649 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3650 get_src(ctx
, instr
->arr
.index
));
3653 unreachable("Unhandled deref_instr deref type");
3656 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3659 static void visit_cf_list(struct ac_nir_context
*ctx
,
3660 struct exec_list
*list
);
3662 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3664 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3665 nir_foreach_instr(instr
, block
)
3667 switch (instr
->type
) {
3668 case nir_instr_type_alu
:
3669 visit_alu(ctx
, nir_instr_as_alu(instr
));
3671 case nir_instr_type_load_const
:
3672 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3674 case nir_instr_type_intrinsic
:
3675 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3677 case nir_instr_type_tex
:
3678 visit_tex(ctx
, nir_instr_as_tex(instr
));
3680 case nir_instr_type_phi
:
3681 visit_phi(ctx
, nir_instr_as_phi(instr
));
3683 case nir_instr_type_ssa_undef
:
3684 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3686 case nir_instr_type_jump
:
3687 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3689 case nir_instr_type_deref
:
3690 visit_deref(ctx
, nir_instr_as_deref(instr
));
3693 fprintf(stderr
, "Unknown NIR instr type: ");
3694 nir_print_instr(instr
, stderr
);
3695 fprintf(stderr
, "\n");
3700 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3703 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3705 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3707 nir_block
*then_block
=
3708 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3710 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3712 visit_cf_list(ctx
, &if_stmt
->then_list
);
3714 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3715 nir_block
*else_block
=
3716 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3718 ac_build_else(&ctx
->ac
, else_block
->index
);
3719 visit_cf_list(ctx
, &if_stmt
->else_list
);
3722 ac_build_endif(&ctx
->ac
, then_block
->index
);
3725 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3727 nir_block
*first_loop_block
=
3728 (nir_block
*) exec_list_get_head(&loop
->body
);
3730 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3732 visit_cf_list(ctx
, &loop
->body
);
3734 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3737 static void visit_cf_list(struct ac_nir_context
*ctx
,
3738 struct exec_list
*list
)
3740 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3742 switch (node
->type
) {
3743 case nir_cf_node_block
:
3744 visit_block(ctx
, nir_cf_node_as_block(node
));
3747 case nir_cf_node_if
:
3748 visit_if(ctx
, nir_cf_node_as_if(node
));
3751 case nir_cf_node_loop
:
3752 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3762 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3763 struct ac_shader_abi
*abi
,
3764 struct nir_shader
*nir
,
3765 struct nir_variable
*variable
,
3766 gl_shader_stage stage
)
3768 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3769 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3771 /* tess ctrl has it's own load/store paths for outputs */
3772 if (stage
== MESA_SHADER_TESS_CTRL
)
3775 if (stage
== MESA_SHADER_VERTEX
||
3776 stage
== MESA_SHADER_TESS_EVAL
||
3777 stage
== MESA_SHADER_GEOMETRY
) {
3778 int idx
= variable
->data
.location
+ variable
->data
.index
;
3779 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3780 int length
= nir
->info
.clip_distance_array_size
+
3781 nir
->info
.cull_distance_array_size
;
3790 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3791 for (unsigned chan
= 0; chan
< 4; chan
++) {
3792 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3793 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3799 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3800 enum glsl_base_type type
)
3804 case GLSL_TYPE_UINT
:
3805 case GLSL_TYPE_BOOL
:
3806 case GLSL_TYPE_SUBROUTINE
:
3808 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3810 case GLSL_TYPE_INT64
:
3811 case GLSL_TYPE_UINT64
:
3813 case GLSL_TYPE_DOUBLE
:
3816 unreachable("unknown GLSL type");
3821 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3822 const struct glsl_type
*type
)
3824 if (glsl_type_is_scalar(type
)) {
3825 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3828 if (glsl_type_is_vector(type
)) {
3829 return LLVMVectorType(
3830 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3831 glsl_get_vector_elements(type
));
3834 if (glsl_type_is_matrix(type
)) {
3835 return LLVMArrayType(
3836 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3837 glsl_get_matrix_columns(type
));
3840 if (glsl_type_is_array(type
)) {
3841 return LLVMArrayType(
3842 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3843 glsl_get_length(type
));
3846 assert(glsl_type_is_struct(type
));
3848 LLVMTypeRef member_types
[glsl_get_length(type
)];
3850 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3852 glsl_to_llvm_type(ac
,
3853 glsl_get_struct_field(type
, i
));
3856 return LLVMStructTypeInContext(ac
->context
, member_types
,
3857 glsl_get_length(type
), false);
3861 setup_locals(struct ac_nir_context
*ctx
,
3862 struct nir_function
*func
)
3865 ctx
->num_locals
= 0;
3866 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3867 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3868 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3869 variable
->data
.location_frac
= 0;
3870 ctx
->num_locals
+= attrib_count
;
3872 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3876 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3877 for (j
= 0; j
< 4; j
++) {
3878 ctx
->locals
[i
* 4 + j
] =
3879 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3885 setup_shared(struct ac_nir_context
*ctx
,
3886 struct nir_shader
*nir
)
3888 nir_foreach_variable(variable
, &nir
->shared
) {
3889 LLVMValueRef shared
=
3890 LLVMAddGlobalInAddressSpace(
3891 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3892 variable
->name
? variable
->name
: "",
3893 AC_LOCAL_ADDR_SPACE
);
3894 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3898 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3899 struct nir_shader
*nir
)
3901 struct ac_nir_context ctx
= {};
3902 struct nir_function
*func
;
3907 ctx
.stage
= nir
->info
.stage
;
3909 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3911 nir_foreach_variable(variable
, &nir
->outputs
)
3912 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3915 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3916 _mesa_key_pointer_equal
);
3917 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3918 _mesa_key_pointer_equal
);
3919 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3920 _mesa_key_pointer_equal
);
3922 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3924 nir_index_ssa_defs(func
->impl
);
3925 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3927 setup_locals(&ctx
, func
);
3929 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3930 setup_shared(&ctx
, nir
);
3932 visit_cf_list(&ctx
, &func
->impl
->body
);
3933 phi_post_pass(&ctx
);
3935 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3936 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3941 ralloc_free(ctx
.defs
);
3942 ralloc_free(ctx
.phis
);
3943 ralloc_free(ctx
.vars
);
3947 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3949 /* While it would be nice not to have this flag, we are constrained
3950 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3953 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3955 /* TODO: Indirect indexing of GS inputs is unimplemented.
3957 * TCS and TES load inputs directly from LDS or offchip memory, so
3958 * indirect indexing is trivial.
3960 nir_variable_mode indirect_mask
= 0;
3961 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3962 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3963 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3964 !llvm_has_working_vgpr_indexing
)) {
3965 indirect_mask
|= nir_var_shader_in
;
3967 if (!llvm_has_working_vgpr_indexing
&&
3968 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3969 indirect_mask
|= nir_var_shader_out
;
3971 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3972 * smart enough to handle indirects without causing excess spilling
3973 * causing the gpu to hang.
3975 * See the following thread for more details of the problem:
3976 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3978 indirect_mask
|= nir_var_local
;
3980 nir_lower_indirect_derefs(nir
, indirect_mask
);