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 "util/u_math.h"
32 #include "ac_shader_abi.h"
33 #include "ac_shader_util.h"
35 struct ac_nir_context
{
36 struct ac_llvm_context ac
;
37 struct ac_shader_abi
*abi
;
39 gl_shader_stage stage
;
41 LLVMValueRef
*ssa_defs
;
43 struct hash_table
*defs
;
44 struct hash_table
*phis
;
45 struct hash_table
*vars
;
47 LLVMValueRef main_function
;
48 LLVMBasicBlockRef continue_block
;
49 LLVMBasicBlockRef break_block
;
55 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
56 const nir_deref_var
*deref
,
57 enum ac_descriptor_type desc_type
,
58 const nir_tex_instr
*instr
,
59 bool image
, bool write
);
62 build_store_values_extended(struct ac_llvm_context
*ac
,
65 unsigned value_stride
,
68 LLVMBuilderRef builder
= ac
->builder
;
71 for (i
= 0; i
< value_count
; i
++) {
72 LLVMValueRef ptr
= values
[i
* value_stride
];
73 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
74 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
75 LLVMBuildStore(builder
, value
, ptr
);
79 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
80 const nir_ssa_def
*def
)
82 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
83 if (def
->num_components
> 1) {
84 type
= LLVMVectorType(type
, def
->num_components
);
89 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
92 return nir
->ssa_defs
[src
.ssa
->index
];
96 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
98 LLVMValueRef ptr
= get_src(ctx
, src
);
99 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
100 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
102 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
103 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
106 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
107 const struct nir_block
*b
)
109 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
110 return (LLVMBasicBlockRef
)entry
->data
;
113 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
115 unsigned num_components
)
117 LLVMValueRef value
= get_src(ctx
, src
.src
);
118 bool need_swizzle
= false;
121 unsigned src_components
= ac_get_llvm_num_components(value
);
122 for (unsigned i
= 0; i
< num_components
; ++i
) {
123 assert(src
.swizzle
[i
] < src_components
);
124 if (src
.swizzle
[i
] != i
)
128 if (need_swizzle
|| num_components
!= src_components
) {
129 LLVMValueRef masks
[] = {
130 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
131 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
132 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
133 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
135 if (src_components
> 1 && num_components
== 1) {
136 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
138 } else if (src_components
== 1 && num_components
> 1) {
139 LLVMValueRef values
[] = {value
, value
, value
, value
};
140 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
142 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
143 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
152 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
153 LLVMIntPredicate pred
, LLVMValueRef src0
,
156 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
157 return LLVMBuildSelect(ctx
->builder
, result
,
158 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
162 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
163 LLVMRealPredicate pred
, LLVMValueRef src0
,
167 src0
= ac_to_float(ctx
, src0
);
168 src1
= ac_to_float(ctx
, src1
);
169 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
170 return LLVMBuildSelect(ctx
->builder
, result
,
171 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
175 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
177 LLVMTypeRef result_type
,
181 LLVMValueRef params
[] = {
182 ac_to_float(ctx
, src0
),
185 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
186 ac_get_elem_bits(ctx
, result_type
));
187 assert(length
< sizeof(name
));
188 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
191 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
193 LLVMTypeRef result_type
,
194 LLVMValueRef src0
, LLVMValueRef src1
)
197 LLVMValueRef params
[] = {
198 ac_to_float(ctx
, src0
),
199 ac_to_float(ctx
, src1
),
202 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
203 ac_get_elem_bits(ctx
, result_type
));
204 assert(length
< sizeof(name
));
205 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
208 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
210 LLVMTypeRef result_type
,
211 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
214 LLVMValueRef params
[] = {
215 ac_to_float(ctx
, src0
),
216 ac_to_float(ctx
, src1
),
217 ac_to_float(ctx
, src2
),
220 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
221 ac_get_elem_bits(ctx
, result_type
));
222 assert(length
< sizeof(name
));
223 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
226 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
227 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
229 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
231 return LLVMBuildSelect(ctx
->builder
, v
, ac_to_integer(ctx
, src1
),
232 ac_to_integer(ctx
, src2
), "");
235 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
236 LLVMIntPredicate pred
,
237 LLVMValueRef src0
, LLVMValueRef src1
)
239 return LLVMBuildSelect(ctx
->builder
,
240 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
245 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
248 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
249 LLVMBuildNeg(ctx
->builder
, src0
, ""));
252 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
254 LLVMValueRef src0
, LLVMValueRef src1
)
256 LLVMTypeRef ret_type
;
257 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
259 LLVMValueRef params
[] = { src0
, src1
};
260 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
263 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
264 params
, 2, AC_FUNC_ATTR_READNONE
);
266 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
267 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
271 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
274 return LLVMBuildAnd(ctx
->builder
, src0
, LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""), "");
277 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
280 src0
= ac_to_float(ctx
, src0
);
281 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
282 return LLVMBuildSExt(ctx
->builder
,
283 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
287 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
291 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
296 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
299 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
302 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
303 return LLVMBuildSExt(ctx
->builder
,
304 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
308 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
312 LLVMValueRef cond
= NULL
;
314 src0
= ac_to_float(ctx
, src0
);
315 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
317 if (ctx
->chip_class
>= VI
) {
318 LLVMValueRef args
[2];
319 /* Check if the result is a denormal - and flush to 0 if so. */
321 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
322 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
325 /* need to convert back up to f32 */
326 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
328 if (ctx
->chip_class
>= VI
)
329 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
332 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
333 * so compare the result and flush to 0 if it's smaller.
335 LLVMValueRef temp
, cond2
;
336 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
337 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
338 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
340 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
341 temp
, ctx
->f32_0
, "");
342 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
343 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
348 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
349 LLVMValueRef src0
, LLVMValueRef src1
)
351 LLVMValueRef dst64
, result
;
352 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
353 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
355 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
356 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
357 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
361 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
362 LLVMValueRef src0
, LLVMValueRef src1
)
364 LLVMValueRef dst64
, result
;
365 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
366 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
368 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
369 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
370 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
374 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
376 const LLVMValueRef srcs
[3])
379 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
381 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
382 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
386 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
387 LLVMValueRef src0
, LLVMValueRef src1
,
388 LLVMValueRef src2
, LLVMValueRef src3
)
390 LLVMValueRef bfi_args
[3], result
;
392 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
393 LLVMBuildSub(ctx
->builder
,
394 LLVMBuildShl(ctx
->builder
,
399 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
402 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
405 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
406 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
408 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
409 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
410 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
412 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
416 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
419 LLVMValueRef comp
[2];
421 src0
= ac_to_float(ctx
, src0
);
422 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
423 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
425 return ac_build_cvt_pkrtz_f16(ctx
, comp
);
428 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
431 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
432 LLVMValueRef temps
[2], result
, val
;
435 for (i
= 0; i
< 2; i
++) {
436 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
437 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
438 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
439 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
442 result
= LLVMBuildInsertElement(ctx
->builder
, LLVMGetUndef(ctx
->v2f32
), temps
[0],
444 result
= LLVMBuildInsertElement(ctx
->builder
, result
, temps
[1],
449 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
457 if (op
== nir_op_fddx_fine
)
458 mask
= AC_TID_MASK_LEFT
;
459 else if (op
== nir_op_fddy_fine
)
460 mask
= AC_TID_MASK_TOP
;
462 mask
= AC_TID_MASK_TOP_LEFT
;
464 /* for DDX we want to next X pixel, DDY next Y pixel. */
465 if (op
== nir_op_fddx_fine
||
466 op
== nir_op_fddx_coarse
||
472 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
477 * this takes an I,J coordinate pair,
478 * and works out the X and Y derivatives.
479 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
481 static LLVMValueRef
emit_ddxy_interp(
482 struct ac_nir_context
*ctx
,
483 LLVMValueRef interp_ij
)
485 LLVMValueRef result
[4], a
;
488 for (i
= 0; i
< 2; i
++) {
489 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
490 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
491 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
492 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
494 return ac_build_gather_values(&ctx
->ac
, result
, 4);
497 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
499 LLVMValueRef src
[4], result
= NULL
;
500 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
501 unsigned src_components
;
502 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
504 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
511 case nir_op_pack_half_2x16
:
514 case nir_op_unpack_half_2x16
:
517 case nir_op_cube_face_coord
:
518 case nir_op_cube_face_index
:
522 src_components
= num_components
;
525 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
526 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
534 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
535 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
538 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
541 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
544 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
547 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
548 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
549 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
552 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
553 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
554 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
557 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
560 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
563 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
566 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
569 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
570 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
571 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
572 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
573 ac_to_float_type(&ctx
->ac
, def_type
), result
);
574 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
575 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
578 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
579 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
580 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
583 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
586 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
589 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
592 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
593 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
594 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
597 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
598 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
602 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
605 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
608 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
611 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
612 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
613 LLVMTypeOf(src
[0]), ""),
617 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
618 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
619 LLVMTypeOf(src
[0]), ""),
623 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
624 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
625 LLVMTypeOf(src
[0]), ""),
629 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
632 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
635 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
638 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
641 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
644 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
647 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
650 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
653 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
656 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
659 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
660 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
663 result
= emit_iabs(&ctx
->ac
, src
[0]);
666 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
669 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
672 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
675 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
678 result
= ac_build_isign(&ctx
->ac
, src
[0],
679 instr
->dest
.dest
.ssa
.bit_size
);
682 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
683 result
= ac_build_fsign(&ctx
->ac
, src
[0],
684 instr
->dest
.dest
.ssa
.bit_size
);
687 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
688 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
691 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
692 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
695 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
696 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
698 case nir_op_fround_even
:
699 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
700 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
703 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
704 result
= ac_build_fract(&ctx
->ac
, src
[0],
705 instr
->dest
.dest
.ssa
.bit_size
);
708 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
709 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
712 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
713 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
716 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
717 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
720 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
721 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
724 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
725 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
728 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
729 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
730 result
= ac_build_fdiv(&ctx
->ac
, instr
->dest
.dest
.ssa
.bit_size
== 32 ? ctx
->ac
.f32_1
: ctx
->ac
.f64_1
,
733 case nir_op_frexp_exp
:
734 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
735 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
736 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
739 case nir_op_frexp_sig
:
740 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
741 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
742 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
745 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
746 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
747 if (ctx
->ac
.chip_class
< GFX9
&&
748 instr
->dest
.dest
.ssa
.bit_size
== 32) {
749 /* Only pre-GFX9 chips do not flush denorms. */
750 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
751 ac_to_float_type(&ctx
->ac
, def_type
),
756 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
757 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
758 if (ctx
->ac
.chip_class
< GFX9
&&
759 instr
->dest
.dest
.ssa
.bit_size
== 32) {
760 /* Only pre-GFX9 chips do not flush denorms. */
761 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
762 ac_to_float_type(&ctx
->ac
, def_type
),
767 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
768 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
771 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
772 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
773 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
775 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
777 case nir_op_ibitfield_extract
:
778 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
780 case nir_op_ubitfield_extract
:
781 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
783 case nir_op_bitfield_insert
:
784 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
786 case nir_op_bitfield_reverse
:
787 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.bitreverse.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
789 case nir_op_bit_count
:
790 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 32)
791 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32", ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
793 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i64", ctx
->ac
.i64
, src
, 1, AC_FUNC_ATTR_READNONE
);
794 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
800 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
801 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
802 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
806 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
807 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
811 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
812 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
816 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
817 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
821 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
822 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
825 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
826 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
829 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
830 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
834 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
835 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
836 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
838 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
842 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
843 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
844 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
846 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
849 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
851 case nir_op_find_lsb
:
852 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
853 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
855 case nir_op_ufind_msb
:
856 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
857 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
859 case nir_op_ifind_msb
:
860 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
861 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
863 case nir_op_uadd_carry
:
864 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
865 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
866 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
868 case nir_op_usub_borrow
:
869 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
870 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
871 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
874 result
= emit_b2f(&ctx
->ac
, src
[0]);
877 result
= emit_f2b(&ctx
->ac
, src
[0]);
880 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
883 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
884 result
= emit_i2b(&ctx
->ac
, src
[0]);
886 case nir_op_fquantize2f16
:
887 result
= emit_f2f16(&ctx
->ac
, src
[0]);
889 case nir_op_umul_high
:
890 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
891 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
892 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
894 case nir_op_imul_high
:
895 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
896 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
897 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
899 case nir_op_pack_half_2x16
:
900 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
902 case nir_op_unpack_half_2x16
:
903 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
907 case nir_op_fddx_fine
:
908 case nir_op_fddy_fine
:
909 case nir_op_fddx_coarse
:
910 case nir_op_fddy_coarse
:
911 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
914 case nir_op_unpack_64_2x32_split_x
: {
915 assert(ac_get_llvm_num_components(src
[0]) == 1);
916 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
919 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
924 case nir_op_unpack_64_2x32_split_y
: {
925 assert(ac_get_llvm_num_components(src
[0]) == 1);
926 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
929 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
934 case nir_op_pack_64_2x32_split
: {
935 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
936 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
937 src
[0], ctx
->ac
.i32_0
, "");
938 tmp
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp
,
939 src
[1], ctx
->ac
.i32_1
, "");
940 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
944 case nir_op_cube_face_coord
: {
945 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
946 LLVMValueRef results
[2];
948 for (unsigned chan
= 0; chan
< 3; chan
++)
949 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
950 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
951 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
952 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
953 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
954 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
958 case nir_op_cube_face_index
: {
959 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
961 for (unsigned chan
= 0; chan
< 3; chan
++)
962 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
963 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
964 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
969 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
970 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
971 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
972 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
975 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
976 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
979 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
980 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, result
, src
[2]);
983 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
984 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
985 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
986 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
989 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
990 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
993 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
994 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
997 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
998 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
999 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1000 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1001 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1002 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1003 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1004 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1007 case nir_op_imed3
: {
1008 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1009 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1010 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1011 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1014 case nir_op_umed3
: {
1015 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1016 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1017 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1018 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1023 fprintf(stderr
, "Unknown NIR alu instr: ");
1024 nir_print_instr(&instr
->instr
, stderr
);
1025 fprintf(stderr
, "\n");
1030 assert(instr
->dest
.dest
.is_ssa
);
1031 result
= ac_to_integer(&ctx
->ac
, result
);
1032 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1036 static void visit_load_const(struct ac_nir_context
*ctx
,
1037 const nir_load_const_instr
*instr
)
1039 LLVMValueRef values
[4], value
= NULL
;
1040 LLVMTypeRef element_type
=
1041 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1043 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1044 switch (instr
->def
.bit_size
) {
1046 values
[i
] = LLVMConstInt(element_type
,
1047 instr
->value
.u32
[i
], false);
1050 values
[i
] = LLVMConstInt(element_type
,
1051 instr
->value
.u64
[i
], false);
1055 "unsupported nir load_const bit_size: %d\n",
1056 instr
->def
.bit_size
);
1060 if (instr
->def
.num_components
> 1) {
1061 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1065 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1069 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1072 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1073 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1076 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1077 /* On VI, the descriptor contains the size in bytes,
1078 * but TXQ must return the size in elements.
1079 * The stride is always non-zero for resources using TXQ.
1081 LLVMValueRef stride
=
1082 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1084 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1085 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1086 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1087 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1089 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1095 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
1098 static void build_int_type_name(
1100 char *buf
, unsigned bufsize
)
1102 assert(bufsize
>= 6);
1104 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
1105 snprintf(buf
, bufsize
, "v%ui32",
1106 LLVMGetVectorSize(type
));
1111 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1112 struct ac_image_args
*args
,
1113 const nir_tex_instr
*instr
)
1115 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1116 LLVMValueRef coord
= args
->addr
;
1117 LLVMValueRef half_texel
[2];
1118 LLVMValueRef compare_cube_wa
= NULL
;
1119 LLVMValueRef result
;
1121 unsigned coord_vgpr_index
= (unsigned)args
->offset
+ (unsigned)args
->compare
;
1125 struct ac_image_args txq_args
= { 0 };
1127 txq_args
.da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
1128 txq_args
.opcode
= ac_image_get_resinfo
;
1129 txq_args
.dmask
= 0xf;
1130 txq_args
.addr
= ctx
->i32_0
;
1131 txq_args
.resource
= args
->resource
;
1132 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1134 for (c
= 0; c
< 2; c
++) {
1135 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1136 LLVMConstInt(ctx
->i32
, c
, false), "");
1137 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1138 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1139 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1140 LLVMConstReal(ctx
->f32
, -0.5), "");
1144 LLVMValueRef orig_coords
= args
->addr
;
1146 for (c
= 0; c
< 2; c
++) {
1148 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1149 tmp
= LLVMBuildExtractElement(ctx
->builder
, coord
, index
, "");
1150 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1151 tmp
= LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1152 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1153 coord
= LLVMBuildInsertElement(ctx
->builder
, coord
, tmp
, index
, "");
1158 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1159 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1160 * workaround by sampling using a scaled type and converting.
1161 * This is taken from amdgpu-pro shaders.
1163 /* NOTE this produces some ugly code compared to amdgpu-pro,
1164 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1165 * and then reads them back. -pro generates two selects,
1166 * one s_cmp for the descriptor rewriting
1167 * one v_cmp for the coordinate and result changes.
1169 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1170 LLVMValueRef tmp
, tmp2
;
1172 /* workaround 8/8/8/8 uint/sint cube gather bug */
1173 /* first detect it then change to a scaled read and f2i */
1174 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1177 /* extract the DATA_FORMAT */
1178 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1179 LLVMConstInt(ctx
->i32
, 6, false), false);
1181 /* is the DATA_FORMAT == 8_8_8_8 */
1182 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1184 if (stype
== GLSL_TYPE_UINT
)
1185 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1186 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1187 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1189 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1190 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1191 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1193 /* replace the NUM FORMAT in the descriptor */
1194 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1195 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1197 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1199 /* don't modify the coordinates for this case */
1200 coord
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, orig_coords
, coord
, "");
1203 result
= ac_build_image_opcode(ctx
, args
);
1205 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1206 LLVMValueRef tmp
, tmp2
;
1208 /* if the cube workaround is in place, f2i the result. */
1209 for (c
= 0; c
< 4; c
++) {
1210 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1211 if (stype
== GLSL_TYPE_UINT
)
1212 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1214 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1215 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1216 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1217 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1218 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1219 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1225 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1226 const nir_tex_instr
*instr
,
1228 struct ac_image_args
*args
)
1230 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1231 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1233 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1234 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1238 util_last_bit(mask
),
1241 return ac_build_buffer_load_format(&ctx
->ac
,
1245 util_last_bit(mask
),
1250 args
->opcode
= ac_image_sample
;
1251 args
->compare
= instr
->is_shadow
;
1253 switch (instr
->op
) {
1255 case nir_texop_txf_ms
:
1256 case nir_texop_samples_identical
:
1257 args
->opcode
= lod_is_zero
||
1258 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1259 ac_image_load
: ac_image_load_mip
;
1260 args
->compare
= false;
1261 args
->offset
= false;
1268 args
->level_zero
= true;
1273 case nir_texop_query_levels
:
1274 args
->opcode
= ac_image_get_resinfo
;
1277 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1278 args
->level_zero
= true;
1284 args
->opcode
= ac_image_gather4
;
1285 args
->level_zero
= true;
1288 args
->opcode
= ac_image_get_lod
;
1289 args
->compare
= false;
1290 args
->offset
= false;
1296 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1297 enum glsl_base_type stype
= glsl_get_sampler_result_type(instr
->texture
->var
->type
);
1298 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1299 return lower_gather4_integer(&ctx
->ac
, args
, instr
);
1302 return ac_build_image_opcode(&ctx
->ac
, args
);
1305 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1306 nir_intrinsic_instr
*instr
)
1308 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1309 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1311 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1312 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1316 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1317 nir_intrinsic_instr
*instr
)
1319 LLVMValueRef ptr
, addr
;
1321 addr
= LLVMConstInt(ctx
->ac
.i32
, nir_intrinsic_base(instr
), 0);
1322 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
,
1323 get_src(ctx
, instr
->src
[0]), "");
1325 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1326 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1328 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1331 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1332 const nir_intrinsic_instr
*instr
)
1334 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1336 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1339 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1341 uint32_t new_mask
= 0;
1342 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1343 if (mask
& (1u << i
))
1344 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1348 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1349 unsigned start
, unsigned count
)
1351 LLVMTypeRef type
= LLVMTypeOf(src
);
1353 if (LLVMGetTypeKind(type
) != LLVMVectorTypeKind
) {
1359 unsigned src_elements
= LLVMGetVectorSize(type
);
1360 assert(start
< src_elements
);
1361 assert(start
+ count
<= src_elements
);
1363 if (start
== 0 && count
== src_elements
)
1367 return LLVMBuildExtractElement(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, start
, false), "");
1370 LLVMValueRef indices
[8];
1371 for (unsigned i
= 0; i
< count
; ++i
)
1372 indices
[i
] = LLVMConstInt(ctx
->i32
, start
+ i
, false);
1374 LLVMValueRef swizzle
= LLVMConstVector(indices
, count
);
1375 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1378 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1379 nir_intrinsic_instr
*instr
)
1381 const char *store_name
;
1382 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1383 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1384 int elem_size_mult
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 32;
1385 int components_32bit
= elem_size_mult
* instr
->num_components
;
1386 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1387 LLVMValueRef base_data
, base_offset
;
1388 LLVMValueRef params
[6];
1390 params
[1] = ctx
->abi
->load_ssbo(ctx
->abi
,
1391 get_src(ctx
, instr
->src
[1]), true);
1392 params
[2] = ctx
->ac
.i32_0
; /* vindex */
1393 params
[4] = ctx
->ac
.i1false
; /* glc */
1394 params
[5] = ctx
->ac
.i1false
; /* slc */
1396 if (components_32bit
> 1)
1397 data_type
= LLVMVectorType(ctx
->ac
.f32
, components_32bit
);
1399 writemask
= widen_mask(writemask
, elem_size_mult
);
1401 base_data
= ac_to_float(&ctx
->ac
, src_data
);
1402 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1403 base_data
= LLVMBuildBitCast(ctx
->ac
.builder
, base_data
,
1405 base_offset
= get_src(ctx
, instr
->src
[2]); /* voffset */
1409 LLVMValueRef offset
;
1411 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1413 /* Due to an LLVM limitation, split 3-element writes
1414 * into a 2-element and a 1-element write. */
1416 writemask
|= 1 << (start
+ 2);
1421 writemask
|= ((1u << (count
- 4)) - 1u) << (start
+ 4);
1426 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1427 } else if (count
== 2) {
1428 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1432 store_name
= "llvm.amdgcn.buffer.store.f32";
1434 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1436 offset
= base_offset
;
1438 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, LLVMConstInt(ctx
->ac
.i32
, start
* 4, false), "");
1442 ac_build_intrinsic(&ctx
->ac
, store_name
,
1443 ctx
->ac
.voidt
, params
, 6, 0);
1447 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1448 const nir_intrinsic_instr
*instr
)
1451 LLVMValueRef params
[6];
1454 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1455 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1457 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1458 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1459 get_src(ctx
, instr
->src
[0]),
1461 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1462 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1463 params
[arg_count
++] = LLVMConstInt(ctx
->ac
.i1
, 0, false); /* slc */
1465 switch (instr
->intrinsic
) {
1466 case nir_intrinsic_ssbo_atomic_add
:
1467 name
= "llvm.amdgcn.buffer.atomic.add";
1469 case nir_intrinsic_ssbo_atomic_imin
:
1470 name
= "llvm.amdgcn.buffer.atomic.smin";
1472 case nir_intrinsic_ssbo_atomic_umin
:
1473 name
= "llvm.amdgcn.buffer.atomic.umin";
1475 case nir_intrinsic_ssbo_atomic_imax
:
1476 name
= "llvm.amdgcn.buffer.atomic.smax";
1478 case nir_intrinsic_ssbo_atomic_umax
:
1479 name
= "llvm.amdgcn.buffer.atomic.umax";
1481 case nir_intrinsic_ssbo_atomic_and
:
1482 name
= "llvm.amdgcn.buffer.atomic.and";
1484 case nir_intrinsic_ssbo_atomic_or
:
1485 name
= "llvm.amdgcn.buffer.atomic.or";
1487 case nir_intrinsic_ssbo_atomic_xor
:
1488 name
= "llvm.amdgcn.buffer.atomic.xor";
1490 case nir_intrinsic_ssbo_atomic_exchange
:
1491 name
= "llvm.amdgcn.buffer.atomic.swap";
1493 case nir_intrinsic_ssbo_atomic_comp_swap
:
1494 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1500 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1503 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1504 const nir_intrinsic_instr
*instr
)
1506 LLVMValueRef results
[2];
1507 int load_components
;
1508 int num_components
= instr
->num_components
;
1509 if (instr
->dest
.ssa
.bit_size
== 64)
1510 num_components
*= 2;
1512 for (int i
= 0; i
< num_components
; i
+= load_components
) {
1513 load_components
= MIN2(num_components
- i
, 4);
1514 const char *load_name
;
1515 LLVMTypeRef data_type
= ctx
->ac
.f32
;
1516 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* 4, false);
1517 offset
= LLVMBuildAdd(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]), offset
, "");
1519 if (load_components
== 3)
1520 data_type
= LLVMVectorType(ctx
->ac
.f32
, 4);
1521 else if (load_components
> 1)
1522 data_type
= LLVMVectorType(ctx
->ac
.f32
, load_components
);
1524 if (load_components
>= 3)
1525 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1526 else if (load_components
== 2)
1527 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1528 else if (load_components
== 1)
1529 load_name
= "llvm.amdgcn.buffer.load.f32";
1531 unreachable("unhandled number of components");
1533 LLVMValueRef params
[] = {
1534 ctx
->abi
->load_ssbo(ctx
->abi
,
1535 get_src(ctx
, instr
->src
[0]),
1543 results
[i
> 0 ? 1 : 0] = ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1547 LLVMValueRef ret
= results
[0];
1548 if (num_components
> 4 || num_components
== 3) {
1549 LLVMValueRef masks
[] = {
1550 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1551 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1552 LLVMConstInt(ctx
->ac
.i32
, 4, false), LLVMConstInt(ctx
->ac
.i32
, 5, false),
1553 LLVMConstInt(ctx
->ac
.i32
, 6, false), LLVMConstInt(ctx
->ac
.i32
, 7, false)
1556 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
1557 ret
= LLVMBuildShuffleVector(ctx
->ac
.builder
, results
[0],
1558 results
[num_components
> 4 ? 1 : 0], swizzle
, "");
1561 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1562 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1565 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1566 const nir_intrinsic_instr
*instr
)
1569 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1570 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1571 int num_components
= instr
->num_components
;
1573 if (ctx
->abi
->load_ubo
)
1574 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1576 if (instr
->dest
.ssa
.bit_size
== 64)
1577 num_components
*= 2;
1579 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1580 NULL
, 0, false, false, true, true);
1581 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1582 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1583 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1587 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_var
*deref
,
1588 bool vs_in
, unsigned *vertex_index_out
,
1589 LLVMValueRef
*vertex_index_ref
,
1590 unsigned *const_out
, LLVMValueRef
*indir_out
)
1592 unsigned const_offset
= 0;
1593 nir_deref
*tail
= &deref
->deref
;
1594 LLVMValueRef offset
= NULL
;
1596 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1598 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1599 if (vertex_index_out
)
1600 *vertex_index_out
= deref_array
->base_offset
;
1602 if (vertex_index_ref
) {
1603 LLVMValueRef vtx
= LLVMConstInt(ctx
->ac
.i32
, deref_array
->base_offset
, false);
1604 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1605 vtx
= LLVMBuildAdd(ctx
->ac
.builder
, vtx
, get_src(ctx
, deref_array
->indirect
), "");
1607 *vertex_index_ref
= vtx
;
1611 if (deref
->var
->data
.compact
) {
1612 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1613 assert(glsl_type_is_scalar(glsl_without_array(deref
->var
->type
)));
1614 nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1615 /* We always lower indirect dereferences for "compact" array vars. */
1616 assert(deref_array
->deref_array_type
== nir_deref_array_type_direct
);
1618 const_offset
= deref_array
->base_offset
;
1622 while (tail
->child
!= NULL
) {
1623 const struct glsl_type
*parent_type
= tail
->type
;
1626 if (tail
->deref_type
== nir_deref_type_array
) {
1627 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
1628 LLVMValueRef index
, stride
, local_offset
;
1629 unsigned size
= glsl_count_attribute_slots(tail
->type
, vs_in
);
1631 const_offset
+= size
* deref_array
->base_offset
;
1632 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
)
1635 assert(deref_array
->deref_array_type
== nir_deref_array_type_indirect
);
1636 index
= get_src(ctx
, deref_array
->indirect
);
1637 stride
= LLVMConstInt(ctx
->ac
.i32
, size
, 0);
1638 local_offset
= LLVMBuildMul(ctx
->ac
.builder
, stride
, index
, "");
1641 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, local_offset
, "");
1643 offset
= local_offset
;
1644 } else if (tail
->deref_type
== nir_deref_type_struct
) {
1645 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
1647 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
1648 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1649 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1652 unreachable("unsupported deref type");
1656 if (const_offset
&& offset
)
1657 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1658 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1661 *const_out
= const_offset
;
1662 *indir_out
= offset
;
1666 build_gep_for_deref(struct ac_nir_context
*ctx
,
1667 nir_deref_var
*deref
)
1669 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1670 assert(entry
->data
);
1671 LLVMValueRef val
= entry
->data
;
1672 nir_deref
*tail
= deref
->deref
.child
;
1673 while (tail
!= NULL
) {
1674 LLVMValueRef offset
;
1675 switch (tail
->deref_type
) {
1676 case nir_deref_type_array
: {
1677 nir_deref_array
*array
= nir_deref_as_array(tail
);
1678 offset
= LLVMConstInt(ctx
->ac
.i32
, array
->base_offset
, 0);
1679 if (array
->deref_array_type
==
1680 nir_deref_array_type_indirect
) {
1681 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1688 case nir_deref_type_struct
: {
1689 nir_deref_struct
*deref_struct
=
1690 nir_deref_as_struct(tail
);
1691 offset
= LLVMConstInt(ctx
->ac
.i32
,
1692 deref_struct
->index
, 0);
1696 unreachable("bad deref type");
1698 val
= ac_build_gep0(&ctx
->ac
, val
, offset
);
1704 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1705 nir_intrinsic_instr
*instr
,
1708 LLVMValueRef result
;
1709 LLVMValueRef vertex_index
= NULL
;
1710 LLVMValueRef indir_index
= NULL
;
1711 unsigned const_index
= 0;
1712 unsigned location
= instr
->variables
[0]->var
->data
.location
;
1713 unsigned driver_location
= instr
->variables
[0]->var
->data
.driver_location
;
1714 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1715 const bool is_compact
= instr
->variables
[0]->var
->data
.compact
;
1717 get_deref_offset(ctx
, instr
->variables
[0],
1718 false, NULL
, is_patch
? NULL
: &vertex_index
,
1719 &const_index
, &indir_index
);
1721 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1723 LLVMTypeRef src_component_type
;
1724 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1725 src_component_type
= LLVMGetElementType(dest_type
);
1727 src_component_type
= dest_type
;
1729 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1730 vertex_index
, indir_index
,
1731 const_index
, location
, driver_location
,
1732 instr
->variables
[0]->var
->data
.location_frac
,
1733 instr
->num_components
,
1734 is_patch
, is_compact
, load_inputs
);
1735 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1738 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1739 nir_intrinsic_instr
*instr
)
1741 LLVMValueRef values
[8];
1742 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1743 int ve
= instr
->dest
.ssa
.num_components
;
1744 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1745 LLVMValueRef indir_index
;
1747 unsigned const_index
;
1748 unsigned stride
= instr
->variables
[0]->var
->data
.compact
? 1 : 4;
1749 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1750 instr
->variables
[0]->var
->data
.mode
== nir_var_shader_in
;
1751 get_deref_offset(ctx
, instr
->variables
[0], vs_in
, NULL
, NULL
,
1752 &const_index
, &indir_index
);
1754 if (instr
->dest
.ssa
.bit_size
== 64)
1757 switch (instr
->variables
[0]->var
->data
.mode
) {
1758 case nir_var_shader_in
:
1759 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1760 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1761 return load_tess_varyings(ctx
, instr
, true);
1764 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1765 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1766 LLVMValueRef indir_index
;
1767 unsigned const_index
, vertex_index
;
1768 get_deref_offset(ctx
, instr
->variables
[0],
1769 false, &vertex_index
, NULL
,
1770 &const_index
, &indir_index
);
1772 return ctx
->abi
->load_inputs(ctx
->abi
, instr
->variables
[0]->var
->data
.location
,
1773 instr
->variables
[0]->var
->data
.driver_location
,
1774 instr
->variables
[0]->var
->data
.location_frac
,
1775 instr
->num_components
, vertex_index
, const_index
, type
);
1778 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1780 unsigned count
= glsl_count_attribute_slots(
1781 instr
->variables
[0]->var
->type
,
1782 ctx
->stage
== MESA_SHADER_VERTEX
);
1784 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1785 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1786 stride
, false, true);
1788 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1792 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1796 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1798 unsigned count
= glsl_count_attribute_slots(
1799 instr
->variables
[0]->var
->type
, false);
1801 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1802 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1803 stride
, true, true);
1805 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1809 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
1813 case nir_var_shared
: {
1814 LLVMValueRef address
= build_gep_for_deref(ctx
,
1815 instr
->variables
[0]);
1816 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
1817 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
1818 get_def_type(ctx
, &instr
->dest
.ssa
),
1821 case nir_var_shader_out
:
1822 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1823 return load_tess_varyings(ctx
, instr
, false);
1826 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1828 unsigned count
= glsl_count_attribute_slots(
1829 instr
->variables
[0]->var
->type
, false);
1831 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1832 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1833 stride
, true, true);
1835 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1839 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
1840 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
1846 unreachable("unhandle variable mode");
1848 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
1849 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1853 visit_store_var(struct ac_nir_context
*ctx
,
1854 nir_intrinsic_instr
*instr
)
1856 LLVMValueRef temp_ptr
, value
;
1857 int idx
= instr
->variables
[0]->var
->data
.driver_location
;
1858 unsigned comp
= instr
->variables
[0]->var
->data
.location_frac
;
1859 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
1860 int writemask
= instr
->const_index
[0];
1861 LLVMValueRef indir_index
;
1862 unsigned const_index
;
1863 get_deref_offset(ctx
, instr
->variables
[0], false,
1864 NULL
, NULL
, &const_index
, &indir_index
);
1866 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64) {
1868 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
1869 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
1872 writemask
= widen_mask(writemask
, 2);
1875 writemask
= writemask
<< comp
;
1877 switch (instr
->variables
[0]->var
->data
.mode
) {
1878 case nir_var_shader_out
:
1880 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
1881 LLVMValueRef vertex_index
= NULL
;
1882 LLVMValueRef indir_index
= NULL
;
1883 unsigned const_index
= 0;
1884 const bool is_patch
= instr
->variables
[0]->var
->data
.patch
;
1886 get_deref_offset(ctx
, instr
->variables
[0],
1887 false, NULL
, is_patch
? NULL
: &vertex_index
,
1888 &const_index
, &indir_index
);
1890 ctx
->abi
->store_tcs_outputs(ctx
->abi
, instr
->variables
[0]->var
,
1891 vertex_index
, indir_index
,
1892 const_index
, src
, writemask
);
1896 for (unsigned chan
= 0; chan
< 8; chan
++) {
1898 if (!(writemask
& (1 << chan
)))
1901 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
1903 if (instr
->variables
[0]->var
->data
.compact
)
1906 unsigned count
= glsl_count_attribute_slots(
1907 instr
->variables
[0]->var
->type
, false);
1909 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1910 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
1911 stride
, true, true);
1913 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1914 value
, indir_index
, "");
1915 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
1916 count
, stride
, tmp_vec
);
1919 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
1921 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1926 for (unsigned chan
= 0; chan
< 8; chan
++) {
1927 if (!(writemask
& (1 << chan
)))
1930 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
1932 unsigned count
= glsl_count_attribute_slots(
1933 instr
->variables
[0]->var
->type
, false);
1935 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1936 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1939 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
1940 value
, indir_index
, "");
1941 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
1944 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
1946 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
1950 case nir_var_shared
: {
1951 int writemask
= instr
->const_index
[0];
1952 LLVMValueRef address
= build_gep_for_deref(ctx
,
1953 instr
->variables
[0]);
1954 LLVMValueRef val
= get_src(ctx
, instr
->src
[0]);
1955 unsigned components
=
1956 glsl_get_vector_elements(
1957 nir_deref_tail(&instr
->variables
[0]->deref
)->type
);
1958 if (writemask
== (1 << components
) - 1) {
1959 val
= LLVMBuildBitCast(
1960 ctx
->ac
.builder
, val
,
1961 LLVMGetElementType(LLVMTypeOf(address
)), "");
1962 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
1964 for (unsigned chan
= 0; chan
< 4; chan
++) {
1965 if (!(writemask
& (1 << chan
)))
1968 LLVMBuildStructGEP(ctx
->ac
.builder
,
1970 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
1972 src
= LLVMBuildBitCast(
1973 ctx
->ac
.builder
, src
,
1974 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
1975 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
1985 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
1988 case GLSL_SAMPLER_DIM_BUF
:
1990 case GLSL_SAMPLER_DIM_1D
:
1991 return array
? 2 : 1;
1992 case GLSL_SAMPLER_DIM_2D
:
1993 return array
? 3 : 2;
1994 case GLSL_SAMPLER_DIM_MS
:
1995 return array
? 4 : 3;
1996 case GLSL_SAMPLER_DIM_3D
:
1997 case GLSL_SAMPLER_DIM_CUBE
:
1999 case GLSL_SAMPLER_DIM_RECT
:
2000 case GLSL_SAMPLER_DIM_SUBPASS
:
2002 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2011 glsl_is_array_image(const struct glsl_type
*type
)
2013 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2015 if (glsl_sampler_type_is_array(type
))
2018 return dim
== GLSL_SAMPLER_DIM_CUBE
||
2019 dim
== GLSL_SAMPLER_DIM_3D
||
2020 dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2021 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
;
2025 /* Adjust the sample index according to FMASK.
2027 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2028 * which is the identity mapping. Each nibble says which physical sample
2029 * should be fetched to get that sample.
2031 * For example, 0x11111100 means there are only 2 samples stored and
2032 * the second sample covers 3/4 of the pixel. When reading samples 0
2033 * and 1, return physical sample 0 (determined by the first two 0s
2034 * in FMASK), otherwise return physical sample 1.
2036 * The sample index should be adjusted as follows:
2037 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2039 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2040 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2041 LLVMValueRef coord_z
,
2042 LLVMValueRef sample_index
,
2043 LLVMValueRef fmask_desc_ptr
)
2045 LLVMValueRef fmask_load_address
[4];
2048 fmask_load_address
[0] = coord_x
;
2049 fmask_load_address
[1] = coord_y
;
2051 fmask_load_address
[2] = coord_z
;
2052 fmask_load_address
[3] = LLVMGetUndef(ctx
->i32
);
2055 struct ac_image_args args
= {0};
2057 args
.opcode
= ac_image_load
;
2058 args
.da
= coord_z
? true : false;
2059 args
.resource
= fmask_desc_ptr
;
2061 args
.addr
= ac_build_gather_values(ctx
, fmask_load_address
, coord_z
? 4 : 2);
2063 res
= ac_build_image_opcode(ctx
, &args
);
2065 res
= ac_to_integer(ctx
, res
);
2066 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2067 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2069 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2073 LLVMValueRef sample_index4
=
2074 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2075 LLVMValueRef shifted_fmask
=
2076 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2077 LLVMValueRef final_sample
=
2078 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2080 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2081 * resource descriptor is 0 (invalid),
2083 LLVMValueRef fmask_desc
=
2084 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2087 LLVMValueRef fmask_word1
=
2088 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2091 LLVMValueRef word1_is_nonzero
=
2092 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2093 fmask_word1
, ctx
->i32_0
, "");
2095 /* Replace the MSAA sample index. */
2097 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2098 final_sample
, sample_index
, "");
2099 return sample_index
;
2102 static LLVMValueRef
get_image_coords(struct ac_nir_context
*ctx
,
2103 const nir_intrinsic_instr
*instr
)
2105 const struct glsl_type
*type
= glsl_without_array(instr
->variables
[0]->var
->type
);
2107 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
2108 LLVMValueRef coords
[4];
2109 LLVMValueRef masks
[] = {
2110 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2111 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2114 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[1]), 0);
2117 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2118 bool is_array
= glsl_sampler_type_is_array(type
);
2119 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2120 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2121 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2122 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2123 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2124 count
= image_type_to_components_count(dim
, is_array
);
2127 LLVMValueRef fmask_load_address
[3];
2130 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2131 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2133 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2135 fmask_load_address
[2] = NULL
;
2137 for (chan
= 0; chan
< 2; ++chan
)
2138 fmask_load_address
[chan
] =
2139 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2140 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2141 ctx
->ac
.i32
, ""), "");
2142 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2144 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2145 fmask_load_address
[0],
2146 fmask_load_address
[1],
2147 fmask_load_address
[2],
2149 get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_FMASK
, NULL
, true, false));
2151 if (count
== 1 && !gfx9_1d
) {
2152 if (instr
->src
[0].ssa
->num_components
)
2153 res
= LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2160 for (chan
= 0; chan
< count
; ++chan
) {
2161 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2164 for (chan
= 0; chan
< 2; ++chan
)
2165 coords
[chan
] = LLVMBuildAdd(ctx
->ac
.builder
, coords
[chan
], LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2166 ctx
->ac
.i32
, ""), "");
2167 coords
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2173 coords
[2] = coords
[1];
2174 coords
[1] = ctx
->ac
.i32_0
;
2176 coords
[1] = ctx
->ac
.i32_0
;
2181 coords
[count
] = sample_index
;
2186 coords
[3] = LLVMGetUndef(ctx
->ac
.i32
);
2189 res
= ac_build_gather_values(&ctx
->ac
, coords
, count
);
2194 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2195 const nir_intrinsic_instr
*instr
)
2197 LLVMValueRef params
[7];
2199 char intrinsic_name
[64];
2200 const nir_variable
*var
= instr
->variables
[0]->var
;
2201 const struct glsl_type
*type
= var
->type
;
2203 if(instr
->variables
[0]->deref
.child
)
2204 type
= instr
->variables
[0]->deref
.child
->type
;
2206 type
= glsl_without_array(type
);
2208 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2209 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2210 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2211 unsigned num_channels
= util_last_bit(mask
);
2212 LLVMValueRef rsrc
, vindex
;
2214 rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, false);
2215 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2218 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2219 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2220 ctx
->ac
.i32_0
, num_channels
,
2222 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2224 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2225 res
= ac_to_integer(&ctx
->ac
, res
);
2227 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2228 LLVMValueRef slc
= ctx
->ac
.i1false
;
2230 params
[0] = get_image_coords(ctx
, instr
);
2231 params
[1] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2232 params
[2] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2233 params
[3] = (var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2234 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2236 params
[5] = ctx
->ac
.i1false
;
2239 ac_get_image_intr_name("llvm.amdgcn.image.load",
2240 ctx
->ac
.v4f32
, /* vdata */
2241 LLVMTypeOf(params
[0]), /* coords */
2242 LLVMTypeOf(params
[1]), /* rsrc */
2243 intrinsic_name
, sizeof(intrinsic_name
));
2245 res
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.v4f32
,
2246 params
, 7, AC_FUNC_ATTR_READONLY
);
2248 return ac_to_integer(&ctx
->ac
, res
);
2251 static void visit_image_store(struct ac_nir_context
*ctx
,
2252 nir_intrinsic_instr
*instr
)
2254 LLVMValueRef params
[8];
2255 char intrinsic_name
[64];
2256 const nir_variable
*var
= instr
->variables
[0]->var
;
2257 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2258 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2259 LLVMValueRef glc
= ctx
->ac
.i1false
;
2260 bool force_glc
= ctx
->ac
.chip_class
== SI
;
2262 glc
= ctx
->ac
.i1true
;
2264 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2265 LLVMValueRef rsrc
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
, NULL
, true, true);
2267 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2268 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2269 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2270 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2272 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2273 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2274 elem_count
, stride
, "");
2276 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2277 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2280 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2])); /* data */
2282 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2283 ctx
->ac
.i32_0
, ""); /* vindex */
2284 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2285 params
[4] = glc
; /* glc */
2286 params
[5] = ctx
->ac
.i1false
; /* slc */
2287 ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32", ctx
->ac
.voidt
,
2290 LLVMValueRef da
= glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2291 LLVMValueRef slc
= ctx
->ac
.i1false
;
2293 params
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[2]));
2294 params
[1] = get_image_coords(ctx
, instr
); /* coords */
2295 params
[2] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, true);
2296 params
[3] = LLVMConstInt(ctx
->ac
.i32
, 15, false); /* dmask */
2297 params
[4] = (force_glc
|| var
->data
.image
._volatile
|| var
->data
.image
.coherent
) ?
2298 ctx
->ac
.i1true
: ctx
->ac
.i1false
;
2300 params
[6] = ctx
->ac
.i1false
;
2303 ac_get_image_intr_name("llvm.amdgcn.image.store",
2304 LLVMTypeOf(params
[0]), /* vdata */
2305 LLVMTypeOf(params
[1]), /* coords */
2306 LLVMTypeOf(params
[2]), /* rsrc */
2307 intrinsic_name
, sizeof(intrinsic_name
));
2309 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.voidt
,
2315 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2316 const nir_intrinsic_instr
*instr
)
2318 LLVMValueRef params
[7];
2319 int param_count
= 0;
2320 const nir_variable
*var
= instr
->variables
[0]->var
;
2322 const char *atomic_name
;
2323 char intrinsic_name
[41];
2324 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2325 MAYBE_UNUSED
int length
;
2327 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2329 switch (instr
->intrinsic
) {
2330 case nir_intrinsic_image_var_atomic_add
:
2331 atomic_name
= "add";
2333 case nir_intrinsic_image_var_atomic_min
:
2334 atomic_name
= is_unsigned
? "umin" : "smin";
2336 case nir_intrinsic_image_var_atomic_max
:
2337 atomic_name
= is_unsigned
? "umax" : "smax";
2339 case nir_intrinsic_image_var_atomic_and
:
2340 atomic_name
= "and";
2342 case nir_intrinsic_image_var_atomic_or
:
2345 case nir_intrinsic_image_var_atomic_xor
:
2346 atomic_name
= "xor";
2348 case nir_intrinsic_image_var_atomic_exchange
:
2349 atomic_name
= "swap";
2351 case nir_intrinsic_image_var_atomic_comp_swap
:
2352 atomic_name
= "cmpswap";
2358 if (instr
->intrinsic
== nir_intrinsic_image_var_atomic_comp_swap
)
2359 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2360 params
[param_count
++] = get_src(ctx
, instr
->src
[2]);
2362 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2363 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_BUFFER
,
2365 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[0]),
2366 ctx
->ac
.i32_0
, ""); /* vindex */
2367 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2368 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2370 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2371 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2373 char coords_type
[8];
2375 LLVMValueRef coords
= params
[param_count
++] = get_image_coords(ctx
, instr
);
2376 params
[param_count
++] = get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
,
2378 params
[param_count
++] = ctx
->ac
.i1false
; /* r128 */
2379 params
[param_count
++] = glsl_is_array_image(type
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
; /* da */
2380 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2382 build_int_type_name(LLVMTypeOf(coords
),
2383 coords_type
, sizeof(coords_type
));
2385 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2386 "llvm.amdgcn.image.atomic.%s.%s", atomic_name
, coords_type
);
2389 assert(length
< sizeof(intrinsic_name
));
2390 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
, params
, param_count
, 0);
2393 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2394 const nir_intrinsic_instr
*instr
)
2396 const nir_variable
*var
= instr
->variables
[0]->var
;
2397 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2399 struct ac_image_args args
= { 0 };
2400 args
.da
= glsl_is_array_image(type
);
2402 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0],
2403 AC_DESC_IMAGE
, NULL
, true, false);
2404 args
.opcode
= ac_image_get_resinfo
;
2405 args
.addr
= ctx
->ac
.i32_0
;
2407 return ac_build_image_opcode(&ctx
->ac
, &args
);
2410 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2411 const nir_intrinsic_instr
*instr
)
2414 const nir_variable
*var
= instr
->variables
[0]->var
;
2415 const struct glsl_type
*type
= glsl_without_array(var
->type
);
2417 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2418 return get_buffer_size(ctx
,
2419 get_sampler_desc(ctx
, instr
->variables
[0],
2420 AC_DESC_BUFFER
, NULL
, true, false), true);
2422 struct ac_image_args args
= { 0 };
2424 args
.da
= glsl_is_array_image(type
);
2426 args
.resource
= get_sampler_desc(ctx
, instr
->variables
[0], AC_DESC_IMAGE
, NULL
, true, false);
2427 args
.opcode
= ac_image_get_resinfo
;
2428 args
.addr
= ctx
->ac
.i32_0
;
2430 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2432 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2434 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2435 glsl_sampler_type_is_array(type
)) {
2436 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2437 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2438 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2439 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2441 if (ctx
->ac
.chip_class
>= GFX9
&&
2442 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2443 glsl_sampler_type_is_array(type
)) {
2444 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2445 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2452 #define NOOP_WAITCNT 0xf7f
2453 #define LGKM_CNT 0x07f
2454 #define VM_CNT 0xf70
2456 static void emit_membar(struct ac_llvm_context
*ac
,
2457 const nir_intrinsic_instr
*instr
)
2459 unsigned waitcnt
= NOOP_WAITCNT
;
2461 switch (instr
->intrinsic
) {
2462 case nir_intrinsic_memory_barrier
:
2463 case nir_intrinsic_group_memory_barrier
:
2464 waitcnt
&= VM_CNT
& LGKM_CNT
;
2466 case nir_intrinsic_memory_barrier_atomic_counter
:
2467 case nir_intrinsic_memory_barrier_buffer
:
2468 case nir_intrinsic_memory_barrier_image
:
2471 case nir_intrinsic_memory_barrier_shared
:
2472 waitcnt
&= LGKM_CNT
;
2477 if (waitcnt
!= NOOP_WAITCNT
)
2478 ac_build_waitcnt(ac
, waitcnt
);
2481 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2483 /* SI only (thanks to a hw bug workaround):
2484 * The real barrier instruction isn’t needed, because an entire patch
2485 * always fits into a single wave.
2487 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2488 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2491 ac_build_intrinsic(ac
, "llvm.amdgcn.s.barrier",
2492 ac
->voidt
, NULL
, 0, AC_FUNC_ATTR_CONVERGENT
);
2495 static void emit_discard(struct ac_nir_context
*ctx
,
2496 const nir_intrinsic_instr
*instr
)
2500 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2501 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2502 get_src(ctx
, instr
->src
[0]),
2505 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2506 cond
= LLVMConstInt(ctx
->ac
.i1
, false, 0);
2509 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2513 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2515 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2516 "llvm.amdgcn.ps.live",
2517 ctx
->ac
.i1
, NULL
, 0,
2518 AC_FUNC_ATTR_READNONE
);
2519 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2520 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2524 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2526 LLVMValueRef result
;
2527 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2528 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2529 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2531 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2535 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2537 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2538 LLVMValueRef result
;
2539 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2540 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2541 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2543 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2548 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2550 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2551 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2552 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2554 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2559 visit_first_invocation(struct ac_nir_context
*ctx
)
2561 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2563 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2564 LLVMValueRef args
[] = {active_set
, LLVMConstInt(ctx
->ac
.i1
, 0, false)};
2565 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2567 ctx
->ac
.i64
, args
, 2,
2568 AC_FUNC_ATTR_NOUNWIND
|
2569 AC_FUNC_ATTR_READNONE
);
2571 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2575 visit_load_shared(struct ac_nir_context
*ctx
,
2576 const nir_intrinsic_instr
*instr
)
2578 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2580 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2582 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2583 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2584 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2585 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2588 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2589 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2593 visit_store_shared(struct ac_nir_context
*ctx
,
2594 const nir_intrinsic_instr
*instr
)
2596 LLVMValueRef derived_ptr
, data
,index
;
2597 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2599 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2600 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2602 int writemask
= nir_intrinsic_write_mask(instr
);
2603 for (int chan
= 0; chan
< 4; chan
++) {
2604 if (!(writemask
& (1 << chan
))) {
2607 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2608 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2609 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2610 LLVMBuildStore(builder
, data
, derived_ptr
);
2614 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2615 const nir_intrinsic_instr
*instr
,
2616 LLVMValueRef ptr
, int src_idx
)
2618 LLVMValueRef result
;
2619 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2621 if (instr
->intrinsic
== nir_intrinsic_var_atomic_comp_swap
||
2622 instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
) {
2623 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2624 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2626 LLVMAtomicOrderingSequentiallyConsistent
,
2627 LLVMAtomicOrderingSequentiallyConsistent
,
2630 LLVMAtomicRMWBinOp op
;
2631 switch (instr
->intrinsic
) {
2632 case nir_intrinsic_var_atomic_add
:
2633 case nir_intrinsic_shared_atomic_add
:
2634 op
= LLVMAtomicRMWBinOpAdd
;
2636 case nir_intrinsic_var_atomic_umin
:
2637 case nir_intrinsic_shared_atomic_umin
:
2638 op
= LLVMAtomicRMWBinOpUMin
;
2640 case nir_intrinsic_var_atomic_umax
:
2641 case nir_intrinsic_shared_atomic_umax
:
2642 op
= LLVMAtomicRMWBinOpUMax
;
2644 case nir_intrinsic_var_atomic_imin
:
2645 case nir_intrinsic_shared_atomic_imin
:
2646 op
= LLVMAtomicRMWBinOpMin
;
2648 case nir_intrinsic_var_atomic_imax
:
2649 case nir_intrinsic_shared_atomic_imax
:
2650 op
= LLVMAtomicRMWBinOpMax
;
2652 case nir_intrinsic_var_atomic_and
:
2653 case nir_intrinsic_shared_atomic_and
:
2654 op
= LLVMAtomicRMWBinOpAnd
;
2656 case nir_intrinsic_var_atomic_or
:
2657 case nir_intrinsic_shared_atomic_or
:
2658 op
= LLVMAtomicRMWBinOpOr
;
2660 case nir_intrinsic_var_atomic_xor
:
2661 case nir_intrinsic_shared_atomic_xor
:
2662 op
= LLVMAtomicRMWBinOpXor
;
2664 case nir_intrinsic_var_atomic_exchange
:
2665 case nir_intrinsic_shared_atomic_exchange
:
2666 op
= LLVMAtomicRMWBinOpXchg
;
2672 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2673 LLVMAtomicOrderingSequentiallyConsistent
,
2679 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2681 LLVMValueRef values
[2];
2682 LLVMValueRef pos
[2];
2684 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2685 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2687 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2688 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2689 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2692 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2693 const nir_intrinsic_instr
*instr
)
2695 LLVMValueRef result
[4];
2696 LLVMValueRef interp_param
, attr_number
;
2699 LLVMValueRef src_c0
= NULL
;
2700 LLVMValueRef src_c1
= NULL
;
2701 LLVMValueRef src0
= NULL
;
2702 int input_index
= instr
->variables
[0]->var
->data
.location
- VARYING_SLOT_VAR0
;
2703 switch (instr
->intrinsic
) {
2704 case nir_intrinsic_interp_var_at_centroid
:
2705 location
= INTERP_CENTROID
;
2707 case nir_intrinsic_interp_var_at_sample
:
2708 case nir_intrinsic_interp_var_at_offset
:
2709 location
= INTERP_CENTER
;
2710 src0
= get_src(ctx
, instr
->src
[0]);
2716 if (instr
->intrinsic
== nir_intrinsic_interp_var_at_offset
) {
2717 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2718 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2719 } else if (instr
->intrinsic
== nir_intrinsic_interp_var_at_sample
) {
2720 LLVMValueRef sample_position
;
2721 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2723 /* fetch sample ID */
2724 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2726 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2727 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2728 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2729 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2731 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, instr
->variables
[0]->var
->data
.interpolation
, location
);
2732 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_index
, false);
2734 if (location
== INTERP_CENTER
) {
2735 LLVMValueRef ij_out
[2];
2736 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2739 * take the I then J parameters, and the DDX/Y for it, and
2740 * calculate the IJ inputs for the interpolator.
2741 * temp1 = ddx * offset/sample.x + I;
2742 * interp_param.I = ddy * offset/sample.y + temp1;
2743 * temp1 = ddx * offset/sample.x + J;
2744 * interp_param.J = ddy * offset/sample.y + temp1;
2746 for (unsigned i
= 0; i
< 2; i
++) {
2747 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2748 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2749 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2750 ddxy_out
, ix_ll
, "");
2751 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2752 ddxy_out
, iy_ll
, "");
2753 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2754 interp_param
, ix_ll
, "");
2755 LLVMValueRef temp1
, temp2
;
2757 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2760 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, src_c0
, "");
2761 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
2763 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, src_c1
, "");
2764 temp2
= LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
2766 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2767 temp2
, ctx
->ac
.i32
, "");
2769 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
2773 for (chan
= 0; chan
< 4; chan
++) {
2774 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2777 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
2778 interp_param
, ctx
->ac
.v2f32
, "");
2779 LLVMValueRef i
= LLVMBuildExtractElement(
2780 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
2781 LLVMValueRef j
= LLVMBuildExtractElement(
2782 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
2784 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2785 llvm_chan
, attr_number
,
2786 ctx
->abi
->prim_mask
, i
, j
);
2788 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2789 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2790 llvm_chan
, attr_number
,
2791 ctx
->abi
->prim_mask
);
2794 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
2795 instr
->variables
[0]->var
->data
.location_frac
);
2798 static void visit_intrinsic(struct ac_nir_context
*ctx
,
2799 nir_intrinsic_instr
*instr
)
2801 LLVMValueRef result
= NULL
;
2803 switch (instr
->intrinsic
) {
2804 case nir_intrinsic_ballot
:
2805 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
2807 case nir_intrinsic_read_invocation
:
2808 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
2809 get_src(ctx
, instr
->src
[1]));
2811 case nir_intrinsic_read_first_invocation
:
2812 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
2814 case nir_intrinsic_load_subgroup_invocation
:
2815 result
= ac_get_thread_id(&ctx
->ac
);
2817 case nir_intrinsic_load_work_group_id
: {
2818 LLVMValueRef values
[3];
2820 for (int i
= 0; i
< 3; i
++) {
2821 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
2822 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
2825 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2828 case nir_intrinsic_load_base_vertex
: {
2829 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
2832 case nir_intrinsic_load_local_group_size
:
2833 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
2835 case nir_intrinsic_load_vertex_id
:
2836 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
2837 ctx
->abi
->base_vertex
, "");
2839 case nir_intrinsic_load_vertex_id_zero_base
: {
2840 result
= ctx
->abi
->vertex_id
;
2843 case nir_intrinsic_load_local_invocation_id
: {
2844 result
= ctx
->abi
->local_invocation_ids
;
2847 case nir_intrinsic_load_base_instance
:
2848 result
= ctx
->abi
->start_instance
;
2850 case nir_intrinsic_load_draw_id
:
2851 result
= ctx
->abi
->draw_id
;
2853 case nir_intrinsic_load_view_index
:
2854 result
= ctx
->abi
->view_index
;
2856 case nir_intrinsic_load_invocation_id
:
2857 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
2858 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
2860 result
= ctx
->abi
->gs_invocation_id
;
2862 case nir_intrinsic_load_primitive_id
:
2863 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2864 result
= ctx
->abi
->gs_prim_id
;
2865 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2866 result
= ctx
->abi
->tcs_patch_id
;
2867 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2868 result
= ctx
->abi
->tes_patch_id
;
2870 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
2872 case nir_intrinsic_load_sample_id
:
2873 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
2875 case nir_intrinsic_load_sample_pos
:
2876 result
= load_sample_pos(ctx
);
2878 case nir_intrinsic_load_sample_mask_in
:
2879 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
2881 case nir_intrinsic_load_frag_coord
: {
2882 LLVMValueRef values
[4] = {
2883 ctx
->abi
->frag_pos
[0],
2884 ctx
->abi
->frag_pos
[1],
2885 ctx
->abi
->frag_pos
[2],
2886 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
2888 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
2891 case nir_intrinsic_load_front_face
:
2892 result
= ctx
->abi
->front_face
;
2894 case nir_intrinsic_load_helper_invocation
:
2895 result
= visit_load_helper_invocation(ctx
);
2897 case nir_intrinsic_load_instance_id
:
2898 result
= ctx
->abi
->instance_id
;
2900 case nir_intrinsic_load_num_work_groups
:
2901 result
= ctx
->abi
->num_work_groups
;
2903 case nir_intrinsic_load_local_invocation_index
:
2904 result
= visit_load_local_invocation_index(ctx
);
2906 case nir_intrinsic_load_subgroup_id
:
2907 result
= visit_load_subgroup_id(ctx
);
2909 case nir_intrinsic_load_num_subgroups
:
2910 result
= visit_load_num_subgroups(ctx
);
2912 case nir_intrinsic_first_invocation
:
2913 result
= visit_first_invocation(ctx
);
2915 case nir_intrinsic_load_push_constant
:
2916 result
= visit_load_push_constant(ctx
, instr
);
2918 case nir_intrinsic_vulkan_resource_index
: {
2919 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
2920 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
2921 unsigned binding
= nir_intrinsic_binding(instr
);
2923 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
2927 case nir_intrinsic_vulkan_resource_reindex
:
2928 result
= visit_vulkan_resource_reindex(ctx
, instr
);
2930 case nir_intrinsic_store_ssbo
:
2931 visit_store_ssbo(ctx
, instr
);
2933 case nir_intrinsic_load_ssbo
:
2934 result
= visit_load_buffer(ctx
, instr
);
2936 case nir_intrinsic_ssbo_atomic_add
:
2937 case nir_intrinsic_ssbo_atomic_imin
:
2938 case nir_intrinsic_ssbo_atomic_umin
:
2939 case nir_intrinsic_ssbo_atomic_imax
:
2940 case nir_intrinsic_ssbo_atomic_umax
:
2941 case nir_intrinsic_ssbo_atomic_and
:
2942 case nir_intrinsic_ssbo_atomic_or
:
2943 case nir_intrinsic_ssbo_atomic_xor
:
2944 case nir_intrinsic_ssbo_atomic_exchange
:
2945 case nir_intrinsic_ssbo_atomic_comp_swap
:
2946 result
= visit_atomic_ssbo(ctx
, instr
);
2948 case nir_intrinsic_load_ubo
:
2949 result
= visit_load_ubo_buffer(ctx
, instr
);
2951 case nir_intrinsic_get_buffer_size
:
2952 result
= visit_get_buffer_size(ctx
, instr
);
2954 case nir_intrinsic_load_var
:
2955 result
= visit_load_var(ctx
, instr
);
2957 case nir_intrinsic_store_var
:
2958 visit_store_var(ctx
, instr
);
2960 case nir_intrinsic_load_shared
:
2961 result
= visit_load_shared(ctx
, instr
);
2963 case nir_intrinsic_store_shared
:
2964 visit_store_shared(ctx
, instr
);
2966 case nir_intrinsic_image_var_samples
:
2967 result
= visit_image_samples(ctx
, instr
);
2969 case nir_intrinsic_image_var_load
:
2970 result
= visit_image_load(ctx
, instr
);
2972 case nir_intrinsic_image_var_store
:
2973 visit_image_store(ctx
, instr
);
2975 case nir_intrinsic_image_var_atomic_add
:
2976 case nir_intrinsic_image_var_atomic_min
:
2977 case nir_intrinsic_image_var_atomic_max
:
2978 case nir_intrinsic_image_var_atomic_and
:
2979 case nir_intrinsic_image_var_atomic_or
:
2980 case nir_intrinsic_image_var_atomic_xor
:
2981 case nir_intrinsic_image_var_atomic_exchange
:
2982 case nir_intrinsic_image_var_atomic_comp_swap
:
2983 result
= visit_image_atomic(ctx
, instr
);
2985 case nir_intrinsic_image_var_size
:
2986 result
= visit_image_size(ctx
, instr
);
2988 case nir_intrinsic_shader_clock
:
2989 result
= ac_build_shader_clock(&ctx
->ac
);
2991 case nir_intrinsic_discard
:
2992 case nir_intrinsic_discard_if
:
2993 emit_discard(ctx
, instr
);
2995 case nir_intrinsic_memory_barrier
:
2996 case nir_intrinsic_group_memory_barrier
:
2997 case nir_intrinsic_memory_barrier_atomic_counter
:
2998 case nir_intrinsic_memory_barrier_buffer
:
2999 case nir_intrinsic_memory_barrier_image
:
3000 case nir_intrinsic_memory_barrier_shared
:
3001 emit_membar(&ctx
->ac
, instr
);
3003 case nir_intrinsic_barrier
:
3004 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3006 case nir_intrinsic_shared_atomic_add
:
3007 case nir_intrinsic_shared_atomic_imin
:
3008 case nir_intrinsic_shared_atomic_umin
:
3009 case nir_intrinsic_shared_atomic_imax
:
3010 case nir_intrinsic_shared_atomic_umax
:
3011 case nir_intrinsic_shared_atomic_and
:
3012 case nir_intrinsic_shared_atomic_or
:
3013 case nir_intrinsic_shared_atomic_xor
:
3014 case nir_intrinsic_shared_atomic_exchange
:
3015 case nir_intrinsic_shared_atomic_comp_swap
: {
3016 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3017 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3020 case nir_intrinsic_var_atomic_add
:
3021 case nir_intrinsic_var_atomic_imin
:
3022 case nir_intrinsic_var_atomic_umin
:
3023 case nir_intrinsic_var_atomic_imax
:
3024 case nir_intrinsic_var_atomic_umax
:
3025 case nir_intrinsic_var_atomic_and
:
3026 case nir_intrinsic_var_atomic_or
:
3027 case nir_intrinsic_var_atomic_xor
:
3028 case nir_intrinsic_var_atomic_exchange
:
3029 case nir_intrinsic_var_atomic_comp_swap
: {
3030 LLVMValueRef ptr
= build_gep_for_deref(ctx
, instr
->variables
[0]);
3031 result
= visit_var_atomic(ctx
, instr
, ptr
, 0);
3034 case nir_intrinsic_interp_var_at_centroid
:
3035 case nir_intrinsic_interp_var_at_sample
:
3036 case nir_intrinsic_interp_var_at_offset
:
3037 result
= visit_interp(ctx
, instr
);
3039 case nir_intrinsic_emit_vertex
:
3040 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3042 case nir_intrinsic_end_primitive
:
3043 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3045 case nir_intrinsic_load_tess_coord
:
3046 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3048 case nir_intrinsic_load_tess_level_outer
:
3049 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3051 case nir_intrinsic_load_tess_level_inner
:
3052 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3054 case nir_intrinsic_load_patch_vertices_in
:
3055 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3057 case nir_intrinsic_vote_all
: {
3058 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3059 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3062 case nir_intrinsic_vote_any
: {
3063 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3064 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3067 case nir_intrinsic_shuffle
:
3068 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3069 get_src(ctx
, instr
->src
[1]));
3071 case nir_intrinsic_reduce
:
3072 result
= ac_build_reduce(&ctx
->ac
,
3073 get_src(ctx
, instr
->src
[0]),
3074 instr
->const_index
[0],
3075 instr
->const_index
[1]);
3077 case nir_intrinsic_inclusive_scan
:
3078 result
= ac_build_inclusive_scan(&ctx
->ac
,
3079 get_src(ctx
, instr
->src
[0]),
3080 instr
->const_index
[0]);
3082 case nir_intrinsic_exclusive_scan
:
3083 result
= ac_build_exclusive_scan(&ctx
->ac
,
3084 get_src(ctx
, instr
->src
[0]),
3085 instr
->const_index
[0]);
3087 case nir_intrinsic_quad_broadcast
: {
3088 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3089 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3090 lane
, lane
, lane
, lane
);
3093 case nir_intrinsic_quad_swap_horizontal
:
3094 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3096 case nir_intrinsic_quad_swap_vertical
:
3097 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3099 case nir_intrinsic_quad_swap_diagonal
:
3100 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3103 fprintf(stderr
, "Unknown intrinsic: ");
3104 nir_print_instr(&instr
->instr
, stderr
);
3105 fprintf(stderr
, "\n");
3109 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3113 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3114 const nir_deref_var
*deref
,
3115 enum ac_descriptor_type desc_type
,
3116 const nir_tex_instr
*tex_instr
,
3117 bool image
, bool write
)
3119 LLVMValueRef index
= NULL
;
3120 unsigned constant_index
= 0;
3121 unsigned descriptor_set
;
3122 unsigned base_index
;
3123 bool bindless
= false;
3126 assert(tex_instr
&& !image
);
3128 base_index
= tex_instr
->sampler_index
;
3130 const nir_deref
*tail
= &deref
->deref
;
3131 while (tail
->child
) {
3132 const nir_deref_array
*child
= nir_deref_as_array(tail
->child
);
3133 unsigned array_size
= glsl_get_aoa_size(tail
->child
->type
);
3138 assert(child
->deref_array_type
!= nir_deref_array_type_wildcard
);
3140 if (child
->deref_array_type
== nir_deref_array_type_indirect
) {
3141 LLVMValueRef indirect
= get_src(ctx
, child
->indirect
);
3143 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3144 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3149 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3152 constant_index
+= child
->base_offset
* array_size
;
3154 tail
= &child
->deref
;
3156 descriptor_set
= deref
->var
->data
.descriptor_set
;
3158 if (deref
->var
->data
.bindless
) {
3159 bindless
= deref
->var
->data
.bindless
;
3160 base_index
= deref
->var
->data
.driver_location
;
3162 base_index
= deref
->var
->data
.binding
;
3166 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3169 constant_index
, index
,
3170 desc_type
, image
, write
, bindless
);
3173 static void set_tex_fetch_args(struct ac_llvm_context
*ctx
,
3174 struct ac_image_args
*args
,
3175 const nir_tex_instr
*instr
,
3177 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3178 LLVMValueRef
*param
, unsigned count
,
3181 unsigned is_rect
= 0;
3182 bool da
= instr
->is_array
|| instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
;
3184 if (op
== nir_texop_lod
)
3186 /* Pad to power of two vector */
3187 while (count
< util_next_power_of_two(count
))
3188 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3191 args
->addr
= ac_build_gather_values(ctx
, param
, count
);
3193 args
->addr
= param
[0];
3195 args
->resource
= res_ptr
;
3196 args
->sampler
= samp_ptr
;
3198 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
&& op
== nir_texop_txf
) {
3199 args
->addr
= param
[0];
3203 args
->dmask
= dmask
;
3204 args
->unorm
= is_rect
;
3208 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3211 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3212 * filtering manually. The driver sets img7 to a mask clearing
3213 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3214 * s_and_b32 samp0, samp0, img7
3217 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3219 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3220 LLVMValueRef res
, LLVMValueRef samp
)
3222 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3223 LLVMValueRef img7
, samp0
;
3225 if (ctx
->ac
.chip_class
>= VI
)
3228 img7
= LLVMBuildExtractElement(builder
, res
,
3229 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3230 samp0
= LLVMBuildExtractElement(builder
, samp
,
3231 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3232 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3233 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3234 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3237 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3238 nir_tex_instr
*instr
,
3239 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3240 LLVMValueRef
*fmask_ptr
)
3242 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3243 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_BUFFER
, instr
, false, false);
3245 *res_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_IMAGE
, instr
, false, false);
3248 *samp_ptr
= get_sampler_desc(ctx
, instr
->sampler
, AC_DESC_SAMPLER
, instr
, false, false);
3250 *samp_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_SAMPLER
, instr
, false, false);
3251 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3252 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3254 if (fmask_ptr
&& !instr
->sampler
&& (instr
->op
== nir_texop_txf_ms
||
3255 instr
->op
== nir_texop_samples_identical
))
3256 *fmask_ptr
= get_sampler_desc(ctx
, instr
->texture
, AC_DESC_FMASK
, instr
, false, false);
3259 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3262 coord
= ac_to_float(ctx
, coord
);
3263 coord
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &coord
, 1, 0);
3264 coord
= ac_to_integer(ctx
, coord
);
3268 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3270 LLVMValueRef result
= NULL
;
3271 struct ac_image_args args
= { 0 };
3272 unsigned dmask
= 0xf;
3273 LLVMValueRef address
[16];
3274 LLVMValueRef coords
[5];
3275 LLVMValueRef coord
= NULL
, lod
= NULL
, comparator
= NULL
;
3276 LLVMValueRef bias
= NULL
, offsets
= NULL
;
3277 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
, sample_index
= NULL
;
3278 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3279 LLVMValueRef derivs
[6];
3280 unsigned chan
, count
= 0;
3281 unsigned const_src
= 0, num_deriv_comp
= 0;
3282 bool lod_is_zero
= false;
3284 tex_fetch_ptrs(ctx
, instr
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3286 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3287 switch (instr
->src
[i
].src_type
) {
3288 case nir_tex_src_coord
:
3289 coord
= get_src(ctx
, instr
->src
[i
].src
);
3291 case nir_tex_src_projector
:
3293 case nir_tex_src_comparator
:
3294 comparator
= get_src(ctx
, instr
->src
[i
].src
);
3296 case nir_tex_src_offset
:
3297 offsets
= get_src(ctx
, instr
->src
[i
].src
);
3300 case nir_tex_src_bias
:
3301 bias
= get_src(ctx
, instr
->src
[i
].src
);
3303 case nir_tex_src_lod
: {
3304 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3306 if (val
&& val
->i32
[0] == 0)
3308 lod
= get_src(ctx
, instr
->src
[i
].src
);
3311 case nir_tex_src_ms_index
:
3312 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3314 case nir_tex_src_ms_mcs
:
3316 case nir_tex_src_ddx
:
3317 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3318 num_deriv_comp
= instr
->src
[i
].src
.ssa
->num_components
;
3320 case nir_tex_src_ddy
:
3321 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3323 case nir_tex_src_texture_offset
:
3324 case nir_tex_src_sampler_offset
:
3325 case nir_tex_src_plane
:
3331 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3332 result
= get_buffer_size(ctx
, res_ptr
, true);
3336 if (instr
->op
== nir_texop_texture_samples
) {
3337 LLVMValueRef res
, samples
, is_msaa
;
3338 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->ac
.v8i32
, "");
3339 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3340 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3341 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3342 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3343 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3344 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3345 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3346 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3348 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3349 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3350 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3351 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3352 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3354 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3361 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3362 coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3364 if (offsets
&& instr
->op
!= nir_texop_txf
) {
3365 LLVMValueRef offset
[3], pack
;
3366 for (chan
= 0; chan
< 3; ++chan
)
3367 offset
[chan
] = ctx
->ac
.i32_0
;
3370 for (chan
= 0; chan
< ac_get_llvm_num_components(offsets
); chan
++) {
3371 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, offsets
, chan
);
3372 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3373 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3375 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3376 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3378 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3379 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3380 address
[count
++] = pack
;
3383 /* pack LOD bias value */
3384 if (instr
->op
== nir_texop_txb
&& bias
) {
3385 address
[count
++] = bias
;
3388 /* Pack depth comparison value */
3389 if (instr
->is_shadow
&& comparator
) {
3390 LLVMValueRef z
= ac_to_float(&ctx
->ac
,
3391 ac_llvm_extract_elem(&ctx
->ac
, comparator
, 0));
3393 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3394 * so the depth comparison value isn't clamped for Z16 and
3395 * Z24 anymore. Do it manually here.
3397 * It's unnecessary if the original texture format was
3398 * Z32_FLOAT, but we don't know that here.
3400 if (ctx
->ac
.chip_class
== VI
&& ctx
->abi
->clamp_shadow_reference
)
3401 z
= ac_build_clamp(&ctx
->ac
, z
);
3403 address
[count
++] = z
;
3406 /* pack derivatives */
3408 int num_src_deriv_channels
, num_dest_deriv_channels
;
3409 switch (instr
->sampler_dim
) {
3410 case GLSL_SAMPLER_DIM_3D
:
3411 case GLSL_SAMPLER_DIM_CUBE
:
3413 num_src_deriv_channels
= 3;
3414 num_dest_deriv_channels
= 3;
3416 case GLSL_SAMPLER_DIM_2D
:
3418 num_src_deriv_channels
= 2;
3419 num_dest_deriv_channels
= 2;
3422 case GLSL_SAMPLER_DIM_1D
:
3423 num_src_deriv_channels
= 1;
3424 if (ctx
->ac
.chip_class
>= GFX9
) {
3425 num_dest_deriv_channels
= 2;
3428 num_dest_deriv_channels
= 1;
3434 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3435 derivs
[i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3436 derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
, ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3438 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3439 derivs
[i
] = ctx
->ac
.f32_0
;
3440 derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3444 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& coord
) {
3445 for (chan
= 0; chan
< instr
->coord_components
; chan
++)
3446 coords
[chan
] = ac_to_float(&ctx
->ac
, coords
[chan
]);
3447 if (instr
->coord_components
== 3)
3448 coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3449 ac_prepare_cube_coords(&ctx
->ac
,
3450 instr
->op
== nir_texop_txd
, instr
->is_array
,
3451 instr
->op
== nir_texop_lod
, coords
, derivs
);
3457 for (unsigned i
= 0; i
< num_deriv_comp
* 2; i
++)
3458 address
[count
++] = derivs
[i
];
3461 /* Pack texture coordinates */
3463 address
[count
++] = coords
[0];
3464 if (instr
->coord_components
> 1) {
3465 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&& instr
->is_array
&& instr
->op
!= nir_texop_txf
) {
3466 coords
[1] = apply_round_slice(&ctx
->ac
, coords
[1]);
3468 address
[count
++] = coords
[1];
3470 if (instr
->coord_components
> 2) {
3471 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3472 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3473 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3474 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3476 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3477 coords
[2] = apply_round_slice(&ctx
->ac
, coords
[2]);
3479 address
[count
++] = coords
[2];
3482 if (ctx
->ac
.chip_class
>= GFX9
) {
3483 LLVMValueRef filler
;
3484 if (instr
->op
== nir_texop_txf
)
3485 filler
= ctx
->ac
.i32_0
;
3487 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3489 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
) {
3490 /* No nir_texop_lod, because it does not take a slice
3491 * even with array textures. */
3492 if (instr
->is_array
&& instr
->op
!= nir_texop_lod
) {
3493 address
[count
] = address
[count
- 1];
3494 address
[count
- 1] = filler
;
3497 address
[count
++] = filler
;
3503 if (lod
&& ((instr
->op
== nir_texop_txl
|| instr
->op
== nir_texop_txf
) && !lod_is_zero
)) {
3504 address
[count
++] = lod
;
3505 } else if (instr
->op
== nir_texop_txf_ms
&& sample_index
) {
3506 address
[count
++] = sample_index
;
3507 } else if(instr
->op
== nir_texop_txs
) {
3510 address
[count
++] = lod
;
3512 address
[count
++] = ctx
->ac
.i32_0
;
3515 for (chan
= 0; chan
< count
; chan
++) {
3516 address
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3517 address
[chan
], ctx
->ac
.i32
, "");
3520 if (instr
->op
== nir_texop_samples_identical
) {
3521 LLVMValueRef txf_address
[4];
3522 struct ac_image_args txf_args
= { 0 };
3523 unsigned txf_count
= count
;
3524 memcpy(txf_address
, address
, sizeof(txf_address
));
3526 if (!instr
->is_array
)
3527 txf_address
[2] = ctx
->ac
.i32_0
;
3528 txf_address
[3] = ctx
->ac
.i32_0
;
3530 set_tex_fetch_args(&ctx
->ac
, &txf_args
, instr
, nir_texop_txf
,
3532 txf_address
, txf_count
, 0xf);
3534 result
= build_tex_intrinsic(ctx
, instr
, false, &txf_args
);
3536 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3537 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3541 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3542 instr
->op
!= nir_texop_txs
) {
3543 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3544 address
[sample_chan
] = adjust_sample_index_using_fmask(&ctx
->ac
,
3547 instr
->is_array
? address
[2] : NULL
,
3548 address
[sample_chan
],
3552 if (offsets
&& instr
->op
== nir_texop_txf
) {
3553 nir_const_value
*const_offset
=
3554 nir_src_as_const_value(instr
->src
[const_src
].src
);
3555 int num_offsets
= instr
->src
[const_src
].src
.ssa
->num_components
;
3556 assert(const_offset
);
3557 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3558 if (num_offsets
> 2)
3559 address
[2] = LLVMBuildAdd(ctx
->ac
.builder
,
3560 address
[2], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[2], false), "");
3561 if (num_offsets
> 1)
3562 address
[1] = LLVMBuildAdd(ctx
->ac
.builder
,
3563 address
[1], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[1], false), "");
3564 address
[0] = LLVMBuildAdd(ctx
->ac
.builder
,
3565 address
[0], LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[0], false), "");
3569 /* TODO TG4 support */
3570 if (instr
->op
== nir_texop_tg4
) {
3571 if (instr
->is_shadow
)
3574 dmask
= 1 << instr
->component
;
3576 set_tex_fetch_args(&ctx
->ac
, &args
, instr
, instr
->op
,
3577 res_ptr
, samp_ptr
, address
, count
, dmask
);
3579 result
= build_tex_intrinsic(ctx
, instr
, lod_is_zero
, &args
);
3581 if (instr
->op
== nir_texop_query_levels
)
3582 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3583 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3584 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3585 instr
->op
!= nir_texop_tg4
)
3586 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3587 else if (instr
->op
== nir_texop_txs
&&
3588 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3590 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3591 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3592 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3593 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3594 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3595 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3596 instr
->op
== nir_texop_txs
&&
3597 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3599 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3600 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3601 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3603 } else if (instr
->dest
.ssa
.num_components
!= 4)
3604 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3608 assert(instr
->dest
.is_ssa
);
3609 result
= ac_to_integer(&ctx
->ac
, result
);
3610 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3615 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3617 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3618 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3620 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3621 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3624 static void visit_post_phi(struct ac_nir_context
*ctx
,
3625 nir_phi_instr
*instr
,
3626 LLVMValueRef llvm_phi
)
3628 nir_foreach_phi_src(src
, instr
) {
3629 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3630 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3632 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3636 static void phi_post_pass(struct ac_nir_context
*ctx
)
3638 struct hash_entry
*entry
;
3639 hash_table_foreach(ctx
->phis
, entry
) {
3640 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3641 (LLVMValueRef
)entry
->data
);
3646 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3647 const nir_ssa_undef_instr
*instr
)
3649 unsigned num_components
= instr
->def
.num_components
;
3650 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3653 if (num_components
== 1)
3654 undef
= LLVMGetUndef(type
);
3656 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3658 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3661 static void visit_jump(struct ac_llvm_context
*ctx
,
3662 const nir_jump_instr
*instr
)
3664 switch (instr
->type
) {
3665 case nir_jump_break
:
3666 ac_build_break(ctx
);
3668 case nir_jump_continue
:
3669 ac_build_continue(ctx
);
3672 fprintf(stderr
, "Unknown NIR jump instr: ");
3673 nir_print_instr(&instr
->instr
, stderr
);
3674 fprintf(stderr
, "\n");
3679 static void visit_cf_list(struct ac_nir_context
*ctx
,
3680 struct exec_list
*list
);
3682 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
3684 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
3685 nir_foreach_instr(instr
, block
)
3687 switch (instr
->type
) {
3688 case nir_instr_type_alu
:
3689 visit_alu(ctx
, nir_instr_as_alu(instr
));
3691 case nir_instr_type_load_const
:
3692 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
3694 case nir_instr_type_intrinsic
:
3695 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
3697 case nir_instr_type_tex
:
3698 visit_tex(ctx
, nir_instr_as_tex(instr
));
3700 case nir_instr_type_phi
:
3701 visit_phi(ctx
, nir_instr_as_phi(instr
));
3703 case nir_instr_type_ssa_undef
:
3704 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
3706 case nir_instr_type_jump
:
3707 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
3710 fprintf(stderr
, "Unknown NIR instr type: ");
3711 nir_print_instr(instr
, stderr
);
3712 fprintf(stderr
, "\n");
3717 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
3720 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
3722 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
3724 nir_block
*then_block
=
3725 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
3727 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
3729 visit_cf_list(ctx
, &if_stmt
->then_list
);
3731 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
3732 nir_block
*else_block
=
3733 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
3735 ac_build_else(&ctx
->ac
, else_block
->index
);
3736 visit_cf_list(ctx
, &if_stmt
->else_list
);
3739 ac_build_endif(&ctx
->ac
, then_block
->index
);
3742 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
3744 nir_block
*first_loop_block
=
3745 (nir_block
*) exec_list_get_head(&loop
->body
);
3747 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
3749 visit_cf_list(ctx
, &loop
->body
);
3751 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
3754 static void visit_cf_list(struct ac_nir_context
*ctx
,
3755 struct exec_list
*list
)
3757 foreach_list_typed(nir_cf_node
, node
, node
, list
)
3759 switch (node
->type
) {
3760 case nir_cf_node_block
:
3761 visit_block(ctx
, nir_cf_node_as_block(node
));
3764 case nir_cf_node_if
:
3765 visit_if(ctx
, nir_cf_node_as_if(node
));
3768 case nir_cf_node_loop
:
3769 visit_loop(ctx
, nir_cf_node_as_loop(node
));
3779 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
3780 struct ac_shader_abi
*abi
,
3781 struct nir_shader
*nir
,
3782 struct nir_variable
*variable
,
3783 gl_shader_stage stage
)
3785 unsigned output_loc
= variable
->data
.driver_location
/ 4;
3786 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3788 /* tess ctrl has it's own load/store paths for outputs */
3789 if (stage
== MESA_SHADER_TESS_CTRL
)
3792 if (stage
== MESA_SHADER_VERTEX
||
3793 stage
== MESA_SHADER_TESS_EVAL
||
3794 stage
== MESA_SHADER_GEOMETRY
) {
3795 int idx
= variable
->data
.location
+ variable
->data
.index
;
3796 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
3797 int length
= nir
->info
.clip_distance_array_size
+
3798 nir
->info
.cull_distance_array_size
;
3807 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
3808 for (unsigned chan
= 0; chan
< 4; chan
++) {
3809 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
3810 ac_build_alloca_undef(ctx
, ctx
->f32
, "");
3816 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3817 enum glsl_base_type type
)
3821 case GLSL_TYPE_UINT
:
3822 case GLSL_TYPE_BOOL
:
3823 case GLSL_TYPE_SUBROUTINE
:
3825 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
3827 case GLSL_TYPE_INT64
:
3828 case GLSL_TYPE_UINT64
:
3830 case GLSL_TYPE_DOUBLE
:
3833 unreachable("unknown GLSL type");
3838 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3839 const struct glsl_type
*type
)
3841 if (glsl_type_is_scalar(type
)) {
3842 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3845 if (glsl_type_is_vector(type
)) {
3846 return LLVMVectorType(
3847 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3848 glsl_get_vector_elements(type
));
3851 if (glsl_type_is_matrix(type
)) {
3852 return LLVMArrayType(
3853 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3854 glsl_get_matrix_columns(type
));
3857 if (glsl_type_is_array(type
)) {
3858 return LLVMArrayType(
3859 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3860 glsl_get_length(type
));
3863 assert(glsl_type_is_struct(type
));
3865 LLVMTypeRef member_types
[glsl_get_length(type
)];
3867 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3869 glsl_to_llvm_type(ac
,
3870 glsl_get_struct_field(type
, i
));
3873 return LLVMStructTypeInContext(ac
->context
, member_types
,
3874 glsl_get_length(type
), false);
3878 setup_locals(struct ac_nir_context
*ctx
,
3879 struct nir_function
*func
)
3882 ctx
->num_locals
= 0;
3883 nir_foreach_variable(variable
, &func
->impl
->locals
) {
3884 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
3885 variable
->data
.driver_location
= ctx
->num_locals
* 4;
3886 variable
->data
.location_frac
= 0;
3887 ctx
->num_locals
+= attrib_count
;
3889 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
3893 for (i
= 0; i
< ctx
->num_locals
; i
++) {
3894 for (j
= 0; j
< 4; j
++) {
3895 ctx
->locals
[i
* 4 + j
] =
3896 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
3902 setup_shared(struct ac_nir_context
*ctx
,
3903 struct nir_shader
*nir
)
3905 nir_foreach_variable(variable
, &nir
->shared
) {
3906 LLVMValueRef shared
=
3907 LLVMAddGlobalInAddressSpace(
3908 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
3909 variable
->name
? variable
->name
: "",
3910 AC_LOCAL_ADDR_SPACE
);
3911 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
3915 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
3916 struct nir_shader
*nir
)
3918 struct ac_nir_context ctx
= {};
3919 struct nir_function
*func
;
3924 ctx
.stage
= nir
->info
.stage
;
3926 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3928 nir_foreach_variable(variable
, &nir
->outputs
)
3929 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
3932 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3933 _mesa_key_pointer_equal
);
3934 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3935 _mesa_key_pointer_equal
);
3936 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
3937 _mesa_key_pointer_equal
);
3939 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
3941 nir_index_ssa_defs(func
->impl
);
3942 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
3944 setup_locals(&ctx
, func
);
3946 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
3947 setup_shared(&ctx
, nir
);
3949 visit_cf_list(&ctx
, &func
->impl
->body
);
3950 phi_post_pass(&ctx
);
3952 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
3953 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
3958 ralloc_free(ctx
.defs
);
3959 ralloc_free(ctx
.phis
);
3960 ralloc_free(ctx
.vars
);
3964 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
3966 /* While it would be nice not to have this flag, we are constrained
3967 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3970 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
3972 /* TODO: Indirect indexing of GS inputs is unimplemented.
3974 * TCS and TES load inputs directly from LDS or offchip memory, so
3975 * indirect indexing is trivial.
3977 nir_variable_mode indirect_mask
= 0;
3978 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
3979 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
3980 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
3981 !llvm_has_working_vgpr_indexing
)) {
3982 indirect_mask
|= nir_var_shader_in
;
3984 if (!llvm_has_working_vgpr_indexing
&&
3985 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
3986 indirect_mask
|= nir_var_shader_out
;
3988 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3989 * smart enough to handle indirects without causing excess spilling
3990 * causing the gpu to hang.
3992 * See the following thread for more details of the problem:
3993 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3995 indirect_mask
|= nir_var_local
;
3997 nir_lower_indirect_derefs(nir
, indirect_mask
);