2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "util/bitscan.h"
31 #include "ac_shader_abi.h"
32 #include "ac_shader_util.h"
34 struct ac_nir_context
{
35 struct ac_llvm_context ac
;
36 struct ac_shader_abi
*abi
;
38 gl_shader_stage stage
;
40 struct hash_table
*defs
;
41 struct hash_table
*phis
;
42 struct hash_table
*vars
;
44 LLVMValueRef main_function
;
45 LLVMBasicBlockRef continue_block
;
46 LLVMBasicBlockRef break_block
;
52 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
53 const nir_deref_var
*deref
,
54 enum ac_descriptor_type desc_type
,
55 const nir_tex_instr
*instr
,
56 bool image
, bool write
);
59 build_store_values_extended(struct ac_llvm_context
*ac
,
62 unsigned value_stride
,
65 LLVMBuilderRef builder
= ac
->builder
;
68 for (i
= 0; i
< value_count
; i
++) {
69 LLVMValueRef ptr
= values
[i
* value_stride
];
70 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
71 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
72 LLVMBuildStore(builder
, value
, ptr
);
76 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
77 const nir_ssa_def
*def
)
79 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
80 if (def
->num_components
> 1) {
81 type
= LLVMVectorType(type
, def
->num_components
);
86 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
89 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, src
.ssa
);
90 return (LLVMValueRef
)entry
->data
;
94 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
96 LLVMValueRef ptr
= get_src(ctx
, src
);
97 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
98 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
100 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
101 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
104 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
105 const struct nir_block
*b
)
107 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
108 return (LLVMBasicBlockRef
)entry
->data
;
111 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
113 unsigned num_components
)
115 LLVMValueRef value
= get_src(ctx
, src
.src
);
116 bool need_swizzle
= false;
119 unsigned src_components
= ac_get_llvm_num_components(value
);
120 for (unsigned i
= 0; i
< num_components
; ++i
) {
121 assert(src
.swizzle
[i
] < src_components
);
122 if (src
.swizzle
[i
] != i
)
126 if (need_swizzle
|| num_components
!= src_components
) {
127 LLVMValueRef masks
[] = {
128 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
129 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
130 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
131 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
133 if (src_components
> 1 && num_components
== 1) {
134 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
136 } else if (src_components
== 1 && num_components
> 1) {
137 LLVMValueRef values
[] = {value
, value
, value
, value
};
138 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
140 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
141 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
150 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
151 LLVMIntPredicate pred
, LLVMValueRef src0
,
154 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
155 return LLVMBuildSelect(ctx
->builder
, result
,
156 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
160 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
161 LLVMRealPredicate pred
, LLVMValueRef src0
,
165 src0
= ac_to_float(ctx
, src0
);
166 src1
= ac_to_float(ctx
, src1
);
167 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
168 return LLVMBuildSelect(ctx
->builder
, result
,
169 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
173 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
175 LLVMTypeRef result_type
,
179 LLVMValueRef params
[] = {
180 ac_to_float(ctx
, src0
),
183 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
184 ac_get_elem_bits(ctx
, result_type
));
185 assert(length
< sizeof(name
));
186 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
189 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
191 LLVMTypeRef result_type
,
192 LLVMValueRef src0
, LLVMValueRef src1
)
195 LLVMValueRef params
[] = {
196 ac_to_float(ctx
, src0
),
197 ac_to_float(ctx
, src1
),
200 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
201 ac_get_elem_bits(ctx
, result_type
));
202 assert(length
< sizeof(name
));
203 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
206 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
208 LLVMTypeRef result_type
,
209 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
212 LLVMValueRef params
[] = {
213 ac_to_float(ctx
, src0
),
214 ac_to_float(ctx
, src1
),
215 ac_to_float(ctx
, src2
),
218 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
219 ac_get_elem_bits(ctx
, result_type
));
220 assert(length
< sizeof(name
));
221 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
224 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
225 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
227 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
229 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
230 ac_to_integer(ctx
, src2
), "");
233 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
234 LLVMIntPredicate pred
,
235 LLVMValueRef src0
, LLVMValueRef src1
)
237 return LLVMBuildSelect(ctx
->builder
,
238 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
243 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
246 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
247 LLVMBuildNeg(ctx
->builder
, src0
, ""));
250 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
252 LLVMValueRef src0
, LLVMValueRef src1
)
254 LLVMTypeRef ret_type
;
255 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
257 LLVMValueRef params
[] = { src0
, src1
};
258 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
261 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
262 params
, 2, AC_FUNC_ATTR_READNONE
);
264 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
265 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
269 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
272 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
275 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
278 src0
= ac_to_float(ctx
, src0
);
279 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
280 return LLVMBuildSExt(ctx
->builder
,
281 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
285 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
289 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
294 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
297 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
300 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
301 return LLVMBuildSExt(ctx
->builder
,
302 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
306 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
310 LLVMValueRef cond
= NULL
;
312 src0
= ac_to_float(ctx
, src0
);
313 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
315 if (ctx
->chip_class
>= VI
) {
316 LLVMValueRef args
[2];
317 /* Check if the result is a denormal - and flush to 0 if so. */
319 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
320 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
323 /* need to convert back up to f32 */
324 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
326 if (ctx
->chip_class
>= VI
)
327 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
330 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
331 * so compare the result and flush to 0 if it's smaller.
333 LLVMValueRef temp
, cond2
;
334 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
335 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
336 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
338 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
339 temp
, ctx
->f32_0
, "");
340 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
341 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
346 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
347 LLVMValueRef src0
, LLVMValueRef src1
)
349 LLVMValueRef dst64
, result
;
350 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
351 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
353 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
354 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
355 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
359 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
360 LLVMValueRef src0
, LLVMValueRef src1
)
362 LLVMValueRef dst64
, result
;
363 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
364 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
366 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
367 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
368 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
372 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
374 const LLVMValueRef srcs
[3])
377 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
379 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
380 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
384 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
385 LLVMValueRef src0
, LLVMValueRef src1
,
386 LLVMValueRef src2
, LLVMValueRef src3
)
388 LLVMValueRef bfi_args
[3], result
;
390 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
391 LLVMBuildSub(ctx
->builder
,
392 LLVMBuildShl(ctx
->builder
,
397 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
400 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
403 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
404 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
406 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
407 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
408 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
410 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
414 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
417 LLVMValueRef comp
[2];
419 src0
= ac_to_float(ctx
, src0
);
420 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
421 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
423 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
426 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
429 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
430 LLVMValueRef temps
[2], result
, val
;
433 for (i
= 0; i
< 2; i
++) {
434 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
435 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
436 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
437 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
440 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
442 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
447 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
455 if (op
== nir_op_fddx_fine
)
456 mask
= AC_TID_MASK_LEFT
;
457 else if (op
== nir_op_fddy_fine
)
458 mask
= AC_TID_MASK_TOP
;
460 mask
= AC_TID_MASK_TOP_LEFT
;
462 /* for DDX we want to next X pixel, DDY next Y pixel. */
463 if (op
== nir_op_fddx_fine
||
464 op
== nir_op_fddx_coarse
||
470 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
475 * this takes an I,J coordinate pair,
476 * and works out the X and Y derivatives.
477 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
479 static LLVMValueRef
emit_ddxy_interp(
480 struct ac_nir_context
*ctx
,
481 LLVMValueRef interp_ij
)
483 LLVMValueRef result
[4], a
;
486 for (i
= 0; i
< 2; i
++) {
487 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
488 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
489 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
490 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
492 return ac_build_gather_values(&ctx
->ac
, result
, 4);
495 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
497 LLVMValueRef src
[4], result
= NULL
;
498 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
499 unsigned src_components
;
500 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
502 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
509 case nir_op_pack_half_2x16
:
512 case nir_op_unpack_half_2x16
:
515 case nir_op_cube_face_coord
:
516 case nir_op_cube_face_index
:
520 src_components
= num_components
;
523 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
524 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
532 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
533 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
536 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
539 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
542 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
545 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
546 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
547 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
550 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
551 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
552 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
555 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
558 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
561 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
564 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
567 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
568 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
569 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
570 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
571 ac_to_float_type(&ctx
->ac
, def_type
), result
);
572 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
573 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
576 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
577 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
578 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
581 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
584 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
587 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
590 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
591 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
592 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
595 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
596 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
600 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
603 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
606 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
609 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
610 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
611 LLVMTypeOf(src
[0]), ""),
615 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
616 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
617 LLVMTypeOf(src
[0]), ""),
621 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
622 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
623 LLVMTypeOf(src
[0]), ""),
627 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
630 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
633 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
636 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
639 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
642 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
645 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
648 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
651 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
654 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
657 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
658 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
661 result
= emit_iabs(&ctx
->ac
, src
[0]);
664 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
667 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
670 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
673 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
676 result
= ac_build_isign(&ctx
->ac
, src
[0],
677 instr
->dest
.dest
.ssa
.bit_size
);
680 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
681 result
= ac_build_fsign(&ctx
->ac
, src
[0],
682 instr
->dest
.dest
.ssa
.bit_size
);
685 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
686 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
689 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
690 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
693 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
694 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
696 case nir_op_fround_even
:
697 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
698 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
701 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
702 result
= ac_build_fract(&ctx
->ac
, src
[0],
703 instr
->dest
.dest
.ssa
.bit_size
);
706 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
707 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
710 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
711 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
714 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
715 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
718 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
719 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
722 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
723 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
726 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
727 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
728 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
732 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
733 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
734 if (ctx
->ac
.chip_class
< GFX9
&&
735 instr
->dest
.dest
.ssa
.bit_size
== 32) {
736 /* Only pre-GFX9 chips do not flush denorms. */
737 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
738 ac_to_float_type(&ctx
->ac
, def_type
),
743 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
744 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
745 if (ctx
->ac
.chip_class
< GFX9
&&
746 instr
->dest
.dest
.ssa
.bit_size
== 32) {
747 /* Only pre-GFX9 chips do not flush denorms. */
748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
749 ac_to_float_type(&ctx
->ac
, def_type
),
754 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
755 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
758 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
759 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
760 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
762 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
764 case nir_op_ibitfield_extract
:
765 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
767 case nir_op_ubitfield_extract
:
768 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
770 case nir_op_bitfield_insert
:
771 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
773 case nir_op_bitfield_reverse
:
774 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
776 case nir_op_bit_count
:
777 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
778 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
780 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
781 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
787 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
788 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
789 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
793 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
794 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
798 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
799 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
803 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
804 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
808 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
809 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
812 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
813 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
816 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
817 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
821 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
822 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
823 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
825 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
829 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
830 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
831 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
833 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
836 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
838 case nir_op_find_lsb
:
839 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
840 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
842 case nir_op_ufind_msb
:
843 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
844 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
846 case nir_op_ifind_msb
:
847 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
848 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
850 case nir_op_uadd_carry
:
851 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
852 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
853 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
855 case nir_op_usub_borrow
:
856 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
857 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
858 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
861 result
= emit_b2f(&ctx
->ac
, src
[0]);
864 result
= emit_f2b(&ctx
->ac
, src
[0]);
867 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
870 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
871 result
= emit_i2b(&ctx
->ac
, src
[0]);
873 case nir_op_fquantize2f16
:
874 result
= emit_f2f16(&ctx
->ac
, src
[0]);
876 case nir_op_umul_high
:
877 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
878 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
879 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
881 case nir_op_imul_high
:
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
884 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
886 case nir_op_pack_half_2x16
:
887 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
889 case nir_op_unpack_half_2x16
:
890 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
894 case nir_op_fddx_fine
:
895 case nir_op_fddy_fine
:
896 case nir_op_fddx_coarse
:
897 case nir_op_fddy_coarse
:
898 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
901 case nir_op_unpack_64_2x32_split_x
: {
902 assert(ac_get_llvm_num_components(src
[0]) == 1);
903 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
906 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
911 case nir_op_unpack_64_2x32_split_y
: {
912 assert(ac_get_llvm_num_components(src
[0]) == 1);
913 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
916 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
921 case nir_op_pack_64_2x32_split
: {
922 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
923 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
924 src
[0], ctx
->ac
.i32_0
, "");
925 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
926 src
[1], ctx
->ac
.i32_1
, "");
927 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
931 case nir_op_cube_face_coord
: {
932 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
933 LLVMValueRef results
[2];
935 for (unsigned chan
= 0; chan
< 3; chan
++)
936 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
937 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
938 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
939 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
940 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
941 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
945 case nir_op_cube_face_index
: {
946 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
948 for (unsigned chan
= 0; chan
< 3; chan
++)
949 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
950 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
951 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
956 fprintf(stderr
, "Unknown NIR alu instr: ");
957 nir_print_instr(&instr
->instr
, stderr
);
958 fprintf(stderr
, "\n");
963 assert(instr
->dest
.dest
.is_ssa
);
964 result
= ac_to_integer(&ctx
->ac
, result
);
965 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.dest
.ssa
,
970 static void visit_load_const(struct ac_nir_context
*ctx
,
971 const nir_load_const_instr
*instr
)
973 LLVMValueRef values
[4], value
= NULL
;
974 LLVMTypeRef element_type
=
975 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
977 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
978 switch (instr
->def
.bit_size
) {
980 values
[i
] = LLVMConstInt(element_type
,
981 instr
->value
.u32
[i
], false);
984 values
[i
] = LLVMConstInt(element_type
,
985 instr
->value
.u64
[i
], false);
989 "unsupported nir load_const bit_size: %d\n",
990 instr
->def
.bit_size
);
994 if (instr
->def
.num_components
> 1) {
995 value
= LLVMConstVector(values
, instr
->def
.num_components
);
999 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, value
);
1003 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1006 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1007 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1010 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1011 /* On VI, the descriptor contains the size in bytes,
1012 * but TXQ must return the size in elements.
1013 * The stride is always non-zero for resources using TXQ.
1015 LLVMValueRef stride
=
1016 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1018 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1019 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1020 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1021 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1023 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1029 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1032 static void build_int_type_name(
1034 char *buf
, unsigned bufsize
)
1036 assert(bufsize
>= 6);
1038 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1039 snprintf(buf
, bufsize
, "v%ui32",
1040 LLVMGetVectorSize(type
));
1045 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1046 struct ac_image_args
*args
,
1047 const nir_tex_instr
*instr
)
1049 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1050 LLVMValueRef coord
= args
->addr
;
1051 LLVMValueRef half_texel
[2];
1052 LLVMValueRef compare_cube_wa
= NULL
;
1053 LLVMValueRef result
;
1055 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1059 struct ac_image_args txq_args
= { 0 };
1061 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1062 txq_args
.opcode
= ac_image_get_resinfo
;
1063 txq_args
.dmask
= 0xf;
1064 txq_args
.addr
= ctx
->i32_0
;
1065 txq_args
.resource
= args
->resource
;
1066 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1068 for (c
= 0; c
< 2; c
++) {
1069 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1070 LLVMConstInt(ctx
->i32
, c
, false), "");
1071 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1072 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1073 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1074 LLVMConstReal(ctx
->f32
, -0.5), "");
1078 LLVMValueRef orig_coords
= args
->addr
;
1080 for (c
= 0; c
< 2; c
++) {
1082 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1083 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1084 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1085 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1086 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1087 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1092 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1093 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1094 * workaround by sampling using a scaled type and converting.
1095 * This is taken from amdgpu-pro shaders.
1097 /* NOTE this produces some ugly code compared to amdgpu-pro,
1098 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1099 * and then reads them back. -pro generates two selects,
1100 * one s_cmp for the descriptor rewriting
1101 * one v_cmp for the coordinate and result changes.
1103 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1104 LLVMValueRef tmp
, tmp2
;
1106 /* workaround 8/8/8/8 uint/sint cube gather bug */
1107 /* first detect it then change to a scaled read and f2i */
1108 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1111 /* extract the DATA_FORMAT */
1112 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1113 LLVMConstInt(ctx
->i32
, 6, false), false);
1115 /* is the DATA_FORMAT == 8_8_8_8 */
1116 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1118 if (stype
== GLSL_TYPE_UINT
)
1119 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1120 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1121 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1123 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1124 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1125 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1127 /* replace the NUM FORMAT in the descriptor */
1128 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1129 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1131 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1133 /* don't modify the coordinates for this case */
1134 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1137 result
= ac_build_image_opcode(ctx
, args
);
1139 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1140 LLVMValueRef tmp
, tmp2
;
1142 /* if the cube workaround is in place, f2i the result. */
1143 for (c
= 0; c
< 4; c
++) {
1144 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1145 if (stype
== GLSL_TYPE_UINT
)
1146 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1148 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1149 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1150 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1151 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1152 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1153 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1159 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1160 const nir_tex_instr
*instr
,
1162 struct ac_image_args
*args
)
1164 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1165 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1167 return ac_build_buffer_load_format(&ctx
->ac
,
1171 util_last_bit(mask
),
1175 args
->opcode
= ac_image_sample
;
1176 args
->compare
= instr
->is_shadow
;
1178 switch (instr
->op
) {
1180 case nir_texop_txf_ms
:
1181 case nir_texop_samples_identical
:
1182 args
->opcode
= lod_is_zero
||
1183 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1184 ac_image_load
: ac_image_load_mip
;
1185 args
->compare
= false;
1186 args
->offset
= false;
1193 args
->level_zero
= true;
1198 case nir_texop_query_levels
:
1199 args
->opcode
= ac_image_get_resinfo
;
1202 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1203 args
->level_zero
= true;
1209 args
->opcode
= ac_image_gather4
;
1210 args
->level_zero
= true;
1213 args
->opcode
= ac_image_get_lod
;
1214 args
->compare
= false;
1215 args
->offset
= false;
1221 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1222 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1223 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1224 return lower_gather4_integer(&ctx
->ac
, args
, instr
);
1227 return ac_build_image_opcode(&ctx
->ac
, args
);
1230 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1231 nir_intrinsic_instr
*instr
)
1233 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1234 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1236 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1237 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1241 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1242 nir_intrinsic_instr
*instr
)
1244 LLVMValueRef ptr
, addr
;
1246 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1247 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1248 get_src(ctx
, instr
->src
[0]), "");
1250 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1251 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1253 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1256 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1257 const nir_intrinsic_instr
*instr
)
1259 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1261 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1264 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1266 uint32_t new_mask
= 0;
1267 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1268 if (mask
& (1u << i
))
1269 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1273 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1274 unsigned start
, unsigned count
)
1276 LLVMTypeRef type
= LLVMTypeOf(src
);
1278 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1284 unsigned src_elements
= LLVMGetVectorSize(type
);
1285 assert(start
< src_elements
);
1286 assert(start
+ count
<= src_elements
);
1288 if (start
== 0 && count
== src_elements
)
1292 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1295 LLVMValueRef indices
[8];
1296 for (unsigned i
= 0; i
< count
; ++i
)
1297 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1299 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1300 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1303 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1304 nir_intrinsic_instr
*instr
)
1306 const char *store_name
;
1307 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1308 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1309 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1310 int components_32bit
= elem_size_mult
* instr
->num_components
;
1311 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1312 LLVMValueRef base_data
, base_offset
;
1313 LLVMValueRef params
[6];
1315 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1316 get_src(ctx
, instr
->src
[1]), true);
1317 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1318 params
[4] = ctx
->ac
.i1false
; /* glc */
1319 params
[5] = ctx
->ac
.i1false
; /* slc */
1321 if (components_32bit
> 1)
1322 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1324 writemask
= widen_mask(writemask
, elem_size_mult
);
1326 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1327 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1328 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1330 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1334 LLVMValueRef offset
;
1336 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1338 /* Due to an LLVM limitation, split 3-element writes
1339 * into a 2-element and a 1-element write. */
1341 writemask
|= 1 << (start
+ 2);
1346 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1351 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1352 } else if (count
== 2) {
1353 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1357 store_name
= "llvm.amdgcn.buffer.store.f32";
1359 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1361 offset
= base_offset
;
1363 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1367 ac_build_intrinsic(&ctx
->ac
, store_name
,
1368 ctx
->ac
.voidt
, params
, 6, 0);
1372 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1373 const nir_intrinsic_instr
*instr
)
1376 LLVMValueRef params
[6];
1379 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1380 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1382 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1383 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1384 get_src(ctx
, instr
->src
[0]),
1386 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1387 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1388 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1390 switch (instr
->intrinsic
) {
1391 case nir_intrinsic_ssbo_atomic_add
:
1392 name
= "llvm.amdgcn.buffer.atomic.add";
1394 case nir_intrinsic_ssbo_atomic_imin
:
1395 name
= "llvm.amdgcn.buffer.atomic.smin";
1397 case nir_intrinsic_ssbo_atomic_umin
:
1398 name
= "llvm.amdgcn.buffer.atomic.umin";
1400 case nir_intrinsic_ssbo_atomic_imax
:
1401 name
= "llvm.amdgcn.buffer.atomic.smax";
1403 case nir_intrinsic_ssbo_atomic_umax
:
1404 name
= "llvm.amdgcn.buffer.atomic.umax";
1406 case nir_intrinsic_ssbo_atomic_and
:
1407 name
= "llvm.amdgcn.buffer.atomic.and";
1409 case nir_intrinsic_ssbo_atomic_or
:
1410 name
= "llvm.amdgcn.buffer.atomic.or";
1412 case nir_intrinsic_ssbo_atomic_xor
:
1413 name
= "llvm.amdgcn.buffer.atomic.xor";
1415 case nir_intrinsic_ssbo_atomic_exchange
:
1416 name
= "llvm.amdgcn.buffer.atomic.swap";
1418 case nir_intrinsic_ssbo_atomic_comp_swap
:
1419 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1425 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1428 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1429 const nir_intrinsic_instr
*instr
)
1431 LLVMValueRef results
[2];
1432 int load_components
;
1433 int num_components
= instr
->num_components
;
1434 if (instr
->dest
.ssa
.bit_size
== 64)
1435 num_components
*= 2;
1437 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1438 load_components
= MIN2(num_components
- i
, 4);
1439 const char *load_name
;
1440 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1441 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1442 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1444 if (load_components
== 3)
1445 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1446 else if (load_components
> 1)
1447 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1449 if (load_components
>= 3)
1450 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1451 else if (load_components
== 2)
1452 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1453 else if (load_components
== 1)
1454 load_name
= "llvm.amdgcn.buffer.load.f32";
1456 unreachable("unhandled number of components");
1458 LLVMValueRef params
[] = {
1459 ctx
->abi
->load_ssbo(ctx
->abi
,
1460 get_src(ctx
, instr
->src
[0]),
1468 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1472 LLVMValueRef ret
= results
[0];
1473 if (num_components
> 4 || num_components
== 3) {
1474 LLVMValueRef masks
[] = {
1475 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1476 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1477 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1478 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1481 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1482 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1483 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1486 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1487 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1490 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1491 const nir_intrinsic_instr
*instr
)
1494 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1495 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1496 int num_components
= instr
->num_components
;
1498 if (ctx
->abi
->load_ubo
)
1499 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1501 if (instr
->dest
.ssa
.bit_size
== 64)
1502 num_components
*= 2;
1504 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1505 NULL
, 0, false, false, true, true);
1506 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1507 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1508 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1512 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1513 bool vs_in
, unsigned *vertex_index_out
,
1514 LLVMValueRef
*vertex_index_ref
,
1515 unsigned *const_out
, LLVMValueRef
*indir_out
)
1517 unsigned const_offset
= 0;
1518 nir_deref
*tail
= &deref
->deref
;
1519 LLVMValueRef offset
= NULL
;
1521 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1523 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1524 if (vertex_index_out
)
1525 *vertex_index_out
= deref_array
->base_offset
;
1527 if (vertex_index_ref
) {
1528 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1529 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1530 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1532 *vertex_index_ref
= vtx
;
1536 if (deref
->var
->data
.compact
) {
1537 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1538 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1539 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1540 /* We always lower indirect dereferences for "compact" array vars. */
1541 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1543 const_offset
= deref_array
->base_offset
;
1547 while (tail
->child
!= NULL
) {
1548 const struct glsl_type
*parent_type
= tail
->type
;
1551 if (tail
->deref_type
== nir_deref_type_array
) {
1552 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1553 LLVMValueRef index
, stride
, local_offset
;
1554 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1556 const_offset
+= size
* deref_array
->base_offset
;
1557 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1560 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1561 index
= get_src(ctx
, deref_array
->indirect
);
1562 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1563 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1566 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1568 offset
= local_offset
;
1569 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1570 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1572 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1573 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1574 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1577 unreachable("unsupported deref type");
1581 if (const_offset
&& offset
)
1582 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1583 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1586 *const_out
= const_offset
;
1587 *indir_out
= offset
;
1591 build_gep_for_deref(struct ac_nir_context
*ctx
,
1592 nir_deref_var
*deref
)
1594 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1595 assert(entry
->data
);
1596 LLVMValueRef val
= entry
->data
;
1597 nir_deref
*tail
= deref
->deref
.child
;
1598 while (tail
!= NULL
) {
1599 LLVMValueRef offset
;
1600 switch (tail
->deref_type
) {
1601 case nir_deref_type_array
: {
1602 nir_deref_array
*array
= nir_deref_as_array(tail
);
1603 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1604 if (array
->deref_array_type
==
1605 nir_deref_array_type_indirect
) {
1606 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1613 case nir_deref_type_struct
: {
1614 nir_deref_struct
*deref_struct
=
1615 nir_deref_as_struct(tail
);
1616 offset
= LLVMConstInt(ctx
->ac
.i32
,
1617 deref_struct
->index
, 0);
1621 unreachable("bad deref type");
1623 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1629 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1630 nir_intrinsic_instr
*instr
,
1633 LLVMValueRef result
;
1634 LLVMValueRef vertex_index
= NULL
;
1635 LLVMValueRef indir_index
= NULL
;
1636 unsigned const_index
= 0;
1637 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1638 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1639 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1640 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1642 get_deref_offset(ctx
, instr
->variables
[0],
1643 false, NULL
, is_patch
? NULL
: &vertex_index
,
1644 &const_index
, &indir_index
);
1646 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1648 LLVMTypeRef src_component_type
;
1649 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1650 src_component_type
= LLVMGetElementType(dest_type
);
1652 src_component_type
= dest_type
;
1654 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1655 vertex_index
, indir_index
,
1656 const_index
, location
, driver_location
,
1657 instr
->variables
[0]->var
->data
.location_frac
,
1658 instr
->num_components
,
1659 is_patch
, is_compact
, load_inputs
);
1660 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1663 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1664 nir_intrinsic_instr
*instr
)
1666 LLVMValueRef values
[8];
1667 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1668 int ve
= instr
->dest
.ssa
.num_components
;
1669 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1670 LLVMValueRef indir_index
;
1672 unsigned const_index
;
1673 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1674 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1675 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1676 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1677 &const_index
, &indir_index
);
1679 if (instr
->dest
.ssa
.bit_size
== 64)
1682 switch (instr
->variables
[0]->var
->data
.mode
) {
1683 case nir_var_shader_in
:
1684 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1685 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1686 return load_tess_varyings(ctx
, instr
, true);
1689 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1690 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1691 LLVMValueRef indir_index
;
1692 unsigned const_index
, vertex_index
;
1693 get_deref_offset(ctx
, instr
->variables
[0],
1694 false, &vertex_index
, NULL
,
1695 &const_index
, &indir_index
);
1697 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1698 instr
->variables
[0]->var
->data
.driver_location
,
1699 instr
->variables
[0]->var
->data
.location_frac
,
1700 instr
->num_components
, vertex_index
, const_index
, type
);
1703 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1705 unsigned count
= glsl_count_attribute_slots(
1706 instr
->variables
[0]->var
->type
,
1707 ctx
->stage
== MESA_SHADER_VERTEX
);
1709 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1710 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1711 stride
, false, true);
1713 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1717 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1721 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1723 unsigned count
= glsl_count_attribute_slots(
1724 instr
->variables
[0]->var
->type
, false);
1726 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1727 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1728 stride
, true, true);
1730 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1734 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1738 case nir_var_shared
: {
1739 LLVMValueRef address
= build_gep_for_deref(ctx
,
1740 instr
->variables
[0]);
1741 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1742 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1743 get_def_type(ctx
, &instr
->dest
.ssa
),
1746 case nir_var_shader_out
:
1747 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1748 return load_tess_varyings(ctx
, instr
, false);
1751 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1753 unsigned count
= glsl_count_attribute_slots(
1754 instr
->variables
[0]->var
->type
, false);
1756 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1757 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1758 stride
, true, true);
1760 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1764 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1765 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1771 unreachable("unhandle variable mode");
1773 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1774 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1778 visit_store_var(struct ac_nir_context
*ctx
,
1779 nir_intrinsic_instr
*instr
)
1781 LLVMValueRef temp_ptr
, value
;
1782 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1783 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1784 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1785 int writemask
= instr
->const_index
[0] << comp
;
1786 LLVMValueRef indir_index
;
1787 unsigned const_index
;
1788 get_deref_offset(ctx
, instr
->variables
[0], false,
1789 NULL
, NULL
, &const_index
, &indir_index
);
1791 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1793 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1794 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1797 writemask
= widen_mask(writemask
, 2);
1800 switch (instr
->variables
[0]->var
->data
.mode
) {
1801 case nir_var_shader_out
:
1803 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1804 LLVMValueRef vertex_index
= NULL
;
1805 LLVMValueRef indir_index
= NULL
;
1806 unsigned const_index
= 0;
1807 const unsigned location
= instr
->variables
[0]->var
->data
.location
;
1808 const unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1809 const unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1810 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1811 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1813 get_deref_offset(ctx
, instr
->variables
[0],
1814 false, NULL
, is_patch
? NULL
: &vertex_index
,
1815 &const_index
, &indir_index
);
1817 ctx
->abi
->store_tcs_outputs(ctx
->abi
, vertex_index
, indir_index
,
1818 const_index
, location
, driver_location
,
1819 src
, comp
, is_patch
, is_compact
, writemask
);
1823 for (unsigned chan
= 0; chan
< 8; chan
++) {
1825 if (!(writemask
& (1 << chan
)))
1828 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1830 if (instr
->variables
[0]->var
->data
.compact
)
1833 unsigned count
= glsl_count_attribute_slots(
1834 instr
->variables
[0]->var
->type
, false);
1836 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1837 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1838 stride
, true, true);
1840 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1841 value
, indir_index
, "");
1842 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1843 count
, stride
, tmp_vec
);
1846 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1848 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1853 for (unsigned chan
= 0; chan
< 8; chan
++) {
1854 if (!(writemask
& (1 << chan
)))
1857 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1859 unsigned count
= glsl_count_attribute_slots(
1860 instr
->variables
[0]->var
->type
, false);
1862 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1863 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1866 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1867 value
, indir_index
, "");
1868 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1871 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1873 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1877 case nir_var_shared
: {
1878 int writemask
= instr
->const_index
[0];
1879 LLVMValueRef address
= build_gep_for_deref(ctx
,
1880 instr
->variables
[0]);
1881 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1882 unsigned components
=
1883 glsl_get_vector_elements(
1884 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1885 if (writemask
== (1 << components
) - 1) {
1886 val
= LLVMBuildBitCast(
1887 ctx
->ac
.builder
, val
,
1888 LLVMGetElementType(LLVMTypeOf(address
)), "");
1889 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1891 for (unsigned chan
= 0; chan
< 4; chan
++) {
1892 if (!(writemask
& (1 << chan
)))
1895 LLVMBuildStructGEP(ctx
->ac
.builder
,
1897 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1899 src
= LLVMBuildBitCast(
1900 ctx
->ac
.builder
, src
,
1901 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
1902 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
1912 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
1915 case GLSL_SAMPLER_DIM_BUF
:
1917 case GLSL_SAMPLER_DIM_1D
:
1918 return array
? 2 : 1;
1919 case GLSL_SAMPLER_DIM_2D
:
1920 return array
? 3 : 2;
1921 case GLSL_SAMPLER_DIM_MS
:
1922 return array
? 4 : 3;
1923 case GLSL_SAMPLER_DIM_3D
:
1924 case GLSL_SAMPLER_DIM_CUBE
:
1926 case GLSL_SAMPLER_DIM_RECT
:
1927 case GLSL_SAMPLER_DIM_SUBPASS
:
1929 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
1938 glsl_is_array_image(const struct glsl_type
*type
)
1940 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
1942 if (glsl_sampler_type_is_array(type
))
1945 return dim
== GLSL_SAMPLER_DIM_CUBE
||
1946 dim
== GLSL_SAMPLER_DIM_3D
||
1947 dim
== GLSL_SAMPLER_DIM_SUBPASS
||
1948 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
;
1952 /* Adjust the sample index according to FMASK.
1954 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1955 * which is the identity mapping. Each nibble says which physical sample
1956 * should be fetched to get that sample.
1958 * For example, 0x11111100 means there are only 2 samples stored and
1959 * the second sample covers 3/4 of the pixel. When reading samples 0
1960 * and 1, return physical sample 0 (determined by the first two 0s
1961 * in FMASK), otherwise return physical sample 1.
1963 * The sample index should be adjusted as follows:
1964 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1966 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
1967 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
1968 LLVMValueRef coord_z
,
1969 LLVMValueRef sample_index
,
1970 LLVMValueRef fmask_desc_ptr
)
1972 LLVMValueRef fmask_load_address
[4];
1975 fmask_load_address
[0] = coord_x
;
1976 fmask_load_address
[1] = coord_y
;
1978 fmask_load_address
[2] = coord_z
;
1979 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
1982 struct ac_image_args args
= {0};
1984 args
.opcode
= ac_image_load
;
1985 args
.da
= coord_z
? true : false;
1986 args
.resource
= fmask_desc_ptr
;
1988 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
1990 res
= ac_build_image_opcode(ctx
, &args
);
1992 res
= ac_to_integer(ctx
, res
);
1993 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
1994 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
1996 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2000 LLVMValueRef sample_index4
=
2001 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2002 LLVMValueRef shifted_fmask
=
2003 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2004 LLVMValueRef final_sample
=
2005 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2007 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2008 * resource descriptor is 0 (invalid),
2010 LLVMValueRef fmask_desc
=
2011 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2014 LLVMValueRef fmask_word1
=
2015 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2018 LLVMValueRef word1_is_nonzero
=
2019 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2020 fmask_word1
, ctx
->i32_0
, "");
2022 /* Replace the MSAA sample index. */
2024 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2025 final_sample
, sample_index
, "");
2026 return sample_index
;
2029 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
2030 const nir_intrinsic_instr
*instr
)
2032 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2034 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2035 LLVMValueRef coords
[4];
2036 LLVMValueRef masks
[] = {
2037 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2038 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2041 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2044 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2045 bool is_array
= glsl_sampler_type_is_array(type
);
2046 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2047 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2048 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2049 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2050 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2051 count
= image_type_to_components_count(dim
, is_array
);
2054 LLVMValueRef fmask_load_address
[3];
2057 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2058 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2060 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2062 fmask_load_address
[2] = NULL
;
2064 for (chan
= 0; chan
< 2; ++chan
)
2065 fmask_load_address
[chan
] =
2066 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2067 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2068 ctx
->ac
.i32
, ""), "");
2069 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2071 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2072 fmask_load_address
[0],
2073 fmask_load_address
[1],
2074 fmask_load_address
[2],
2076 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
2078 if (count
== 1 && !gfx9_1d
) {
2079 if (instr
->src
[0].ssa
->num_components
)
2080 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2087 for (chan
= 0; chan
< count
; ++chan
) {
2088 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2091 for (chan
= 0; chan
< 2; ++chan
)
2092 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2093 ctx
->ac
.i32
, ""), "");
2094 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2100 coords
[2] = coords
[1];
2101 coords
[1] = ctx
->ac
.i32_0
;
2103 coords
[1] = ctx
->ac
.i32_0
;
2108 coords
[count
] = sample_index
;
2113 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
2116 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2121 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2122 const nir_intrinsic_instr
*instr
)
2124 LLVMValueRef params
[7];
2126 char intrinsic_name
[64];
2127 const nir_variable
*var
= instr
->variables
[0]->var
;
2128 const struct glsl_type
*type
= var
->type
;
2130 if(instr
->variables
[0]->deref
.child
)
2131 type
= instr
->variables
[0]->deref
.child
->type
;
2133 type
= glsl_without_array(type
);
2135 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2136 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2137 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2138 unsigned num_channels
= util_last_bit(mask
);
2139 LLVMValueRef rsrc
, vindex
;
2141 rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
2142 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2145 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2146 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2147 ctx
->ac
.i32_0
, num_channels
,
2149 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2151 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2152 res
= ac_to_integer(&ctx
->ac
, res
);
2154 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2155 LLVMValueRef slc
= ctx
->ac
.i1false
;
2157 params
[0] = get_image_coords(ctx
, instr
);
2158 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2159 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2160 params
[3] = (var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2161 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2163 params
[5] = ctx
->ac
.i1false
;
2166 ac_get_image_intr_name("llvm.amdgcn.image.load",
2167 ctx
->ac
.v4f32
, /* vdata */
2168 LLVMTypeOf(params
[0]), /* coords */
2169 LLVMTypeOf(params
[1]), /* rsrc */
2170 intrinsic_name
, sizeof(intrinsic_name
));
2172 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
2173 params
, 7, AC_FUNC_ATTR_READONLY
);
2175 return ac_to_integer(&ctx
->ac
, res
);
2178 static void visit_image_store(struct ac_nir_context
*ctx
,
2179 nir_intrinsic_instr
*instr
)
2181 LLVMValueRef params
[8];
2182 char intrinsic_name
[64];
2183 const nir_variable
*var
= instr
->variables
[0]->var
;
2184 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2185 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2186 LLVMValueRef glc
= ctx
->ac
.i1false
;
2187 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2189 glc
= ctx
->ac
.i1true
;
2191 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2192 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2193 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
2194 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2195 ctx
->ac
.i32_0
, ""); /* vindex */
2196 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2197 params
[4] = glc
; /* glc */
2198 params
[5] = ctx
->ac
.i1false
; /* slc */
2199 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2202 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2203 LLVMValueRef slc
= ctx
->ac
.i1false
;
2205 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2206 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2207 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
2208 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2209 params
[4] = (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2210 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2212 params
[6] = ctx
->ac
.i1false
;
2215 ac_get_image_intr_name("llvm.amdgcn.image.store",
2216 LLVMTypeOf(params
[0]), /* vdata */
2217 LLVMTypeOf(params
[1]), /* coords */
2218 LLVMTypeOf(params
[2]), /* rsrc */
2219 intrinsic_name
, sizeof(intrinsic_name
));
2221 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
2227 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2228 const nir_intrinsic_instr
*instr
)
2230 LLVMValueRef params
[7];
2231 int param_count
= 0;
2232 const nir_variable
*var
= instr
->variables
[0]->var
;
2234 const char *atomic_name
;
2235 char intrinsic_name
[41];
2236 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2237 MAYBE_UNUSED
int length
;
2239 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2241 switch (instr
->intrinsic
) {
2242 case nir_intrinsic_image_atomic_add
:
2243 atomic_name
= "add";
2245 case nir_intrinsic_image_atomic_min
:
2246 atomic_name
= is_unsigned
? "umin" : "smin";
2248 case nir_intrinsic_image_atomic_max
:
2249 atomic_name
= is_unsigned
? "umax" : "smax";
2251 case nir_intrinsic_image_atomic_and
:
2252 atomic_name
= "and";
2254 case nir_intrinsic_image_atomic_or
:
2257 case nir_intrinsic_image_atomic_xor
:
2258 atomic_name
= "xor";
2260 case nir_intrinsic_image_atomic_exchange
:
2261 atomic_name
= "swap";
2263 case nir_intrinsic_image_atomic_comp_swap
:
2264 atomic_name
= "cmpswap";
2270 if (instr
->intrinsic
== nir_intrinsic_image_atomic_comp_swap
)
2271 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2272 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2274 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2275 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
2277 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2278 ctx
->ac
.i32_0
, ""); /* vindex */
2279 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2280 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2282 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2283 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2285 char coords_type
[8];
2287 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2288 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
2290 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
2291 params
[param_count
++] = glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
2292 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2294 build_int_type_name(LLVMTypeOf(coords
),
2295 coords_type
, sizeof(coords_type
));
2297 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2298 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
2301 assert(length
< sizeof(intrinsic_name
));
2302 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
2305 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2306 const nir_intrinsic_instr
*instr
)
2308 const nir_variable
*var
= instr
->variables
[0]->var
;
2309 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2311 struct ac_image_args args
= { 0 };
2312 args
.da
= glsl_is_array_image(type
);
2314 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2315 AC_DESC_IMAGE
, NULL
, true, false);
2316 args
.opcode
= ac_image_get_resinfo
;
2317 args
.addr
= ctx
->ac
.i32_0
;
2319 return ac_build_image_opcode(&ctx
->ac
, &args
);
2322 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2323 const nir_intrinsic_instr
*instr
)
2326 const nir_variable
*var
= instr
->variables
[0]->var
;
2327 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2329 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2330 return get_buffer_size(ctx
,
2331 get_sampler_desc(ctx
, instr
->variables
[0],
2332 AC_DESC_BUFFER
, NULL
, true, false), true);
2334 struct ac_image_args args
= { 0 };
2336 args
.da
= glsl_is_array_image(type
);
2338 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2339 args
.opcode
= ac_image_get_resinfo
;
2340 args
.addr
= ctx
->ac
.i32_0
;
2342 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2344 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2346 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2347 glsl_sampler_type_is_array(type
)) {
2348 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2349 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2350 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2351 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2353 if (ctx
->ac
.chip_class
>= GFX9
&&
2354 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2355 glsl_sampler_type_is_array(type
)) {
2356 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2357 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2364 #define NOOP_WAITCNT 0xf7f
2365 #define LGKM_CNT 0x07f
2366 #define VM_CNT 0xf70
2368 static void emit_membar(struct ac_llvm_context
*ac
,
2369 const nir_intrinsic_instr
*instr
)
2371 unsigned waitcnt
= NOOP_WAITCNT
;
2373 switch (instr
->intrinsic
) {
2374 case nir_intrinsic_memory_barrier
:
2375 case nir_intrinsic_group_memory_barrier
:
2376 waitcnt
&= VM_CNT
& LGKM_CNT
;
2378 case nir_intrinsic_memory_barrier_atomic_counter
:
2379 case nir_intrinsic_memory_barrier_buffer
:
2380 case nir_intrinsic_memory_barrier_image
:
2383 case nir_intrinsic_memory_barrier_shared
:
2384 waitcnt
&= LGKM_CNT
;
2389 if (waitcnt
!= NOOP_WAITCNT
)
2390 ac_build_waitcnt(ac
, waitcnt
);
2393 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2395 /* SI only (thanks to a hw bug workaround):
2396 * The real barrier instruction isn’t needed, because an entire patch
2397 * always fits into a single wave.
2399 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2400 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2403 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2404 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2407 static void emit_discard(struct ac_nir_context
*ctx
,
2408 const nir_intrinsic_instr
*instr
)
2412 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2413 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2414 get_src(ctx
, instr
->src
[0]),
2417 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2418 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2421 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2425 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2427 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2428 "llvm.amdgcn.ps.live",
2429 ctx
->ac
.i1
, NULL
, 0,
2430 AC_FUNC_ATTR_READNONE
);
2431 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2432 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2436 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2438 LLVMValueRef result
;
2439 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2440 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2441 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2443 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2447 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2449 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2450 LLVMValueRef result
;
2451 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2452 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2453 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2455 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2460 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2462 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2463 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2464 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2466 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2471 visit_first_invocation(struct ac_nir_context
*ctx
)
2473 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2475 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2476 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2477 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2479 ctx
->ac
.i64
, args
, 2,
2480 AC_FUNC_ATTR_NOUNWIND
|
2481 AC_FUNC_ATTR_READNONE
);
2483 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2487 visit_load_shared(struct ac_nir_context
*ctx
,
2488 const nir_intrinsic_instr
*instr
)
2490 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2492 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2494 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2495 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2496 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2497 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2500 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2501 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2505 visit_store_shared(struct ac_nir_context
*ctx
,
2506 const nir_intrinsic_instr
*instr
)
2508 LLVMValueRef derived_ptr
, data
,index
;
2509 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2511 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2512 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2514 int writemask
= nir_intrinsic_write_mask(instr
);
2515 for (int chan
= 0; chan
< 4; chan
++) {
2516 if (!(writemask
& (1 << chan
))) {
2519 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2520 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2521 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2522 LLVMBuildStore(builder
, data
, derived_ptr
);
2526 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2527 const nir_intrinsic_instr
*instr
,
2528 LLVMValueRef ptr
, int src_idx
)
2530 LLVMValueRef result
;
2531 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2533 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2534 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2535 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2536 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2538 LLVMAtomicOrderingSequentiallyConsistent
,
2539 LLVMAtomicOrderingSequentiallyConsistent
,
2542 LLVMAtomicRMWBinOp op
;
2543 switch (instr
->intrinsic
) {
2544 case nir_intrinsic_var_atomic_add
:
2545 case nir_intrinsic_shared_atomic_add
:
2546 op
= LLVMAtomicRMWBinOpAdd
;
2548 case nir_intrinsic_var_atomic_umin
:
2549 case nir_intrinsic_shared_atomic_umin
:
2550 op
= LLVMAtomicRMWBinOpUMin
;
2552 case nir_intrinsic_var_atomic_umax
:
2553 case nir_intrinsic_shared_atomic_umax
:
2554 op
= LLVMAtomicRMWBinOpUMax
;
2556 case nir_intrinsic_var_atomic_imin
:
2557 case nir_intrinsic_shared_atomic_imin
:
2558 op
= LLVMAtomicRMWBinOpMin
;
2560 case nir_intrinsic_var_atomic_imax
:
2561 case nir_intrinsic_shared_atomic_imax
:
2562 op
= LLVMAtomicRMWBinOpMax
;
2564 case nir_intrinsic_var_atomic_and
:
2565 case nir_intrinsic_shared_atomic_and
:
2566 op
= LLVMAtomicRMWBinOpAnd
;
2568 case nir_intrinsic_var_atomic_or
:
2569 case nir_intrinsic_shared_atomic_or
:
2570 op
= LLVMAtomicRMWBinOpOr
;
2572 case nir_intrinsic_var_atomic_xor
:
2573 case nir_intrinsic_shared_atomic_xor
:
2574 op
= LLVMAtomicRMWBinOpXor
;
2576 case nir_intrinsic_var_atomic_exchange
:
2577 case nir_intrinsic_shared_atomic_exchange
:
2578 op
= LLVMAtomicRMWBinOpXchg
;
2584 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2585 LLVMAtomicOrderingSequentiallyConsistent
,
2591 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2593 LLVMValueRef values
[2];
2594 LLVMValueRef pos
[2];
2596 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2597 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2599 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2600 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2601 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2604 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2605 const nir_intrinsic_instr
*instr
)
2607 LLVMValueRef result
[4];
2608 LLVMValueRef interp_param
, attr_number
;
2611 LLVMValueRef src_c0
= NULL
;
2612 LLVMValueRef src_c1
= NULL
;
2613 LLVMValueRef src0
= NULL
;
2614 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2615 switch (instr
->intrinsic
) {
2616 case nir_intrinsic_interp_var_at_centroid
:
2617 location
= INTERP_CENTROID
;
2619 case nir_intrinsic_interp_var_at_sample
:
2620 case nir_intrinsic_interp_var_at_offset
:
2621 location
= INTERP_CENTER
;
2622 src0
= get_src(ctx
, instr
->src
[0]);
2628 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2629 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2630 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2631 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2632 LLVMValueRef sample_position
;
2633 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2635 /* fetch sample ID */
2636 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2638 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2639 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2640 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2641 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2643 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2644 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2646 if (location
== INTERP_CENTER
) {
2647 LLVMValueRef ij_out
[2];
2648 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2651 * take the I then J parameters, and the DDX/Y for it, and
2652 * calculate the IJ inputs for the interpolator.
2653 * temp1 = ddx * offset/sample.x + I;
2654 * interp_param.I = ddy * offset/sample.y + temp1;
2655 * temp1 = ddx * offset/sample.x + J;
2656 * interp_param.J = ddy * offset/sample.y + temp1;
2658 for (unsigned i
= 0; i
< 2; i
++) {
2659 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2660 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2661 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2662 ddxy_out
, ix_ll
, "");
2663 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2664 ddxy_out
, iy_ll
, "");
2665 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2666 interp_param
, ix_ll
, "");
2667 LLVMValueRef temp1
, temp2
;
2669 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2672 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2673 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2675 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2676 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2678 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2679 temp2
, ctx
->ac
.i32
, "");
2681 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2685 for (chan
= 0; chan
< 4; chan
++) {
2686 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2689 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2690 interp_param
, ctx
->ac
.v2f32
, "");
2691 LLVMValueRef i
= LLVMBuildExtractElement(
2692 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2693 LLVMValueRef j
= LLVMBuildExtractElement(
2694 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2696 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2697 llvm_chan
, attr_number
,
2698 ctx
->abi
->prim_mask
, i
, j
);
2700 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2701 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2702 llvm_chan
, attr_number
,
2703 ctx
->abi
->prim_mask
);
2706 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2707 instr
->variables
[0]->var
->data
.location_frac
);
2710 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2711 nir_intrinsic_instr
*instr
)
2713 LLVMValueRef result
= NULL
;
2715 switch (instr
->intrinsic
) {
2716 case nir_intrinsic_ballot
:
2717 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2719 case nir_intrinsic_read_invocation
:
2720 case nir_intrinsic_read_first_invocation
: {
2721 LLVMValueRef args
[2];
2724 args
[0] = get_src(ctx
, instr
->src
[0]);
2727 const char *intr_name
;
2728 if (instr
->intrinsic
== nir_intrinsic_read_invocation
) {
2730 intr_name
= "llvm.amdgcn.readlane";
2733 args
[1] = get_src(ctx
, instr
->src
[1]);
2736 intr_name
= "llvm.amdgcn.readfirstlane";
2739 /* We currently have no other way to prevent LLVM from lifting the icmp
2740 * calls to a dominating basic block.
2742 ac_build_optimization_barrier(&ctx
->ac
, &args
[0]);
2744 result
= ac_build_intrinsic(&ctx
->ac
, intr_name
,
2745 ctx
->ac
.i32
, args
, num_args
,
2746 AC_FUNC_ATTR_READNONE
|
2747 AC_FUNC_ATTR_CONVERGENT
);
2750 case nir_intrinsic_load_subgroup_invocation
:
2751 result
= ac_get_thread_id(&ctx
->ac
);
2753 case nir_intrinsic_load_work_group_id
: {
2754 LLVMValueRef values
[3];
2756 for (int i
= 0; i
< 3; i
++) {
2757 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2758 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2761 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2764 case nir_intrinsic_load_base_vertex
: {
2765 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2768 case nir_intrinsic_load_local_group_size
:
2769 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2771 case nir_intrinsic_load_vertex_id
:
2772 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2773 ctx
->abi
->base_vertex
, "");
2775 case nir_intrinsic_load_vertex_id_zero_base
: {
2776 result
= ctx
->abi
->vertex_id
;
2779 case nir_intrinsic_load_local_invocation_id
: {
2780 result
= ctx
->abi
->local_invocation_ids
;
2783 case nir_intrinsic_load_base_instance
:
2784 result
= ctx
->abi
->start_instance
;
2786 case nir_intrinsic_load_draw_id
:
2787 result
= ctx
->abi
->draw_id
;
2789 case nir_intrinsic_load_view_index
:
2790 result
= ctx
->abi
->view_index
;
2792 case nir_intrinsic_load_invocation_id
:
2793 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2794 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2796 result
= ctx
->abi
->gs_invocation_id
;
2798 case nir_intrinsic_load_primitive_id
:
2799 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2800 result
= ctx
->abi
->gs_prim_id
;
2801 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2802 result
= ctx
->abi
->tcs_patch_id
;
2803 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2804 result
= ctx
->abi
->tes_patch_id
;
2806 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2808 case nir_intrinsic_load_sample_id
:
2809 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2811 case nir_intrinsic_load_sample_pos
:
2812 result
= load_sample_pos(ctx
);
2814 case nir_intrinsic_load_sample_mask_in
:
2815 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2817 case nir_intrinsic_load_frag_coord
: {
2818 LLVMValueRef values
[4] = {
2819 ctx
->abi
->frag_pos
[0],
2820 ctx
->abi
->frag_pos
[1],
2821 ctx
->abi
->frag_pos
[2],
2822 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2824 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2827 case nir_intrinsic_load_front_face
:
2828 result
= ctx
->abi
->front_face
;
2830 case nir_intrinsic_load_helper_invocation
:
2831 result
= visit_load_helper_invocation(ctx
);
2833 case nir_intrinsic_load_instance_id
:
2834 result
= ctx
->abi
->instance_id
;
2836 case nir_intrinsic_load_num_work_groups
:
2837 result
= ctx
->abi
->num_work_groups
;
2839 case nir_intrinsic_load_local_invocation_index
:
2840 result
= visit_load_local_invocation_index(ctx
);
2842 case nir_intrinsic_load_subgroup_id
:
2843 result
= visit_load_subgroup_id(ctx
);
2845 case nir_intrinsic_load_num_subgroups
:
2846 result
= visit_load_num_subgroups(ctx
);
2848 case nir_intrinsic_first_invocation
:
2849 result
= visit_first_invocation(ctx
);
2851 case nir_intrinsic_load_push_constant
:
2852 result
= visit_load_push_constant(ctx
, instr
);
2854 case nir_intrinsic_vulkan_resource_index
: {
2855 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2856 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2857 unsigned binding
= nir_intrinsic_binding(instr
);
2859 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2863 case nir_intrinsic_vulkan_resource_reindex
:
2864 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2866 case nir_intrinsic_store_ssbo
:
2867 visit_store_ssbo(ctx
, instr
);
2869 case nir_intrinsic_load_ssbo
:
2870 result
= visit_load_buffer(ctx
, instr
);
2872 case nir_intrinsic_ssbo_atomic_add
:
2873 case nir_intrinsic_ssbo_atomic_imin
:
2874 case nir_intrinsic_ssbo_atomic_umin
:
2875 case nir_intrinsic_ssbo_atomic_imax
:
2876 case nir_intrinsic_ssbo_atomic_umax
:
2877 case nir_intrinsic_ssbo_atomic_and
:
2878 case nir_intrinsic_ssbo_atomic_or
:
2879 case nir_intrinsic_ssbo_atomic_xor
:
2880 case nir_intrinsic_ssbo_atomic_exchange
:
2881 case nir_intrinsic_ssbo_atomic_comp_swap
:
2882 result
= visit_atomic_ssbo(ctx
, instr
);
2884 case nir_intrinsic_load_ubo
:
2885 result
= visit_load_ubo_buffer(ctx
, instr
);
2887 case nir_intrinsic_get_buffer_size
:
2888 result
= visit_get_buffer_size(ctx
, instr
);
2890 case nir_intrinsic_load_var
:
2891 result
= visit_load_var(ctx
, instr
);
2893 case nir_intrinsic_store_var
:
2894 visit_store_var(ctx
, instr
);
2896 case nir_intrinsic_load_shared
:
2897 result
= visit_load_shared(ctx
, instr
);
2899 case nir_intrinsic_store_shared
:
2900 visit_store_shared(ctx
, instr
);
2902 case nir_intrinsic_image_samples
:
2903 result
= visit_image_samples(ctx
, instr
);
2905 case nir_intrinsic_image_load
:
2906 result
= visit_image_load(ctx
, instr
);
2908 case nir_intrinsic_image_store
:
2909 visit_image_store(ctx
, instr
);
2911 case nir_intrinsic_image_atomic_add
:
2912 case nir_intrinsic_image_atomic_min
:
2913 case nir_intrinsic_image_atomic_max
:
2914 case nir_intrinsic_image_atomic_and
:
2915 case nir_intrinsic_image_atomic_or
:
2916 case nir_intrinsic_image_atomic_xor
:
2917 case nir_intrinsic_image_atomic_exchange
:
2918 case nir_intrinsic_image_atomic_comp_swap
:
2919 result
= visit_image_atomic(ctx
, instr
);
2921 case nir_intrinsic_image_size
:
2922 result
= visit_image_size(ctx
, instr
);
2924 case nir_intrinsic_shader_clock
:
2925 result
= ac_build_shader_clock(&ctx
->ac
);
2927 case nir_intrinsic_discard
:
2928 case nir_intrinsic_discard_if
:
2929 emit_discard(ctx
, instr
);
2931 case nir_intrinsic_memory_barrier
:
2932 case nir_intrinsic_group_memory_barrier
:
2933 case nir_intrinsic_memory_barrier_atomic_counter
:
2934 case nir_intrinsic_memory_barrier_buffer
:
2935 case nir_intrinsic_memory_barrier_image
:
2936 case nir_intrinsic_memory_barrier_shared
:
2937 emit_membar(&ctx
->ac
, instr
);
2939 case nir_intrinsic_barrier
:
2940 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
2942 case nir_intrinsic_shared_atomic_add
:
2943 case nir_intrinsic_shared_atomic_imin
:
2944 case nir_intrinsic_shared_atomic_umin
:
2945 case nir_intrinsic_shared_atomic_imax
:
2946 case nir_intrinsic_shared_atomic_umax
:
2947 case nir_intrinsic_shared_atomic_and
:
2948 case nir_intrinsic_shared_atomic_or
:
2949 case nir_intrinsic_shared_atomic_xor
:
2950 case nir_intrinsic_shared_atomic_exchange
:
2951 case nir_intrinsic_shared_atomic_comp_swap
: {
2952 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2953 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
2956 case nir_intrinsic_var_atomic_add
:
2957 case nir_intrinsic_var_atomic_imin
:
2958 case nir_intrinsic_var_atomic_umin
:
2959 case nir_intrinsic_var_atomic_imax
:
2960 case nir_intrinsic_var_atomic_umax
:
2961 case nir_intrinsic_var_atomic_and
:
2962 case nir_intrinsic_var_atomic_or
:
2963 case nir_intrinsic_var_atomic_xor
:
2964 case nir_intrinsic_var_atomic_exchange
:
2965 case nir_intrinsic_var_atomic_comp_swap
: {
2966 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
2967 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
2970 case nir_intrinsic_interp_var_at_centroid
:
2971 case nir_intrinsic_interp_var_at_sample
:
2972 case nir_intrinsic_interp_var_at_offset
:
2973 result
= visit_interp(ctx
, instr
);
2975 case nir_intrinsic_emit_vertex
:
2976 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
2978 case nir_intrinsic_end_primitive
:
2979 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
2981 case nir_intrinsic_load_tess_coord
:
2982 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
2984 case nir_intrinsic_load_tess_level_outer
:
2985 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
2987 case nir_intrinsic_load_tess_level_inner
:
2988 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
2990 case nir_intrinsic_load_patch_vertices_in
:
2991 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
2993 case nir_intrinsic_vote_all
: {
2994 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2995 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
2998 case nir_intrinsic_vote_any
: {
2999 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3000 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3004 fprintf(stderr
, "Unknown intrinsic: ");
3005 nir_print_instr(&instr
->instr
, stderr
);
3006 fprintf(stderr
, "\n");
3010 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3014 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3015 const nir_deref_var
*deref
,
3016 enum ac_descriptor_type desc_type
,
3017 const nir_tex_instr
*tex_instr
,
3018 bool image
, bool write
)
3020 LLVMValueRef index
= NULL
;
3021 unsigned constant_index
= 0;
3022 unsigned descriptor_set
;
3023 unsigned base_index
;
3026 assert(tex_instr
&& !image
);
3028 base_index
= tex_instr
->sampler_index
;
3030 const nir_deref
*tail
= &deref
->deref
;
3031 while (tail
->child
) {
3032 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3033 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3038 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3040 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3041 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3043 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3044 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3049 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3052 constant_index
+= child
->base_offset
* array_size
;
3054 tail
= &child
->deref
;
3056 descriptor_set
= deref
->var
->data
.descriptor_set
;
3057 base_index
= deref
->var
->data
.binding
;
3060 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3063 constant_index
, index
,
3064 desc_type
, image
, write
);
3067 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
3068 struct ac_image_args
*args
,
3069 const nir_tex_instr
*instr
,
3071 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3072 LLVMValueRef
*param
, unsigned count
,
3075 unsigned is_rect
= 0;
3076 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3078 if (op
== nir_texop_lod
)
3080 /* Pad to power of two vector */
3081 while (count
< util_next_power_of_two(count
))
3082 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3085 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
3087 args
->addr
= param
[0];
3089 args
->resource
= res_ptr
;
3090 args
->sampler
= samp_ptr
;
3092 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3093 args
->addr
= param
[0];
3097 args
->dmask
= dmask
;
3098 args
->unorm
= is_rect
;
3102 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3105 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3106 * filtering manually. The driver sets img7 to a mask clearing
3107 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3108 * s_and_b32 samp0, samp0, img7
3111 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3113 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3114 LLVMValueRef res
, LLVMValueRef samp
)
3116 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3117 LLVMValueRef img7
, samp0
;
3119 if (ctx
->ac
.chip_class
>= VI
)
3122 img7
= LLVMBuildExtractElement(builder
, res
,
3123 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3124 samp0
= LLVMBuildExtractElement(builder
, samp
,
3125 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3126 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3127 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3128 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3131 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3132 nir_tex_instr
*instr
,
3133 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3134 LLVMValueRef
*fmask_ptr
)
3136 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3137 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
3139 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
3142 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
3144 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
3145 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3146 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3148 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3149 instr
->op
== nir_texop_samples_identical
))
3150 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
3153 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3156 coord
= ac_to_float(ctx
, coord
);
3157 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3158 coord
= ac_to_integer(ctx
, coord
);
3162 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3164 LLVMValueRef result
= NULL
;
3165 struct ac_image_args args
= { 0 };
3166 unsigned dmask
= 0xf;
3167 LLVMValueRef address
[16];
3168 LLVMValueRef coords
[5];
3169 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3170 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3171 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3172 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3173 LLVMValueRef derivs
[6];
3174 unsigned chan
, count
= 0;
3175 unsigned const_src
= 0, num_deriv_comp
= 0;
3176 bool lod_is_zero
= false;
3178 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3180 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3181 switch (instr
->src
[i
].src_type
) {
3182 case nir_tex_src_coord
:
3183 coord
= get_src(ctx
, instr
->src
[i
].src
);
3185 case nir_tex_src_projector
:
3187 case nir_tex_src_comparator
:
3188 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3190 case nir_tex_src_offset
:
3191 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3194 case nir_tex_src_bias
:
3195 bias
= get_src(ctx
, instr
->src
[i
].src
);
3197 case nir_tex_src_lod
: {
3198 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3200 if (val
&& val
->i32
[0] == 0)
3202 lod
= get_src(ctx
, instr
->src
[i
].src
);
3205 case nir_tex_src_ms_index
:
3206 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3208 case nir_tex_src_ms_mcs
:
3210 case nir_tex_src_ddx
:
3211 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3212 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3214 case nir_tex_src_ddy
:
3215 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3217 case nir_tex_src_texture_offset
:
3218 case nir_tex_src_sampler_offset
:
3219 case nir_tex_src_plane
:
3225 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3226 result
= get_buffer_size(ctx
, res_ptr
, true);
3230 if (instr
->op
== nir_texop_texture_samples
) {
3231 LLVMValueRef res
, samples
, is_msaa
;
3232 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
3233 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3234 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3235 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3236 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3237 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3238 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3239 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3240 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3242 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3243 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3244 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3245 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3246 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3248 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3255 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3256 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3258 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3259 LLVMValueRef offset
[3], pack
;
3260 for (chan
= 0; chan
< 3; ++chan
)
3261 offset
[chan
] = ctx
->ac
.i32_0
;
3264 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
3265 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
3266 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3267 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3269 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3270 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3272 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3273 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3274 address
[count
++] = pack
;
3277 /* pack LOD bias value */
3278 if (instr
->op
== nir_texop_txb
&& bias
) {
3279 address
[count
++] = bias
;
3282 /* Pack depth comparison value */
3283 if (instr
->is_shadow
&& comparator
) {
3284 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
3285 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
3287 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3288 * so the depth comparison value isn't clamped for Z16 and
3289 * Z24 anymore. Do it manually here.
3291 * It's unnecessary if the original texture format was
3292 * Z32_FLOAT, but we don't know that here.
3294 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3295 z
= ac_build_clamp(&ctx
->ac
, z
);
3297 address
[count
++] = z
;
3300 /* pack derivatives */
3302 int num_src_deriv_channels
, num_dest_deriv_channels
;
3303 switch (instr
->sampler_dim
) {
3304 case GLSL_SAMPLER_DIM_3D
:
3305 case GLSL_SAMPLER_DIM_CUBE
:
3307 num_src_deriv_channels
= 3;
3308 num_dest_deriv_channels
= 3;
3310 case GLSL_SAMPLER_DIM_2D
:
3312 num_src_deriv_channels
= 2;
3313 num_dest_deriv_channels
= 2;
3316 case GLSL_SAMPLER_DIM_1D
:
3317 num_src_deriv_channels
= 1;
3318 if (ctx
->ac
.chip_class
>= GFX9
) {
3319 num_dest_deriv_channels
= 2;
3322 num_dest_deriv_channels
= 1;
3328 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3329 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3330 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3332 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3333 derivs
[i
] = ctx
->ac
.f32_0
;
3334 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3338 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3339 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3340 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
3341 if (instr
->coord_components
== 3)
3342 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3343 ac_prepare_cube_coords(&ctx
->ac
,
3344 instr
->op
== nir_texop_txd
, instr
->is_array
,
3345 instr
->op
== nir_texop_lod
, coords
, derivs
);
3351 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3352 address
[count
++] = derivs
[i
];
3355 /* Pack texture coordinates */
3357 address
[count
++] = coords
[0];
3358 if (instr
->coord_components
> 1) {
3359 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3360 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
3362 address
[count
++] = coords
[1];
3364 if (instr
->coord_components
> 2) {
3365 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3366 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3367 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3368 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3370 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3371 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
3373 address
[count
++] = coords
[2];
3376 if (ctx
->ac
.chip_class
>= GFX9
) {
3377 LLVMValueRef filler
;
3378 if (instr
->op
== nir_texop_txf
)
3379 filler
= ctx
->ac
.i32_0
;
3381 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3383 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
3384 /* No nir_texop_lod, because it does not take a slice
3385 * even with array textures. */
3386 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
3387 address
[count
] = address
[count
- 1];
3388 address
[count
- 1] = filler
;
3391 address
[count
++] = filler
;
3397 if (lod
&& ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && !lod_is_zero
)) {
3398 address
[count
++] = lod
;
3399 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3400 address
[count
++] = sample_index
;
3401 } else if(instr
->op
== nir_texop_txs
) {
3404 address
[count
++] = lod
;
3406 address
[count
++] = ctx
->ac
.i32_0
;
3409 for (chan
= 0; chan
< count
; chan
++) {
3410 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3411 address
[chan
], ctx
->ac
.i32
, "");
3414 if (instr
->op
== nir_texop_samples_identical
) {
3415 LLVMValueRef txf_address
[4];
3416 struct ac_image_args txf_args
= { 0 };
3417 unsigned txf_count
= count
;
3418 memcpy(txf_address
, address
, sizeof(txf_address
));
3420 if (!instr
->is_array
)
3421 txf_address
[2] = ctx
->ac
.i32_0
;
3422 txf_address
[3] = ctx
->ac
.i32_0
;
3424 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
3426 txf_address
, txf_count
, 0xf);
3428 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
3430 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3431 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3435 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3436 instr
->op
!= nir_texop_txs
) {
3437 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3438 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
3441 instr
->is_array
? address
[2] : NULL
,
3442 address
[sample_chan
],
3446 if (offsets
&& instr
->op
== nir_texop_txf
) {
3447 nir_const_value
*const_offset
=
3448 nir_src_as_const_value(instr
->src
[const_src
].src
);
3449 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3450 assert(const_offset
);
3451 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3452 if (num_offsets
> 2)
3453 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
3454 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
3455 if (num_offsets
> 1)
3456 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
3457 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
3458 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
3459 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
3463 /* TODO TG4 support */
3464 if (instr
->op
== nir_texop_tg4
) {
3465 if (instr
->is_shadow
)
3468 dmask
= 1 << instr
->component
;
3470 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
3471 res_ptr
, samp_ptr
, address
, count
, dmask
);
3473 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
3475 if (instr
->op
== nir_texop_query_levels
)
3476 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3477 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3478 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3479 instr
->op
!= nir_texop_tg4
)
3480 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3481 else if (instr
->op
== nir_texop_txs
&&
3482 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3484 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3485 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3486 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3487 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3488 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3489 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3490 instr
->op
== nir_texop_txs
&&
3491 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3493 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3494 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3495 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3497 } else if (instr
->dest
.ssa
.num_components
!= 4)
3498 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3502 assert(instr
->dest
.is_ssa
);
3503 result
= ac_to_integer(&ctx
->ac
, result
);
3504 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3509 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3511 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3512 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3514 _mesa_hash_table_insert(ctx
->defs
, &instr
->dest
.ssa
, result
);
3515 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3518 static void visit_post_phi(struct ac_nir_context
*ctx
,
3519 nir_phi_instr
*instr
,
3520 LLVMValueRef llvm_phi
)
3522 nir_foreach_phi_src(src
, instr
) {
3523 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3524 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3526 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3530 static void phi_post_pass(struct ac_nir_context
*ctx
)
3532 struct hash_entry
*entry
;
3533 hash_table_foreach(ctx
->phis
, entry
) {
3534 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3535 (LLVMValueRef
)entry
->data
);
3540 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3541 const nir_ssa_undef_instr
*instr
)
3543 unsigned num_components
= instr
->def
.num_components
;
3544 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3547 if (num_components
== 1)
3548 undef
= LLVMGetUndef(type
);
3550 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3552 _mesa_hash_table_insert(ctx
->defs
, &instr
->def
, undef
);
3555 static void visit_jump(struct ac_llvm_context
*ctx
,
3556 const nir_jump_instr
*instr
)
3558 switch (instr
->type
) {
3559 case nir_jump_break
:
3560 ac_build_break(ctx
);
3562 case nir_jump_continue
:
3563 ac_build_continue(ctx
);
3566 fprintf(stderr
, "Unknown NIR jump instr: ");
3567 nir_print_instr(&instr
->instr
, stderr
);
3568 fprintf(stderr
, "\n");
3573 static void visit_cf_list(struct ac_nir_context
*ctx
,
3574 struct exec_list
*list
);
3576 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3578 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3579 nir_foreach_instr(instr
, block
)
3581 switch (instr
->type
) {
3582 case nir_instr_type_alu
:
3583 visit_alu(ctx
, nir_instr_as_alu(instr
));
3585 case nir_instr_type_load_const
:
3586 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3588 case nir_instr_type_intrinsic
:
3589 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3591 case nir_instr_type_tex
:
3592 visit_tex(ctx
, nir_instr_as_tex(instr
));
3594 case nir_instr_type_phi
:
3595 visit_phi(ctx
, nir_instr_as_phi(instr
));
3597 case nir_instr_type_ssa_undef
:
3598 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3600 case nir_instr_type_jump
:
3601 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3604 fprintf(stderr
, "Unknown NIR instr type: ");
3605 nir_print_instr(instr
, stderr
);
3606 fprintf(stderr
, "\n");
3611 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3614 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3616 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3618 nir_block
*then_block
=
3619 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3621 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3623 visit_cf_list(ctx
, &if_stmt
->then_list
);
3625 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3626 nir_block
*else_block
=
3627 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3629 ac_build_else(&ctx
->ac
, else_block
->index
);
3630 visit_cf_list(ctx
, &if_stmt
->else_list
);
3633 ac_build_endif(&ctx
->ac
, then_block
->index
);
3636 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3638 nir_block
*first_loop_block
=
3639 (nir_block
*) exec_list_get_head(&loop
->body
);
3641 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3643 visit_cf_list(ctx
, &loop
->body
);
3645 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3648 static void visit_cf_list(struct ac_nir_context
*ctx
,
3649 struct exec_list
*list
)
3651 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3653 switch (node
->type
) {
3654 case nir_cf_node_block
:
3655 visit_block(ctx
, nir_cf_node_as_block(node
));
3658 case nir_cf_node_if
:
3659 visit_if(ctx
, nir_cf_node_as_if(node
));
3662 case nir_cf_node_loop
:
3663 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3673 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3674 struct ac_shader_abi
*abi
,
3675 struct nir_shader
*nir
,
3676 struct nir_variable
*variable
,
3677 gl_shader_stage stage
)
3679 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3680 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3682 /* tess ctrl has it's own load/store paths for outputs */
3683 if (stage
== MESA_SHADER_TESS_CTRL
)
3686 if (stage
== MESA_SHADER_VERTEX
||
3687 stage
== MESA_SHADER_TESS_EVAL
||
3688 stage
== MESA_SHADER_GEOMETRY
) {
3689 int idx
= variable
->data
.location
+ variable
->data
.index
;
3690 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3691 int length
= nir
->info
.clip_distance_array_size
+
3692 nir
->info
.cull_distance_array_size
;
3701 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3702 for (unsigned chan
= 0; chan
< 4; chan
++) {
3703 abi
->outputs
[radeon_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3704 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3710 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3711 enum glsl_base_type type
)
3715 case GLSL_TYPE_UINT
:
3716 case GLSL_TYPE_BOOL
:
3717 case GLSL_TYPE_SUBROUTINE
:
3719 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3721 case GLSL_TYPE_INT64
:
3722 case GLSL_TYPE_UINT64
:
3724 case GLSL_TYPE_DOUBLE
:
3727 unreachable("unknown GLSL type");
3732 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3733 const struct glsl_type
*type
)
3735 if (glsl_type_is_scalar(type
)) {
3736 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3739 if (glsl_type_is_vector(type
)) {
3740 return LLVMVectorType(
3741 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3742 glsl_get_vector_elements(type
));
3745 if (glsl_type_is_matrix(type
)) {
3746 return LLVMArrayType(
3747 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3748 glsl_get_matrix_columns(type
));
3751 if (glsl_type_is_array(type
)) {
3752 return LLVMArrayType(
3753 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3754 glsl_get_length(type
));
3757 assert(glsl_type_is_struct(type
));
3759 LLVMTypeRef member_types
[glsl_get_length(type
)];
3761 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3763 glsl_to_llvm_type(ac
,
3764 glsl_get_struct_field(type
, i
));
3767 return LLVMStructTypeInContext(ac
->context
, member_types
,
3768 glsl_get_length(type
), false);
3772 setup_locals(struct ac_nir_context
*ctx
,
3773 struct nir_function
*func
)
3776 ctx
->num_locals
= 0;
3777 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3778 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3779 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3780 variable
->data
.location_frac
= 0;
3781 ctx
->num_locals
+= attrib_count
;
3783 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3787 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3788 for (j
= 0; j
< 4; j
++) {
3789 ctx
->locals
[i
* 4 + j
] =
3790 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3796 setup_shared(struct ac_nir_context
*ctx
,
3797 struct nir_shader
*nir
)
3799 nir_foreach_variable(variable
, &nir
->shared
) {
3800 LLVMValueRef shared
=
3801 LLVMAddGlobalInAddressSpace(
3802 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3803 variable
->name
? variable
->name
: "",
3804 AC_LOCAL_ADDR_SPACE
);
3805 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3809 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3810 struct nir_shader
*nir
)
3812 struct ac_nir_context ctx
= {};
3813 struct nir_function
*func
;
3815 /* Last minute passes for both radv & radeonsi */
3816 ac_lower_subgroups(nir
);
3821 ctx
.stage
= nir
->info
.stage
;
3823 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3825 nir_foreach_variable(variable
, &nir
->outputs
)
3826 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3829 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3830 _mesa_key_pointer_equal
);
3831 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3832 _mesa_key_pointer_equal
);
3833 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3834 _mesa_key_pointer_equal
);
3836 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3838 setup_locals(&ctx
, func
);
3840 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3841 setup_shared(&ctx
, nir
);
3843 visit_cf_list(&ctx
, &func
->impl
->body
);
3844 phi_post_pass(&ctx
);
3846 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3847 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3851 ralloc_free(ctx
.defs
);
3852 ralloc_free(ctx
.phis
);
3853 ralloc_free(ctx
.vars
);
3857 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3859 /* While it would be nice not to have this flag, we are constrained
3860 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3863 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3865 /* TODO: Indirect indexing of GS inputs is unimplemented.
3867 * TCS and TES load inputs directly from LDS or offchip memory, so
3868 * indirect indexing is trivial.
3870 nir_variable_mode indirect_mask
= 0;
3871 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3872 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3873 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3874 !llvm_has_working_vgpr_indexing
)) {
3875 indirect_mask
|= nir_var_shader_in
;
3877 if (!llvm_has_working_vgpr_indexing
&&
3878 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3879 indirect_mask
|= nir_var_shader_out
;
3881 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3882 * smart enough to handle indirects without causing excess spilling
3883 * causing the gpu to hang.
3885 * See the following thread for more details of the problem:
3886 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3888 indirect_mask
|= nir_var_local
;
3890 nir_lower_indirect_derefs(nir
, indirect_mask
);