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
,
274 ac_to_integer_or_pointer(ctx
, src1
),
275 ac_to_integer_or_pointer(ctx
, src2
), "");
278 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
279 LLVMIntPredicate pred
,
280 LLVMValueRef src0
, LLVMValueRef src1
)
282 return LLVMBuildSelect(ctx
->builder
,
283 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
288 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
291 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
292 LLVMBuildNeg(ctx
->builder
, src0
, ""));
295 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
297 LLVMValueRef src0
, LLVMValueRef src1
)
299 LLVMTypeRef ret_type
;
300 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
302 LLVMValueRef params
[] = { src0
, src1
};
303 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
306 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
307 params
, 2, AC_FUNC_ATTR_READNONE
);
309 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
310 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
314 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
318 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
319 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
321 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
326 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
329 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
332 src0
= ac_to_float(ctx
, src0
);
333 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
334 return LLVMBuildSExt(ctx
->builder
,
335 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
339 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
343 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
348 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
351 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
354 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
355 return LLVMBuildSExt(ctx
->builder
,
356 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
360 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
364 LLVMValueRef cond
= NULL
;
366 src0
= ac_to_float(ctx
, src0
);
367 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
369 if (ctx
->chip_class
>= VI
) {
370 LLVMValueRef args
[2];
371 /* Check if the result is a denormal - and flush to 0 if so. */
373 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
374 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
377 /* need to convert back up to f32 */
378 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
380 if (ctx
->chip_class
>= VI
)
381 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
384 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
385 * so compare the result and flush to 0 if it's smaller.
387 LLVMValueRef temp
, cond2
;
388 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
389 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
390 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
392 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
393 temp
, ctx
->f32_0
, "");
394 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
395 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
400 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
401 LLVMValueRef src0
, LLVMValueRef src1
)
403 LLVMValueRef dst64
, result
;
404 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
405 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
407 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
408 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
409 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
413 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
414 LLVMValueRef src0
, LLVMValueRef src1
)
416 LLVMValueRef dst64
, result
;
417 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
418 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
420 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
421 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
422 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
426 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
428 const LLVMValueRef srcs
[3])
432 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
433 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
435 LLVMValueRef zero
= ctx
->i32_0
;
436 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
437 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
439 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
440 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
441 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
446 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
447 LLVMValueRef src0
, LLVMValueRef src1
,
448 LLVMValueRef src2
, LLVMValueRef src3
)
450 LLVMValueRef bfi_args
[3], result
;
452 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
453 LLVMBuildSub(ctx
->builder
,
454 LLVMBuildShl(ctx
->builder
,
459 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
462 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
465 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
466 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
468 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
469 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
470 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
472 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
476 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
479 LLVMValueRef comp
[2];
481 src0
= ac_to_float(ctx
, src0
);
482 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
483 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
485 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
489 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
492 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
493 LLVMValueRef temps
[2], val
;
496 for (i
= 0; i
< 2; i
++) {
497 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
498 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
499 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
500 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
502 return ac_build_gather_values(ctx
, temps
, 2);
505 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
513 if (op
== nir_op_fddx_fine
)
514 mask
= AC_TID_MASK_LEFT
;
515 else if (op
== nir_op_fddy_fine
)
516 mask
= AC_TID_MASK_TOP
;
518 mask
= AC_TID_MASK_TOP_LEFT
;
520 /* for DDX we want to next X pixel, DDY next Y pixel. */
521 if (op
== nir_op_fddx_fine
||
522 op
== nir_op_fddx_coarse
||
528 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
533 * this takes an I,J coordinate pair,
534 * and works out the X and Y derivatives.
535 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
537 static LLVMValueRef
emit_ddxy_interp(
538 struct ac_nir_context
*ctx
,
539 LLVMValueRef interp_ij
)
541 LLVMValueRef result
[4], a
;
544 for (i
= 0; i
< 2; i
++) {
545 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
546 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
547 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
548 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
550 return ac_build_gather_values(&ctx
->ac
, result
, 4);
553 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
555 LLVMValueRef src
[4], result
= NULL
;
556 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
557 unsigned src_components
;
558 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
560 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
567 case nir_op_pack_half_2x16
:
570 case nir_op_unpack_half_2x16
:
573 case nir_op_cube_face_coord
:
574 case nir_op_cube_face_index
:
578 src_components
= num_components
;
581 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
582 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
590 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
591 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
594 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
597 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
600 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
603 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
604 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
605 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
608 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
609 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
610 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
613 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
616 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
619 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
622 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
625 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
626 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
627 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
628 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
629 ac_to_float_type(&ctx
->ac
, def_type
), result
);
630 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
631 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
634 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
635 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
636 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
639 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
642 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
645 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
648 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
649 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
650 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
653 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
654 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
658 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
661 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
664 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
667 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
668 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
669 LLVMTypeOf(src
[0]), ""),
673 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
674 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
675 LLVMTypeOf(src
[0]), ""),
679 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
680 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
681 LLVMTypeOf(src
[0]), ""),
685 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
688 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
691 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
694 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
697 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
700 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
703 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
706 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
709 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
712 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
715 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
716 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
719 result
= emit_iabs(&ctx
->ac
, src
[0]);
722 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
725 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
728 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
731 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
734 result
= ac_build_isign(&ctx
->ac
, src
[0],
735 instr
->dest
.dest
.ssa
.bit_size
);
738 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
739 result
= ac_build_fsign(&ctx
->ac
, src
[0],
740 instr
->dest
.dest
.ssa
.bit_size
);
743 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
744 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
747 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
748 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
751 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
752 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
754 case nir_op_fround_even
:
755 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
756 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
759 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
760 result
= ac_build_fract(&ctx
->ac
, src
[0],
761 instr
->dest
.dest
.ssa
.bit_size
);
764 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
765 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
768 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
769 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
772 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
773 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
776 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
777 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
780 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
781 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
784 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
785 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
786 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
789 case nir_op_frexp_exp
:
790 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
791 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
792 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
795 case nir_op_frexp_sig
:
796 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
797 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
798 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
801 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
802 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
803 if (ctx
->ac
.chip_class
< GFX9
&&
804 instr
->dest
.dest
.ssa
.bit_size
== 32) {
805 /* Only pre-GFX9 chips do not flush denorms. */
806 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
807 ac_to_float_type(&ctx
->ac
, def_type
),
812 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
813 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
814 if (ctx
->ac
.chip_class
< GFX9
&&
815 instr
->dest
.dest
.ssa
.bit_size
== 32) {
816 /* Only pre-GFX9 chips do not flush denorms. */
817 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
818 ac_to_float_type(&ctx
->ac
, def_type
),
823 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
824 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
827 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
828 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
829 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
831 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
833 case nir_op_ibitfield_extract
:
834 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
836 case nir_op_ubitfield_extract
:
837 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
839 case nir_op_bitfield_insert
:
840 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
842 case nir_op_bitfield_reverse
:
843 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
845 case nir_op_bit_count
:
846 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
851 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
852 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
853 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
858 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
859 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
870 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
877 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
879 case nir_op_f2f16_rtz
:
880 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
881 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
882 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
883 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
885 case nir_op_f2f16_rtne
:
889 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
890 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
891 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
893 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
898 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
899 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
900 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
902 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
907 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
908 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
909 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
911 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
914 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
916 case nir_op_find_lsb
:
917 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
918 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
920 case nir_op_ufind_msb
:
921 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
922 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
924 case nir_op_ifind_msb
:
925 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
926 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
928 case nir_op_uadd_carry
:
929 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
930 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
931 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
933 case nir_op_usub_borrow
:
934 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
935 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
936 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
941 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
944 result
= emit_f2b(&ctx
->ac
, src
[0]);
949 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
952 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
953 result
= emit_i2b(&ctx
->ac
, src
[0]);
955 case nir_op_fquantize2f16
:
956 result
= emit_f2f16(&ctx
->ac
, src
[0]);
958 case nir_op_umul_high
:
959 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
960 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
961 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
963 case nir_op_imul_high
:
964 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
965 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
966 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
968 case nir_op_pack_half_2x16
:
969 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
971 case nir_op_unpack_half_2x16
:
972 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
976 case nir_op_fddx_fine
:
977 case nir_op_fddy_fine
:
978 case nir_op_fddx_coarse
:
979 case nir_op_fddy_coarse
:
980 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
983 case nir_op_unpack_64_2x32_split_x
: {
984 assert(ac_get_llvm_num_components(src
[0]) == 1);
985 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
988 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
993 case nir_op_unpack_64_2x32_split_y
: {
994 assert(ac_get_llvm_num_components(src
[0]) == 1);
995 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
998 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1003 case nir_op_pack_64_2x32_split
: {
1004 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1005 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1006 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1010 case nir_op_cube_face_coord
: {
1011 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1012 LLVMValueRef results
[2];
1014 for (unsigned chan
= 0; chan
< 3; chan
++)
1015 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1016 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1017 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1018 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1019 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1020 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1024 case nir_op_cube_face_index
: {
1025 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1027 for (unsigned chan
= 0; chan
< 3; chan
++)
1028 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1029 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1030 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1035 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1036 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1037 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1038 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1041 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1042 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1045 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1046 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
1049 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1050 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1051 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1052 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1055 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1056 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1059 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1060 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1062 case nir_op_fmed3
: {
1063 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1064 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1065 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1066 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1067 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1068 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1069 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1070 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1073 case nir_op_imed3
: {
1074 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1075 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1076 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1077 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1080 case nir_op_umed3
: {
1081 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1082 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1083 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1084 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1089 fprintf(stderr
, "Unknown NIR alu instr: ");
1090 nir_print_instr(&instr
->instr
, stderr
);
1091 fprintf(stderr
, "\n");
1096 assert(instr
->dest
.dest
.is_ssa
);
1097 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1098 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1102 static void visit_load_const(struct ac_nir_context
*ctx
,
1103 const nir_load_const_instr
*instr
)
1105 LLVMValueRef values
[4], value
= NULL
;
1106 LLVMTypeRef element_type
=
1107 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1109 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1110 switch (instr
->def
.bit_size
) {
1112 values
[i
] = LLVMConstInt(element_type
,
1113 instr
->value
.u16
[i
], false);
1116 values
[i
] = LLVMConstInt(element_type
,
1117 instr
->value
.u32
[i
], false);
1120 values
[i
] = LLVMConstInt(element_type
,
1121 instr
->value
.u64
[i
], false);
1125 "unsupported nir load_const bit_size: %d\n",
1126 instr
->def
.bit_size
);
1130 if (instr
->def
.num_components
> 1) {
1131 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1135 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1139 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1142 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1143 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1146 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1147 /* On VI, the descriptor contains the size in bytes,
1148 * but TXQ must return the size in elements.
1149 * The stride is always non-zero for resources using TXQ.
1151 LLVMValueRef stride
=
1152 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1154 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1155 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1156 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1157 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1159 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1164 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1166 struct ac_image_args
*args
,
1167 const nir_tex_instr
*instr
)
1169 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1170 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1171 LLVMValueRef half_texel
[2];
1172 LLVMValueRef compare_cube_wa
= NULL
;
1173 LLVMValueRef result
;
1177 struct ac_image_args txq_args
= { 0 };
1179 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1180 txq_args
.opcode
= ac_image_get_resinfo
;
1181 txq_args
.dmask
= 0xf;
1182 txq_args
.lod
= ctx
->i32_0
;
1183 txq_args
.resource
= args
->resource
;
1184 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1185 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1187 for (unsigned c
= 0; c
< 2; c
++) {
1188 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1189 LLVMConstInt(ctx
->i32
, c
, false), "");
1190 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1191 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1192 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1193 LLVMConstReal(ctx
->f32
, -0.5), "");
1197 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1199 for (unsigned c
= 0; c
< 2; c
++) {
1201 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1202 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1206 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1207 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1208 * workaround by sampling using a scaled type and converting.
1209 * This is taken from amdgpu-pro shaders.
1211 /* NOTE this produces some ugly code compared to amdgpu-pro,
1212 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1213 * and then reads them back. -pro generates two selects,
1214 * one s_cmp for the descriptor rewriting
1215 * one v_cmp for the coordinate and result changes.
1217 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1218 LLVMValueRef tmp
, tmp2
;
1220 /* workaround 8/8/8/8 uint/sint cube gather bug */
1221 /* first detect it then change to a scaled read and f2i */
1222 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1225 /* extract the DATA_FORMAT */
1226 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1227 LLVMConstInt(ctx
->i32
, 6, false), false);
1229 /* is the DATA_FORMAT == 8_8_8_8 */
1230 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1232 if (stype
== GLSL_TYPE_UINT
)
1233 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1234 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1235 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1237 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1238 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1239 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1241 /* replace the NUM FORMAT in the descriptor */
1242 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1243 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1245 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1247 /* don't modify the coordinates for this case */
1248 for (unsigned c
= 0; c
< 2; ++c
)
1249 args
->coords
[c
] = LLVMBuildSelect(
1250 ctx
->builder
, compare_cube_wa
,
1251 orig_coords
[c
], args
->coords
[c
], "");
1254 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1255 result
= ac_build_image_opcode(ctx
, args
);
1257 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1258 LLVMValueRef tmp
, tmp2
;
1260 /* if the cube workaround is in place, f2i the result. */
1261 for (unsigned c
= 0; c
< 4; c
++) {
1262 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1263 if (stype
== GLSL_TYPE_UINT
)
1264 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1266 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1267 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1268 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1269 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1270 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1271 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1277 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1279 nir_deref_instr
*texture_deref_instr
= NULL
;
1281 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1282 switch (instr
->src
[i
].src_type
) {
1283 case nir_tex_src_texture_deref
:
1284 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1290 return texture_deref_instr
;
1293 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1294 const nir_tex_instr
*instr
,
1295 struct ac_image_args
*args
)
1297 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1298 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1300 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1301 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1305 util_last_bit(mask
),
1308 return ac_build_buffer_load_format(&ctx
->ac
,
1312 util_last_bit(mask
),
1317 args
->opcode
= ac_image_sample
;
1319 switch (instr
->op
) {
1321 case nir_texop_txf_ms
:
1322 case nir_texop_samples_identical
:
1323 args
->opcode
= args
->level_zero
||
1324 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1325 ac_image_load
: ac_image_load_mip
;
1326 args
->level_zero
= false;
1329 case nir_texop_query_levels
:
1330 args
->opcode
= ac_image_get_resinfo
;
1332 args
->lod
= ctx
->ac
.i32_0
;
1333 args
->level_zero
= false;
1336 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1338 args
->level_zero
= true;
1342 args
->opcode
= ac_image_gather4
;
1343 args
->level_zero
= true;
1346 args
->opcode
= ac_image_get_lod
;
1352 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1353 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1354 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1355 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1356 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1357 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1358 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1362 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1363 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1364 if ((args
->dim
== ac_image_2darray
||
1365 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1366 args
->coords
[1] = ctx
->ac
.i32_0
;
1370 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1371 return ac_build_image_opcode(&ctx
->ac
, args
);
1374 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1375 nir_intrinsic_instr
*instr
)
1377 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1378 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1380 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1381 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1385 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1386 nir_intrinsic_instr
*instr
)
1388 LLVMValueRef ptr
, addr
;
1390 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1391 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1392 get_src(ctx
, instr
->src
[0]), "");
1394 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1396 if (instr
->dest
.ssa
.bit_size
== 16) {
1397 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1398 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1399 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1400 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1401 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1402 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1403 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1404 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1405 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1406 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1407 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1408 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1409 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1410 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1411 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1412 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1415 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1417 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1420 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1421 const nir_intrinsic_instr
*instr
)
1423 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1425 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1428 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1430 uint32_t new_mask
= 0;
1431 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1432 if (mask
& (1u << i
))
1433 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1437 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1438 unsigned start
, unsigned count
)
1440 LLVMValueRef mask
[] = {
1441 ctx
->i32_0
, ctx
->i32_1
,
1442 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1444 unsigned src_elements
= ac_get_llvm_num_components(src
);
1446 if (count
== src_elements
) {
1449 } else if (count
== 1) {
1450 assert(start
< src_elements
);
1451 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1453 assert(start
+ count
<= src_elements
);
1455 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1456 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1460 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1461 nir_intrinsic_instr
*instr
)
1463 const char *store_name
;
1464 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1465 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1466 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1467 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1468 LLVMValueRef glc
= ctx
->ac
.i1false
;
1470 if (access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
1471 glc
= ctx
->ac
.i1true
;
1473 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1474 get_src(ctx
, instr
->src
[1]), true);
1475 LLVMValueRef base_data
= ac_to_float(&ctx
->ac
, src_data
);
1476 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1477 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1481 LLVMValueRef data
, offset
;
1482 LLVMTypeRef data_type
;
1484 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1486 /* Due to an LLVM limitation, split 3-element writes
1487 * into a 2-element and a 1-element write. */
1489 writemask
|= 1 << (start
+ 2);
1492 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1494 /* we can only store 4 DWords at the same time.
1495 * can only happen for 64 Bit vectors. */
1496 if (num_bytes
> 16) {
1497 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1502 /* check alignment of 16 Bit stores */
1503 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1504 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1508 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1511 offset
= base_offset
;
1513 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1514 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1516 if (num_bytes
== 2) {
1517 store_name
= "llvm.amdgcn.tbuffer.store.i32";
1518 data_type
= ctx
->ac
.i32
;
1519 LLVMValueRef tbuffer_params
[] = {
1522 ctx
->ac
.i32_0
, /* vindex */
1523 offset
, /* voffset */
1526 LLVMConstInt(ctx
->ac
.i32
, 2, false), // dfmt (= 16bit)
1527 LLVMConstInt(ctx
->ac
.i32
, 4, false), // nfmt (= uint)
1531 ac_build_intrinsic(&ctx
->ac
, store_name
,
1532 ctx
->ac
.voidt
, tbuffer_params
, 10, 0);
1534 switch (num_bytes
) {
1535 case 16: /* v4f32 */
1536 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1537 data_type
= ctx
->ac
.v4f32
;
1540 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1541 data_type
= ctx
->ac
.v2f32
;
1544 store_name
= "llvm.amdgcn.buffer.store.f32";
1545 data_type
= ctx
->ac
.f32
;
1548 unreachable("Malformed vector store.");
1550 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1551 LLVMValueRef params
[] = {
1554 ctx
->ac
.i32_0
, /* vindex */
1557 ctx
->ac
.i1false
, /* slc */
1559 ac_build_intrinsic(&ctx
->ac
, store_name
,
1560 ctx
->ac
.voidt
, params
, 6, 0);
1565 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1566 const nir_intrinsic_instr
*instr
)
1569 LLVMValueRef params
[6];
1572 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1573 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1575 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1576 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1577 get_src(ctx
, instr
->src
[0]),
1579 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1580 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1581 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1583 switch (instr
->intrinsic
) {
1584 case nir_intrinsic_ssbo_atomic_add
:
1585 name
= "llvm.amdgcn.buffer.atomic.add";
1587 case nir_intrinsic_ssbo_atomic_imin
:
1588 name
= "llvm.amdgcn.buffer.atomic.smin";
1590 case nir_intrinsic_ssbo_atomic_umin
:
1591 name
= "llvm.amdgcn.buffer.atomic.umin";
1593 case nir_intrinsic_ssbo_atomic_imax
:
1594 name
= "llvm.amdgcn.buffer.atomic.smax";
1596 case nir_intrinsic_ssbo_atomic_umax
:
1597 name
= "llvm.amdgcn.buffer.atomic.umax";
1599 case nir_intrinsic_ssbo_atomic_and
:
1600 name
= "llvm.amdgcn.buffer.atomic.and";
1602 case nir_intrinsic_ssbo_atomic_or
:
1603 name
= "llvm.amdgcn.buffer.atomic.or";
1605 case nir_intrinsic_ssbo_atomic_xor
:
1606 name
= "llvm.amdgcn.buffer.atomic.xor";
1608 case nir_intrinsic_ssbo_atomic_exchange
:
1609 name
= "llvm.amdgcn.buffer.atomic.swap";
1611 case nir_intrinsic_ssbo_atomic_comp_swap
:
1612 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1618 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1621 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1622 const nir_intrinsic_instr
*instr
)
1624 LLVMValueRef results
[2];
1626 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1627 int num_components
= instr
->num_components
;
1628 int num_bytes
= num_components
* elem_size_bytes
;
1629 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1630 LLVMValueRef glc
= ctx
->ac
.i1false
;
1632 if (access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
1633 glc
= ctx
->ac
.i1true
;
1635 for (int i
= 0; i
< num_bytes
; i
+= load_bytes
) {
1636 load_bytes
= MIN2(num_bytes
- i
, 16);
1637 const char *load_name
;
1638 LLVMTypeRef data_type
;
1639 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1640 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
1641 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1642 get_src(ctx
, instr
->src
[0]), false);
1643 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1645 int idx
= i
? 1 : 0;
1646 if (load_bytes
== 2) {
1647 results
[idx
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1655 switch (load_bytes
) {
1658 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1659 data_type
= ctx
->ac
.v4f32
;
1663 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1664 data_type
= ctx
->ac
.v2f32
;
1667 load_name
= "llvm.amdgcn.buffer.load.f32";
1668 data_type
= ctx
->ac
.f32
;
1671 unreachable("Malformed load buffer.");
1673 LLVMValueRef params
[] = {
1676 LLVMBuildAdd(ctx
->ac
.builder
, offset
, immoffset
, ""),
1680 results
[idx
] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1681 unsigned num_elems
= ac_get_type_size(data_type
) / elem_size_bytes
;
1682 LLVMTypeRef resTy
= LLVMVectorType(LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
), num_elems
);
1683 results
[idx
] = LLVMBuildBitCast(ctx
->ac
.builder
, results
[idx
], resTy
, "");
1688 LLVMValueRef ret
= results
[0];
1689 if (num_bytes
> 16 || num_components
== 3) {
1690 LLVMValueRef masks
[] = {
1691 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1692 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1695 if (num_bytes
> 16 && num_components
== 3) {
1696 /* we end up with a v2i64 and i64 but shuffle fails on that */
1697 results
[1] = ac_build_expand(&ctx
->ac
, results
[1], 1, 2);
1700 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1701 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1702 results
[num_bytes
> 16 ? 1 : 0], swizzle
, "");
1705 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1706 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1709 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1710 const nir_intrinsic_instr
*instr
)
1713 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1714 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1715 int num_components
= instr
->num_components
;
1717 if (ctx
->abi
->load_ubo
)
1718 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1720 if (instr
->dest
.ssa
.bit_size
== 64)
1721 num_components
*= 2;
1723 if (instr
->dest
.ssa
.bit_size
== 16) {
1724 LLVMValueRef results
[num_components
];
1725 for (unsigned i
= 0; i
< num_components
; ++i
) {
1726 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1731 LLVMConstInt(ctx
->ac
.i32
, 2 * i
, 0),
1734 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1736 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1737 NULL
, 0, false, false, true, true);
1739 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1742 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1743 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1747 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1748 bool vs_in
, unsigned *vertex_index_out
,
1749 LLVMValueRef
*vertex_index_ref
,
1750 unsigned *const_out
, LLVMValueRef
*indir_out
)
1752 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1753 nir_deref_path path
;
1754 unsigned idx_lvl
= 1;
1756 nir_deref_path_init(&path
, instr
, NULL
);
1758 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1759 if (vertex_index_ref
) {
1760 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1761 if (vertex_index_out
)
1762 *vertex_index_out
= 0;
1764 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1766 *vertex_index_out
= v
->u32
[0];
1771 uint32_t const_offset
= 0;
1772 LLVMValueRef offset
= NULL
;
1774 if (var
->data
.compact
) {
1775 assert(instr
->deref_type
== nir_deref_type_array
);
1776 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1778 const_offset
= v
->u32
[0];
1782 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1783 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1784 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1785 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1787 for (unsigned i
= 0; i
< index
; i
++) {
1788 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1789 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1791 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1792 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1793 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1794 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1796 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1800 unreachable("Uhandled deref type in get_deref_instr_offset");
1804 nir_deref_path_finish(&path
);
1806 if (const_offset
&& offset
)
1807 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1808 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1811 *const_out
= const_offset
;
1812 *indir_out
= offset
;
1815 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1816 nir_intrinsic_instr
*instr
,
1819 LLVMValueRef result
;
1820 LLVMValueRef vertex_index
= NULL
;
1821 LLVMValueRef indir_index
= NULL
;
1822 unsigned const_index
= 0;
1824 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1826 unsigned location
= var
->data
.location
;
1827 unsigned driver_location
= var
->data
.driver_location
;
1828 const bool is_patch
= var
->data
.patch
;
1829 const bool is_compact
= var
->data
.compact
;
1831 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1832 false, NULL
, is_patch
? NULL
: &vertex_index
,
1833 &const_index
, &indir_index
);
1835 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1837 LLVMTypeRef src_component_type
;
1838 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1839 src_component_type
= LLVMGetElementType(dest_type
);
1841 src_component_type
= dest_type
;
1843 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1844 vertex_index
, indir_index
,
1845 const_index
, location
, driver_location
,
1846 var
->data
.location_frac
,
1847 instr
->num_components
,
1848 is_patch
, is_compact
, load_inputs
);
1849 if (instr
->dest
.ssa
.bit_size
== 16) {
1850 result
= ac_to_integer(&ctx
->ac
, result
);
1851 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1853 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1856 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1857 nir_intrinsic_instr
*instr
)
1859 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1861 LLVMValueRef values
[8];
1863 int ve
= instr
->dest
.ssa
.num_components
;
1865 LLVMValueRef indir_index
;
1867 unsigned const_index
;
1868 unsigned stride
= 4;
1869 int mode
= nir_var_shared
;
1872 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1873 var
->data
.mode
== nir_var_shader_in
;
1874 if (var
->data
.compact
)
1876 idx
= var
->data
.driver_location
;
1877 comp
= var
->data
.location_frac
;
1878 mode
= var
->data
.mode
;
1880 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), vs_in
, NULL
, NULL
,
1881 &const_index
, &indir_index
);
1884 if (instr
->dest
.ssa
.bit_size
== 64)
1888 case nir_var_shader_in
:
1889 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1890 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1891 return load_tess_varyings(ctx
, instr
, true);
1894 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1895 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1896 LLVMValueRef indir_index
;
1897 unsigned const_index
, vertex_index
;
1898 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1899 false, &vertex_index
, NULL
, &const_index
, &indir_index
);
1901 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1902 var
->data
.driver_location
,
1903 var
->data
.location_frac
,
1904 instr
->num_components
, vertex_index
, const_index
, type
);
1907 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1909 unsigned count
= glsl_count_attribute_slots(
1911 ctx
->stage
== MESA_SHADER_VERTEX
);
1913 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1914 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1915 stride
, false, true);
1917 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1921 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1925 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1927 unsigned count
= glsl_count_attribute_slots(
1930 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1931 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1932 stride
, true, true);
1934 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1938 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1942 case nir_var_shared
: {
1943 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
1944 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1945 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1946 get_def_type(ctx
, &instr
->dest
.ssa
),
1949 case nir_var_shader_out
:
1950 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1951 return load_tess_varyings(ctx
, instr
, false);
1954 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1956 unsigned count
= glsl_count_attribute_slots(
1959 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1960 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1961 stride
, true, true);
1963 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1967 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1968 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1974 unreachable("unhandle variable mode");
1976 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1977 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1981 visit_store_var(struct ac_nir_context
*ctx
,
1982 nir_intrinsic_instr
*instr
)
1984 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1986 LLVMValueRef temp_ptr
, value
;
1987 int idx
= var
->data
.driver_location
;
1988 unsigned comp
= var
->data
.location_frac
;
1989 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
1990 int writemask
= instr
->const_index
[0];
1991 LLVMValueRef indir_index
;
1992 unsigned const_index
;
1994 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), false,
1995 NULL
, NULL
, &const_index
, &indir_index
);
1997 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1999 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2000 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2003 writemask
= widen_mask(writemask
, 2);
2006 writemask
= writemask
<< comp
;
2008 switch (var
->data
.mode
) {
2009 case nir_var_shader_out
:
2011 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2012 LLVMValueRef vertex_index
= NULL
;
2013 LLVMValueRef indir_index
= NULL
;
2014 unsigned const_index
= 0;
2015 const bool is_patch
= var
->data
.patch
;
2017 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2018 false, NULL
, is_patch
? NULL
: &vertex_index
,
2019 &const_index
, &indir_index
);
2021 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2022 vertex_index
, indir_index
,
2023 const_index
, src
, writemask
);
2027 for (unsigned chan
= 0; chan
< 8; chan
++) {
2029 if (!(writemask
& (1 << chan
)))
2032 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2034 if (var
->data
.compact
)
2037 unsigned count
= glsl_count_attribute_slots(
2040 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2041 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2042 stride
, true, true);
2044 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2045 value
, indir_index
, "");
2046 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2047 count
, stride
, tmp_vec
);
2050 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2052 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2057 for (unsigned chan
= 0; chan
< 8; chan
++) {
2058 if (!(writemask
& (1 << chan
)))
2061 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2063 unsigned count
= glsl_count_attribute_slots(
2066 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2067 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2070 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2071 value
, indir_index
, "");
2072 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2075 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2077 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2081 case nir_var_shared
: {
2082 int writemask
= instr
->const_index
[0];
2083 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2084 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2085 if (util_is_power_of_two_nonzero(writemask
)) {
2086 val
= LLVMBuildBitCast(
2087 ctx
->ac
.builder
, val
,
2088 LLVMGetElementType(LLVMTypeOf(address
)), "");
2089 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2091 for (unsigned chan
= 0; chan
< 4; chan
++) {
2092 if (!(writemask
& (1 << chan
)))
2095 LLVMBuildStructGEP(ctx
->ac
.builder
,
2097 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2099 src
= LLVMBuildBitCast(
2100 ctx
->ac
.builder
, src
,
2101 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2102 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2112 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2115 case GLSL_SAMPLER_DIM_BUF
:
2117 case GLSL_SAMPLER_DIM_1D
:
2118 return array
? 2 : 1;
2119 case GLSL_SAMPLER_DIM_2D
:
2120 return array
? 3 : 2;
2121 case GLSL_SAMPLER_DIM_MS
:
2122 return array
? 4 : 3;
2123 case GLSL_SAMPLER_DIM_3D
:
2124 case GLSL_SAMPLER_DIM_CUBE
:
2126 case GLSL_SAMPLER_DIM_RECT
:
2127 case GLSL_SAMPLER_DIM_SUBPASS
:
2129 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2138 /* Adjust the sample index according to FMASK.
2140 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2141 * which is the identity mapping. Each nibble says which physical sample
2142 * should be fetched to get that sample.
2144 * For example, 0x11111100 means there are only 2 samples stored and
2145 * the second sample covers 3/4 of the pixel. When reading samples 0
2146 * and 1, return physical sample 0 (determined by the first two 0s
2147 * in FMASK), otherwise return physical sample 1.
2149 * The sample index should be adjusted as follows:
2150 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2152 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2153 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2154 LLVMValueRef coord_z
,
2155 LLVMValueRef sample_index
,
2156 LLVMValueRef fmask_desc_ptr
)
2158 struct ac_image_args args
= {0};
2161 args
.coords
[0] = coord_x
;
2162 args
.coords
[1] = coord_y
;
2164 args
.coords
[2] = coord_z
;
2166 args
.opcode
= ac_image_load
;
2167 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2168 args
.resource
= fmask_desc_ptr
;
2170 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2172 res
= ac_build_image_opcode(ctx
, &args
);
2174 res
= ac_to_integer(ctx
, res
);
2175 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2176 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2178 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2182 LLVMValueRef sample_index4
=
2183 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2184 LLVMValueRef shifted_fmask
=
2185 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2186 LLVMValueRef final_sample
=
2187 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2189 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2190 * resource descriptor is 0 (invalid),
2192 LLVMValueRef fmask_desc
=
2193 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2196 LLVMValueRef fmask_word1
=
2197 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2200 LLVMValueRef word1_is_nonzero
=
2201 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2202 fmask_word1
, ctx
->i32_0
, "");
2204 /* Replace the MSAA sample index. */
2206 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2207 final_sample
, sample_index
, "");
2208 return sample_index
;
2211 static nir_variable
*get_image_variable(const nir_intrinsic_instr
*instr
)
2213 assert(instr
->src
[0].is_ssa
);
2214 return nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2217 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2218 const nir_intrinsic_instr
*instr
,
2219 enum ac_descriptor_type desc_type
,
2222 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2225 static void get_image_coords(struct ac_nir_context
*ctx
,
2226 const nir_intrinsic_instr
*instr
,
2227 struct ac_image_args
*args
)
2229 const struct glsl_type
*type
= glsl_without_array(get_image_variable(instr
)->type
);
2231 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2232 LLVMValueRef masks
[] = {
2233 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2234 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2236 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2239 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2240 bool is_array
= glsl_sampler_type_is_array(type
);
2241 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2242 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2243 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2244 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2245 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2246 count
= image_type_to_components_count(dim
, is_array
);
2249 LLVMValueRef fmask_load_address
[3];
2252 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2253 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2255 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2257 fmask_load_address
[2] = NULL
;
2259 for (chan
= 0; chan
< 2; ++chan
)
2260 fmask_load_address
[chan
] =
2261 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2262 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2263 ctx
->ac
.i32
, ""), "");
2264 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2266 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2267 fmask_load_address
[0],
2268 fmask_load_address
[1],
2269 fmask_load_address
[2],
2271 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2272 AC_DESC_FMASK
, NULL
, false, false));
2274 if (count
== 1 && !gfx9_1d
) {
2275 if (instr
->src
[1].ssa
->num_components
)
2276 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2278 args
->coords
[0] = src0
;
2283 for (chan
= 0; chan
< count
; ++chan
) {
2284 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2287 for (chan
= 0; chan
< 2; ++chan
) {
2288 args
->coords
[chan
] = LLVMBuildAdd(
2289 ctx
->ac
.builder
, args
->coords
[chan
],
2291 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2292 ctx
->ac
.i32
, ""), "");
2294 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2295 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2301 args
->coords
[2] = args
->coords
[1];
2302 args
->coords
[1] = ctx
->ac
.i32_0
;
2304 args
->coords
[1] = ctx
->ac
.i32_0
;
2309 args
->coords
[count
] = sample_index
;
2315 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2316 const nir_intrinsic_instr
*instr
, bool write
)
2318 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2319 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2320 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2321 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2322 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2324 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2325 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2326 elem_count
, stride
, "");
2328 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2329 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2334 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2335 const nir_intrinsic_instr
*instr
)
2338 const nir_variable
*var
= get_image_variable(instr
);
2339 const struct glsl_type
*type
= var
->type
;
2341 type
= glsl_without_array(type
);
2343 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2344 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2345 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2346 unsigned num_channels
= util_last_bit(mask
);
2347 LLVMValueRef rsrc
, vindex
;
2349 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2350 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2353 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2354 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2355 ctx
->ac
.i32_0
, num_channels
,
2357 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2359 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2360 res
= ac_to_integer(&ctx
->ac
, res
);
2362 struct ac_image_args args
= {};
2363 args
.opcode
= ac_image_load
;
2364 get_image_coords(ctx
, instr
, &args
);
2365 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2366 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2367 glsl_sampler_type_is_array(type
));
2369 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2370 if (var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
))
2371 args
.cache_policy
|= ac_glc
;
2373 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2375 return ac_to_integer(&ctx
->ac
, res
);
2378 static void visit_image_store(struct ac_nir_context
*ctx
,
2379 nir_intrinsic_instr
*instr
)
2381 LLVMValueRef params
[8];
2382 const nir_variable
*var
= get_image_variable(instr
);
2383 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2384 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2385 LLVMValueRef glc
= ctx
->ac
.i1false
;
2386 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2388 glc
= ctx
->ac
.i1true
;
2390 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2391 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2393 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3])); /* data */
2395 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2396 ctx
->ac
.i32_0
, ""); /* vindex */
2397 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2398 if (HAVE_LLVM
>= 0x800) {
2399 params
[4] = ctx
->ac
.i32_0
; /* soffset */
2400 params
[5] = glc
? ctx
->ac
.i32_1
: ctx
->ac
.i32_0
;
2401 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.struct.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2404 params
[4] = glc
; /* glc */
2405 params
[5] = ctx
->ac
.i1false
; /* slc */
2406 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2410 struct ac_image_args args
= {};
2411 args
.opcode
= ac_image_store
;
2412 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2413 get_image_coords(ctx
, instr
, &args
);
2414 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2415 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2416 glsl_sampler_type_is_array(type
));
2418 if (force_glc
|| (var
->data
.image
.access
& (ACCESS_VOLATILE
| ACCESS_COHERENT
)))
2419 args
.cache_policy
|= ac_glc
;
2421 ac_build_image_opcode(&ctx
->ac
, &args
);
2426 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2427 const nir_intrinsic_instr
*instr
)
2429 LLVMValueRef params
[7];
2430 int param_count
= 0;
2431 const nir_variable
*var
= get_image_variable(instr
);
2433 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2434 const char *atomic_name
;
2435 char intrinsic_name
[64];
2436 enum ac_atomic_op atomic_subop
;
2437 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2438 MAYBE_UNUSED
int length
;
2440 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2442 switch (instr
->intrinsic
) {
2443 case nir_intrinsic_image_deref_atomic_add
:
2444 atomic_name
= "add";
2445 atomic_subop
= ac_atomic_add
;
2447 case nir_intrinsic_image_deref_atomic_min
:
2448 atomic_name
= is_unsigned
? "umin" : "smin";
2449 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2451 case nir_intrinsic_image_deref_atomic_max
:
2452 atomic_name
= is_unsigned
? "umax" : "smax";
2453 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2455 case nir_intrinsic_image_deref_atomic_and
:
2456 atomic_name
= "and";
2457 atomic_subop
= ac_atomic_and
;
2459 case nir_intrinsic_image_deref_atomic_or
:
2461 atomic_subop
= ac_atomic_or
;
2463 case nir_intrinsic_image_deref_atomic_xor
:
2464 atomic_name
= "xor";
2465 atomic_subop
= ac_atomic_xor
;
2467 case nir_intrinsic_image_deref_atomic_exchange
:
2468 atomic_name
= "swap";
2469 atomic_subop
= ac_atomic_swap
;
2471 case nir_intrinsic_image_deref_atomic_comp_swap
:
2472 atomic_name
= "cmpswap";
2473 atomic_subop
= 0; /* not used */
2480 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2481 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2483 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2484 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2485 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2486 ctx
->ac
.i32_0
, ""); /* vindex */
2487 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2488 if (HAVE_LLVM
>= 0x800) {
2489 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2490 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2492 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2493 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2495 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2497 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2498 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2501 assert(length
< sizeof(intrinsic_name
));
2502 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2503 params
, param_count
, 0);
2505 struct ac_image_args args
= {};
2506 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2507 args
.atomic
= atomic_subop
;
2508 args
.data
[0] = params
[0];
2510 args
.data
[1] = params
[1];
2511 get_image_coords(ctx
, instr
, &args
);
2512 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2513 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2514 glsl_sampler_type_is_array(type
));
2516 return ac_build_image_opcode(&ctx
->ac
, &args
);
2520 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2521 const nir_intrinsic_instr
*instr
)
2523 const nir_variable
*var
= get_image_variable(instr
);
2524 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2526 struct ac_image_args args
= { 0 };
2527 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2528 glsl_sampler_type_is_array(type
));
2530 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2531 args
.opcode
= ac_image_get_resinfo
;
2532 args
.lod
= ctx
->ac
.i32_0
;
2533 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2535 return ac_build_image_opcode(&ctx
->ac
, &args
);
2538 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2539 const nir_intrinsic_instr
*instr
)
2542 const nir_variable
*var
= get_image_variable(instr
);
2543 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2545 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2546 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2548 struct ac_image_args args
= { 0 };
2550 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2551 glsl_sampler_type_is_array(type
));
2553 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2554 args
.opcode
= ac_image_get_resinfo
;
2555 args
.lod
= ctx
->ac
.i32_0
;
2556 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2558 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2560 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2562 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2563 glsl_sampler_type_is_array(type
)) {
2564 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2565 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2566 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2567 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2569 if (ctx
->ac
.chip_class
>= GFX9
&&
2570 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2571 glsl_sampler_type_is_array(type
)) {
2572 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2573 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2580 static void emit_membar(struct ac_llvm_context
*ac
,
2581 const nir_intrinsic_instr
*instr
)
2583 unsigned waitcnt
= NOOP_WAITCNT
;
2585 switch (instr
->intrinsic
) {
2586 case nir_intrinsic_memory_barrier
:
2587 case nir_intrinsic_group_memory_barrier
:
2588 waitcnt
&= VM_CNT
& LGKM_CNT
;
2590 case nir_intrinsic_memory_barrier_atomic_counter
:
2591 case nir_intrinsic_memory_barrier_buffer
:
2592 case nir_intrinsic_memory_barrier_image
:
2595 case nir_intrinsic_memory_barrier_shared
:
2596 waitcnt
&= LGKM_CNT
;
2601 if (waitcnt
!= NOOP_WAITCNT
)
2602 ac_build_waitcnt(ac
, waitcnt
);
2605 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2607 /* SI only (thanks to a hw bug workaround):
2608 * The real barrier instruction isn’t needed, because an entire patch
2609 * always fits into a single wave.
2611 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2612 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2615 ac_build_s_barrier(ac
);
2618 static void emit_discard(struct ac_nir_context
*ctx
,
2619 const nir_intrinsic_instr
*instr
)
2623 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2624 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2625 get_src(ctx
, instr
->src
[0]),
2628 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2629 cond
= ctx
->ac
.i1false
;
2632 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2636 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2638 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2639 "llvm.amdgcn.ps.live",
2640 ctx
->ac
.i1
, NULL
, 0,
2641 AC_FUNC_ATTR_READNONE
);
2642 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2643 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2647 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2649 LLVMValueRef result
;
2650 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2651 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2652 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2654 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2658 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2660 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2661 LLVMValueRef result
;
2662 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2663 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2664 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2666 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2671 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2673 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2674 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2675 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2677 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2682 visit_first_invocation(struct ac_nir_context
*ctx
)
2684 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2686 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2687 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2688 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2690 ctx
->ac
.i64
, args
, 2,
2691 AC_FUNC_ATTR_NOUNWIND
|
2692 AC_FUNC_ATTR_READNONE
);
2694 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2698 visit_load_shared(struct ac_nir_context
*ctx
,
2699 const nir_intrinsic_instr
*instr
)
2701 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2703 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2705 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2706 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2707 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2708 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2711 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2712 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2716 visit_store_shared(struct ac_nir_context
*ctx
,
2717 const nir_intrinsic_instr
*instr
)
2719 LLVMValueRef derived_ptr
, data
,index
;
2720 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2722 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2723 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2725 int writemask
= nir_intrinsic_write_mask(instr
);
2726 for (int chan
= 0; chan
< 4; chan
++) {
2727 if (!(writemask
& (1 << chan
))) {
2730 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2731 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2732 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2733 LLVMBuildStore(builder
, data
, derived_ptr
);
2737 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2738 const nir_intrinsic_instr
*instr
,
2739 LLVMValueRef ptr
, int src_idx
)
2741 LLVMValueRef result
;
2742 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2744 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2745 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2746 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2747 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2749 LLVMAtomicOrderingSequentiallyConsistent
,
2750 LLVMAtomicOrderingSequentiallyConsistent
,
2752 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2754 LLVMAtomicRMWBinOp op
;
2755 switch (instr
->intrinsic
) {
2756 case nir_intrinsic_shared_atomic_add
:
2757 case nir_intrinsic_deref_atomic_add
:
2758 op
= LLVMAtomicRMWBinOpAdd
;
2760 case nir_intrinsic_shared_atomic_umin
:
2761 case nir_intrinsic_deref_atomic_umin
:
2762 op
= LLVMAtomicRMWBinOpUMin
;
2764 case nir_intrinsic_shared_atomic_umax
:
2765 case nir_intrinsic_deref_atomic_umax
:
2766 op
= LLVMAtomicRMWBinOpUMax
;
2768 case nir_intrinsic_shared_atomic_imin
:
2769 case nir_intrinsic_deref_atomic_imin
:
2770 op
= LLVMAtomicRMWBinOpMin
;
2772 case nir_intrinsic_shared_atomic_imax
:
2773 case nir_intrinsic_deref_atomic_imax
:
2774 op
= LLVMAtomicRMWBinOpMax
;
2776 case nir_intrinsic_shared_atomic_and
:
2777 case nir_intrinsic_deref_atomic_and
:
2778 op
= LLVMAtomicRMWBinOpAnd
;
2780 case nir_intrinsic_shared_atomic_or
:
2781 case nir_intrinsic_deref_atomic_or
:
2782 op
= LLVMAtomicRMWBinOpOr
;
2784 case nir_intrinsic_shared_atomic_xor
:
2785 case nir_intrinsic_deref_atomic_xor
:
2786 op
= LLVMAtomicRMWBinOpXor
;
2788 case nir_intrinsic_shared_atomic_exchange
:
2789 case nir_intrinsic_deref_atomic_exchange
:
2790 op
= LLVMAtomicRMWBinOpXchg
;
2796 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2797 LLVMAtomicOrderingSequentiallyConsistent
,
2803 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2805 LLVMValueRef values
[2];
2806 LLVMValueRef pos
[2];
2808 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2809 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2811 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2812 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2813 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2816 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2817 const nir_intrinsic_instr
*instr
)
2819 LLVMValueRef result
[4];
2820 LLVMValueRef interp_param
, attr_number
;
2823 LLVMValueRef src_c0
= NULL
;
2824 LLVMValueRef src_c1
= NULL
;
2825 LLVMValueRef src0
= NULL
;
2827 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2828 int input_index
= var
->data
.location
- VARYING_SLOT_VAR0
;
2829 switch (instr
->intrinsic
) {
2830 case nir_intrinsic_interp_deref_at_centroid
:
2831 location
= INTERP_CENTROID
;
2833 case nir_intrinsic_interp_deref_at_sample
:
2834 case nir_intrinsic_interp_deref_at_offset
:
2835 location
= INTERP_CENTER
;
2836 src0
= get_src(ctx
, instr
->src
[1]);
2842 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2843 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2844 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2845 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2846 LLVMValueRef sample_position
;
2847 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2849 /* fetch sample ID */
2850 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2852 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2853 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2854 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2855 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2857 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
2858 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2860 if (location
== INTERP_CENTER
) {
2861 LLVMValueRef ij_out
[2];
2862 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2865 * take the I then J parameters, and the DDX/Y for it, and
2866 * calculate the IJ inputs for the interpolator.
2867 * temp1 = ddx * offset/sample.x + I;
2868 * interp_param.I = ddy * offset/sample.y + temp1;
2869 * temp1 = ddx * offset/sample.x + J;
2870 * interp_param.J = ddy * offset/sample.y + temp1;
2872 for (unsigned i
= 0; i
< 2; i
++) {
2873 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2874 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2875 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2876 ddxy_out
, ix_ll
, "");
2877 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2878 ddxy_out
, iy_ll
, "");
2879 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2880 interp_param
, ix_ll
, "");
2881 LLVMValueRef temp1
, temp2
;
2883 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2886 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
2887 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
2889 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2890 temp2
, ctx
->ac
.i32
, "");
2892 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2896 for (chan
= 0; chan
< 4; chan
++) {
2897 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2900 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2901 interp_param
, ctx
->ac
.v2f32
, "");
2902 LLVMValueRef i
= LLVMBuildExtractElement(
2903 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2904 LLVMValueRef j
= LLVMBuildExtractElement(
2905 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2907 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2908 llvm_chan
, attr_number
,
2909 ctx
->abi
->prim_mask
, i
, j
);
2911 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2912 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2913 llvm_chan
, attr_number
,
2914 ctx
->abi
->prim_mask
);
2917 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2918 var
->data
.location_frac
);
2921 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2922 nir_intrinsic_instr
*instr
)
2924 LLVMValueRef result
= NULL
;
2926 switch (instr
->intrinsic
) {
2927 case nir_intrinsic_ballot
:
2928 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2930 case nir_intrinsic_read_invocation
:
2931 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2932 get_src(ctx
, instr
->src
[1]));
2934 case nir_intrinsic_read_first_invocation
:
2935 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2937 case nir_intrinsic_load_subgroup_invocation
:
2938 result
= ac_get_thread_id(&ctx
->ac
);
2940 case nir_intrinsic_load_work_group_id
: {
2941 LLVMValueRef values
[3];
2943 for (int i
= 0; i
< 3; i
++) {
2944 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2945 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2948 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2951 case nir_intrinsic_load_base_vertex
:
2952 case nir_intrinsic_load_first_vertex
:
2953 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2955 case nir_intrinsic_load_local_group_size
:
2956 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2958 case nir_intrinsic_load_vertex_id
:
2959 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2960 ctx
->abi
->base_vertex
, "");
2962 case nir_intrinsic_load_vertex_id_zero_base
: {
2963 result
= ctx
->abi
->vertex_id
;
2966 case nir_intrinsic_load_local_invocation_id
: {
2967 result
= ctx
->abi
->local_invocation_ids
;
2970 case nir_intrinsic_load_base_instance
:
2971 result
= ctx
->abi
->start_instance
;
2973 case nir_intrinsic_load_draw_id
:
2974 result
= ctx
->abi
->draw_id
;
2976 case nir_intrinsic_load_view_index
:
2977 result
= ctx
->abi
->view_index
;
2979 case nir_intrinsic_load_invocation_id
:
2980 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2981 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2983 result
= ctx
->abi
->gs_invocation_id
;
2985 case nir_intrinsic_load_primitive_id
:
2986 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2987 result
= ctx
->abi
->gs_prim_id
;
2988 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2989 result
= ctx
->abi
->tcs_patch_id
;
2990 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2991 result
= ctx
->abi
->tes_patch_id
;
2993 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2995 case nir_intrinsic_load_sample_id
:
2996 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2998 case nir_intrinsic_load_sample_pos
:
2999 result
= load_sample_pos(ctx
);
3001 case nir_intrinsic_load_sample_mask_in
:
3002 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3004 case nir_intrinsic_load_frag_coord
: {
3005 LLVMValueRef values
[4] = {
3006 ctx
->abi
->frag_pos
[0],
3007 ctx
->abi
->frag_pos
[1],
3008 ctx
->abi
->frag_pos
[2],
3009 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3011 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
3014 case nir_intrinsic_load_front_face
:
3015 result
= ctx
->abi
->front_face
;
3017 case nir_intrinsic_load_helper_invocation
:
3018 result
= visit_load_helper_invocation(ctx
);
3020 case nir_intrinsic_load_instance_id
:
3021 result
= ctx
->abi
->instance_id
;
3023 case nir_intrinsic_load_num_work_groups
:
3024 result
= ctx
->abi
->num_work_groups
;
3026 case nir_intrinsic_load_local_invocation_index
:
3027 result
= visit_load_local_invocation_index(ctx
);
3029 case nir_intrinsic_load_subgroup_id
:
3030 result
= visit_load_subgroup_id(ctx
);
3032 case nir_intrinsic_load_num_subgroups
:
3033 result
= visit_load_num_subgroups(ctx
);
3035 case nir_intrinsic_first_invocation
:
3036 result
= visit_first_invocation(ctx
);
3038 case nir_intrinsic_load_push_constant
:
3039 result
= visit_load_push_constant(ctx
, instr
);
3041 case nir_intrinsic_vulkan_resource_index
: {
3042 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3043 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3044 unsigned binding
= nir_intrinsic_binding(instr
);
3046 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3050 case nir_intrinsic_vulkan_resource_reindex
:
3051 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3053 case nir_intrinsic_store_ssbo
:
3054 visit_store_ssbo(ctx
, instr
);
3056 case nir_intrinsic_load_ssbo
:
3057 result
= visit_load_buffer(ctx
, instr
);
3059 case nir_intrinsic_ssbo_atomic_add
:
3060 case nir_intrinsic_ssbo_atomic_imin
:
3061 case nir_intrinsic_ssbo_atomic_umin
:
3062 case nir_intrinsic_ssbo_atomic_imax
:
3063 case nir_intrinsic_ssbo_atomic_umax
:
3064 case nir_intrinsic_ssbo_atomic_and
:
3065 case nir_intrinsic_ssbo_atomic_or
:
3066 case nir_intrinsic_ssbo_atomic_xor
:
3067 case nir_intrinsic_ssbo_atomic_exchange
:
3068 case nir_intrinsic_ssbo_atomic_comp_swap
:
3069 result
= visit_atomic_ssbo(ctx
, instr
);
3071 case nir_intrinsic_load_ubo
:
3072 result
= visit_load_ubo_buffer(ctx
, instr
);
3074 case nir_intrinsic_get_buffer_size
:
3075 result
= visit_get_buffer_size(ctx
, instr
);
3077 case nir_intrinsic_load_deref
:
3078 result
= visit_load_var(ctx
, instr
);
3080 case nir_intrinsic_store_deref
:
3081 visit_store_var(ctx
, instr
);
3083 case nir_intrinsic_load_shared
:
3084 result
= visit_load_shared(ctx
, instr
);
3086 case nir_intrinsic_store_shared
:
3087 visit_store_shared(ctx
, instr
);
3089 case nir_intrinsic_image_deref_samples
:
3090 result
= visit_image_samples(ctx
, instr
);
3092 case nir_intrinsic_image_deref_load
:
3093 result
= visit_image_load(ctx
, instr
);
3095 case nir_intrinsic_image_deref_store
:
3096 visit_image_store(ctx
, instr
);
3098 case nir_intrinsic_image_deref_atomic_add
:
3099 case nir_intrinsic_image_deref_atomic_min
:
3100 case nir_intrinsic_image_deref_atomic_max
:
3101 case nir_intrinsic_image_deref_atomic_and
:
3102 case nir_intrinsic_image_deref_atomic_or
:
3103 case nir_intrinsic_image_deref_atomic_xor
:
3104 case nir_intrinsic_image_deref_atomic_exchange
:
3105 case nir_intrinsic_image_deref_atomic_comp_swap
:
3106 result
= visit_image_atomic(ctx
, instr
);
3108 case nir_intrinsic_image_deref_size
:
3109 result
= visit_image_size(ctx
, instr
);
3111 case nir_intrinsic_shader_clock
:
3112 result
= ac_build_shader_clock(&ctx
->ac
);
3114 case nir_intrinsic_discard
:
3115 case nir_intrinsic_discard_if
:
3116 emit_discard(ctx
, instr
);
3118 case nir_intrinsic_memory_barrier
:
3119 case nir_intrinsic_group_memory_barrier
:
3120 case nir_intrinsic_memory_barrier_atomic_counter
:
3121 case nir_intrinsic_memory_barrier_buffer
:
3122 case nir_intrinsic_memory_barrier_image
:
3123 case nir_intrinsic_memory_barrier_shared
:
3124 emit_membar(&ctx
->ac
, instr
);
3126 case nir_intrinsic_barrier
:
3127 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3129 case nir_intrinsic_shared_atomic_add
:
3130 case nir_intrinsic_shared_atomic_imin
:
3131 case nir_intrinsic_shared_atomic_umin
:
3132 case nir_intrinsic_shared_atomic_imax
:
3133 case nir_intrinsic_shared_atomic_umax
:
3134 case nir_intrinsic_shared_atomic_and
:
3135 case nir_intrinsic_shared_atomic_or
:
3136 case nir_intrinsic_shared_atomic_xor
:
3137 case nir_intrinsic_shared_atomic_exchange
:
3138 case nir_intrinsic_shared_atomic_comp_swap
: {
3139 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3140 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3143 case nir_intrinsic_deref_atomic_add
:
3144 case nir_intrinsic_deref_atomic_imin
:
3145 case nir_intrinsic_deref_atomic_umin
:
3146 case nir_intrinsic_deref_atomic_imax
:
3147 case nir_intrinsic_deref_atomic_umax
:
3148 case nir_intrinsic_deref_atomic_and
:
3149 case nir_intrinsic_deref_atomic_or
:
3150 case nir_intrinsic_deref_atomic_xor
:
3151 case nir_intrinsic_deref_atomic_exchange
:
3152 case nir_intrinsic_deref_atomic_comp_swap
: {
3153 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3154 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3157 case nir_intrinsic_interp_deref_at_centroid
:
3158 case nir_intrinsic_interp_deref_at_sample
:
3159 case nir_intrinsic_interp_deref_at_offset
:
3160 result
= visit_interp(ctx
, instr
);
3162 case nir_intrinsic_emit_vertex
:
3163 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3165 case nir_intrinsic_end_primitive
:
3166 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3168 case nir_intrinsic_load_tess_coord
:
3169 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3171 case nir_intrinsic_load_tess_level_outer
:
3172 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3174 case nir_intrinsic_load_tess_level_inner
:
3175 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3177 case nir_intrinsic_load_patch_vertices_in
:
3178 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3180 case nir_intrinsic_vote_all
: {
3181 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3182 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3185 case nir_intrinsic_vote_any
: {
3186 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3187 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3190 case nir_intrinsic_shuffle
:
3191 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3192 get_src(ctx
, instr
->src
[1]));
3194 case nir_intrinsic_reduce
:
3195 result
= ac_build_reduce(&ctx
->ac
,
3196 get_src(ctx
, instr
->src
[0]),
3197 instr
->const_index
[0],
3198 instr
->const_index
[1]);
3200 case nir_intrinsic_inclusive_scan
:
3201 result
= ac_build_inclusive_scan(&ctx
->ac
,
3202 get_src(ctx
, instr
->src
[0]),
3203 instr
->const_index
[0]);
3205 case nir_intrinsic_exclusive_scan
:
3206 result
= ac_build_exclusive_scan(&ctx
->ac
,
3207 get_src(ctx
, instr
->src
[0]),
3208 instr
->const_index
[0]);
3210 case nir_intrinsic_quad_broadcast
: {
3211 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3212 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3213 lane
, lane
, lane
, lane
);
3216 case nir_intrinsic_quad_swap_horizontal
:
3217 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3219 case nir_intrinsic_quad_swap_vertical
:
3220 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3222 case nir_intrinsic_quad_swap_diagonal
:
3223 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3226 fprintf(stderr
, "Unknown intrinsic: ");
3227 nir_print_instr(&instr
->instr
, stderr
);
3228 fprintf(stderr
, "\n");
3232 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3236 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3237 nir_deref_instr
*deref_instr
,
3238 enum ac_descriptor_type desc_type
,
3239 const nir_tex_instr
*tex_instr
,
3240 bool image
, bool write
)
3242 LLVMValueRef index
= NULL
;
3243 unsigned constant_index
= 0;
3244 unsigned descriptor_set
;
3245 unsigned base_index
;
3246 bool bindless
= false;
3249 assert(tex_instr
&& !image
);
3251 base_index
= tex_instr
->sampler_index
;
3253 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3254 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3258 assert(deref_instr
->deref_type
== nir_deref_type_array
);
3259 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3261 constant_index
+= array_size
* const_value
->u32
[0];
3263 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3265 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3266 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3271 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3274 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3276 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3277 base_index
= deref_instr
->var
->data
.binding
;
3280 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3283 constant_index
, index
,
3284 desc_type
, image
, write
, bindless
);
3287 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3290 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3291 * filtering manually. The driver sets img7 to a mask clearing
3292 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3293 * s_and_b32 samp0, samp0, img7
3296 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3298 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3299 LLVMValueRef res
, LLVMValueRef samp
)
3301 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3302 LLVMValueRef img7
, samp0
;
3304 if (ctx
->ac
.chip_class
>= VI
)
3307 img7
= LLVMBuildExtractElement(builder
, res
,
3308 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3309 samp0
= LLVMBuildExtractElement(builder
, samp
,
3310 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3311 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3312 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3313 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3316 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3317 nir_tex_instr
*instr
,
3318 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3319 LLVMValueRef
*fmask_ptr
)
3321 nir_deref_instr
*texture_deref_instr
= NULL
;
3322 nir_deref_instr
*sampler_deref_instr
= NULL
;
3324 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3325 switch (instr
->src
[i
].src_type
) {
3326 case nir_tex_src_texture_deref
:
3327 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3329 case nir_tex_src_sampler_deref
:
3330 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3337 if (!sampler_deref_instr
)
3338 sampler_deref_instr
= texture_deref_instr
;
3340 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3341 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3343 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3345 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3346 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3347 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3349 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3350 instr
->op
== nir_texop_samples_identical
))
3351 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3354 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3357 coord
= ac_to_float(ctx
, coord
);
3358 coord
= ac_build_round(ctx
, coord
);
3359 coord
= ac_to_integer(ctx
, coord
);
3363 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3365 LLVMValueRef result
= NULL
;
3366 struct ac_image_args args
= { 0 };
3367 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3368 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3369 unsigned offset_src
= 0;
3371 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3373 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3374 switch (instr
->src
[i
].src_type
) {
3375 case nir_tex_src_coord
: {
3376 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3377 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3378 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3381 case nir_tex_src_projector
:
3383 case nir_tex_src_comparator
:
3384 if (instr
->is_shadow
)
3385 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3387 case nir_tex_src_offset
:
3388 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3391 case nir_tex_src_bias
:
3392 if (instr
->op
== nir_texop_txb
)
3393 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3395 case nir_tex_src_lod
: {
3396 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3398 if (val
&& val
->i32
[0] == 0)
3399 args
.level_zero
= true;
3401 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3404 case nir_tex_src_ms_index
:
3405 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3407 case nir_tex_src_ms_mcs
:
3409 case nir_tex_src_ddx
:
3410 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3412 case nir_tex_src_ddy
:
3413 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3415 case nir_tex_src_texture_offset
:
3416 case nir_tex_src_sampler_offset
:
3417 case nir_tex_src_plane
:
3423 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3424 result
= get_buffer_size(ctx
, args
.resource
, true);
3428 if (instr
->op
== nir_texop_texture_samples
) {
3429 LLVMValueRef res
, samples
, is_msaa
;
3430 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3431 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3432 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3433 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3434 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3435 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3436 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3437 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3438 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3440 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3441 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3442 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3443 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3444 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3446 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3452 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3453 LLVMValueRef offset
[3], pack
;
3454 for (unsigned chan
= 0; chan
< 3; ++chan
)
3455 offset
[chan
] = ctx
->ac
.i32_0
;
3457 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3458 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3459 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3460 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3461 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3463 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3464 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3466 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3467 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3471 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3472 * so the depth comparison value isn't clamped for Z16 and
3473 * Z24 anymore. Do it manually here.
3475 * It's unnecessary if the original texture format was
3476 * Z32_FLOAT, but we don't know that here.
3478 if (args
.compare
&& ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3479 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3481 /* pack derivatives */
3483 int num_src_deriv_channels
, num_dest_deriv_channels
;
3484 switch (instr
->sampler_dim
) {
3485 case GLSL_SAMPLER_DIM_3D
:
3486 case GLSL_SAMPLER_DIM_CUBE
:
3487 num_src_deriv_channels
= 3;
3488 num_dest_deriv_channels
= 3;
3490 case GLSL_SAMPLER_DIM_2D
:
3492 num_src_deriv_channels
= 2;
3493 num_dest_deriv_channels
= 2;
3495 case GLSL_SAMPLER_DIM_1D
:
3496 num_src_deriv_channels
= 1;
3497 if (ctx
->ac
.chip_class
>= GFX9
) {
3498 num_dest_deriv_channels
= 2;
3500 num_dest_deriv_channels
= 1;
3505 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3506 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3507 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3508 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3509 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3511 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3512 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3513 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3517 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3518 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3519 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3520 if (instr
->coord_components
== 3)
3521 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3522 ac_prepare_cube_coords(&ctx
->ac
,
3523 instr
->op
== nir_texop_txd
, instr
->is_array
,
3524 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3527 /* Texture coordinates fixups */
3528 if (instr
->coord_components
> 1 &&
3529 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3531 instr
->op
!= nir_texop_txf
) {
3532 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3535 if (instr
->coord_components
> 2 &&
3536 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3537 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3538 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3539 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3541 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3542 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3545 if (ctx
->ac
.chip_class
>= GFX9
&&
3546 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3547 instr
->op
!= nir_texop_lod
) {
3548 LLVMValueRef filler
;
3549 if (instr
->op
== nir_texop_txf
)
3550 filler
= ctx
->ac
.i32_0
;
3552 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3554 if (instr
->is_array
)
3555 args
.coords
[2] = args
.coords
[1];
3556 args
.coords
[1] = filler
;
3559 /* Pack sample index */
3560 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3561 args
.coords
[instr
->coord_components
] = sample_index
;
3563 if (instr
->op
== nir_texop_samples_identical
) {
3564 struct ac_image_args txf_args
= { 0 };
3565 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3567 txf_args
.dmask
= 0xf;
3568 txf_args
.resource
= fmask_ptr
;
3569 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3570 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3572 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3573 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3577 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3578 instr
->op
!= nir_texop_txs
) {
3579 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3580 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3581 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3582 instr
->is_array
? args
.coords
[2] : NULL
,
3583 args
.coords
[sample_chan
], fmask_ptr
);
3586 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3587 nir_const_value
*const_offset
=
3588 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3589 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3590 assert(const_offset
);
3591 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3592 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3593 args
.coords
[i
] = LLVMBuildAdd(
3594 ctx
->ac
.builder
, args
.coords
[i
],
3595 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3600 /* TODO TG4 support */
3602 if (instr
->op
== nir_texop_tg4
) {
3603 if (instr
->is_shadow
)
3606 args
.dmask
= 1 << instr
->component
;
3609 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3610 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3611 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3613 if (instr
->op
== nir_texop_query_levels
)
3614 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3615 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3616 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3617 instr
->op
!= nir_texop_tg4
)
3618 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3619 else if (instr
->op
== nir_texop_txs
&&
3620 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3622 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3623 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3624 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3625 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3626 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3627 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3628 instr
->op
== nir_texop_txs
&&
3629 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3631 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3632 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3633 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3635 } else if (instr
->dest
.ssa
.num_components
!= 4)
3636 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3640 assert(instr
->dest
.is_ssa
);
3641 result
= ac_to_integer(&ctx
->ac
, result
);
3642 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3647 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3649 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3650 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3652 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3653 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3656 static void visit_post_phi(struct ac_nir_context
*ctx
,
3657 nir_phi_instr
*instr
,
3658 LLVMValueRef llvm_phi
)
3660 nir_foreach_phi_src(src
, instr
) {
3661 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3662 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3664 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3668 static void phi_post_pass(struct ac_nir_context
*ctx
)
3670 hash_table_foreach(ctx
->phis
, entry
) {
3671 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3672 (LLVMValueRef
)entry
->data
);
3677 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3678 const nir_ssa_undef_instr
*instr
)
3680 unsigned num_components
= instr
->def
.num_components
;
3681 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3684 if (num_components
== 1)
3685 undef
= LLVMGetUndef(type
);
3687 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3689 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3692 static void visit_jump(struct ac_llvm_context
*ctx
,
3693 const nir_jump_instr
*instr
)
3695 switch (instr
->type
) {
3696 case nir_jump_break
:
3697 ac_build_break(ctx
);
3699 case nir_jump_continue
:
3700 ac_build_continue(ctx
);
3703 fprintf(stderr
, "Unknown NIR jump instr: ");
3704 nir_print_instr(&instr
->instr
, stderr
);
3705 fprintf(stderr
, "\n");
3710 static void visit_deref(struct ac_nir_context
*ctx
,
3711 nir_deref_instr
*instr
)
3713 if (instr
->mode
!= nir_var_shared
)
3716 LLVMValueRef result
= NULL
;
3717 switch(instr
->deref_type
) {
3718 case nir_deref_type_var
: {
3719 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3720 result
= entry
->data
;
3723 case nir_deref_type_struct
:
3724 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3725 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
3727 case nir_deref_type_array
:
3728 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3729 get_src(ctx
, instr
->arr
.index
));
3731 case nir_deref_type_cast
:
3732 result
= get_src(ctx
, instr
->parent
);
3735 unreachable("Unhandled deref_instr deref type");
3738 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3741 static void visit_cf_list(struct ac_nir_context
*ctx
,
3742 struct exec_list
*list
);
3744 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3746 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3747 nir_foreach_instr(instr
, block
)
3749 switch (instr
->type
) {
3750 case nir_instr_type_alu
:
3751 visit_alu(ctx
, nir_instr_as_alu(instr
));
3753 case nir_instr_type_load_const
:
3754 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3756 case nir_instr_type_intrinsic
:
3757 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3759 case nir_instr_type_tex
:
3760 visit_tex(ctx
, nir_instr_as_tex(instr
));
3762 case nir_instr_type_phi
:
3763 visit_phi(ctx
, nir_instr_as_phi(instr
));
3765 case nir_instr_type_ssa_undef
:
3766 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3768 case nir_instr_type_jump
:
3769 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3771 case nir_instr_type_deref
:
3772 visit_deref(ctx
, nir_instr_as_deref(instr
));
3775 fprintf(stderr
, "Unknown NIR instr type: ");
3776 nir_print_instr(instr
, stderr
);
3777 fprintf(stderr
, "\n");
3782 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3785 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3787 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3789 nir_block
*then_block
=
3790 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3792 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3794 visit_cf_list(ctx
, &if_stmt
->then_list
);
3796 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3797 nir_block
*else_block
=
3798 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3800 ac_build_else(&ctx
->ac
, else_block
->index
);
3801 visit_cf_list(ctx
, &if_stmt
->else_list
);
3804 ac_build_endif(&ctx
->ac
, then_block
->index
);
3807 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3809 nir_block
*first_loop_block
=
3810 (nir_block
*) exec_list_get_head(&loop
->body
);
3812 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3814 visit_cf_list(ctx
, &loop
->body
);
3816 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3819 static void visit_cf_list(struct ac_nir_context
*ctx
,
3820 struct exec_list
*list
)
3822 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3824 switch (node
->type
) {
3825 case nir_cf_node_block
:
3826 visit_block(ctx
, nir_cf_node_as_block(node
));
3829 case nir_cf_node_if
:
3830 visit_if(ctx
, nir_cf_node_as_if(node
));
3833 case nir_cf_node_loop
:
3834 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3844 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3845 struct ac_shader_abi
*abi
,
3846 struct nir_shader
*nir
,
3847 struct nir_variable
*variable
,
3848 gl_shader_stage stage
)
3850 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3851 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3853 /* tess ctrl has it's own load/store paths for outputs */
3854 if (stage
== MESA_SHADER_TESS_CTRL
)
3857 if (stage
== MESA_SHADER_VERTEX
||
3858 stage
== MESA_SHADER_TESS_EVAL
||
3859 stage
== MESA_SHADER_GEOMETRY
) {
3860 int idx
= variable
->data
.location
+ variable
->data
.index
;
3861 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3862 int length
= nir
->info
.clip_distance_array_size
+
3863 nir
->info
.cull_distance_array_size
;
3872 bool is_16bit
= glsl_type_is_16bit(variable
->type
);
3873 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
3874 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3875 for (unsigned chan
= 0; chan
< 4; chan
++) {
3876 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3877 ac_build_alloca_undef(ctx
, type
, "");
3883 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3884 enum glsl_base_type type
)
3888 case GLSL_TYPE_UINT
:
3889 case GLSL_TYPE_BOOL
:
3890 case GLSL_TYPE_SUBROUTINE
:
3892 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3894 case GLSL_TYPE_INT64
:
3895 case GLSL_TYPE_UINT64
:
3897 case GLSL_TYPE_DOUBLE
:
3900 unreachable("unknown GLSL type");
3905 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3906 const struct glsl_type
*type
)
3908 if (glsl_type_is_scalar(type
)) {
3909 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3912 if (glsl_type_is_vector(type
)) {
3913 return LLVMVectorType(
3914 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3915 glsl_get_vector_elements(type
));
3918 if (glsl_type_is_matrix(type
)) {
3919 return LLVMArrayType(
3920 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3921 glsl_get_matrix_columns(type
));
3924 if (glsl_type_is_array(type
)) {
3925 return LLVMArrayType(
3926 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3927 glsl_get_length(type
));
3930 assert(glsl_type_is_struct(type
));
3932 LLVMTypeRef member_types
[glsl_get_length(type
)];
3934 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3936 glsl_to_llvm_type(ac
,
3937 glsl_get_struct_field(type
, i
));
3940 return LLVMStructTypeInContext(ac
->context
, member_types
,
3941 glsl_get_length(type
), false);
3945 setup_locals(struct ac_nir_context
*ctx
,
3946 struct nir_function
*func
)
3949 ctx
->num_locals
= 0;
3950 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3951 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3952 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3953 variable
->data
.location_frac
= 0;
3954 ctx
->num_locals
+= attrib_count
;
3956 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3960 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3961 for (j
= 0; j
< 4; j
++) {
3962 ctx
->locals
[i
* 4 + j
] =
3963 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3969 setup_shared(struct ac_nir_context
*ctx
,
3970 struct nir_shader
*nir
)
3972 nir_foreach_variable(variable
, &nir
->shared
) {
3973 LLVMValueRef shared
=
3974 LLVMAddGlobalInAddressSpace(
3975 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3976 variable
->name
? variable
->name
: "",
3978 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3982 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3983 struct nir_shader
*nir
)
3985 struct ac_nir_context ctx
= {};
3986 struct nir_function
*func
;
3991 ctx
.stage
= nir
->info
.stage
;
3993 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3995 nir_foreach_variable(variable
, &nir
->outputs
)
3996 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3999 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4000 _mesa_key_pointer_equal
);
4001 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4002 _mesa_key_pointer_equal
);
4003 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4004 _mesa_key_pointer_equal
);
4006 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4008 nir_index_ssa_defs(func
->impl
);
4009 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4011 setup_locals(&ctx
, func
);
4013 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
4014 setup_shared(&ctx
, nir
);
4016 visit_cf_list(&ctx
, &func
->impl
->body
);
4017 phi_post_pass(&ctx
);
4019 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
4020 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4025 ralloc_free(ctx
.defs
);
4026 ralloc_free(ctx
.phis
);
4027 ralloc_free(ctx
.vars
);
4031 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4033 /* While it would be nice not to have this flag, we are constrained
4034 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4037 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4039 /* TODO: Indirect indexing of GS inputs is unimplemented.
4041 * TCS and TES load inputs directly from LDS or offchip memory, so
4042 * indirect indexing is trivial.
4044 nir_variable_mode indirect_mask
= 0;
4045 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4046 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4047 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4048 !llvm_has_working_vgpr_indexing
)) {
4049 indirect_mask
|= nir_var_shader_in
;
4051 if (!llvm_has_working_vgpr_indexing
&&
4052 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4053 indirect_mask
|= nir_var_shader_out
;
4055 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4056 * smart enough to handle indirects without causing excess spilling
4057 * causing the gpu to hang.
4059 * See the following thread for more details of the problem:
4060 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4062 indirect_mask
|= nir_var_local
;
4064 nir_lower_indirect_derefs(nir
, indirect_mask
);