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 <llvm/Config/llvm-config.h>
26 #include "ac_nir_to_llvm.h"
27 #include "ac_llvm_build.h"
28 #include "ac_llvm_util.h"
29 #include "ac_binary.h"
32 #include "nir/nir_deref.h"
33 #include "util/bitscan.h"
34 #include "util/u_math.h"
35 #include "ac_shader_abi.h"
36 #include "ac_shader_util.h"
38 struct ac_nir_context
{
39 struct ac_llvm_context ac
;
40 struct ac_shader_abi
*abi
;
41 const struct ac_shader_args
*args
;
43 gl_shader_stage stage
;
46 LLVMValueRef
*ssa_defs
;
49 LLVMValueRef constant_data
;
51 struct hash_table
*defs
;
52 struct hash_table
*phis
;
53 struct hash_table
*vars
;
55 LLVMValueRef main_function
;
56 LLVMBasicBlockRef continue_block
;
57 LLVMBasicBlockRef break_block
;
63 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
64 nir_deref_instr
*deref_instr
,
65 enum ac_descriptor_type desc_type
,
66 const nir_instr
*instr
,
67 bool image
, bool write
);
70 build_store_values_extended(struct ac_llvm_context
*ac
,
73 unsigned value_stride
,
76 LLVMBuilderRef builder
= ac
->builder
;
79 for (i
= 0; i
< value_count
; i
++) {
80 LLVMValueRef ptr
= values
[i
* value_stride
];
81 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
82 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
83 LLVMBuildStore(builder
, value
, ptr
);
87 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
88 const nir_ssa_def
*def
)
90 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
91 if (def
->num_components
> 1) {
92 type
= LLVMVectorType(type
, def
->num_components
);
97 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
100 return nir
->ssa_defs
[src
.ssa
->index
];
104 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
, unsigned bit_size
)
106 LLVMValueRef ptr
= get_src(ctx
, src
);
107 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
108 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
110 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, bit_size
);
112 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
113 LLVMPointerType(type
, addr_space
), "");
116 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
117 const struct nir_block
*b
)
119 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
120 return (LLVMBasicBlockRef
)entry
->data
;
123 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
125 unsigned num_components
)
127 LLVMValueRef value
= get_src(ctx
, src
.src
);
128 bool need_swizzle
= false;
131 unsigned src_components
= ac_get_llvm_num_components(value
);
132 for (unsigned i
= 0; i
< num_components
; ++i
) {
133 assert(src
.swizzle
[i
] < src_components
);
134 if (src
.swizzle
[i
] != i
)
138 if (need_swizzle
|| num_components
!= src_components
) {
139 LLVMValueRef masks
[] = {
140 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
141 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
142 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
143 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
145 if (src_components
> 1 && num_components
== 1) {
146 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
148 } else if (src_components
== 1 && num_components
> 1) {
149 LLVMValueRef values
[] = {value
, value
, value
, value
};
150 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
152 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
153 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
162 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
163 LLVMIntPredicate pred
, LLVMValueRef src0
,
166 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
167 return LLVMBuildSelect(ctx
->builder
, result
,
168 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
172 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
173 LLVMRealPredicate pred
, LLVMValueRef src0
,
177 src0
= ac_to_float(ctx
, src0
);
178 src1
= ac_to_float(ctx
, src1
);
179 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
180 return LLVMBuildSelect(ctx
->builder
, result
,
181 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
185 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
187 LLVMTypeRef result_type
,
191 LLVMValueRef params
[] = {
192 ac_to_float(ctx
, src0
),
195 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
196 ac_get_elem_bits(ctx
, result_type
));
197 assert(length
< sizeof(name
));
198 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
201 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
203 LLVMTypeRef result_type
,
204 LLVMValueRef src0
, LLVMValueRef src1
)
207 LLVMValueRef params
[] = {
208 ac_to_float(ctx
, src0
),
209 ac_to_float(ctx
, src1
),
212 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
213 ac_get_elem_bits(ctx
, result_type
));
214 assert(length
< sizeof(name
));
215 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
218 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
220 LLVMTypeRef result_type
,
221 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
224 LLVMValueRef params
[] = {
225 ac_to_float(ctx
, src0
),
226 ac_to_float(ctx
, src1
),
227 ac_to_float(ctx
, src2
),
230 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
231 ac_get_elem_bits(ctx
, result_type
));
232 assert(length
< sizeof(name
));
233 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
236 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
237 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
239 LLVMTypeRef src1_type
= LLVMTypeOf(src1
);
240 LLVMTypeRef src2_type
= LLVMTypeOf(src2
);
242 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
244 if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
245 LLVMGetTypeKind(src2_type
) != LLVMPointerTypeKind
) {
246 src2
= LLVMBuildIntToPtr(ctx
->builder
, src2
, src1_type
, "");
247 } else if (LLVMGetTypeKind(src2_type
) == LLVMPointerTypeKind
&&
248 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
249 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src2_type
, "");
252 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
254 return LLVMBuildSelect(ctx
->builder
, v
,
255 ac_to_integer_or_pointer(ctx
, src1
),
256 ac_to_integer_or_pointer(ctx
, src2
), "");
259 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
262 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
265 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
267 LLVMValueRef src0
, LLVMValueRef src1
)
269 LLVMTypeRef ret_type
;
270 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
272 LLVMValueRef params
[] = { src0
, src1
};
273 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
276 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
277 params
, 2, AC_FUNC_ATTR_READNONE
);
279 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
280 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
284 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
288 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
289 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
291 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
295 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
299 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
301 unreachable("Unsupported bit size.");
305 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
308 src0
= ac_to_float(ctx
, src0
);
309 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
310 return LLVMBuildSExt(ctx
->builder
,
311 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
315 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
319 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
323 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
325 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
329 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
331 unreachable("Unsupported bit size.");
335 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
338 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
339 return LLVMBuildSExt(ctx
->builder
,
340 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
344 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
348 LLVMValueRef cond
= NULL
;
350 src0
= ac_to_float(ctx
, src0
);
351 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
353 if (ctx
->chip_class
>= GFX8
) {
354 LLVMValueRef args
[2];
355 /* Check if the result is a denormal - and flush to 0 if so. */
357 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
358 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
361 /* need to convert back up to f32 */
362 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
364 if (ctx
->chip_class
>= GFX8
)
365 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
368 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
369 * so compare the result and flush to 0 if it's smaller.
371 LLVMValueRef temp
, cond2
;
372 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
373 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
,
374 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
376 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealONE
,
377 temp
, ctx
->f32_0
, "");
378 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
379 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
384 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
385 LLVMValueRef src0
, LLVMValueRef src1
)
387 LLVMValueRef dst64
, result
;
388 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
389 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
391 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
392 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
393 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
397 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
398 LLVMValueRef src0
, LLVMValueRef src1
)
400 LLVMValueRef dst64
, result
;
401 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
402 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
404 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
405 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
406 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
410 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
411 LLVMValueRef bits
, LLVMValueRef offset
)
413 /* mask = ((1 << bits) - 1) << offset */
414 return LLVMBuildShl(ctx
->builder
,
415 LLVMBuildSub(ctx
->builder
,
416 LLVMBuildShl(ctx
->builder
,
423 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
424 LLVMValueRef mask
, LLVMValueRef insert
,
428 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
429 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
431 return LLVMBuildXor(ctx
->builder
, base
,
432 LLVMBuildAnd(ctx
->builder
, mask
,
433 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
436 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
438 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
439 LLVMValueRef args
[2]))
441 LLVMValueRef comp
[2];
443 src0
= ac_to_float(ctx
, src0
);
444 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
445 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
447 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
450 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
453 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
454 LLVMValueRef temps
[2], val
;
457 for (i
= 0; i
< 2; i
++) {
458 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
459 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
460 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
461 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
463 return ac_build_gather_values(ctx
, temps
, 2);
466 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
474 if (op
== nir_op_fddx_fine
)
475 mask
= AC_TID_MASK_LEFT
;
476 else if (op
== nir_op_fddy_fine
)
477 mask
= AC_TID_MASK_TOP
;
479 mask
= AC_TID_MASK_TOP_LEFT
;
481 /* for DDX we want to next X pixel, DDY next Y pixel. */
482 if (op
== nir_op_fddx_fine
||
483 op
== nir_op_fddx_coarse
||
489 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
493 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
495 LLVMValueRef src
[4], result
= NULL
;
496 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
497 unsigned src_components
;
498 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
500 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
507 case nir_op_pack_half_2x16
:
508 case nir_op_pack_snorm_2x16
:
509 case nir_op_pack_unorm_2x16
:
512 case nir_op_unpack_half_2x16
:
515 case nir_op_cube_face_coord
:
516 case nir_op_cube_face_index
:
520 src_components
= num_components
;
523 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
524 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
531 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
532 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
533 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
534 /* fneg will be optimized by backend compiler with sign
535 * bit removed via XOR. This is probably a LLVM bug.
537 result
= ac_build_canonicalize(&ctx
->ac
, result
,
538 instr
->dest
.dest
.ssa
.bit_size
);
542 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
545 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
548 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
551 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
552 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
553 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
556 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
557 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
558 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
561 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
564 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
567 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
570 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
573 /* lower_fmod only lower 16-bit and 32-bit fmod */
574 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
575 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
576 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
577 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
578 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
579 ac_to_float_type(&ctx
->ac
, def_type
), result
);
580 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
581 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
584 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
587 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
590 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
593 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
594 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
595 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
598 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
599 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
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 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
612 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
613 LLVMTypeOf(src
[0]), "");
614 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
615 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
616 LLVMTypeOf(src
[0]), "");
617 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
620 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
621 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
622 LLVMTypeOf(src
[0]), "");
623 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
624 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
625 LLVMTypeOf(src
[0]), "");
626 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
629 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
630 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
631 LLVMTypeOf(src
[0]), "");
632 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
633 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
634 LLVMTypeOf(src
[0]), "");
635 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
638 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
641 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
644 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
647 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
650 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
653 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
656 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
659 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
662 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
665 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
668 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
669 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
670 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
671 /* fabs will be optimized by backend compiler with sign
672 * bit removed via AND.
674 result
= ac_build_canonicalize(&ctx
->ac
, result
,
675 instr
->dest
.dest
.ssa
.bit_size
);
679 result
= emit_iabs(&ctx
->ac
, src
[0]);
682 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
685 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
688 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
691 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
694 result
= ac_build_isign(&ctx
->ac
, src
[0],
695 instr
->dest
.dest
.ssa
.bit_size
);
698 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
699 result
= ac_build_fsign(&ctx
->ac
, src
[0],
700 instr
->dest
.dest
.ssa
.bit_size
);
703 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
704 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
707 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
708 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
711 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
712 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
714 case nir_op_fround_even
:
715 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
716 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
719 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
720 result
= ac_build_fract(&ctx
->ac
, src
[0],
721 instr
->dest
.dest
.ssa
.bit_size
);
724 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
725 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
728 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
729 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
732 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
733 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
736 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
737 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
740 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
741 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
746 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
748 case nir_op_frexp_exp
:
749 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
750 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
751 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
752 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
753 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
756 case nir_op_frexp_sig
:
757 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
758 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
759 instr
->dest
.dest
.ssa
.bit_size
);
762 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
763 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
766 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
767 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
768 if (ctx
->ac
.chip_class
< GFX9
&&
769 instr
->dest
.dest
.ssa
.bit_size
== 32) {
770 /* Only pre-GFX9 chips do not flush denorms. */
771 result
= ac_build_canonicalize(&ctx
->ac
, result
,
772 instr
->dest
.dest
.ssa
.bit_size
);
776 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
777 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
778 if (ctx
->ac
.chip_class
< GFX9
&&
779 instr
->dest
.dest
.ssa
.bit_size
== 32) {
780 /* Only pre-GFX9 chips do not flush denorms. */
781 result
= ac_build_canonicalize(&ctx
->ac
, result
,
782 instr
->dest
.dest
.ssa
.bit_size
);
786 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
787 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
788 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
791 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
792 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
793 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
794 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
795 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
797 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
800 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
802 case nir_op_bitfield_select
:
803 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
806 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
809 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
811 case nir_op_bitfield_reverse
:
812 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
814 case nir_op_bit_count
:
815 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
820 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
821 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
822 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
828 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
829 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
835 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
836 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
841 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
846 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
848 case nir_op_f2f16_rtz
:
849 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
850 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
851 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
852 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
853 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
854 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
856 case nir_op_f2f16_rtne
:
860 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
861 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
862 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
864 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
870 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
871 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
873 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
879 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
880 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
882 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
885 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
887 case nir_op_find_lsb
:
888 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
890 case nir_op_ufind_msb
:
891 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
893 case nir_op_ifind_msb
:
894 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
896 case nir_op_uadd_carry
:
897 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
899 case nir_op_usub_borrow
:
900 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
905 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
908 result
= emit_f2b(&ctx
->ac
, src
[0]);
914 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
917 result
= emit_i2b(&ctx
->ac
, src
[0]);
919 case nir_op_fquantize2f16
:
920 result
= emit_f2f16(&ctx
->ac
, src
[0]);
922 case nir_op_umul_high
:
923 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
925 case nir_op_imul_high
:
926 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
928 case nir_op_pack_half_2x16
:
929 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
931 case nir_op_pack_snorm_2x16
:
932 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
934 case nir_op_pack_unorm_2x16
:
935 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
937 case nir_op_unpack_half_2x16
:
938 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
942 case nir_op_fddx_fine
:
943 case nir_op_fddy_fine
:
944 case nir_op_fddx_coarse
:
945 case nir_op_fddy_coarse
:
946 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
949 case nir_op_unpack_64_2x32_split_x
: {
950 assert(ac_get_llvm_num_components(src
[0]) == 1);
951 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
954 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
959 case nir_op_unpack_64_2x32_split_y
: {
960 assert(ac_get_llvm_num_components(src
[0]) == 1);
961 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
964 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
969 case nir_op_pack_64_2x32_split
: {
970 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
971 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
975 case nir_op_pack_32_2x16_split
: {
976 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
977 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
981 case nir_op_unpack_32_2x16_split_x
: {
982 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
985 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
990 case nir_op_unpack_32_2x16_split_y
: {
991 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
994 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
999 case nir_op_cube_face_coord
: {
1000 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1001 LLVMValueRef results
[2];
1003 for (unsigned chan
= 0; chan
< 3; chan
++)
1004 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1005 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1006 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1007 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1008 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1009 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1010 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1011 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1012 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1013 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1014 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1015 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1016 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1020 case nir_op_cube_face_index
: {
1021 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1023 for (unsigned chan
= 0; chan
< 3; chan
++)
1024 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1025 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1026 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1031 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1032 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1033 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1034 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1037 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1038 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1041 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1042 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1045 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1046 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1047 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1048 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1051 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1052 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1055 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1056 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1058 case nir_op_fmed3
: {
1059 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1060 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1061 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1062 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1063 instr
->dest
.dest
.ssa
.bit_size
);
1066 case nir_op_imed3
: {
1067 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1068 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1069 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1070 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1073 case nir_op_umed3
: {
1074 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1075 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1076 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1077 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1082 fprintf(stderr
, "Unknown NIR alu instr: ");
1083 nir_print_instr(&instr
->instr
, stderr
);
1084 fprintf(stderr
, "\n");
1089 assert(instr
->dest
.dest
.is_ssa
);
1090 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1091 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1095 static void visit_load_const(struct ac_nir_context
*ctx
,
1096 const nir_load_const_instr
*instr
)
1098 LLVMValueRef values
[4], value
= NULL
;
1099 LLVMTypeRef element_type
=
1100 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1102 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1103 switch (instr
->def
.bit_size
) {
1105 values
[i
] = LLVMConstInt(element_type
,
1106 instr
->value
[i
].u8
, false);
1109 values
[i
] = LLVMConstInt(element_type
,
1110 instr
->value
[i
].u16
, false);
1113 values
[i
] = LLVMConstInt(element_type
,
1114 instr
->value
[i
].u32
, false);
1117 values
[i
] = LLVMConstInt(element_type
,
1118 instr
->value
[i
].u64
, false);
1122 "unsupported nir load_const bit_size: %d\n",
1123 instr
->def
.bit_size
);
1127 if (instr
->def
.num_components
> 1) {
1128 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1132 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1136 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1139 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1140 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1143 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1144 /* On GFX8, the descriptor contains the size in bytes,
1145 * but TXQ must return the size in elements.
1146 * The stride is always non-zero for resources using TXQ.
1148 LLVMValueRef stride
=
1149 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1151 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1152 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1153 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1154 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1156 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1161 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1162 * incorrectly forces nearest filtering if the texture format is integer.
1163 * The only effect it has on Gather4, which always returns 4 texels for
1164 * bilinear filtering, is that the final coordinates are off by 0.5 of
1167 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1168 * or (0.5 / size) from the normalized coordinates.
1170 * However, cube textures with 8_8_8_8 data formats require a different
1171 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1172 * precision in 32-bit data formats, so it needs to be applied dynamically at
1173 * runtime. In this case, return an i1 value that indicates whether the
1174 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1176 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1178 struct ac_image_args
*args
,
1179 const nir_tex_instr
*instr
)
1181 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1182 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1183 LLVMValueRef wa_8888
= NULL
;
1184 LLVMValueRef half_texel
[2];
1185 LLVMValueRef result
;
1187 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1189 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1190 LLVMValueRef formats
;
1191 LLVMValueRef data_format
;
1192 LLVMValueRef wa_formats
;
1194 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1196 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1197 LLVMConstInt(ctx
->i32
, 20, false), "");
1198 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1199 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1200 wa_8888
= LLVMBuildICmp(
1201 ctx
->builder
, LLVMIntEQ
, data_format
,
1202 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1205 uint32_t wa_num_format
=
1206 stype
== GLSL_TYPE_UINT
?
1207 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1208 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1209 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1210 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1212 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1213 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1215 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1216 args
->resource
= LLVMBuildInsertElement(
1217 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1220 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1222 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1224 struct ac_image_args resinfo
= {};
1225 LLVMBasicBlockRef bbs
[2];
1227 LLVMValueRef unnorm
= NULL
;
1228 LLVMValueRef default_offset
= ctx
->f32_0
;
1229 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1231 /* In vulkan, whether the sampler uses unnormalized
1232 * coordinates or not is a dynamic property of the
1233 * sampler. Hence, to figure out whether or not we
1234 * need to divide by the texture size, we need to test
1235 * the sampler at runtime. This tests the bit set by
1236 * radv_init_sampler().
1238 LLVMValueRef sampler0
=
1239 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1240 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1241 LLVMConstInt(ctx
->i32
, 15, false), "");
1242 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1243 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1244 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1247 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1248 if (wa_8888
|| unnorm
) {
1249 assert(!(wa_8888
&& unnorm
));
1250 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1251 /* Skip the texture size query entirely if we don't need it. */
1252 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1253 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1256 /* Query the texture size. */
1257 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1258 resinfo
.opcode
= ac_image_get_resinfo
;
1259 resinfo
.dmask
= 0xf;
1260 resinfo
.lod
= ctx
->i32_0
;
1261 resinfo
.resource
= args
->resource
;
1262 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1263 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1265 /* Compute -0.5 / size. */
1266 for (unsigned c
= 0; c
< 2; c
++) {
1268 LLVMBuildExtractElement(ctx
->builder
, size
,
1269 LLVMConstInt(ctx
->i32
, c
, 0), "");
1270 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1271 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1272 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1273 LLVMConstReal(ctx
->f32
, -0.5), "");
1276 if (wa_8888
|| unnorm
) {
1277 ac_build_endif(ctx
, 2000);
1279 for (unsigned c
= 0; c
< 2; c
++) {
1280 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1281 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1287 for (unsigned c
= 0; c
< 2; c
++) {
1289 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1290 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1293 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1294 result
= ac_build_image_opcode(ctx
, args
);
1296 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1297 LLVMValueRef tmp
, tmp2
;
1299 /* if the cube workaround is in place, f2i the result. */
1300 for (unsigned c
= 0; c
< 4; c
++) {
1301 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1302 if (stype
== GLSL_TYPE_UINT
)
1303 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1305 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1306 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1307 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1308 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1309 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1310 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1316 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1318 nir_deref_instr
*texture_deref_instr
= NULL
;
1320 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1321 switch (instr
->src
[i
].src_type
) {
1322 case nir_tex_src_texture_deref
:
1323 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1329 return texture_deref_instr
;
1332 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1333 const nir_tex_instr
*instr
,
1334 struct ac_image_args
*args
)
1336 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1337 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1339 return ac_build_buffer_load_format(&ctx
->ac
,
1343 util_last_bit(mask
),
1347 args
->opcode
= ac_image_sample
;
1349 switch (instr
->op
) {
1351 case nir_texop_txf_ms
:
1352 case nir_texop_samples_identical
:
1353 args
->opcode
= args
->level_zero
||
1354 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1355 ac_image_load
: ac_image_load_mip
;
1356 args
->level_zero
= false;
1359 case nir_texop_query_levels
:
1360 args
->opcode
= ac_image_get_resinfo
;
1362 args
->lod
= ctx
->ac
.i32_0
;
1363 args
->level_zero
= false;
1366 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1368 args
->level_zero
= true;
1372 args
->opcode
= ac_image_gather4
;
1373 args
->level_zero
= true;
1376 args
->opcode
= ac_image_get_lod
;
1382 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1383 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1384 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1385 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1386 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1387 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1388 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1392 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1393 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1394 if ((args
->dim
== ac_image_2darray
||
1395 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1396 args
->coords
[1] = ctx
->ac
.i32_0
;
1400 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1401 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1402 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1403 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1404 /* Prevent texture instructions with implicit derivatives from being
1405 * sinked into branches. */
1406 switch (instr
->op
) {
1410 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1417 return ac_build_image_opcode(&ctx
->ac
, args
);
1420 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1421 nir_intrinsic_instr
*instr
)
1423 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1424 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1426 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1427 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1431 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1432 nir_intrinsic_instr
*instr
)
1434 LLVMValueRef ptr
, addr
;
1435 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1436 unsigned index
= nir_intrinsic_base(instr
);
1438 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1439 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1441 /* Load constant values from user SGPRS when possible, otherwise
1442 * fallback to the default path that loads directly from memory.
1444 if (LLVMIsConstant(src0
) &&
1445 instr
->dest
.ssa
.bit_size
== 32) {
1446 unsigned count
= instr
->dest
.ssa
.num_components
;
1447 unsigned offset
= index
;
1449 offset
+= LLVMConstIntGetZExtValue(src0
);
1452 offset
-= ctx
->args
->base_inline_push_consts
;
1454 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1455 if (offset
+ count
<= num_inline_push_consts
) {
1456 LLVMValueRef push_constants
[num_inline_push_consts
];
1457 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1458 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1459 ctx
->args
->inline_push_consts
[i
]);
1460 return ac_build_gather_values(&ctx
->ac
,
1461 push_constants
+ offset
,
1466 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1467 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1469 if (instr
->dest
.ssa
.bit_size
== 8) {
1470 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1471 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1472 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1473 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1475 LLVMValueRef params
[3];
1476 if (load_dwords
> 1) {
1477 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1478 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1479 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1481 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1482 params
[0] = ctx
->ac
.i32_0
;
1486 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1488 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1489 if (instr
->dest
.ssa
.num_components
> 1)
1490 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1492 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1493 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1494 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1495 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1496 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1497 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1498 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1499 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1500 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1501 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1502 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1503 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1504 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1505 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1506 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1507 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1508 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1511 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1513 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1516 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1517 const nir_intrinsic_instr
*instr
)
1519 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1521 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1524 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1526 uint32_t new_mask
= 0;
1527 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1528 if (mask
& (1u << i
))
1529 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1533 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1534 unsigned start
, unsigned count
)
1536 LLVMValueRef mask
[] = {
1537 ctx
->i32_0
, ctx
->i32_1
,
1538 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1540 unsigned src_elements
= ac_get_llvm_num_components(src
);
1542 if (count
== src_elements
) {
1545 } else if (count
== 1) {
1546 assert(start
< src_elements
);
1547 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1549 assert(start
+ count
<= src_elements
);
1551 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1552 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1556 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1557 enum gl_access_qualifier access
,
1558 bool may_store_unaligned
,
1559 bool writeonly_memory
)
1561 unsigned cache_policy
= 0;
1563 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1564 * store opcodes not aligned to a dword are affected. The only way to
1565 * get unaligned stores is through shader images.
1567 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1568 /* If this is write-only, don't keep data in L1 to prevent
1569 * evicting L1 cache lines that may be needed by other
1573 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1574 cache_policy
|= ac_glc
;
1577 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1578 cache_policy
|= ac_slc
;
1580 return cache_policy
;
1583 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1584 nir_intrinsic_instr
*instr
)
1586 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1587 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1588 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1589 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1590 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1591 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1593 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1594 get_src(ctx
, instr
->src
[1]), true);
1595 LLVMValueRef base_data
= src_data
;
1596 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1597 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1601 LLVMValueRef data
, offset
;
1602 LLVMTypeRef data_type
;
1604 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1606 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1607 * writes into a 2-element and a 1-element write. */
1609 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1610 writemask
|= 1 << (start
+ 2);
1613 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1615 /* we can only store 4 DWords at the same time.
1616 * can only happen for 64 Bit vectors. */
1617 if (num_bytes
> 16) {
1618 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1623 /* check alignment of 16 Bit stores */
1624 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1625 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1629 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1631 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1632 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1634 if (num_bytes
== 1) {
1635 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1636 offset
, ctx
->ac
.i32_0
,
1638 } else if (num_bytes
== 2) {
1639 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1640 offset
, ctx
->ac
.i32_0
,
1643 int num_channels
= num_bytes
/ 4;
1645 switch (num_bytes
) {
1646 case 16: /* v4f32 */
1647 data_type
= ctx
->ac
.v4f32
;
1649 case 12: /* v3f32 */
1650 data_type
= ctx
->ac
.v3f32
;
1653 data_type
= ctx
->ac
.v2f32
;
1656 data_type
= ctx
->ac
.f32
;
1659 unreachable("Malformed vector store.");
1661 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1663 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1664 num_channels
, offset
,
1671 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1672 LLVMValueRef descriptor
,
1673 LLVMValueRef offset
,
1674 LLVMValueRef compare
,
1675 LLVMValueRef exchange
)
1677 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1678 if (ctx
->abi
->robust_buffer_access
) {
1679 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1681 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1682 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1684 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1686 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1689 LLVMValueRef ptr_parts
[2] = {
1690 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1691 LLVMBuildAnd(ctx
->ac
.builder
,
1692 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1693 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1696 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1697 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1699 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1701 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1702 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1703 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1704 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1706 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1707 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1709 if (ctx
->abi
->robust_buffer_access
) {
1710 ac_build_endif(&ctx
->ac
, -1);
1712 LLVMBasicBlockRef incoming_blocks
[2] = {
1717 LLVMValueRef incoming_values
[2] = {
1718 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1721 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1722 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1729 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1730 const nir_intrinsic_instr
*instr
)
1732 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1734 char name
[64], type
[8];
1735 LLVMValueRef params
[6], descriptor
;
1738 switch (instr
->intrinsic
) {
1739 case nir_intrinsic_ssbo_atomic_add
:
1742 case nir_intrinsic_ssbo_atomic_imin
:
1745 case nir_intrinsic_ssbo_atomic_umin
:
1748 case nir_intrinsic_ssbo_atomic_imax
:
1751 case nir_intrinsic_ssbo_atomic_umax
:
1754 case nir_intrinsic_ssbo_atomic_and
:
1757 case nir_intrinsic_ssbo_atomic_or
:
1760 case nir_intrinsic_ssbo_atomic_xor
:
1763 case nir_intrinsic_ssbo_atomic_exchange
:
1766 case nir_intrinsic_ssbo_atomic_comp_swap
:
1773 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1774 get_src(ctx
, instr
->src
[0]),
1777 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1778 return_type
== ctx
->ac
.i64
) {
1779 return emit_ssbo_comp_swap_64(ctx
, descriptor
,
1780 get_src(ctx
, instr
->src
[1]),
1781 get_src(ctx
, instr
->src
[2]),
1782 get_src(ctx
, instr
->src
[3]));
1784 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1785 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1787 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1788 params
[arg_count
++] = descriptor
;
1790 if (LLVM_VERSION_MAJOR
>= 9) {
1791 /* XXX: The new raw/struct atomic intrinsics are buggy with
1792 * LLVM 8, see r358579.
1794 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1795 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1796 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1798 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1799 snprintf(name
, sizeof(name
),
1800 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1802 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1803 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1804 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1806 assert(return_type
== ctx
->ac
.i32
);
1807 snprintf(name
, sizeof(name
),
1808 "llvm.amdgcn.buffer.atomic.%s", op
);
1811 return ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1815 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1816 const nir_intrinsic_instr
*instr
)
1818 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1819 int num_components
= instr
->num_components
;
1820 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1821 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1823 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1824 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1825 get_src(ctx
, instr
->src
[0]), false);
1826 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1828 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1829 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1831 LLVMValueRef results
[4];
1832 for (int i
= 0; i
< num_components
;) {
1833 int num_elems
= num_components
- i
;
1834 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1836 if (num_elems
* elem_size_bytes
> 16)
1837 num_elems
= 16 / elem_size_bytes
;
1838 int load_bytes
= num_elems
* elem_size_bytes
;
1840 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1844 if (load_bytes
== 1) {
1845 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1851 } else if (load_bytes
== 2) {
1852 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1859 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1860 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
1862 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1863 vindex
, offset
, immoffset
, 0,
1864 cache_policy
, can_speculate
, false);
1867 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1868 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1869 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1871 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1872 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1874 for (unsigned j
= 0; j
< num_elems
; j
++) {
1875 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1880 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1883 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1884 const nir_intrinsic_instr
*instr
)
1887 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1888 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1889 int num_components
= instr
->num_components
;
1891 if (ctx
->abi
->load_ubo
)
1892 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1894 if (instr
->dest
.ssa
.bit_size
== 64)
1895 num_components
*= 2;
1897 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1898 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1899 LLVMValueRef results
[num_components
];
1900 for (unsigned i
= 0; i
< num_components
; ++i
) {
1901 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1904 if (load_bytes
== 1) {
1905 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1912 assert(load_bytes
== 2);
1913 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1921 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1923 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1924 NULL
, 0, 0, true, true);
1926 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1929 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1930 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1934 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1935 bool vs_in
, unsigned *vertex_index_out
,
1936 LLVMValueRef
*vertex_index_ref
,
1937 unsigned *const_out
, LLVMValueRef
*indir_out
)
1939 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1940 nir_deref_path path
;
1941 unsigned idx_lvl
= 1;
1943 nir_deref_path_init(&path
, instr
, NULL
);
1945 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1946 if (vertex_index_ref
) {
1947 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1948 if (vertex_index_out
)
1949 *vertex_index_out
= 0;
1951 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1956 uint32_t const_offset
= 0;
1957 LLVMValueRef offset
= NULL
;
1959 if (var
->data
.compact
) {
1960 assert(instr
->deref_type
== nir_deref_type_array
);
1961 const_offset
= nir_src_as_uint(instr
->arr
.index
);
1965 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1966 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1967 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1968 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1970 for (unsigned i
= 0; i
< index
; i
++) {
1971 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1972 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1974 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1975 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1976 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
1977 const_offset
+= size
*
1978 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1980 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1981 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1983 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1988 unreachable("Uhandled deref type in get_deref_instr_offset");
1992 nir_deref_path_finish(&path
);
1994 if (const_offset
&& offset
)
1995 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1996 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1999 *const_out
= const_offset
;
2000 *indir_out
= offset
;
2003 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2004 nir_intrinsic_instr
*instr
,
2007 LLVMValueRef result
;
2008 LLVMValueRef vertex_index
= NULL
;
2009 LLVMValueRef indir_index
= NULL
;
2010 unsigned const_index
= 0;
2012 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2014 unsigned location
= var
->data
.location
;
2015 unsigned driver_location
= var
->data
.driver_location
;
2016 const bool is_patch
= var
->data
.patch
||
2017 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2018 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2019 const bool is_compact
= var
->data
.compact
;
2021 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2022 false, NULL
, is_patch
? NULL
: &vertex_index
,
2023 &const_index
, &indir_index
);
2025 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2027 LLVMTypeRef src_component_type
;
2028 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2029 src_component_type
= LLVMGetElementType(dest_type
);
2031 src_component_type
= dest_type
;
2033 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2034 vertex_index
, indir_index
,
2035 const_index
, location
, driver_location
,
2036 var
->data
.location_frac
,
2037 instr
->num_components
,
2038 is_patch
, is_compact
, load_inputs
);
2039 if (instr
->dest
.ssa
.bit_size
== 16) {
2040 result
= ac_to_integer(&ctx
->ac
, result
);
2041 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2043 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2047 type_scalar_size_bytes(const struct glsl_type
*type
)
2049 assert(glsl_type_is_vector_or_scalar(type
) ||
2050 glsl_type_is_matrix(type
));
2051 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2054 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2055 nir_intrinsic_instr
*instr
)
2057 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2058 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2060 LLVMValueRef values
[8];
2062 int ve
= instr
->dest
.ssa
.num_components
;
2064 LLVMValueRef indir_index
;
2066 unsigned const_index
;
2067 unsigned stride
= 4;
2068 int mode
= deref
->mode
;
2071 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2072 var
->data
.mode
== nir_var_shader_in
;
2073 idx
= var
->data
.driver_location
;
2074 comp
= var
->data
.location_frac
;
2075 mode
= var
->data
.mode
;
2077 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2078 &const_index
, &indir_index
);
2080 if (var
->data
.compact
) {
2082 const_index
+= comp
;
2087 if (instr
->dest
.ssa
.bit_size
== 64 &&
2088 (deref
->mode
== nir_var_shader_in
||
2089 deref
->mode
== nir_var_shader_out
||
2090 deref
->mode
== nir_var_function_temp
))
2094 case nir_var_shader_in
:
2095 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2096 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2097 return load_tess_varyings(ctx
, instr
, true);
2100 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2101 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2102 LLVMValueRef indir_index
;
2103 unsigned const_index
, vertex_index
;
2104 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2105 &const_index
, &indir_index
);
2106 assert(indir_index
== NULL
);
2108 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2109 var
->data
.driver_location
,
2110 var
->data
.location_frac
,
2111 instr
->num_components
, vertex_index
, const_index
, type
);
2114 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2116 unsigned count
= glsl_count_attribute_slots(
2118 ctx
->stage
== MESA_SHADER_VERTEX
);
2120 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2121 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2122 stride
, false, true);
2124 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2128 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2131 case nir_var_function_temp
:
2132 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2134 unsigned count
= glsl_count_attribute_slots(
2137 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2138 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2139 stride
, true, true);
2141 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2145 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2149 case nir_var_shader_out
:
2150 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2151 return load_tess_varyings(ctx
, instr
, false);
2154 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2155 var
->data
.fb_fetch_output
&&
2156 ctx
->abi
->emit_fbfetch
)
2157 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2159 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2161 unsigned count
= glsl_count_attribute_slots(
2164 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2165 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2166 stride
, true, true);
2168 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2172 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2173 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2178 case nir_var_mem_global
: {
2179 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2180 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2181 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2182 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2184 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2185 if (stride
!= natural_stride
) {
2186 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2187 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2188 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2190 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2191 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2192 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2193 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2195 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2197 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2198 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2199 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2200 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2205 unreachable("unhandle variable mode");
2207 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2208 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2212 visit_store_var(struct ac_nir_context
*ctx
,
2213 nir_intrinsic_instr
*instr
)
2215 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2216 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2218 LLVMValueRef temp_ptr
, value
;
2221 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2222 int writemask
= instr
->const_index
[0];
2223 LLVMValueRef indir_index
;
2224 unsigned const_index
;
2227 get_deref_offset(ctx
, deref
, false,
2228 NULL
, NULL
, &const_index
, &indir_index
);
2229 idx
= var
->data
.driver_location
;
2230 comp
= var
->data
.location_frac
;
2232 if (var
->data
.compact
) {
2233 const_index
+= comp
;
2238 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2239 (deref
->mode
== nir_var_shader_out
||
2240 deref
->mode
== nir_var_function_temp
)) {
2242 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2243 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2246 writemask
= widen_mask(writemask
, 2);
2249 writemask
= writemask
<< comp
;
2251 switch (deref
->mode
) {
2252 case nir_var_shader_out
:
2254 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2255 LLVMValueRef vertex_index
= NULL
;
2256 LLVMValueRef indir_index
= NULL
;
2257 unsigned const_index
= 0;
2258 const bool is_patch
= var
->data
.patch
||
2259 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2260 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2262 get_deref_offset(ctx
, deref
, false, NULL
,
2263 is_patch
? NULL
: &vertex_index
,
2264 &const_index
, &indir_index
);
2266 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2267 vertex_index
, indir_index
,
2268 const_index
, src
, writemask
);
2272 for (unsigned chan
= 0; chan
< 8; chan
++) {
2274 if (!(writemask
& (1 << chan
)))
2277 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2279 if (var
->data
.compact
)
2282 unsigned count
= glsl_count_attribute_slots(
2285 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2286 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2287 stride
, true, true);
2289 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2290 value
, indir_index
, "");
2291 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2292 count
, stride
, tmp_vec
);
2295 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2297 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2301 case nir_var_function_temp
:
2302 for (unsigned chan
= 0; chan
< 8; chan
++) {
2303 if (!(writemask
& (1 << chan
)))
2306 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2308 unsigned count
= glsl_count_attribute_slots(
2311 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2312 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2315 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2316 value
, indir_index
, "");
2317 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2320 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2322 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2327 case nir_var_mem_global
: {
2328 int writemask
= instr
->const_index
[0];
2329 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2330 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2332 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2333 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2334 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2336 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2337 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2338 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2340 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2341 stride
== natural_stride
) {
2342 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2343 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2344 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2346 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2347 LLVMGetElementType(LLVMTypeOf(address
)), "");
2348 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2350 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2351 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2352 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2353 for (unsigned chan
= 0; chan
< 4; chan
++) {
2354 if (!(writemask
& (1 << chan
)))
2357 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2359 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2360 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2362 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2363 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2364 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2375 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2378 case GLSL_SAMPLER_DIM_BUF
:
2380 case GLSL_SAMPLER_DIM_1D
:
2381 return array
? 2 : 1;
2382 case GLSL_SAMPLER_DIM_2D
:
2383 return array
? 3 : 2;
2384 case GLSL_SAMPLER_DIM_MS
:
2385 return array
? 4 : 3;
2386 case GLSL_SAMPLER_DIM_3D
:
2387 case GLSL_SAMPLER_DIM_CUBE
:
2389 case GLSL_SAMPLER_DIM_RECT
:
2390 case GLSL_SAMPLER_DIM_SUBPASS
:
2392 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2400 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2401 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2402 LLVMValueRef coord_z
,
2403 LLVMValueRef sample_index
,
2404 LLVMValueRef fmask_desc_ptr
)
2406 unsigned sample_chan
= coord_z
? 3 : 2;
2407 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2408 addr
[sample_chan
] = sample_index
;
2410 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2411 return addr
[sample_chan
];
2414 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2416 assert(instr
->src
[0].is_ssa
);
2417 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2420 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2421 const nir_intrinsic_instr
*instr
,
2422 enum ac_descriptor_type desc_type
,
2425 nir_deref_instr
*deref_instr
=
2426 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2427 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2429 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2432 static void get_image_coords(struct ac_nir_context
*ctx
,
2433 const nir_intrinsic_instr
*instr
,
2434 struct ac_image_args
*args
,
2435 enum glsl_sampler_dim dim
,
2438 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2439 LLVMValueRef masks
[] = {
2440 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2441 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2443 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2446 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2447 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2448 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2449 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2450 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2451 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2452 count
= image_type_to_components_count(dim
, is_array
);
2454 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2455 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2456 LLVMValueRef fmask_load_address
[3];
2458 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2459 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2461 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2463 fmask_load_address
[2] = NULL
;
2465 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2466 fmask_load_address
[0],
2467 fmask_load_address
[1],
2468 fmask_load_address
[2],
2470 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2471 AC_DESC_FMASK
, &instr
->instr
, true, false));
2473 if (count
== 1 && !gfx9_1d
) {
2474 if (instr
->src
[1].ssa
->num_components
)
2475 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2477 args
->coords
[0] = src0
;
2482 for (chan
= 0; chan
< count
; ++chan
) {
2483 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2488 args
->coords
[2] = args
->coords
[1];
2489 args
->coords
[1] = ctx
->ac
.i32_0
;
2491 args
->coords
[1] = ctx
->ac
.i32_0
;
2494 if (ctx
->ac
.chip_class
== GFX9
&&
2495 dim
== GLSL_SAMPLER_DIM_2D
&&
2497 /* The hw can't bind a slice of a 3D image as a 2D
2498 * image, because it ignores BASE_ARRAY if the target
2499 * is 3D. The workaround is to read BASE_ARRAY and set
2500 * it as the 3rd address operand for all 2D images.
2502 LLVMValueRef first_layer
, const5
, mask
;
2504 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2505 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2506 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2507 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2509 args
->coords
[count
] = first_layer
;
2515 args
->coords
[count
] = sample_index
;
2521 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2522 const nir_intrinsic_instr
*instr
,
2523 bool write
, bool atomic
)
2525 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2526 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2527 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2528 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2529 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2531 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2532 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2533 elem_count
, stride
, "");
2535 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2536 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2541 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2542 const nir_intrinsic_instr
*instr
,
2547 enum glsl_sampler_dim dim
;
2548 enum gl_access_qualifier access
;
2551 dim
= nir_intrinsic_image_dim(instr
);
2552 access
= nir_intrinsic_access(instr
);
2553 is_array
= nir_intrinsic_image_array(instr
);
2555 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2556 const struct glsl_type
*type
= image_deref
->type
;
2557 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2558 dim
= glsl_get_sampler_dim(type
);
2559 access
= var
->data
.access
;
2560 is_array
= glsl_sampler_type_is_array(type
);
2563 struct ac_image_args args
= {};
2565 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2567 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2568 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2569 unsigned num_channels
= util_last_bit(mask
);
2570 LLVMValueRef rsrc
, vindex
;
2572 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false, false);
2573 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2576 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2577 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2578 ctx
->ac
.i32_0
, num_channels
,
2581 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2583 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2584 res
= ac_to_integer(&ctx
->ac
, res
);
2586 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2588 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2589 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2590 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2591 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2593 args
.lod
= get_src(ctx
, instr
->src
[3]);
2595 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2597 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2602 static void visit_image_store(struct ac_nir_context
*ctx
,
2603 nir_intrinsic_instr
*instr
,
2608 enum glsl_sampler_dim dim
;
2609 enum gl_access_qualifier access
;
2612 dim
= nir_intrinsic_image_dim(instr
);
2613 access
= nir_intrinsic_access(instr
);
2614 is_array
= nir_intrinsic_image_array(instr
);
2616 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2617 const struct glsl_type
*type
= image_deref
->type
;
2618 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2619 dim
= glsl_get_sampler_dim(type
);
2620 access
= var
->data
.access
;
2621 is_array
= glsl_sampler_type_is_array(type
);
2624 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2625 struct ac_image_args args
= {};
2627 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2629 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2630 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true, false);
2631 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2632 unsigned src_channels
= ac_get_llvm_num_components(src
);
2633 LLVMValueRef vindex
;
2635 if (src_channels
== 3)
2636 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2638 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2639 get_src(ctx
, instr
->src
[1]),
2642 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2643 ctx
->ac
.i32_0
, src_channels
,
2646 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2648 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2649 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2650 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2651 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2652 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2654 args
.lod
= get_src(ctx
, instr
->src
[4]);
2657 ac_build_image_opcode(&ctx
->ac
, &args
);
2662 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2663 const nir_intrinsic_instr
*instr
,
2666 LLVMValueRef params
[7];
2667 int param_count
= 0;
2669 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2670 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2671 const char *atomic_name
;
2672 char intrinsic_name
[64];
2673 enum ac_atomic_op atomic_subop
;
2674 ASSERTED
int length
;
2676 enum glsl_sampler_dim dim
;
2679 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
2680 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
2681 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
2682 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
2683 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
2684 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2686 dim
= nir_intrinsic_image_dim(instr
);
2687 is_array
= nir_intrinsic_image_array(instr
);
2689 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2690 dim
= glsl_get_sampler_dim(type
);
2691 is_array
= glsl_sampler_type_is_array(type
);
2694 switch (instr
->intrinsic
) {
2695 case nir_intrinsic_bindless_image_atomic_add
:
2696 case nir_intrinsic_image_deref_atomic_add
:
2697 atomic_name
= "add";
2698 atomic_subop
= ac_atomic_add
;
2700 case nir_intrinsic_bindless_image_atomic_imin
:
2701 case nir_intrinsic_image_deref_atomic_imin
:
2702 atomic_name
= "smin";
2703 atomic_subop
= ac_atomic_smin
;
2705 case nir_intrinsic_bindless_image_atomic_umin
:
2706 case nir_intrinsic_image_deref_atomic_umin
:
2707 atomic_name
= "umin";
2708 atomic_subop
= ac_atomic_umin
;
2710 case nir_intrinsic_bindless_image_atomic_imax
:
2711 case nir_intrinsic_image_deref_atomic_imax
:
2712 atomic_name
= "smax";
2713 atomic_subop
= ac_atomic_smax
;
2715 case nir_intrinsic_bindless_image_atomic_umax
:
2716 case nir_intrinsic_image_deref_atomic_umax
:
2717 atomic_name
= "umax";
2718 atomic_subop
= ac_atomic_umax
;
2720 case nir_intrinsic_bindless_image_atomic_and
:
2721 case nir_intrinsic_image_deref_atomic_and
:
2722 atomic_name
= "and";
2723 atomic_subop
= ac_atomic_and
;
2725 case nir_intrinsic_bindless_image_atomic_or
:
2726 case nir_intrinsic_image_deref_atomic_or
:
2728 atomic_subop
= ac_atomic_or
;
2730 case nir_intrinsic_bindless_image_atomic_xor
:
2731 case nir_intrinsic_image_deref_atomic_xor
:
2732 atomic_name
= "xor";
2733 atomic_subop
= ac_atomic_xor
;
2735 case nir_intrinsic_bindless_image_atomic_exchange
:
2736 case nir_intrinsic_image_deref_atomic_exchange
:
2737 atomic_name
= "swap";
2738 atomic_subop
= ac_atomic_swap
;
2740 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2741 case nir_intrinsic_image_deref_atomic_comp_swap
:
2742 atomic_name
= "cmpswap";
2743 atomic_subop
= 0; /* not used */
2745 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
2746 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
2747 atomic_name
= "inc";
2748 atomic_subop
= ac_atomic_inc_wrap
;
2749 /* ATOMIC_INC instruction does:
2750 * value = (value + 1) % (data + 1)
2752 * value = (value + 1) % data
2753 * So replace 'data' by 'data - 1'.
2755 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
] =
2756 LLVMBuildSub(ctx
->ac
.builder
,
2757 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
],
2761 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
2762 case nir_intrinsic_image_deref_atomic_dec_wrap
:
2763 atomic_name
= "dec";
2764 atomic_subop
= ac_atomic_dec_wrap
;
2771 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2772 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2774 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2775 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true, true);
2776 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2777 ctx
->ac
.i32_0
, ""); /* vindex */
2778 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2779 if (LLVM_VERSION_MAJOR
>= 9) {
2780 /* XXX: The new raw/struct atomic intrinsics are buggy
2781 * with LLVM 8, see r358579.
2783 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2784 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2786 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2787 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2789 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2791 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2792 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2795 assert(length
< sizeof(intrinsic_name
));
2796 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2797 params
, param_count
, 0);
2799 struct ac_image_args args
= {};
2800 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2801 args
.atomic
= atomic_subop
;
2802 args
.data
[0] = params
[0];
2804 args
.data
[1] = params
[1];
2805 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2806 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2807 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2809 return ac_build_image_opcode(&ctx
->ac
, &args
);
2813 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2814 const nir_intrinsic_instr
*instr
)
2816 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2818 return ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
2821 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2822 const nir_intrinsic_instr
*instr
,
2827 enum glsl_sampler_dim dim
;
2830 dim
= nir_intrinsic_image_dim(instr
);
2831 is_array
= nir_intrinsic_image_array(instr
);
2833 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2834 dim
= glsl_get_sampler_dim(type
);
2835 is_array
= glsl_sampler_type_is_array(type
);
2838 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2839 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2841 struct ac_image_args args
= { 0 };
2843 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2845 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2846 args
.opcode
= ac_image_get_resinfo
;
2847 args
.lod
= ctx
->ac
.i32_0
;
2848 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2850 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2852 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2854 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2855 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2856 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2857 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2858 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2860 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2861 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2862 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2869 static void emit_membar(struct ac_llvm_context
*ac
,
2870 const nir_intrinsic_instr
*instr
)
2872 unsigned wait_flags
= 0;
2874 switch (instr
->intrinsic
) {
2875 case nir_intrinsic_memory_barrier
:
2876 case nir_intrinsic_group_memory_barrier
:
2877 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2879 case nir_intrinsic_memory_barrier_atomic_counter
:
2880 case nir_intrinsic_memory_barrier_buffer
:
2881 case nir_intrinsic_memory_barrier_image
:
2882 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2884 case nir_intrinsic_memory_barrier_shared
:
2885 wait_flags
= AC_WAIT_LGKM
;
2891 ac_build_waitcnt(ac
, wait_flags
);
2894 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2896 /* GFX6 only (thanks to a hw bug workaround):
2897 * The real barrier instruction isn’t needed, because an entire patch
2898 * always fits into a single wave.
2900 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
2901 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
2904 ac_build_s_barrier(ac
);
2907 static void emit_discard(struct ac_nir_context
*ctx
,
2908 const nir_intrinsic_instr
*instr
)
2912 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2913 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2914 get_src(ctx
, instr
->src
[0]),
2917 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2918 cond
= ctx
->ac
.i1false
;
2921 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2925 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2927 LLVMValueRef result
;
2928 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2929 result
= LLVMBuildAnd(ctx
->ac
.builder
,
2930 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
2931 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2933 if (ctx
->ac
.wave_size
== 32)
2934 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
2935 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
2937 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2941 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2943 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2944 LLVMValueRef result
;
2945 result
= LLVMBuildAnd(ctx
->ac
.builder
,
2946 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
2947 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2948 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2950 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2955 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2957 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2958 return LLVMBuildAnd(ctx
->ac
.builder
,
2959 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
2960 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2962 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2967 visit_first_invocation(struct ac_nir_context
*ctx
)
2969 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2970 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
2972 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2973 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2974 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
2975 ctx
->ac
.iN_wavemask
, args
, 2,
2976 AC_FUNC_ATTR_NOUNWIND
|
2977 AC_FUNC_ATTR_READNONE
);
2979 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2983 visit_load_shared(struct ac_nir_context
*ctx
,
2984 const nir_intrinsic_instr
*instr
)
2986 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2988 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
2989 instr
->dest
.ssa
.bit_size
);
2991 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2992 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2993 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2994 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2997 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2998 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3002 visit_store_shared(struct ac_nir_context
*ctx
,
3003 const nir_intrinsic_instr
*instr
)
3005 LLVMValueRef derived_ptr
, data
,index
;
3006 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3008 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3009 instr
->src
[0].ssa
->bit_size
);
3010 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3012 int writemask
= nir_intrinsic_write_mask(instr
);
3013 for (int chan
= 0; chan
< 4; chan
++) {
3014 if (!(writemask
& (1 << chan
))) {
3017 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3018 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3019 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3020 LLVMBuildStore(builder
, data
, derived_ptr
);
3024 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3025 const nir_intrinsic_instr
*instr
,
3026 LLVMValueRef ptr
, int src_idx
)
3028 LLVMValueRef result
;
3029 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3031 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3033 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3034 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3035 if (deref
->mode
== nir_var_mem_global
) {
3036 /* use "singlethread" sync scope to implement relaxed ordering */
3037 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3039 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3040 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3044 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3045 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3046 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3047 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3048 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3050 LLVMAtomicRMWBinOp op
;
3051 switch (instr
->intrinsic
) {
3052 case nir_intrinsic_shared_atomic_add
:
3053 case nir_intrinsic_deref_atomic_add
:
3054 op
= LLVMAtomicRMWBinOpAdd
;
3056 case nir_intrinsic_shared_atomic_umin
:
3057 case nir_intrinsic_deref_atomic_umin
:
3058 op
= LLVMAtomicRMWBinOpUMin
;
3060 case nir_intrinsic_shared_atomic_umax
:
3061 case nir_intrinsic_deref_atomic_umax
:
3062 op
= LLVMAtomicRMWBinOpUMax
;
3064 case nir_intrinsic_shared_atomic_imin
:
3065 case nir_intrinsic_deref_atomic_imin
:
3066 op
= LLVMAtomicRMWBinOpMin
;
3068 case nir_intrinsic_shared_atomic_imax
:
3069 case nir_intrinsic_deref_atomic_imax
:
3070 op
= LLVMAtomicRMWBinOpMax
;
3072 case nir_intrinsic_shared_atomic_and
:
3073 case nir_intrinsic_deref_atomic_and
:
3074 op
= LLVMAtomicRMWBinOpAnd
;
3076 case nir_intrinsic_shared_atomic_or
:
3077 case nir_intrinsic_deref_atomic_or
:
3078 op
= LLVMAtomicRMWBinOpOr
;
3080 case nir_intrinsic_shared_atomic_xor
:
3081 case nir_intrinsic_deref_atomic_xor
:
3082 op
= LLVMAtomicRMWBinOpXor
;
3084 case nir_intrinsic_shared_atomic_exchange
:
3085 case nir_intrinsic_deref_atomic_exchange
:
3086 op
= LLVMAtomicRMWBinOpXchg
;
3092 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
3097 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3099 LLVMValueRef values
[2];
3100 LLVMValueRef pos
[2];
3102 pos
[0] = ac_to_float(&ctx
->ac
,
3103 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3104 pos
[1] = ac_to_float(&ctx
->ac
,
3105 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3107 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3108 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3109 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3112 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3113 enum glsl_interp_mode interp
, unsigned location
)
3116 case INTERP_MODE_FLAT
:
3119 case INTERP_MODE_SMOOTH
:
3120 case INTERP_MODE_NONE
:
3121 if (location
== INTERP_CENTER
)
3122 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3123 else if (location
== INTERP_CENTROID
)
3124 return ctx
->abi
->persp_centroid
;
3125 else if (location
== INTERP_SAMPLE
)
3126 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3128 case INTERP_MODE_NOPERSPECTIVE
:
3129 if (location
== INTERP_CENTER
)
3130 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3131 else if (location
== INTERP_CENTROID
)
3132 return ctx
->abi
->linear_centroid
;
3133 else if (location
== INTERP_SAMPLE
)
3134 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3140 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3143 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3144 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3147 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3149 LLVMValueRef offset
)
3151 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3152 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3153 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3155 LLVMValueRef ij_out
[2];
3156 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3159 * take the I then J parameters, and the DDX/Y for it, and
3160 * calculate the IJ inputs for the interpolator.
3161 * temp1 = ddx * offset/sample.x + I;
3162 * interp_param.I = ddy * offset/sample.y + temp1;
3163 * temp1 = ddx * offset/sample.x + J;
3164 * interp_param.J = ddy * offset/sample.y + temp1;
3166 for (unsigned i
= 0; i
< 2; i
++) {
3167 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3168 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3169 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3170 ddxy_out
, ix_ll
, "");
3171 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3172 ddxy_out
, iy_ll
, "");
3173 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3174 interp_param
, ix_ll
, "");
3175 LLVMValueRef temp1
, temp2
;
3177 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3180 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3181 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3183 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3184 temp2
, ctx
->ac
.i32
, "");
3186 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3187 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3190 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3193 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3194 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3197 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3199 LLVMValueRef sample_id
)
3201 if (ctx
->abi
->interp_at_sample_force_center
)
3202 return barycentric_center(ctx
, mode
);
3204 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3206 /* fetch sample ID */
3207 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3209 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3210 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3211 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3212 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3213 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3214 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3216 return barycentric_offset(ctx
, mode
, offset
);
3220 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3223 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3224 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3227 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3228 LLVMValueRef interp_param
,
3229 unsigned index
, unsigned comp_start
,
3230 unsigned num_components
,
3233 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3235 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3236 interp_param
, ctx
->ac
.v2f32
, "");
3237 LLVMValueRef i
= LLVMBuildExtractElement(
3238 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3239 LLVMValueRef j
= LLVMBuildExtractElement(
3240 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3242 LLVMValueRef values
[4];
3243 assert(bitsize
== 16 || bitsize
== 32);
3244 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3245 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3246 if (bitsize
== 16) {
3247 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3248 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3250 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3251 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3255 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3258 static LLVMValueRef
load_flat_input(struct ac_nir_context
*ctx
,
3259 unsigned index
, unsigned comp_start
,
3260 unsigned num_components
,
3263 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3265 LLVMValueRef values
[8];
3267 /* Each component of a 64-bit value takes up two GL-level channels. */
3269 bit_size
== 64 ? num_components
* 2 : num_components
;
3271 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3272 if (comp_start
+ chan
> 4)
3273 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3274 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (comp_start
+ chan
) % 4, false);
3275 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3276 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3279 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3280 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3281 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3282 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3285 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3286 if (bit_size
== 64) {
3287 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3288 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3289 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3294 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3295 nir_intrinsic_instr
*instr
)
3297 LLVMValueRef result
= NULL
;
3299 switch (instr
->intrinsic
) {
3300 case nir_intrinsic_ballot
:
3301 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3302 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3303 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3305 case nir_intrinsic_read_invocation
:
3306 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3307 get_src(ctx
, instr
->src
[1]));
3309 case nir_intrinsic_read_first_invocation
:
3310 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3312 case nir_intrinsic_load_subgroup_invocation
:
3313 result
= ac_get_thread_id(&ctx
->ac
);
3315 case nir_intrinsic_load_work_group_id
: {
3316 LLVMValueRef values
[3];
3318 for (int i
= 0; i
< 3; i
++) {
3319 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3320 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3323 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3326 case nir_intrinsic_load_base_vertex
:
3327 case nir_intrinsic_load_first_vertex
:
3328 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3330 case nir_intrinsic_load_local_group_size
:
3331 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3333 case nir_intrinsic_load_vertex_id
:
3334 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3335 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3336 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3338 case nir_intrinsic_load_vertex_id_zero_base
: {
3339 result
= ctx
->abi
->vertex_id
;
3342 case nir_intrinsic_load_local_invocation_id
: {
3343 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3346 case nir_intrinsic_load_base_instance
:
3347 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3349 case nir_intrinsic_load_draw_id
:
3350 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3352 case nir_intrinsic_load_view_index
:
3353 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3355 case nir_intrinsic_load_invocation_id
:
3356 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3357 result
= ac_unpack_param(&ctx
->ac
,
3358 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3361 if (ctx
->ac
.chip_class
>= GFX10
) {
3362 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3363 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3364 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3366 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3370 case nir_intrinsic_load_primitive_id
:
3371 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3372 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3373 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3374 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3375 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3376 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3378 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3380 case nir_intrinsic_load_sample_id
:
3381 result
= ac_unpack_param(&ctx
->ac
,
3382 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3385 case nir_intrinsic_load_sample_pos
:
3386 result
= load_sample_pos(ctx
);
3388 case nir_intrinsic_load_sample_mask_in
:
3389 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3391 case nir_intrinsic_load_frag_coord
: {
3392 LLVMValueRef values
[4] = {
3393 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3394 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3395 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3396 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3397 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3399 result
= ac_to_integer(&ctx
->ac
,
3400 ac_build_gather_values(&ctx
->ac
, values
, 4));
3403 case nir_intrinsic_load_layer_id
:
3404 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3406 case nir_intrinsic_load_front_face
:
3407 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3409 case nir_intrinsic_load_helper_invocation
:
3410 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3412 case nir_intrinsic_load_color0
:
3413 result
= ctx
->abi
->color0
;
3415 case nir_intrinsic_load_color1
:
3416 result
= ctx
->abi
->color1
;
3418 case nir_intrinsic_load_user_data_amd
:
3419 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3420 result
= ctx
->abi
->user_data
;
3422 case nir_intrinsic_load_instance_id
:
3423 result
= ctx
->abi
->instance_id
;
3425 case nir_intrinsic_load_num_work_groups
:
3426 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3428 case nir_intrinsic_load_local_invocation_index
:
3429 result
= visit_load_local_invocation_index(ctx
);
3431 case nir_intrinsic_load_subgroup_id
:
3432 result
= visit_load_subgroup_id(ctx
);
3434 case nir_intrinsic_load_num_subgroups
:
3435 result
= visit_load_num_subgroups(ctx
);
3437 case nir_intrinsic_first_invocation
:
3438 result
= visit_first_invocation(ctx
);
3440 case nir_intrinsic_load_push_constant
:
3441 result
= visit_load_push_constant(ctx
, instr
);
3443 case nir_intrinsic_vulkan_resource_index
: {
3444 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3445 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3446 unsigned binding
= nir_intrinsic_binding(instr
);
3448 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3452 case nir_intrinsic_vulkan_resource_reindex
:
3453 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3455 case nir_intrinsic_store_ssbo
:
3456 visit_store_ssbo(ctx
, instr
);
3458 case nir_intrinsic_load_ssbo
:
3459 result
= visit_load_buffer(ctx
, instr
);
3461 case nir_intrinsic_ssbo_atomic_add
:
3462 case nir_intrinsic_ssbo_atomic_imin
:
3463 case nir_intrinsic_ssbo_atomic_umin
:
3464 case nir_intrinsic_ssbo_atomic_imax
:
3465 case nir_intrinsic_ssbo_atomic_umax
:
3466 case nir_intrinsic_ssbo_atomic_and
:
3467 case nir_intrinsic_ssbo_atomic_or
:
3468 case nir_intrinsic_ssbo_atomic_xor
:
3469 case nir_intrinsic_ssbo_atomic_exchange
:
3470 case nir_intrinsic_ssbo_atomic_comp_swap
:
3471 result
= visit_atomic_ssbo(ctx
, instr
);
3473 case nir_intrinsic_load_ubo
:
3474 result
= visit_load_ubo_buffer(ctx
, instr
);
3476 case nir_intrinsic_get_buffer_size
:
3477 result
= visit_get_buffer_size(ctx
, instr
);
3479 case nir_intrinsic_load_deref
:
3480 result
= visit_load_var(ctx
, instr
);
3482 case nir_intrinsic_store_deref
:
3483 visit_store_var(ctx
, instr
);
3485 case nir_intrinsic_load_shared
:
3486 result
= visit_load_shared(ctx
, instr
);
3488 case nir_intrinsic_store_shared
:
3489 visit_store_shared(ctx
, instr
);
3491 case nir_intrinsic_bindless_image_samples
:
3492 case nir_intrinsic_image_deref_samples
:
3493 result
= visit_image_samples(ctx
, instr
);
3495 case nir_intrinsic_bindless_image_load
:
3496 result
= visit_image_load(ctx
, instr
, true);
3498 case nir_intrinsic_image_deref_load
:
3499 result
= visit_image_load(ctx
, instr
, false);
3501 case nir_intrinsic_bindless_image_store
:
3502 visit_image_store(ctx
, instr
, true);
3504 case nir_intrinsic_image_deref_store
:
3505 visit_image_store(ctx
, instr
, false);
3507 case nir_intrinsic_bindless_image_atomic_add
:
3508 case nir_intrinsic_bindless_image_atomic_imin
:
3509 case nir_intrinsic_bindless_image_atomic_umin
:
3510 case nir_intrinsic_bindless_image_atomic_imax
:
3511 case nir_intrinsic_bindless_image_atomic_umax
:
3512 case nir_intrinsic_bindless_image_atomic_and
:
3513 case nir_intrinsic_bindless_image_atomic_or
:
3514 case nir_intrinsic_bindless_image_atomic_xor
:
3515 case nir_intrinsic_bindless_image_atomic_exchange
:
3516 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3517 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3518 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3519 result
= visit_image_atomic(ctx
, instr
, true);
3521 case nir_intrinsic_image_deref_atomic_add
:
3522 case nir_intrinsic_image_deref_atomic_imin
:
3523 case nir_intrinsic_image_deref_atomic_umin
:
3524 case nir_intrinsic_image_deref_atomic_imax
:
3525 case nir_intrinsic_image_deref_atomic_umax
:
3526 case nir_intrinsic_image_deref_atomic_and
:
3527 case nir_intrinsic_image_deref_atomic_or
:
3528 case nir_intrinsic_image_deref_atomic_xor
:
3529 case nir_intrinsic_image_deref_atomic_exchange
:
3530 case nir_intrinsic_image_deref_atomic_comp_swap
:
3531 case nir_intrinsic_image_deref_atomic_inc_wrap
:
3532 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3533 result
= visit_image_atomic(ctx
, instr
, false);
3535 case nir_intrinsic_bindless_image_size
:
3536 result
= visit_image_size(ctx
, instr
, true);
3538 case nir_intrinsic_image_deref_size
:
3539 result
= visit_image_size(ctx
, instr
, false);
3541 case nir_intrinsic_shader_clock
:
3542 result
= ac_build_shader_clock(&ctx
->ac
);
3544 case nir_intrinsic_discard
:
3545 case nir_intrinsic_discard_if
:
3546 emit_discard(ctx
, instr
);
3548 case nir_intrinsic_memory_barrier
:
3549 case nir_intrinsic_group_memory_barrier
:
3550 case nir_intrinsic_memory_barrier_atomic_counter
:
3551 case nir_intrinsic_memory_barrier_buffer
:
3552 case nir_intrinsic_memory_barrier_image
:
3553 case nir_intrinsic_memory_barrier_shared
:
3554 emit_membar(&ctx
->ac
, instr
);
3556 case nir_intrinsic_memory_barrier_tcs_patch
:
3558 case nir_intrinsic_control_barrier
:
3559 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3561 case nir_intrinsic_shared_atomic_add
:
3562 case nir_intrinsic_shared_atomic_imin
:
3563 case nir_intrinsic_shared_atomic_umin
:
3564 case nir_intrinsic_shared_atomic_imax
:
3565 case nir_intrinsic_shared_atomic_umax
:
3566 case nir_intrinsic_shared_atomic_and
:
3567 case nir_intrinsic_shared_atomic_or
:
3568 case nir_intrinsic_shared_atomic_xor
:
3569 case nir_intrinsic_shared_atomic_exchange
:
3570 case nir_intrinsic_shared_atomic_comp_swap
: {
3571 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3572 instr
->src
[1].ssa
->bit_size
);
3573 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3576 case nir_intrinsic_deref_atomic_add
:
3577 case nir_intrinsic_deref_atomic_imin
:
3578 case nir_intrinsic_deref_atomic_umin
:
3579 case nir_intrinsic_deref_atomic_imax
:
3580 case nir_intrinsic_deref_atomic_umax
:
3581 case nir_intrinsic_deref_atomic_and
:
3582 case nir_intrinsic_deref_atomic_or
:
3583 case nir_intrinsic_deref_atomic_xor
:
3584 case nir_intrinsic_deref_atomic_exchange
:
3585 case nir_intrinsic_deref_atomic_comp_swap
: {
3586 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3587 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3590 case nir_intrinsic_load_barycentric_pixel
:
3591 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
3593 case nir_intrinsic_load_barycentric_centroid
:
3594 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
3596 case nir_intrinsic_load_barycentric_sample
:
3597 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
3599 case nir_intrinsic_load_barycentric_at_offset
: {
3600 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3601 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
3604 case nir_intrinsic_load_barycentric_at_sample
: {
3605 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
3606 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
3609 case nir_intrinsic_load_interpolated_input
: {
3610 /* We assume any indirect loads have been lowered away */
3611 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
3613 assert(offset
[0].i32
== 0);
3615 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
3616 unsigned index
= nir_intrinsic_base(instr
);
3617 unsigned component
= nir_intrinsic_component(instr
);
3618 result
= load_interpolated_input(ctx
, interp_param
, index
,
3620 instr
->dest
.ssa
.num_components
,
3621 instr
->dest
.ssa
.bit_size
);
3624 case nir_intrinsic_load_input
: {
3625 /* We only lower inputs for fragment shaders ATM */
3626 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[0]);
3628 assert(offset
[0].i32
== 0);
3630 unsigned index
= nir_intrinsic_base(instr
);
3631 unsigned component
= nir_intrinsic_component(instr
);
3632 result
= load_flat_input(ctx
, index
, component
,
3633 instr
->dest
.ssa
.num_components
,
3634 instr
->dest
.ssa
.bit_size
);
3637 case nir_intrinsic_emit_vertex
:
3638 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3640 case nir_intrinsic_end_primitive
:
3641 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3643 case nir_intrinsic_load_tess_coord
:
3644 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3646 case nir_intrinsic_load_tess_level_outer
:
3647 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
3649 case nir_intrinsic_load_tess_level_inner
:
3650 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
3652 case nir_intrinsic_load_tess_level_outer_default
:
3653 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
3655 case nir_intrinsic_load_tess_level_inner_default
:
3656 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
3658 case nir_intrinsic_load_patch_vertices_in
:
3659 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3661 case nir_intrinsic_vote_all
: {
3662 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3663 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3666 case nir_intrinsic_vote_any
: {
3667 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3668 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3671 case nir_intrinsic_shuffle
:
3672 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3673 get_src(ctx
, instr
->src
[1]));
3675 case nir_intrinsic_reduce
:
3676 result
= ac_build_reduce(&ctx
->ac
,
3677 get_src(ctx
, instr
->src
[0]),
3678 instr
->const_index
[0],
3679 instr
->const_index
[1]);
3681 case nir_intrinsic_inclusive_scan
:
3682 result
= ac_build_inclusive_scan(&ctx
->ac
,
3683 get_src(ctx
, instr
->src
[0]),
3684 instr
->const_index
[0]);
3686 case nir_intrinsic_exclusive_scan
:
3687 result
= ac_build_exclusive_scan(&ctx
->ac
,
3688 get_src(ctx
, instr
->src
[0]),
3689 instr
->const_index
[0]);
3691 case nir_intrinsic_quad_broadcast
: {
3692 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
3693 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3694 lane
, lane
, lane
, lane
);
3697 case nir_intrinsic_quad_swap_horizontal
:
3698 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3700 case nir_intrinsic_quad_swap_vertical
:
3701 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3703 case nir_intrinsic_quad_swap_diagonal
:
3704 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3706 case nir_intrinsic_quad_swizzle_amd
: {
3707 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3708 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3709 mask
& 0x3, (mask
>> 2) & 0x3,
3710 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
3713 case nir_intrinsic_masked_swizzle_amd
: {
3714 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3715 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
3718 case nir_intrinsic_write_invocation_amd
:
3719 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3720 get_src(ctx
, instr
->src
[1]),
3721 get_src(ctx
, instr
->src
[2]));
3723 case nir_intrinsic_mbcnt_amd
:
3724 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3726 case nir_intrinsic_load_scratch
: {
3727 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
3728 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
3730 LLVMTypeRef comp_type
=
3731 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3732 LLVMTypeRef vec_type
=
3733 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
3734 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
3735 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3736 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
3737 LLVMPointerType(vec_type
, addr_space
), "");
3738 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
3741 case nir_intrinsic_store_scratch
: {
3742 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
3743 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
3745 LLVMTypeRef comp_type
=
3746 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
3747 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3748 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
3749 LLVMPointerType(comp_type
, addr_space
), "");
3750 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3751 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
3754 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
3756 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
3757 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
3758 LLVMTypeRef vec_type
=
3759 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
3760 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
3762 LLVMPointerType(vec_type
, addr_space
),
3764 LLVMValueRef offset_src
=
3765 ac_extract_components(&ctx
->ac
, src
, start
, count
);
3766 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
3770 case nir_intrinsic_load_constant
: {
3771 unsigned base
= nir_intrinsic_base(instr
);
3772 unsigned range
= nir_intrinsic_range(instr
);
3774 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
3775 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
3776 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
3778 /* Clamp the offset to avoid out-of-bound access because global
3779 * instructions can't handle them.
3781 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
3782 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
3784 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
3786 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
3788 LLVMTypeRef comp_type
=
3789 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
3790 LLVMTypeRef vec_type
=
3791 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
3792 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
3793 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3794 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
3795 LLVMPointerType(vec_type
, addr_space
), "");
3796 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
3800 fprintf(stderr
, "Unknown intrinsic: ");
3801 nir_print_instr(&instr
->instr
, stderr
);
3802 fprintf(stderr
, "\n");
3806 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3810 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3811 unsigned base_index
,
3812 unsigned constant_index
,
3813 LLVMValueRef dynamic_index
)
3815 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3816 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3817 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3819 /* Bindless uniforms are 64bit so multiple index by 8 */
3820 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3821 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3823 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3825 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3826 NULL
, 0, 0, true, true);
3828 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3831 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3832 nir_deref_instr
*deref_instr
,
3833 enum ac_descriptor_type desc_type
,
3834 const nir_instr
*instr
,
3835 bool image
, bool write
)
3837 LLVMValueRef index
= NULL
;
3838 unsigned constant_index
= 0;
3839 unsigned descriptor_set
;
3840 unsigned base_index
;
3841 bool bindless
= false;
3846 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3849 index
= get_src(ctx
, img_instr
->src
[0]);
3851 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3852 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3853 nir_tex_src_sampler_handle
);
3854 if (sampSrcIdx
!= -1) {
3857 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3859 assert(tex_instr
&& !image
);
3860 base_index
= tex_instr
->sampler_index
;
3864 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3865 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3866 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3870 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3871 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
3873 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3875 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3876 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3881 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3884 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3885 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3886 unsigned sidx
= deref_instr
->strct
.index
;
3887 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3888 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3890 unreachable("Unsupported deref type");
3893 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3895 if (deref_instr
->var
->data
.bindless
) {
3896 /* For now just assert on unhandled variable types */
3897 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3899 base_index
= deref_instr
->var
->data
.driver_location
;
3902 index
= index
? index
: ctx
->ac
.i32_0
;
3903 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3904 constant_index
, index
);
3906 base_index
= deref_instr
->var
->data
.binding
;
3909 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3912 constant_index
, index
,
3913 desc_type
, image
, write
, bindless
);
3916 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3919 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3920 * filtering manually. The driver sets img7 to a mask clearing
3921 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3922 * s_and_b32 samp0, samp0, img7
3925 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3927 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3928 LLVMValueRef res
, LLVMValueRef samp
)
3930 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3931 LLVMValueRef img7
, samp0
;
3933 if (ctx
->ac
.chip_class
>= GFX8
)
3936 img7
= LLVMBuildExtractElement(builder
, res
,
3937 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3938 samp0
= LLVMBuildExtractElement(builder
, samp
,
3939 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3940 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3941 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3942 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3945 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3946 nir_tex_instr
*instr
,
3947 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3948 LLVMValueRef
*fmask_ptr
)
3950 nir_deref_instr
*texture_deref_instr
= NULL
;
3951 nir_deref_instr
*sampler_deref_instr
= NULL
;
3954 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3955 switch (instr
->src
[i
].src_type
) {
3956 case nir_tex_src_texture_deref
:
3957 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3959 case nir_tex_src_sampler_deref
:
3960 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3962 case nir_tex_src_plane
:
3963 plane
= nir_src_as_int(instr
->src
[i
].src
);
3970 if (!sampler_deref_instr
)
3971 sampler_deref_instr
= texture_deref_instr
;
3973 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
3976 assert(instr
->op
!= nir_texop_txf_ms
&&
3977 instr
->op
!= nir_texop_samples_identical
);
3978 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
3980 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
3983 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
, false, false);
3986 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3987 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3988 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3990 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3991 instr
->op
== nir_texop_samples_identical
))
3992 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3995 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3998 coord
= ac_to_float(ctx
, coord
);
3999 coord
= ac_build_round(ctx
, coord
);
4000 coord
= ac_to_integer(ctx
, coord
);
4004 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4006 LLVMValueRef result
= NULL
;
4007 struct ac_image_args args
= { 0 };
4008 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4009 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4010 unsigned offset_src
= 0;
4012 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4014 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4015 switch (instr
->src
[i
].src_type
) {
4016 case nir_tex_src_coord
: {
4017 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4018 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4019 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4022 case nir_tex_src_projector
:
4024 case nir_tex_src_comparator
:
4025 if (instr
->is_shadow
) {
4026 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4027 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4030 case nir_tex_src_offset
:
4031 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4034 case nir_tex_src_bias
:
4035 if (instr
->op
== nir_texop_txb
)
4036 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4038 case nir_tex_src_lod
: {
4039 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4040 args
.level_zero
= true;
4042 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4045 case nir_tex_src_ms_index
:
4046 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4048 case nir_tex_src_ms_mcs
:
4050 case nir_tex_src_ddx
:
4051 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4053 case nir_tex_src_ddy
:
4054 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4056 case nir_tex_src_texture_offset
:
4057 case nir_tex_src_sampler_offset
:
4058 case nir_tex_src_plane
:
4064 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4065 result
= get_buffer_size(ctx
, args
.resource
, true);
4069 if (instr
->op
== nir_texop_texture_samples
) {
4070 LLVMValueRef res
, samples
, is_msaa
;
4071 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4072 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4073 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4074 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4075 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4076 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4077 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4078 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4079 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4081 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4082 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4083 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4084 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4085 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4087 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4093 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4094 LLVMValueRef offset
[3], pack
;
4095 for (unsigned chan
= 0; chan
< 3; ++chan
)
4096 offset
[chan
] = ctx
->ac
.i32_0
;
4098 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4099 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4100 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4101 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4102 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4104 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4105 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4107 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4108 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4112 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4113 * OpenGL 4.5 spec says:
4115 * "If the texture’s internal format indicates a fixed-point
4116 * depth texture, then D_t and D_ref are clamped to the
4117 * range [0, 1]; otherwise no clamping is performed."
4119 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4120 * so the depth comparison value isn't clamped for Z16 and
4121 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4122 * an explicitly clamped 32-bit float format.
4125 ctx
->ac
.chip_class
>= GFX8
&&
4126 ctx
->ac
.chip_class
<= GFX9
&&
4127 ctx
->abi
->clamp_shadow_reference
) {
4128 LLVMValueRef upgraded
, clamped
;
4130 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4131 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4132 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4133 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4134 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4135 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4136 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4140 /* pack derivatives */
4142 int num_src_deriv_channels
, num_dest_deriv_channels
;
4143 switch (instr
->sampler_dim
) {
4144 case GLSL_SAMPLER_DIM_3D
:
4145 case GLSL_SAMPLER_DIM_CUBE
:
4146 num_src_deriv_channels
= 3;
4147 num_dest_deriv_channels
= 3;
4149 case GLSL_SAMPLER_DIM_2D
:
4151 num_src_deriv_channels
= 2;
4152 num_dest_deriv_channels
= 2;
4154 case GLSL_SAMPLER_DIM_1D
:
4155 num_src_deriv_channels
= 1;
4156 if (ctx
->ac
.chip_class
== GFX9
) {
4157 num_dest_deriv_channels
= 2;
4159 num_dest_deriv_channels
= 1;
4164 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4165 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4166 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4167 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4168 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4170 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4171 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4172 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4176 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4177 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4178 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4179 if (instr
->coord_components
== 3)
4180 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4181 ac_prepare_cube_coords(&ctx
->ac
,
4182 instr
->op
== nir_texop_txd
, instr
->is_array
,
4183 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4186 /* Texture coordinates fixups */
4187 if (instr
->coord_components
> 1 &&
4188 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4190 instr
->op
!= nir_texop_txf
) {
4191 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4194 if (instr
->coord_components
> 2 &&
4195 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4196 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4197 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4198 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4200 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4201 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4204 if (ctx
->ac
.chip_class
== GFX9
&&
4205 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4206 instr
->op
!= nir_texop_lod
) {
4207 LLVMValueRef filler
;
4208 if (instr
->op
== nir_texop_txf
)
4209 filler
= ctx
->ac
.i32_0
;
4211 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4213 if (instr
->is_array
)
4214 args
.coords
[2] = args
.coords
[1];
4215 args
.coords
[1] = filler
;
4218 /* Pack sample index */
4219 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
4220 args
.coords
[instr
->coord_components
] = sample_index
;
4222 if (instr
->op
== nir_texop_samples_identical
) {
4223 struct ac_image_args txf_args
= { 0 };
4224 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4226 txf_args
.dmask
= 0xf;
4227 txf_args
.resource
= fmask_ptr
;
4228 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4229 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4231 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4232 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4236 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4237 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4238 instr
->op
!= nir_texop_txs
) {
4239 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4240 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4241 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4242 instr
->is_array
? args
.coords
[2] : NULL
,
4243 args
.coords
[sample_chan
], fmask_ptr
);
4246 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4247 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4248 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4249 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4250 args
.coords
[i
] = LLVMBuildAdd(
4251 ctx
->ac
.builder
, args
.coords
[i
],
4252 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4257 /* DMASK was repurposed for GATHER4. 4 components are always
4258 * returned and DMASK works like a swizzle - it selects
4259 * the component to fetch. The only valid DMASK values are
4260 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4261 * (red,red,red,red) etc.) The ISA document doesn't mention
4265 if (instr
->op
== nir_texop_tg4
) {
4266 if (instr
->is_shadow
)
4269 args
.dmask
= 1 << instr
->component
;
4272 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4273 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4274 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4276 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4278 if (instr
->op
== nir_texop_query_levels
)
4279 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4280 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4281 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4282 instr
->op
!= nir_texop_tg4
)
4283 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4284 else if (instr
->op
== nir_texop_txs
&&
4285 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4287 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4288 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4289 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4290 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4291 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4292 } else if (ctx
->ac
.chip_class
== GFX9
&&
4293 instr
->op
== nir_texop_txs
&&
4294 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4296 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4297 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4298 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4300 } else if (instr
->dest
.ssa
.num_components
!= 4)
4301 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4305 assert(instr
->dest
.is_ssa
);
4306 result
= ac_to_integer(&ctx
->ac
, result
);
4307 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4312 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4314 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4315 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4317 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4318 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4321 static void visit_post_phi(struct ac_nir_context
*ctx
,
4322 nir_phi_instr
*instr
,
4323 LLVMValueRef llvm_phi
)
4325 nir_foreach_phi_src(src
, instr
) {
4326 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4327 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4329 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4333 static void phi_post_pass(struct ac_nir_context
*ctx
)
4335 hash_table_foreach(ctx
->phis
, entry
) {
4336 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4337 (LLVMValueRef
)entry
->data
);
4342 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4343 const nir_ssa_undef_instr
*instr
)
4345 unsigned num_components
= instr
->def
.num_components
;
4346 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4349 if (num_components
== 1)
4350 undef
= LLVMGetUndef(type
);
4352 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4354 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4357 static void visit_jump(struct ac_llvm_context
*ctx
,
4358 const nir_jump_instr
*instr
)
4360 switch (instr
->type
) {
4361 case nir_jump_break
:
4362 ac_build_break(ctx
);
4364 case nir_jump_continue
:
4365 ac_build_continue(ctx
);
4368 fprintf(stderr
, "Unknown NIR jump instr: ");
4369 nir_print_instr(&instr
->instr
, stderr
);
4370 fprintf(stderr
, "\n");
4376 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4377 enum glsl_base_type type
)
4381 case GLSL_TYPE_UINT
:
4382 case GLSL_TYPE_BOOL
:
4383 case GLSL_TYPE_SUBROUTINE
:
4385 case GLSL_TYPE_INT8
:
4386 case GLSL_TYPE_UINT8
:
4388 case GLSL_TYPE_INT16
:
4389 case GLSL_TYPE_UINT16
:
4391 case GLSL_TYPE_FLOAT
:
4393 case GLSL_TYPE_FLOAT16
:
4395 case GLSL_TYPE_INT64
:
4396 case GLSL_TYPE_UINT64
:
4398 case GLSL_TYPE_DOUBLE
:
4401 unreachable("unknown GLSL type");
4406 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4407 const struct glsl_type
*type
)
4409 if (glsl_type_is_scalar(type
)) {
4410 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4413 if (glsl_type_is_vector(type
)) {
4414 return LLVMVectorType(
4415 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4416 glsl_get_vector_elements(type
));
4419 if (glsl_type_is_matrix(type
)) {
4420 return LLVMArrayType(
4421 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4422 glsl_get_matrix_columns(type
));
4425 if (glsl_type_is_array(type
)) {
4426 return LLVMArrayType(
4427 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4428 glsl_get_length(type
));
4431 assert(glsl_type_is_struct_or_ifc(type
));
4433 LLVMTypeRef member_types
[glsl_get_length(type
)];
4435 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4437 glsl_to_llvm_type(ac
,
4438 glsl_get_struct_field(type
, i
));
4441 return LLVMStructTypeInContext(ac
->context
, member_types
,
4442 glsl_get_length(type
), false);
4445 static void visit_deref(struct ac_nir_context
*ctx
,
4446 nir_deref_instr
*instr
)
4448 if (instr
->mode
!= nir_var_mem_shared
&&
4449 instr
->mode
!= nir_var_mem_global
)
4452 LLVMValueRef result
= NULL
;
4453 switch(instr
->deref_type
) {
4454 case nir_deref_type_var
: {
4455 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4456 result
= entry
->data
;
4459 case nir_deref_type_struct
:
4460 if (instr
->mode
== nir_var_mem_global
) {
4461 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4462 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4463 instr
->strct
.index
);
4464 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4465 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4467 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4468 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4471 case nir_deref_type_array
:
4472 if (instr
->mode
== nir_var_mem_global
) {
4473 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4474 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4476 if ((glsl_type_is_matrix(parent
->type
) &&
4477 glsl_matrix_type_is_row_major(parent
->type
)) ||
4478 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4479 stride
= type_scalar_size_bytes(parent
->type
);
4482 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4483 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4484 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4486 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4488 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4490 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4491 get_src(ctx
, instr
->arr
.index
));
4494 case nir_deref_type_ptr_as_array
:
4495 if (instr
->mode
== nir_var_mem_global
) {
4496 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4498 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4499 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4500 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4502 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4504 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4506 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4507 get_src(ctx
, instr
->arr
.index
));
4510 case nir_deref_type_cast
: {
4511 result
= get_src(ctx
, instr
->parent
);
4513 /* We can't use the structs from LLVM because the shader
4514 * specifies its own offsets. */
4515 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4516 if (instr
->mode
== nir_var_mem_shared
)
4517 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4519 unsigned address_space
;
4521 switch(instr
->mode
) {
4522 case nir_var_mem_shared
:
4523 address_space
= AC_ADDR_SPACE_LDS
;
4525 case nir_var_mem_global
:
4526 address_space
= AC_ADDR_SPACE_GLOBAL
;
4529 unreachable("Unhandled address space");
4532 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4534 if (LLVMTypeOf(result
) != type
) {
4535 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4536 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4539 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4546 unreachable("Unhandled deref_instr deref type");
4549 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4552 static void visit_cf_list(struct ac_nir_context
*ctx
,
4553 struct exec_list
*list
);
4555 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4557 nir_foreach_instr(instr
, block
)
4559 switch (instr
->type
) {
4560 case nir_instr_type_alu
:
4561 visit_alu(ctx
, nir_instr_as_alu(instr
));
4563 case nir_instr_type_load_const
:
4564 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4566 case nir_instr_type_intrinsic
:
4567 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4569 case nir_instr_type_tex
:
4570 visit_tex(ctx
, nir_instr_as_tex(instr
));
4572 case nir_instr_type_phi
:
4573 visit_phi(ctx
, nir_instr_as_phi(instr
));
4575 case nir_instr_type_ssa_undef
:
4576 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4578 case nir_instr_type_jump
:
4579 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4581 case nir_instr_type_deref
:
4582 visit_deref(ctx
, nir_instr_as_deref(instr
));
4585 fprintf(stderr
, "Unknown NIR instr type: ");
4586 nir_print_instr(instr
, stderr
);
4587 fprintf(stderr
, "\n");
4592 _mesa_hash_table_insert(ctx
->defs
, block
,
4593 LLVMGetInsertBlock(ctx
->ac
.builder
));
4596 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4598 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4600 nir_block
*then_block
=
4601 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4603 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4605 visit_cf_list(ctx
, &if_stmt
->then_list
);
4607 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4608 nir_block
*else_block
=
4609 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4611 ac_build_else(&ctx
->ac
, else_block
->index
);
4612 visit_cf_list(ctx
, &if_stmt
->else_list
);
4615 ac_build_endif(&ctx
->ac
, then_block
->index
);
4618 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4620 nir_block
*first_loop_block
=
4621 (nir_block
*) exec_list_get_head(&loop
->body
);
4623 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4625 visit_cf_list(ctx
, &loop
->body
);
4627 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4630 static void visit_cf_list(struct ac_nir_context
*ctx
,
4631 struct exec_list
*list
)
4633 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4635 switch (node
->type
) {
4636 case nir_cf_node_block
:
4637 visit_block(ctx
, nir_cf_node_as_block(node
));
4640 case nir_cf_node_if
:
4641 visit_if(ctx
, nir_cf_node_as_if(node
));
4644 case nir_cf_node_loop
:
4645 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4655 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4656 struct ac_shader_abi
*abi
,
4657 struct nir_shader
*nir
,
4658 struct nir_variable
*variable
,
4659 gl_shader_stage stage
)
4661 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4662 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4664 /* tess ctrl has it's own load/store paths for outputs */
4665 if (stage
== MESA_SHADER_TESS_CTRL
)
4668 if (stage
== MESA_SHADER_VERTEX
||
4669 stage
== MESA_SHADER_TESS_EVAL
||
4670 stage
== MESA_SHADER_GEOMETRY
) {
4671 int idx
= variable
->data
.location
+ variable
->data
.index
;
4672 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4673 int length
= nir
->info
.clip_distance_array_size
+
4674 nir
->info
.cull_distance_array_size
;
4683 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4684 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4685 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4686 for (unsigned chan
= 0; chan
< 4; chan
++) {
4687 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4688 ac_build_alloca_undef(ctx
, type
, "");
4694 setup_locals(struct ac_nir_context
*ctx
,
4695 struct nir_function
*func
)
4698 ctx
->num_locals
= 0;
4699 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4700 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4701 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4702 variable
->data
.location_frac
= 0;
4703 ctx
->num_locals
+= attrib_count
;
4705 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4709 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4710 for (j
= 0; j
< 4; j
++) {
4711 ctx
->locals
[i
* 4 + j
] =
4712 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4718 setup_scratch(struct ac_nir_context
*ctx
,
4719 struct nir_shader
*shader
)
4721 if (shader
->scratch_size
== 0)
4724 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
4725 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
4730 setup_constant_data(struct ac_nir_context
*ctx
,
4731 struct nir_shader
*shader
)
4733 if (!shader
->constant_data
)
4737 LLVMConstStringInContext(ctx
->ac
.context
,
4738 shader
->constant_data
,
4739 shader
->constant_data_size
,
4741 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
4743 /* We want to put the constant data in the CONST address space so that
4744 * we can use scalar loads. However, LLVM versions before 10 put these
4745 * variables in the same section as the code, which is unacceptable
4746 * for RadeonSI as it needs to relocate all the data sections after
4747 * the code sections. See https://reviews.llvm.org/D65813.
4749 unsigned address_space
=
4750 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
4752 LLVMValueRef global
=
4753 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
4757 LLVMSetInitializer(global
, data
);
4758 LLVMSetGlobalConstant(global
, true);
4759 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
4760 ctx
->constant_data
= global
;
4764 setup_shared(struct ac_nir_context
*ctx
,
4765 struct nir_shader
*nir
)
4770 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
4771 nir
->info
.cs
.shared_size
);
4774 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
4777 LLVMSetAlignment(lds
, 64 * 1024);
4779 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
4780 LLVMPointerType(ctx
->ac
.i8
,
4781 AC_ADDR_SPACE_LDS
), "");
4784 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4785 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
4787 struct ac_nir_context ctx
= {};
4788 struct nir_function
*func
;
4794 ctx
.stage
= nir
->info
.stage
;
4795 ctx
.info
= &nir
->info
;
4797 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4799 nir_foreach_variable(variable
, &nir
->outputs
)
4800 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4803 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4804 _mesa_key_pointer_equal
);
4805 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4806 _mesa_key_pointer_equal
);
4807 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4808 _mesa_key_pointer_equal
);
4810 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4812 nir_index_ssa_defs(func
->impl
);
4813 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4815 setup_locals(&ctx
, func
);
4816 setup_scratch(&ctx
, nir
);
4817 setup_constant_data(&ctx
, nir
);
4819 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4820 setup_shared(&ctx
, nir
);
4822 visit_cf_list(&ctx
, &func
->impl
->body
);
4823 phi_post_pass(&ctx
);
4825 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4826 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4831 ralloc_free(ctx
.defs
);
4832 ralloc_free(ctx
.phis
);
4833 ralloc_free(ctx
.vars
);
4837 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4839 bool progress
= false;
4841 /* Lower large variables to scratch first so that we won't bloat the
4842 * shader by generating large if ladders for them. We later lower
4843 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
4845 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
4846 nir_var_function_temp
,
4848 glsl_get_natural_size_align_bytes
);
4850 /* While it would be nice not to have this flag, we are constrained
4851 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
4853 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
4855 /* TODO: Indirect indexing of GS inputs is unimplemented.
4857 * TCS and TES load inputs directly from LDS or offchip memory, so
4858 * indirect indexing is trivial.
4860 nir_variable_mode indirect_mask
= 0;
4861 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4862 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4863 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4864 !llvm_has_working_vgpr_indexing
)) {
4865 indirect_mask
|= nir_var_shader_in
;
4867 if (!llvm_has_working_vgpr_indexing
&&
4868 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4869 indirect_mask
|= nir_var_shader_out
;
4871 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4872 * smart enough to handle indirects without causing excess spilling
4873 * causing the gpu to hang.
4875 * See the following thread for more details of the problem:
4876 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4878 indirect_mask
|= nir_var_function_temp
;
4880 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
);
4885 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4887 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4891 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4893 if (var
->data
.mode
!= nir_var_shader_out
)
4896 unsigned writemask
= 0;
4897 const int location
= var
->data
.location
;
4898 unsigned first_component
= var
->data
.location_frac
;
4899 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4901 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4902 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4903 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4904 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4910 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4911 unsigned *cond_block_tf_writemask
,
4912 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4914 switch (cf_node
->type
) {
4915 case nir_cf_node_block
: {
4916 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4917 nir_foreach_instr(instr
, block
) {
4918 if (instr
->type
!= nir_instr_type_intrinsic
)
4921 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4922 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
4924 /* If we find a barrier in nested control flow put this in the
4925 * too hard basket. In GLSL this is not possible but it is in
4929 *tessfactors_are_def_in_all_invocs
= false;
4933 /* The following case must be prevented:
4934 * gl_TessLevelInner = ...;
4936 * if (gl_InvocationID == 1)
4937 * gl_TessLevelInner = ...;
4939 * If you consider disjoint code segments separated by barriers, each
4940 * such segment that writes tess factor channels should write the same
4941 * channels in all codepaths within that segment.
4943 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4944 /* Accumulate the result: */
4945 *tessfactors_are_def_in_all_invocs
&=
4946 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4948 /* Analyze the next code segment from scratch. */
4949 *upper_block_tf_writemask
= 0;
4950 *cond_block_tf_writemask
= 0;
4953 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4958 case nir_cf_node_if
: {
4959 unsigned then_tessfactor_writemask
= 0;
4960 unsigned else_tessfactor_writemask
= 0;
4962 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4963 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4964 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4965 cond_block_tf_writemask
,
4966 tessfactors_are_def_in_all_invocs
, true);
4969 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4970 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4971 cond_block_tf_writemask
,
4972 tessfactors_are_def_in_all_invocs
, true);
4975 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4976 /* If both statements write the same tess factor channels,
4977 * we can say that the upper block writes them too.
4979 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4980 else_tessfactor_writemask
;
4981 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4982 else_tessfactor_writemask
;
4987 case nir_cf_node_loop
: {
4988 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4989 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4990 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4991 cond_block_tf_writemask
,
4992 tessfactors_are_def_in_all_invocs
, true);
4998 unreachable("unknown cf node type");
5003 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5005 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5007 /* The pass works as follows:
5008 * If all codepaths write tess factors, we can say that all
5009 * invocations define tess factors.
5011 * Each tess factor channel is tracked separately.
5013 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5014 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5016 /* Initial value = true. Here the pass will accumulate results from
5017 * multiple segments surrounded by barriers. If tess factors aren't
5018 * written at all, it's a shader bug and we don't care if this will be
5021 bool tessfactors_are_def_in_all_invocs
= true;
5023 nir_foreach_function(function
, nir
) {
5024 if (function
->impl
) {
5025 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5026 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5027 &cond_block_tf_writemask
,
5028 &tessfactors_are_def_in_all_invocs
,
5034 /* Accumulate the result for the last code segment separated by a
5037 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5038 tessfactors_are_def_in_all_invocs
&=
5039 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5042 return tessfactors_are_def_in_all_invocs
;