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_index(struct ac_nir_context
*ctx
,
64 nir_deref_instr
*deref_instr
,
65 const nir_instr
*instr
,
68 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
69 nir_deref_instr
*deref_instr
,
70 enum ac_descriptor_type desc_type
,
71 const nir_instr
*instr
,
73 bool image
, bool write
);
76 build_store_values_extended(struct ac_llvm_context
*ac
,
79 unsigned value_stride
,
82 LLVMBuilderRef builder
= ac
->builder
;
85 for (i
= 0; i
< value_count
; i
++) {
86 LLVMValueRef ptr
= values
[i
* value_stride
];
87 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
88 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
89 LLVMBuildStore(builder
, value
, ptr
);
93 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
94 const nir_ssa_def
*def
)
96 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
97 if (def
->num_components
> 1) {
98 type
= LLVMVectorType(type
, def
->num_components
);
103 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
106 return nir
->ssa_defs
[src
.ssa
->index
];
110 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
, unsigned bit_size
)
112 LLVMValueRef ptr
= get_src(ctx
, src
);
113 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
114 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
116 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, bit_size
);
118 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
119 LLVMPointerType(type
, addr_space
), "");
122 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
123 const struct nir_block
*b
)
125 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
126 return (LLVMBasicBlockRef
)entry
->data
;
129 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
131 unsigned num_components
)
133 LLVMValueRef value
= get_src(ctx
, src
.src
);
134 bool need_swizzle
= false;
137 unsigned src_components
= ac_get_llvm_num_components(value
);
138 for (unsigned i
= 0; i
< num_components
; ++i
) {
139 assert(src
.swizzle
[i
] < src_components
);
140 if (src
.swizzle
[i
] != i
)
144 if (need_swizzle
|| num_components
!= src_components
) {
145 LLVMValueRef masks
[] = {
146 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
147 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
148 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
149 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
151 if (src_components
> 1 && num_components
== 1) {
152 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
154 } else if (src_components
== 1 && num_components
> 1) {
155 LLVMValueRef values
[] = {value
, value
, value
, value
};
156 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
158 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
159 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
168 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
169 LLVMIntPredicate pred
, LLVMValueRef src0
,
172 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
173 return LLVMBuildSelect(ctx
->builder
, result
,
174 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
178 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
179 LLVMRealPredicate pred
, LLVMValueRef src0
,
183 src0
= ac_to_float(ctx
, src0
);
184 src1
= ac_to_float(ctx
, src1
);
185 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
186 return LLVMBuildSelect(ctx
->builder
, result
,
187 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
191 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
193 LLVMTypeRef result_type
,
197 LLVMValueRef params
[] = {
198 ac_to_float(ctx
, src0
),
201 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
202 ac_get_elem_bits(ctx
, result_type
));
203 assert(length
< sizeof(name
));
204 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
207 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
209 LLVMTypeRef result_type
,
210 LLVMValueRef src0
, LLVMValueRef src1
)
213 LLVMValueRef params
[] = {
214 ac_to_float(ctx
, src0
),
215 ac_to_float(ctx
, src1
),
218 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
219 ac_get_elem_bits(ctx
, result_type
));
220 assert(length
< sizeof(name
));
221 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
224 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
226 LLVMTypeRef result_type
,
227 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
230 LLVMValueRef params
[] = {
231 ac_to_float(ctx
, src0
),
232 ac_to_float(ctx
, src1
),
233 ac_to_float(ctx
, src2
),
236 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
237 ac_get_elem_bits(ctx
, result_type
));
238 assert(length
< sizeof(name
));
239 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
242 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
243 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
245 LLVMTypeRef src1_type
= LLVMTypeOf(src1
);
246 LLVMTypeRef src2_type
= LLVMTypeOf(src2
);
248 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
250 if (LLVMGetTypeKind(src1_type
) == LLVMPointerTypeKind
&&
251 LLVMGetTypeKind(src2_type
) != LLVMPointerTypeKind
) {
252 src2
= LLVMBuildIntToPtr(ctx
->builder
, src2
, src1_type
, "");
253 } else if (LLVMGetTypeKind(src2_type
) == LLVMPointerTypeKind
&&
254 LLVMGetTypeKind(src1_type
) != LLVMPointerTypeKind
) {
255 src1
= LLVMBuildIntToPtr(ctx
->builder
, src1
, src2_type
, "");
258 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
260 return LLVMBuildSelect(ctx
->builder
, v
,
261 ac_to_integer_or_pointer(ctx
, src1
),
262 ac_to_integer_or_pointer(ctx
, src2
), "");
265 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
268 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
271 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
273 LLVMValueRef src0
, LLVMValueRef src1
)
275 LLVMTypeRef ret_type
;
276 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
278 LLVMValueRef params
[] = { src0
, src1
};
279 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
282 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
283 params
, 2, AC_FUNC_ATTR_READNONE
);
285 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
286 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
290 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
294 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
295 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
297 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
301 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
305 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
307 unreachable("Unsupported bit size.");
311 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
314 src0
= ac_to_float(ctx
, src0
);
315 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
316 return LLVMBuildSExt(ctx
->builder
,
317 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
321 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
325 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
329 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
331 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
335 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
337 unreachable("Unsupported bit size.");
341 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
344 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
345 return LLVMBuildSExt(ctx
->builder
,
346 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
350 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
354 LLVMValueRef cond
= NULL
;
356 src0
= ac_to_float(ctx
, src0
);
357 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
359 if (ctx
->chip_class
>= GFX8
) {
360 LLVMValueRef args
[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
363 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
364 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
367 /* need to convert back up to f32 */
368 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
370 if (ctx
->chip_class
>= GFX8
)
371 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
377 LLVMValueRef temp
, cond2
;
378 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
379 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
,
380 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
382 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealONE
,
383 temp
, ctx
->f32_0
, "");
384 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
385 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
391 LLVMValueRef src0
, LLVMValueRef src1
)
393 LLVMValueRef dst64
, result
;
394 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
395 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
397 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
398 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
399 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
403 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
404 LLVMValueRef src0
, LLVMValueRef src1
)
406 LLVMValueRef dst64
, result
;
407 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
408 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
410 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
411 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
412 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
416 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
417 LLVMValueRef bits
, LLVMValueRef offset
)
419 /* mask = ((1 << bits) - 1) << offset */
420 return LLVMBuildShl(ctx
->builder
,
421 LLVMBuildSub(ctx
->builder
,
422 LLVMBuildShl(ctx
->builder
,
429 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
430 LLVMValueRef mask
, LLVMValueRef insert
,
434 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
435 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
437 return LLVMBuildXor(ctx
->builder
, base
,
438 LLVMBuildAnd(ctx
->builder
, mask
,
439 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
442 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
444 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
445 LLVMValueRef args
[2]))
447 LLVMValueRef comp
[2];
449 src0
= ac_to_float(ctx
, src0
);
450 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
451 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
453 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
456 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
459 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
460 LLVMValueRef temps
[2], val
;
463 for (i
= 0; i
< 2; i
++) {
464 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
465 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
466 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
467 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
469 return ac_build_gather_values(ctx
, temps
, 2);
472 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
480 if (op
== nir_op_fddx_fine
)
481 mask
= AC_TID_MASK_LEFT
;
482 else if (op
== nir_op_fddy_fine
)
483 mask
= AC_TID_MASK_TOP
;
485 mask
= AC_TID_MASK_TOP_LEFT
;
487 /* for DDX we want to next X pixel, DDY next Y pixel. */
488 if (op
== nir_op_fddx_fine
||
489 op
== nir_op_fddx_coarse
||
495 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
499 struct waterfall_context
{
500 LLVMBasicBlockRef phi_bb
[2];
504 /* To deal with divergent descriptors we can create a loop that handles all
505 * lanes with the same descriptor on a given iteration (henceforth a
508 * These helper create the begin and end of the loop leaving the caller
509 * to implement the body.
512 * - ctx is the usal nir context
513 * - wctx is a temporary struct containing some loop info. Can be left uninitialized.
514 * - value is the possibly divergent value for which we built the loop
515 * - divergent is whether value is actually divergent. If false we just pass
518 static LLVMValueRef
enter_waterfall(struct ac_nir_context
*ctx
,
519 struct waterfall_context
*wctx
,
520 LLVMValueRef value
, bool divergent
)
522 /* If the app claims the value is divergent but it is constant we can
523 * end up with a dynamic index of NULL. */
527 wctx
->use_waterfall
= divergent
;
531 ac_build_bgnloop(&ctx
->ac
, 6000);
533 LLVMValueRef scalar_value
= ac_build_readlane(&ctx
->ac
, value
, NULL
);
535 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, value
,
536 scalar_value
, "uniform_active");
538 wctx
->phi_bb
[0] = LLVMGetInsertBlock(ctx
->ac
.builder
);
539 ac_build_ifcc(&ctx
->ac
, active
, 6001);
544 static LLVMValueRef
exit_waterfall(struct ac_nir_context
*ctx
,
545 struct waterfall_context
*wctx
,
548 LLVMValueRef ret
= NULL
;
549 LLVMValueRef phi_src
[2];
550 LLVMValueRef cc_phi_src
[2] = {
551 LLVMConstInt(ctx
->ac
.i32
, 0, false),
552 LLVMConstInt(ctx
->ac
.i32
, 0xffffffff, false),
555 if (!wctx
->use_waterfall
)
558 wctx
->phi_bb
[1] = LLVMGetInsertBlock(ctx
->ac
.builder
);
560 ac_build_endif(&ctx
->ac
, 6001);
563 phi_src
[0] = LLVMGetUndef(LLVMTypeOf(value
));
566 ret
= ac_build_phi(&ctx
->ac
, LLVMTypeOf(value
), 2, phi_src
, wctx
->phi_bb
);
570 * By using the optimization barrier on the exit decision, we decouple
571 * the operations from the break, and hence avoid LLVM hoisting the
572 * opteration into the break block.
574 LLVMValueRef cc
= ac_build_phi(&ctx
->ac
, ctx
->ac
.i32
, 2, cc_phi_src
, wctx
->phi_bb
);
575 ac_build_optimization_barrier(&ctx
->ac
, &cc
);
577 LLVMValueRef active
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, cc
, ctx
->ac
.i32_0
, "uniform_active2");
578 ac_build_ifcc(&ctx
->ac
, active
, 6002);
579 ac_build_break(&ctx
->ac
);
580 ac_build_endif(&ctx
->ac
, 6002);
582 ac_build_endloop(&ctx
->ac
, 6000);
586 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
588 LLVMValueRef src
[4], result
= NULL
;
589 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
590 unsigned src_components
;
591 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
593 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
600 case nir_op_pack_half_2x16
:
601 case nir_op_pack_snorm_2x16
:
602 case nir_op_pack_unorm_2x16
:
605 case nir_op_unpack_half_2x16
:
608 case nir_op_cube_face_coord
:
609 case nir_op_cube_face_index
:
613 src_components
= num_components
;
616 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
617 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
624 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
625 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
626 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
627 /* fneg will be optimized by backend compiler with sign
628 * bit removed via XOR. This is probably a LLVM bug.
630 result
= ac_build_canonicalize(&ctx
->ac
, result
,
631 instr
->dest
.dest
.ssa
.bit_size
);
635 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
638 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
641 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
644 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
645 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
646 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
649 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
650 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
651 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
654 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
657 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
660 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
663 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
666 /* lower_fmod only lower 16-bit and 32-bit fmod */
667 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
668 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
669 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
670 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
671 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
672 ac_to_float_type(&ctx
->ac
, def_type
), result
);
673 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
674 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
677 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
680 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
683 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
686 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
687 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
688 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
691 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rcp",
692 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
695 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
698 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
701 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
704 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
705 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
706 LLVMTypeOf(src
[0]), "");
707 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
708 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
709 LLVMTypeOf(src
[0]), "");
710 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
713 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
714 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
715 LLVMTypeOf(src
[0]), "");
716 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
717 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
718 LLVMTypeOf(src
[0]), "");
719 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
722 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
723 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
724 LLVMTypeOf(src
[0]), "");
725 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
726 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
727 LLVMTypeOf(src
[0]), "");
728 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
731 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
734 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
737 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
740 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
743 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
746 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
749 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
752 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
755 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
758 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
761 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
762 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
763 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
764 /* fabs will be optimized by backend compiler with sign
765 * bit removed via AND.
767 result
= ac_build_canonicalize(&ctx
->ac
, result
,
768 instr
->dest
.dest
.ssa
.bit_size
);
772 result
= emit_iabs(&ctx
->ac
, src
[0]);
775 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
778 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
781 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
784 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
787 result
= ac_build_isign(&ctx
->ac
, src
[0],
788 instr
->dest
.dest
.ssa
.bit_size
);
791 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
792 result
= ac_build_fsign(&ctx
->ac
, src
[0],
793 instr
->dest
.dest
.ssa
.bit_size
);
796 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
797 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
800 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
801 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
804 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
805 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
807 case nir_op_fround_even
:
808 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
809 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
812 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
813 result
= ac_build_fract(&ctx
->ac
, src
[0],
814 instr
->dest
.dest
.ssa
.bit_size
);
817 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
818 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
821 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
822 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
825 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
826 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
829 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
830 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
833 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
834 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
837 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rsq",
838 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
840 case nir_op_frexp_exp
:
841 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
842 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
843 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
844 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
845 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
848 case nir_op_frexp_sig
:
849 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
850 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
851 instr
->dest
.dest
.ssa
.bit_size
);
854 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
855 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
858 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
859 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
860 if (ctx
->ac
.chip_class
< GFX9
&&
861 instr
->dest
.dest
.ssa
.bit_size
== 32) {
862 /* Only pre-GFX9 chips do not flush denorms. */
863 result
= ac_build_canonicalize(&ctx
->ac
, result
,
864 instr
->dest
.dest
.ssa
.bit_size
);
868 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
869 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
870 if (ctx
->ac
.chip_class
< GFX9
&&
871 instr
->dest
.dest
.ssa
.bit_size
== 32) {
872 /* Only pre-GFX9 chips do not flush denorms. */
873 result
= ac_build_canonicalize(&ctx
->ac
, result
,
874 instr
->dest
.dest
.ssa
.bit_size
);
878 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
879 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
880 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
883 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
884 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
885 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
886 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
887 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
889 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
892 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
894 case nir_op_bitfield_select
:
895 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
898 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
901 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
903 case nir_op_bitfield_reverse
:
904 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
906 case nir_op_bit_count
:
907 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
912 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
913 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
914 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
920 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
921 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
927 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
928 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
933 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
938 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
940 case nir_op_f2f16_rtz
:
941 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
942 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
943 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
944 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
945 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
946 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
948 case nir_op_f2f16_rtne
:
952 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
953 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
954 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
956 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
962 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
963 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
965 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
971 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
972 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
974 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
977 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
979 case nir_op_find_lsb
:
980 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
982 case nir_op_ufind_msb
:
983 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
985 case nir_op_ifind_msb
:
986 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
988 case nir_op_uadd_carry
:
989 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
991 case nir_op_usub_borrow
:
992 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
997 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1000 result
= emit_f2b(&ctx
->ac
, src
[0]);
1006 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1009 result
= emit_i2b(&ctx
->ac
, src
[0]);
1011 case nir_op_fquantize2f16
:
1012 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1014 case nir_op_umul_high
:
1015 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1017 case nir_op_imul_high
:
1018 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1020 case nir_op_pack_half_2x16
:
1021 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1023 case nir_op_pack_snorm_2x16
:
1024 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1026 case nir_op_pack_unorm_2x16
:
1027 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1029 case nir_op_unpack_half_2x16
:
1030 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1034 case nir_op_fddx_fine
:
1035 case nir_op_fddy_fine
:
1036 case nir_op_fddx_coarse
:
1037 case nir_op_fddy_coarse
:
1038 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1041 case nir_op_unpack_64_2x32_split_x
: {
1042 assert(ac_get_llvm_num_components(src
[0]) == 1);
1043 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1046 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1051 case nir_op_unpack_64_2x32_split_y
: {
1052 assert(ac_get_llvm_num_components(src
[0]) == 1);
1053 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1056 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1061 case nir_op_pack_64_2x32_split
: {
1062 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1063 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1067 case nir_op_pack_32_2x16_split
: {
1068 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1069 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1073 case nir_op_unpack_32_2x16_split_x
: {
1074 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1077 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1082 case nir_op_unpack_32_2x16_split_y
: {
1083 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1086 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1091 case nir_op_cube_face_coord
: {
1092 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1093 LLVMValueRef results
[2];
1095 for (unsigned chan
= 0; chan
< 3; chan
++)
1096 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1097 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1098 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1099 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1100 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1101 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1102 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1103 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1104 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1105 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1106 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1107 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1108 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1112 case nir_op_cube_face_index
: {
1113 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1115 for (unsigned chan
= 0; chan
< 3; chan
++)
1116 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1117 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1118 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1123 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1124 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1125 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1126 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1129 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1130 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1133 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1134 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1137 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1138 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1139 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1140 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1143 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1144 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1147 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1148 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1150 case nir_op_fmed3
: {
1151 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1152 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1153 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1154 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1155 instr
->dest
.dest
.ssa
.bit_size
);
1158 case nir_op_imed3
: {
1159 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1160 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1161 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1162 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1165 case nir_op_umed3
: {
1166 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1167 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1168 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1169 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1174 fprintf(stderr
, "Unknown NIR alu instr: ");
1175 nir_print_instr(&instr
->instr
, stderr
);
1176 fprintf(stderr
, "\n");
1181 assert(instr
->dest
.dest
.is_ssa
);
1182 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1183 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1187 static void visit_load_const(struct ac_nir_context
*ctx
,
1188 const nir_load_const_instr
*instr
)
1190 LLVMValueRef values
[4], value
= NULL
;
1191 LLVMTypeRef element_type
=
1192 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1194 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1195 switch (instr
->def
.bit_size
) {
1197 values
[i
] = LLVMConstInt(element_type
,
1198 instr
->value
[i
].u8
, false);
1201 values
[i
] = LLVMConstInt(element_type
,
1202 instr
->value
[i
].u16
, false);
1205 values
[i
] = LLVMConstInt(element_type
,
1206 instr
->value
[i
].u32
, false);
1209 values
[i
] = LLVMConstInt(element_type
,
1210 instr
->value
[i
].u64
, false);
1214 "unsupported nir load_const bit_size: %d\n",
1215 instr
->def
.bit_size
);
1219 if (instr
->def
.num_components
> 1) {
1220 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1224 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1228 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1231 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1232 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1235 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1236 /* On GFX8, the descriptor contains the size in bytes,
1237 * but TXQ must return the size in elements.
1238 * The stride is always non-zero for resources using TXQ.
1240 LLVMValueRef stride
=
1241 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1243 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1244 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1245 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1246 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1248 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1253 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1254 * incorrectly forces nearest filtering if the texture format is integer.
1255 * The only effect it has on Gather4, which always returns 4 texels for
1256 * bilinear filtering, is that the final coordinates are off by 0.5 of
1259 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1260 * or (0.5 / size) from the normalized coordinates.
1262 * However, cube textures with 8_8_8_8 data formats require a different
1263 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1264 * precision in 32-bit data formats, so it needs to be applied dynamically at
1265 * runtime. In this case, return an i1 value that indicates whether the
1266 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1268 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1270 struct ac_image_args
*args
,
1271 const nir_tex_instr
*instr
)
1273 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1274 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1275 LLVMValueRef wa_8888
= NULL
;
1276 LLVMValueRef half_texel
[2];
1277 LLVMValueRef result
;
1279 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1281 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1282 LLVMValueRef formats
;
1283 LLVMValueRef data_format
;
1284 LLVMValueRef wa_formats
;
1286 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1288 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1289 LLVMConstInt(ctx
->i32
, 20, false), "");
1290 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1291 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1292 wa_8888
= LLVMBuildICmp(
1293 ctx
->builder
, LLVMIntEQ
, data_format
,
1294 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1297 uint32_t wa_num_format
=
1298 stype
== GLSL_TYPE_UINT
?
1299 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1300 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1301 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1302 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1304 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1305 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1307 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1308 args
->resource
= LLVMBuildInsertElement(
1309 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1312 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1314 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1316 struct ac_image_args resinfo
= {};
1317 LLVMBasicBlockRef bbs
[2];
1319 LLVMValueRef unnorm
= NULL
;
1320 LLVMValueRef default_offset
= ctx
->f32_0
;
1321 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1323 /* In vulkan, whether the sampler uses unnormalized
1324 * coordinates or not is a dynamic property of the
1325 * sampler. Hence, to figure out whether or not we
1326 * need to divide by the texture size, we need to test
1327 * the sampler at runtime. This tests the bit set by
1328 * radv_init_sampler().
1330 LLVMValueRef sampler0
=
1331 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1332 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1333 LLVMConstInt(ctx
->i32
, 15, false), "");
1334 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1335 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1336 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1339 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1340 if (wa_8888
|| unnorm
) {
1341 assert(!(wa_8888
&& unnorm
));
1342 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1343 /* Skip the texture size query entirely if we don't need it. */
1344 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1345 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1348 /* Query the texture size. */
1349 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1350 resinfo
.opcode
= ac_image_get_resinfo
;
1351 resinfo
.dmask
= 0xf;
1352 resinfo
.lod
= ctx
->i32_0
;
1353 resinfo
.resource
= args
->resource
;
1354 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1355 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1357 /* Compute -0.5 / size. */
1358 for (unsigned c
= 0; c
< 2; c
++) {
1360 LLVMBuildExtractElement(ctx
->builder
, size
,
1361 LLVMConstInt(ctx
->i32
, c
, 0), "");
1362 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1363 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1364 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1365 LLVMConstReal(ctx
->f32
, -0.5), "");
1368 if (wa_8888
|| unnorm
) {
1369 ac_build_endif(ctx
, 2000);
1371 for (unsigned c
= 0; c
< 2; c
++) {
1372 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1373 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1379 for (unsigned c
= 0; c
< 2; c
++) {
1381 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1382 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1385 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1386 result
= ac_build_image_opcode(ctx
, args
);
1388 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1389 LLVMValueRef tmp
, tmp2
;
1391 /* if the cube workaround is in place, f2i the result. */
1392 for (unsigned c
= 0; c
< 4; c
++) {
1393 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1394 if (stype
== GLSL_TYPE_UINT
)
1395 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1397 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1398 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1399 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1400 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1401 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1402 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1408 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1410 nir_deref_instr
*texture_deref_instr
= NULL
;
1412 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1413 switch (instr
->src
[i
].src_type
) {
1414 case nir_tex_src_texture_deref
:
1415 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1421 return texture_deref_instr
;
1424 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1425 const nir_tex_instr
*instr
,
1426 struct ac_image_args
*args
)
1428 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1429 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1431 return ac_build_buffer_load_format(&ctx
->ac
,
1435 util_last_bit(mask
),
1439 args
->opcode
= ac_image_sample
;
1441 switch (instr
->op
) {
1443 case nir_texop_txf_ms
:
1444 case nir_texop_samples_identical
:
1445 args
->opcode
= args
->level_zero
||
1446 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1447 ac_image_load
: ac_image_load_mip
;
1448 args
->level_zero
= false;
1451 case nir_texop_query_levels
:
1452 args
->opcode
= ac_image_get_resinfo
;
1454 args
->lod
= ctx
->ac
.i32_0
;
1455 args
->level_zero
= false;
1458 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1460 args
->level_zero
= true;
1464 args
->opcode
= ac_image_gather4
;
1465 args
->level_zero
= true;
1468 args
->opcode
= ac_image_get_lod
;
1470 case nir_texop_fragment_fetch
:
1471 case nir_texop_fragment_mask_fetch
:
1472 args
->opcode
= ac_image_load
;
1473 args
->level_zero
= false;
1479 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1480 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1481 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1482 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1483 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1484 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1485 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1489 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1490 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1491 if ((args
->dim
== ac_image_2darray
||
1492 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1493 args
->coords
[1] = ctx
->ac
.i32_0
;
1497 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1498 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1499 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1500 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1501 /* Prevent texture instructions with implicit derivatives from being
1502 * sinked into branches. */
1503 switch (instr
->op
) {
1507 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1514 return ac_build_image_opcode(&ctx
->ac
, args
);
1517 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1518 nir_intrinsic_instr
*instr
)
1520 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1521 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1523 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1524 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1528 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1529 nir_intrinsic_instr
*instr
)
1531 LLVMValueRef ptr
, addr
;
1532 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1533 unsigned index
= nir_intrinsic_base(instr
);
1535 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1536 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1538 /* Load constant values from user SGPRS when possible, otherwise
1539 * fallback to the default path that loads directly from memory.
1541 if (LLVMIsConstant(src0
) &&
1542 instr
->dest
.ssa
.bit_size
== 32) {
1543 unsigned count
= instr
->dest
.ssa
.num_components
;
1544 unsigned offset
= index
;
1546 offset
+= LLVMConstIntGetZExtValue(src0
);
1549 offset
-= ctx
->args
->base_inline_push_consts
;
1551 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1552 if (offset
+ count
<= num_inline_push_consts
) {
1553 LLVMValueRef push_constants
[num_inline_push_consts
];
1554 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1555 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1556 ctx
->args
->inline_push_consts
[i
]);
1557 return ac_build_gather_values(&ctx
->ac
,
1558 push_constants
+ offset
,
1563 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1564 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1566 if (instr
->dest
.ssa
.bit_size
== 8) {
1567 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1568 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1569 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1570 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1572 LLVMValueRef params
[3];
1573 if (load_dwords
> 1) {
1574 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1575 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1576 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1578 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1579 params
[0] = ctx
->ac
.i32_0
;
1583 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1585 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1586 if (instr
->dest
.ssa
.num_components
> 1)
1587 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1589 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1590 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1591 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1592 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1593 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1594 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1595 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1596 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1597 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1598 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1599 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1600 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1601 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1602 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1603 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1604 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1605 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1608 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1610 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1613 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1614 const nir_intrinsic_instr
*instr
)
1616 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1618 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1621 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1623 uint32_t new_mask
= 0;
1624 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1625 if (mask
& (1u << i
))
1626 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1630 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1631 unsigned start
, unsigned count
)
1633 LLVMValueRef mask
[] = {
1634 ctx
->i32_0
, ctx
->i32_1
,
1635 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1637 unsigned src_elements
= ac_get_llvm_num_components(src
);
1639 if (count
== src_elements
) {
1642 } else if (count
== 1) {
1643 assert(start
< src_elements
);
1644 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1646 assert(start
+ count
<= src_elements
);
1648 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1649 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1653 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1654 enum gl_access_qualifier access
,
1655 bool may_store_unaligned
,
1656 bool writeonly_memory
)
1658 unsigned cache_policy
= 0;
1660 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1661 * store opcodes not aligned to a dword are affected. The only way to
1662 * get unaligned stores is through shader images.
1664 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1665 /* If this is write-only, don't keep data in L1 to prevent
1666 * evicting L1 cache lines that may be needed by other
1670 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1671 cache_policy
|= ac_glc
;
1674 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1675 cache_policy
|= ac_slc
;
1677 return cache_policy
;
1680 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1681 struct waterfall_context
*wctx
,
1682 const nir_intrinsic_instr
*instr
,
1685 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1686 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1689 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1690 nir_intrinsic_instr
*instr
)
1692 if (ctx
->ac
.postponed_kill
) {
1693 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1694 ctx
->ac
.postponed_kill
, "");
1695 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1698 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1699 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1700 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1701 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1702 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1703 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1705 struct waterfall_context wctx
;
1706 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1708 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1709 LLVMValueRef base_data
= src_data
;
1710 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1711 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1715 LLVMValueRef data
, offset
;
1716 LLVMTypeRef data_type
;
1718 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1720 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1721 * writes into a 2-element and a 1-element write. */
1723 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1724 writemask
|= 1 << (start
+ 2);
1727 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1729 /* we can only store 4 DWords at the same time.
1730 * can only happen for 64 Bit vectors. */
1731 if (num_bytes
> 16) {
1732 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1737 /* check alignment of 16 Bit stores */
1738 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1739 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1744 /* Due to alignment issues, split stores of 8-bit/16-bit
1747 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1748 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1750 num_bytes
= elem_size_bytes
;
1753 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1755 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1756 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1758 if (num_bytes
== 1) {
1759 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1760 offset
, ctx
->ac
.i32_0
,
1762 } else if (num_bytes
== 2) {
1763 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1764 offset
, ctx
->ac
.i32_0
,
1767 int num_channels
= num_bytes
/ 4;
1769 switch (num_bytes
) {
1770 case 16: /* v4f32 */
1771 data_type
= ctx
->ac
.v4f32
;
1773 case 12: /* v3f32 */
1774 data_type
= ctx
->ac
.v3f32
;
1777 data_type
= ctx
->ac
.v2f32
;
1780 data_type
= ctx
->ac
.f32
;
1783 unreachable("Malformed vector store.");
1785 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1787 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1788 num_channels
, offset
,
1794 exit_waterfall(ctx
, &wctx
, NULL
);
1796 if (ctx
->ac
.postponed_kill
)
1797 ac_build_endif(&ctx
->ac
, 7000);
1800 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1801 LLVMValueRef descriptor
,
1802 LLVMValueRef offset
,
1803 LLVMValueRef compare
,
1804 LLVMValueRef exchange
)
1806 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1807 if (ctx
->abi
->robust_buffer_access
) {
1808 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1810 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1811 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1813 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1815 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1818 LLVMValueRef ptr_parts
[2] = {
1819 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1820 LLVMBuildAnd(ctx
->ac
.builder
,
1821 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1822 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1825 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1826 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1828 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1830 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1831 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1832 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1833 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1835 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1836 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1838 if (ctx
->abi
->robust_buffer_access
) {
1839 ac_build_endif(&ctx
->ac
, -1);
1841 LLVMBasicBlockRef incoming_blocks
[2] = {
1846 LLVMValueRef incoming_values
[2] = {
1847 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1850 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1851 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1858 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1859 nir_intrinsic_instr
*instr
)
1861 if (ctx
->ac
.postponed_kill
) {
1862 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1863 ctx
->ac
.postponed_kill
, "");
1864 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1867 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1869 char name
[64], type
[8];
1870 LLVMValueRef params
[6], descriptor
;
1871 LLVMValueRef result
;
1874 struct waterfall_context wctx
;
1875 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1877 switch (instr
->intrinsic
) {
1878 case nir_intrinsic_ssbo_atomic_add
:
1881 case nir_intrinsic_ssbo_atomic_imin
:
1884 case nir_intrinsic_ssbo_atomic_umin
:
1887 case nir_intrinsic_ssbo_atomic_imax
:
1890 case nir_intrinsic_ssbo_atomic_umax
:
1893 case nir_intrinsic_ssbo_atomic_and
:
1896 case nir_intrinsic_ssbo_atomic_or
:
1899 case nir_intrinsic_ssbo_atomic_xor
:
1902 case nir_intrinsic_ssbo_atomic_exchange
:
1905 case nir_intrinsic_ssbo_atomic_comp_swap
:
1912 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1916 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1917 return_type
== ctx
->ac
.i64
) {
1918 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1919 get_src(ctx
, instr
->src
[1]),
1920 get_src(ctx
, instr
->src
[2]),
1921 get_src(ctx
, instr
->src
[3]));
1923 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1924 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1926 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1927 params
[arg_count
++] = descriptor
;
1929 if (LLVM_VERSION_MAJOR
>= 9) {
1930 /* XXX: The new raw/struct atomic intrinsics are buggy with
1931 * LLVM 8, see r358579.
1933 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1934 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1935 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1937 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1938 snprintf(name
, sizeof(name
),
1939 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1941 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1942 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1943 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1945 assert(return_type
== ctx
->ac
.i32
);
1946 snprintf(name
, sizeof(name
),
1947 "llvm.amdgcn.buffer.atomic.%s", op
);
1950 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1954 result
= exit_waterfall(ctx
, &wctx
, result
);
1955 if (ctx
->ac
.postponed_kill
)
1956 ac_build_endif(&ctx
->ac
, 7001);
1960 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1961 nir_intrinsic_instr
*instr
)
1963 struct waterfall_context wctx
;
1964 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1966 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1967 int num_components
= instr
->num_components
;
1968 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1969 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1971 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1972 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
1973 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1975 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1976 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1978 LLVMValueRef results
[4];
1979 for (int i
= 0; i
< num_components
;) {
1980 int num_elems
= num_components
- i
;
1981 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1983 if (num_elems
* elem_size_bytes
> 16)
1984 num_elems
= 16 / elem_size_bytes
;
1985 int load_bytes
= num_elems
* elem_size_bytes
;
1987 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1991 if (load_bytes
== 1) {
1992 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1998 } else if (load_bytes
== 2) {
1999 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2006 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2007 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2009 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2010 vindex
, offset
, immoffset
, 0,
2011 cache_policy
, can_speculate
, false);
2014 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2015 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2016 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2018 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2019 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2021 for (unsigned j
= 0; j
< num_elems
; j
++) {
2022 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2027 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2028 return exit_waterfall(ctx
, &wctx
, ret
);
2031 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2032 struct waterfall_context
*wctx
,
2033 const nir_intrinsic_instr
*instr
)
2035 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2036 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2039 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2040 nir_intrinsic_instr
*instr
)
2042 struct waterfall_context wctx
;
2043 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2046 LLVMValueRef rsrc
= rsrc_base
;
2047 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2048 int num_components
= instr
->num_components
;
2050 if (ctx
->abi
->load_ubo
)
2051 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2053 if (instr
->dest
.ssa
.bit_size
== 64)
2054 num_components
*= 2;
2056 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2057 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2058 LLVMValueRef results
[num_components
];
2059 for (unsigned i
= 0; i
< num_components
; ++i
) {
2060 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2063 if (load_bytes
== 1) {
2064 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2071 assert(load_bytes
== 2);
2072 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2080 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2082 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2083 NULL
, 0, 0, true, true);
2085 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2088 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2089 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2091 return exit_waterfall(ctx
, &wctx
, ret
);
2095 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2096 bool vs_in
, unsigned *vertex_index_out
,
2097 LLVMValueRef
*vertex_index_ref
,
2098 unsigned *const_out
, LLVMValueRef
*indir_out
)
2100 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2101 nir_deref_path path
;
2102 unsigned idx_lvl
= 1;
2104 nir_deref_path_init(&path
, instr
, NULL
);
2106 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2107 if (vertex_index_ref
) {
2108 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2109 if (vertex_index_out
)
2110 *vertex_index_out
= 0;
2112 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2117 uint32_t const_offset
= 0;
2118 LLVMValueRef offset
= NULL
;
2120 if (var
->data
.compact
) {
2121 assert(instr
->deref_type
== nir_deref_type_array
);
2122 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2126 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2127 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2128 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2129 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2131 for (unsigned i
= 0; i
< index
; i
++) {
2132 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2133 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2135 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2136 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2137 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2138 const_offset
+= size
*
2139 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2141 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2142 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2144 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2149 unreachable("Uhandled deref type in get_deref_instr_offset");
2153 nir_deref_path_finish(&path
);
2155 if (const_offset
&& offset
)
2156 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2157 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2160 *const_out
= const_offset
;
2161 *indir_out
= offset
;
2164 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2165 nir_intrinsic_instr
*instr
,
2168 LLVMValueRef result
;
2169 LLVMValueRef vertex_index
= NULL
;
2170 LLVMValueRef indir_index
= NULL
;
2171 unsigned const_index
= 0;
2173 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2175 unsigned location
= var
->data
.location
;
2176 unsigned driver_location
= var
->data
.driver_location
;
2177 const bool is_patch
= var
->data
.patch
||
2178 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2179 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2180 const bool is_compact
= var
->data
.compact
;
2182 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2183 false, NULL
, is_patch
? NULL
: &vertex_index
,
2184 &const_index
, &indir_index
);
2186 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2188 LLVMTypeRef src_component_type
;
2189 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
2190 src_component_type
= LLVMGetElementType(dest_type
);
2192 src_component_type
= dest_type
;
2194 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2195 vertex_index
, indir_index
,
2196 const_index
, location
, driver_location
,
2197 var
->data
.location_frac
,
2198 instr
->num_components
,
2199 is_patch
, is_compact
, load_inputs
);
2200 if (instr
->dest
.ssa
.bit_size
== 16) {
2201 result
= ac_to_integer(&ctx
->ac
, result
);
2202 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2204 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2208 type_scalar_size_bytes(const struct glsl_type
*type
)
2210 assert(glsl_type_is_vector_or_scalar(type
) ||
2211 glsl_type_is_matrix(type
));
2212 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2215 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2216 nir_intrinsic_instr
*instr
)
2218 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2219 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2221 LLVMValueRef values
[8];
2223 int ve
= instr
->dest
.ssa
.num_components
;
2225 LLVMValueRef indir_index
;
2227 unsigned const_index
;
2228 unsigned stride
= 4;
2229 int mode
= deref
->mode
;
2232 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2233 var
->data
.mode
== nir_var_shader_in
;
2234 idx
= var
->data
.driver_location
;
2235 comp
= var
->data
.location_frac
;
2236 mode
= var
->data
.mode
;
2238 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2239 &const_index
, &indir_index
);
2241 if (var
->data
.compact
) {
2243 const_index
+= comp
;
2248 if (instr
->dest
.ssa
.bit_size
== 64 &&
2249 (deref
->mode
== nir_var_shader_in
||
2250 deref
->mode
== nir_var_shader_out
||
2251 deref
->mode
== nir_var_function_temp
))
2255 case nir_var_shader_in
:
2256 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2257 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2258 return load_tess_varyings(ctx
, instr
, true);
2261 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2262 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2263 LLVMValueRef indir_index
;
2264 unsigned const_index
, vertex_index
;
2265 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2266 &const_index
, &indir_index
);
2267 assert(indir_index
== NULL
);
2269 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2270 var
->data
.driver_location
,
2271 var
->data
.location_frac
,
2272 instr
->num_components
, vertex_index
, const_index
, type
);
2275 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2277 unsigned count
= glsl_count_attribute_slots(
2279 ctx
->stage
== MESA_SHADER_VERTEX
);
2281 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2282 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2283 stride
, false, true);
2285 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2289 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2292 case nir_var_function_temp
:
2293 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2295 unsigned count
= glsl_count_attribute_slots(
2298 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2299 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2300 stride
, true, true);
2302 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2306 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2310 case nir_var_shader_out
:
2311 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2312 return load_tess_varyings(ctx
, instr
, false);
2315 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2316 var
->data
.fb_fetch_output
&&
2317 ctx
->abi
->emit_fbfetch
)
2318 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2320 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2322 unsigned count
= glsl_count_attribute_slots(
2325 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2326 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2327 stride
, true, true);
2329 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2333 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2334 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2339 case nir_var_mem_global
: {
2340 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2341 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2342 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2343 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2344 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2345 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2346 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2348 if (stride
!= natural_stride
|| split_loads
) {
2349 if (LLVMGetTypeKind(result_type
) == LLVMVectorTypeKind
)
2350 result_type
= LLVMGetElementType(result_type
);
2352 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2353 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2354 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2356 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2357 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2358 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2359 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2361 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2363 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2364 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2365 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2366 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2371 unreachable("unhandle variable mode");
2373 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2374 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2378 visit_store_var(struct ac_nir_context
*ctx
,
2379 nir_intrinsic_instr
*instr
)
2381 if (ctx
->ac
.postponed_kill
) {
2382 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2383 ctx
->ac
.postponed_kill
, "");
2384 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2387 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2388 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2390 LLVMValueRef temp_ptr
, value
;
2393 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2394 int writemask
= instr
->const_index
[0];
2395 LLVMValueRef indir_index
;
2396 unsigned const_index
;
2399 get_deref_offset(ctx
, deref
, false,
2400 NULL
, NULL
, &const_index
, &indir_index
);
2401 idx
= var
->data
.driver_location
;
2402 comp
= var
->data
.location_frac
;
2404 if (var
->data
.compact
) {
2405 const_index
+= comp
;
2410 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2411 (deref
->mode
== nir_var_shader_out
||
2412 deref
->mode
== nir_var_function_temp
)) {
2414 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2415 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2418 writemask
= widen_mask(writemask
, 2);
2421 writemask
= writemask
<< comp
;
2423 switch (deref
->mode
) {
2424 case nir_var_shader_out
:
2426 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2427 LLVMValueRef vertex_index
= NULL
;
2428 LLVMValueRef indir_index
= NULL
;
2429 unsigned const_index
= 0;
2430 const bool is_patch
= var
->data
.patch
||
2431 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2432 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2434 get_deref_offset(ctx
, deref
, false, NULL
,
2435 is_patch
? NULL
: &vertex_index
,
2436 &const_index
, &indir_index
);
2438 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2439 vertex_index
, indir_index
,
2440 const_index
, src
, writemask
);
2444 for (unsigned chan
= 0; chan
< 8; chan
++) {
2446 if (!(writemask
& (1 << chan
)))
2449 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2451 if (var
->data
.compact
)
2454 unsigned count
= glsl_count_attribute_slots(
2457 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2458 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2459 stride
, true, true);
2461 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2462 value
, indir_index
, "");
2463 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2464 count
, stride
, tmp_vec
);
2467 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2469 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2473 case nir_var_function_temp
:
2474 for (unsigned chan
= 0; chan
< 8; chan
++) {
2475 if (!(writemask
& (1 << chan
)))
2478 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2480 unsigned count
= glsl_count_attribute_slots(
2483 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2484 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2487 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2488 value
, indir_index
, "");
2489 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2492 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2494 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2499 case nir_var_mem_global
: {
2500 int writemask
= instr
->const_index
[0];
2501 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2502 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2504 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2505 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2506 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2507 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2508 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2510 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2511 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2512 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2514 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2515 stride
== natural_stride
&& !split_stores
) {
2516 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2517 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2518 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2520 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2521 LLVMGetElementType(LLVMTypeOf(address
)), "");
2522 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2524 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2525 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMVectorTypeKind
)
2526 val_type
= LLVMGetElementType(val_type
);
2528 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2529 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2530 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2531 for (unsigned chan
= 0; chan
< 4; chan
++) {
2532 if (!(writemask
& (1 << chan
)))
2535 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2537 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2538 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2540 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2541 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2542 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2552 if (ctx
->ac
.postponed_kill
)
2553 ac_build_endif(&ctx
->ac
, 7002);
2556 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2559 case GLSL_SAMPLER_DIM_BUF
:
2561 case GLSL_SAMPLER_DIM_1D
:
2562 return array
? 2 : 1;
2563 case GLSL_SAMPLER_DIM_2D
:
2564 return array
? 3 : 2;
2565 case GLSL_SAMPLER_DIM_MS
:
2566 return array
? 4 : 3;
2567 case GLSL_SAMPLER_DIM_3D
:
2568 case GLSL_SAMPLER_DIM_CUBE
:
2570 case GLSL_SAMPLER_DIM_RECT
:
2571 case GLSL_SAMPLER_DIM_SUBPASS
:
2573 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2581 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2582 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2583 LLVMValueRef coord_z
,
2584 LLVMValueRef sample_index
,
2585 LLVMValueRef fmask_desc_ptr
)
2587 unsigned sample_chan
= coord_z
? 3 : 2;
2588 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2589 addr
[sample_chan
] = sample_index
;
2591 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2592 return addr
[sample_chan
];
2595 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2597 assert(instr
->src
[0].is_ssa
);
2598 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2601 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2602 const nir_intrinsic_instr
*instr
,
2603 LLVMValueRef dynamic_index
,
2604 enum ac_descriptor_type desc_type
,
2607 nir_deref_instr
*deref_instr
=
2608 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2609 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2611 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2614 static void get_image_coords(struct ac_nir_context
*ctx
,
2615 const nir_intrinsic_instr
*instr
,
2616 LLVMValueRef dynamic_desc_index
,
2617 struct ac_image_args
*args
,
2618 enum glsl_sampler_dim dim
,
2621 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2622 LLVMValueRef masks
[] = {
2623 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2624 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2626 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2629 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2630 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2631 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2632 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2633 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2634 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2635 count
= image_type_to_components_count(dim
, is_array
);
2637 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2638 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2639 LLVMValueRef fmask_load_address
[3];
2641 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2642 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2644 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2646 fmask_load_address
[2] = NULL
;
2648 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2649 fmask_load_address
[0],
2650 fmask_load_address
[1],
2651 fmask_load_address
[2],
2653 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2654 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2656 if (count
== 1 && !gfx9_1d
) {
2657 if (instr
->src
[1].ssa
->num_components
)
2658 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2660 args
->coords
[0] = src0
;
2665 for (chan
= 0; chan
< count
; ++chan
) {
2666 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2671 args
->coords
[2] = args
->coords
[1];
2672 args
->coords
[1] = ctx
->ac
.i32_0
;
2674 args
->coords
[1] = ctx
->ac
.i32_0
;
2677 if (ctx
->ac
.chip_class
== GFX9
&&
2678 dim
== GLSL_SAMPLER_DIM_2D
&&
2680 /* The hw can't bind a slice of a 3D image as a 2D
2681 * image, because it ignores BASE_ARRAY if the target
2682 * is 3D. The workaround is to read BASE_ARRAY and set
2683 * it as the 3rd address operand for all 2D images.
2685 LLVMValueRef first_layer
, const5
, mask
;
2687 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2688 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2689 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2690 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2692 args
->coords
[count
] = first_layer
;
2698 args
->coords
[count
] = sample_index
;
2704 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2705 const nir_intrinsic_instr
*instr
,
2706 LLVMValueRef dynamic_index
,
2707 bool write
, bool atomic
)
2709 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2710 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2711 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2712 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2713 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2715 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2716 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2717 elem_count
, stride
, "");
2719 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2720 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2725 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2726 struct waterfall_context
*wctx
,
2727 const nir_intrinsic_instr
*instr
)
2729 nir_deref_instr
*deref_instr
= NULL
;
2731 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2732 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2734 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2735 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2738 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2739 const nir_intrinsic_instr
*instr
,
2744 enum glsl_sampler_dim dim
;
2745 enum gl_access_qualifier access
;
2748 dim
= nir_intrinsic_image_dim(instr
);
2749 access
= nir_intrinsic_access(instr
);
2750 is_array
= nir_intrinsic_image_array(instr
);
2752 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2753 const struct glsl_type
*type
= image_deref
->type
;
2754 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2755 dim
= glsl_get_sampler_dim(type
);
2756 access
= var
->data
.access
;
2757 is_array
= glsl_sampler_type_is_array(type
);
2760 struct waterfall_context wctx
;
2761 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2763 struct ac_image_args args
= {};
2765 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2767 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2768 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2769 unsigned num_channels
= util_last_bit(mask
);
2770 LLVMValueRef rsrc
, vindex
;
2772 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2773 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2776 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2777 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2778 ctx
->ac
.i32_0
, num_channels
,
2781 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2783 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2784 res
= ac_to_integer(&ctx
->ac
, res
);
2786 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2788 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2789 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2790 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2791 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2793 args
.lod
= get_src(ctx
, instr
->src
[3]);
2795 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2797 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2799 return exit_waterfall(ctx
, &wctx
, res
);
2802 static void visit_image_store(struct ac_nir_context
*ctx
,
2803 const nir_intrinsic_instr
*instr
,
2806 if (ctx
->ac
.postponed_kill
) {
2807 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2808 ctx
->ac
.postponed_kill
, "");
2809 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2812 enum glsl_sampler_dim dim
;
2813 enum gl_access_qualifier access
;
2817 dim
= nir_intrinsic_image_dim(instr
);
2818 access
= nir_intrinsic_access(instr
);
2819 is_array
= nir_intrinsic_image_array(instr
);
2821 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2822 const struct glsl_type
*type
= image_deref
->type
;
2823 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2824 dim
= glsl_get_sampler_dim(type
);
2825 access
= var
->data
.access
;
2826 is_array
= glsl_sampler_type_is_array(type
);
2829 struct waterfall_context wctx
;
2830 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2832 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2833 struct ac_image_args args
= {};
2835 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2837 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2838 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2839 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2840 unsigned src_channels
= ac_get_llvm_num_components(src
);
2841 LLVMValueRef vindex
;
2843 if (src_channels
== 3)
2844 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2846 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2847 get_src(ctx
, instr
->src
[1]),
2850 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2851 ctx
->ac
.i32_0
, src_channels
,
2854 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2856 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2857 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2858 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2859 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2860 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2862 args
.lod
= get_src(ctx
, instr
->src
[4]);
2865 ac_build_image_opcode(&ctx
->ac
, &args
);
2868 exit_waterfall(ctx
, &wctx
, NULL
);
2869 if (ctx
->ac
.postponed_kill
)
2870 ac_build_endif(&ctx
->ac
, 7003);
2873 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2874 const nir_intrinsic_instr
*instr
,
2877 if (ctx
->ac
.postponed_kill
) {
2878 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2879 ctx
->ac
.postponed_kill
, "");
2880 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
2883 LLVMValueRef params
[7];
2884 int param_count
= 0;
2886 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2887 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2888 const char *atomic_name
;
2889 char intrinsic_name
[64];
2890 enum ac_atomic_op atomic_subop
;
2891 ASSERTED
int length
;
2893 enum glsl_sampler_dim dim
;
2896 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
2897 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
2898 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
2899 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
2900 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
2901 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2903 dim
= nir_intrinsic_image_dim(instr
);
2904 is_array
= nir_intrinsic_image_array(instr
);
2906 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2907 dim
= glsl_get_sampler_dim(type
);
2908 is_array
= glsl_sampler_type_is_array(type
);
2911 struct waterfall_context wctx
;
2912 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2914 switch (instr
->intrinsic
) {
2915 case nir_intrinsic_bindless_image_atomic_add
:
2916 case nir_intrinsic_image_deref_atomic_add
:
2917 atomic_name
= "add";
2918 atomic_subop
= ac_atomic_add
;
2920 case nir_intrinsic_bindless_image_atomic_imin
:
2921 case nir_intrinsic_image_deref_atomic_imin
:
2922 atomic_name
= "smin";
2923 atomic_subop
= ac_atomic_smin
;
2925 case nir_intrinsic_bindless_image_atomic_umin
:
2926 case nir_intrinsic_image_deref_atomic_umin
:
2927 atomic_name
= "umin";
2928 atomic_subop
= ac_atomic_umin
;
2930 case nir_intrinsic_bindless_image_atomic_imax
:
2931 case nir_intrinsic_image_deref_atomic_imax
:
2932 atomic_name
= "smax";
2933 atomic_subop
= ac_atomic_smax
;
2935 case nir_intrinsic_bindless_image_atomic_umax
:
2936 case nir_intrinsic_image_deref_atomic_umax
:
2937 atomic_name
= "umax";
2938 atomic_subop
= ac_atomic_umax
;
2940 case nir_intrinsic_bindless_image_atomic_and
:
2941 case nir_intrinsic_image_deref_atomic_and
:
2942 atomic_name
= "and";
2943 atomic_subop
= ac_atomic_and
;
2945 case nir_intrinsic_bindless_image_atomic_or
:
2946 case nir_intrinsic_image_deref_atomic_or
:
2948 atomic_subop
= ac_atomic_or
;
2950 case nir_intrinsic_bindless_image_atomic_xor
:
2951 case nir_intrinsic_image_deref_atomic_xor
:
2952 atomic_name
= "xor";
2953 atomic_subop
= ac_atomic_xor
;
2955 case nir_intrinsic_bindless_image_atomic_exchange
:
2956 case nir_intrinsic_image_deref_atomic_exchange
:
2957 atomic_name
= "swap";
2958 atomic_subop
= ac_atomic_swap
;
2960 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2961 case nir_intrinsic_image_deref_atomic_comp_swap
:
2962 atomic_name
= "cmpswap";
2963 atomic_subop
= 0; /* not used */
2965 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
2966 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
2967 atomic_name
= "inc";
2968 atomic_subop
= ac_atomic_inc_wrap
;
2969 /* ATOMIC_INC instruction does:
2970 * value = (value + 1) % (data + 1)
2972 * value = (value + 1) % data
2973 * So replace 'data' by 'data - 1'.
2975 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
] =
2976 LLVMBuildSub(ctx
->ac
.builder
,
2977 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
],
2981 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
2982 case nir_intrinsic_image_deref_atomic_dec_wrap
:
2983 atomic_name
= "dec";
2984 atomic_subop
= ac_atomic_dec_wrap
;
2991 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2992 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2994 LLVMValueRef result
;
2995 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2996 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
2997 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2998 ctx
->ac
.i32_0
, ""); /* vindex */
2999 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3000 if (LLVM_VERSION_MAJOR
>= 9) {
3001 /* XXX: The new raw/struct atomic intrinsics are buggy
3002 * with LLVM 8, see r358579.
3004 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3005 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3007 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3008 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3010 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3012 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3013 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3016 assert(length
< sizeof(intrinsic_name
));
3017 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3018 params
, param_count
, 0);
3020 struct ac_image_args args
= {};
3021 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3022 args
.atomic
= atomic_subop
;
3023 args
.data
[0] = params
[0];
3025 args
.data
[1] = params
[1];
3026 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3027 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3028 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3030 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3033 result
= exit_waterfall(ctx
, &wctx
, result
);
3034 if (ctx
->ac
.postponed_kill
)
3035 ac_build_endif(&ctx
->ac
, 7004);
3039 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3040 nir_intrinsic_instr
*instr
)
3042 struct waterfall_context wctx
;
3043 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3044 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3046 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3048 return exit_waterfall(ctx
, &wctx
, ret
);
3051 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3052 const nir_intrinsic_instr
*instr
,
3057 enum glsl_sampler_dim dim
;
3060 dim
= nir_intrinsic_image_dim(instr
);
3061 is_array
= nir_intrinsic_image_array(instr
);
3063 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3064 dim
= glsl_get_sampler_dim(type
);
3065 is_array
= glsl_sampler_type_is_array(type
);
3068 struct waterfall_context wctx
;
3069 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3071 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3072 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3075 struct ac_image_args args
= { 0 };
3077 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3079 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3080 args
.opcode
= ac_image_get_resinfo
;
3081 args
.lod
= ctx
->ac
.i32_0
;
3082 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3084 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3086 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3088 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3089 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3090 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3091 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3092 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3095 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3096 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3097 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3101 return exit_waterfall(ctx
, &wctx
, res
);
3104 static void emit_membar(struct ac_llvm_context
*ac
,
3105 const nir_intrinsic_instr
*instr
)
3107 unsigned wait_flags
= 0;
3109 switch (instr
->intrinsic
) {
3110 case nir_intrinsic_memory_barrier
:
3111 case nir_intrinsic_group_memory_barrier
:
3112 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3114 case nir_intrinsic_memory_barrier_buffer
:
3115 case nir_intrinsic_memory_barrier_image
:
3116 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3118 case nir_intrinsic_memory_barrier_shared
:
3119 wait_flags
= AC_WAIT_LGKM
;
3125 ac_build_waitcnt(ac
, wait_flags
);
3128 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3130 /* GFX6 only (thanks to a hw bug workaround):
3131 * The real barrier instruction isn’t needed, because an entire patch
3132 * always fits into a single wave.
3134 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3135 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3138 ac_build_s_barrier(ac
);
3141 static void emit_discard(struct ac_nir_context
*ctx
,
3142 const nir_intrinsic_instr
*instr
)
3146 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3147 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3148 get_src(ctx
, instr
->src
[0]),
3151 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3152 cond
= ctx
->ac
.i1false
;
3155 ac_build_kill_if_false(&ctx
->ac
, cond
);
3158 static void emit_demote(struct ac_nir_context
*ctx
,
3159 const nir_intrinsic_instr
*instr
)
3163 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3164 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3165 get_src(ctx
, instr
->src
[0]),
3168 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3169 cond
= ctx
->ac
.i1false
;
3172 /* Kill immediately while maintaining WQM. */
3173 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3175 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3176 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3177 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3182 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3184 LLVMValueRef result
;
3185 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3186 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3187 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3188 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3190 if (ctx
->ac
.wave_size
== 32)
3191 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3192 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3194 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3198 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3200 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3201 LLVMValueRef result
;
3202 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3203 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3204 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3205 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3207 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3212 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3214 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3215 return LLVMBuildAnd(ctx
->ac
.builder
,
3216 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3217 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3219 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3224 visit_first_invocation(struct ac_nir_context
*ctx
)
3226 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3227 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3229 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3230 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3231 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3232 ctx
->ac
.iN_wavemask
, args
, 2,
3233 AC_FUNC_ATTR_NOUNWIND
|
3234 AC_FUNC_ATTR_READNONE
);
3236 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3240 visit_load_shared(struct ac_nir_context
*ctx
,
3241 const nir_intrinsic_instr
*instr
)
3243 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3245 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3246 instr
->dest
.ssa
.bit_size
);
3248 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3249 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3250 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3251 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3254 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3255 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3259 visit_store_shared(struct ac_nir_context
*ctx
,
3260 const nir_intrinsic_instr
*instr
)
3262 LLVMValueRef derived_ptr
, data
,index
;
3263 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3265 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3266 instr
->src
[0].ssa
->bit_size
);
3267 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3269 int writemask
= nir_intrinsic_write_mask(instr
);
3270 for (int chan
= 0; chan
< 4; chan
++) {
3271 if (!(writemask
& (1 << chan
))) {
3274 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3275 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3276 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3277 LLVMBuildStore(builder
, data
, derived_ptr
);
3281 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3282 const nir_intrinsic_instr
*instr
,
3283 LLVMValueRef ptr
, int src_idx
)
3285 if (ctx
->ac
.postponed_kill
) {
3286 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3287 ctx
->ac
.postponed_kill
, "");
3288 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3291 LLVMValueRef result
;
3292 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3294 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3296 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3297 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3298 if (deref
->mode
== nir_var_mem_global
) {
3299 /* use "singlethread" sync scope to implement relaxed ordering */
3300 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3302 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3303 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3307 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3308 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3309 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3310 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3311 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3313 LLVMAtomicRMWBinOp op
;
3314 switch (instr
->intrinsic
) {
3315 case nir_intrinsic_shared_atomic_add
:
3316 case nir_intrinsic_deref_atomic_add
:
3317 op
= LLVMAtomicRMWBinOpAdd
;
3319 case nir_intrinsic_shared_atomic_umin
:
3320 case nir_intrinsic_deref_atomic_umin
:
3321 op
= LLVMAtomicRMWBinOpUMin
;
3323 case nir_intrinsic_shared_atomic_umax
:
3324 case nir_intrinsic_deref_atomic_umax
:
3325 op
= LLVMAtomicRMWBinOpUMax
;
3327 case nir_intrinsic_shared_atomic_imin
:
3328 case nir_intrinsic_deref_atomic_imin
:
3329 op
= LLVMAtomicRMWBinOpMin
;
3331 case nir_intrinsic_shared_atomic_imax
:
3332 case nir_intrinsic_deref_atomic_imax
:
3333 op
= LLVMAtomicRMWBinOpMax
;
3335 case nir_intrinsic_shared_atomic_and
:
3336 case nir_intrinsic_deref_atomic_and
:
3337 op
= LLVMAtomicRMWBinOpAnd
;
3339 case nir_intrinsic_shared_atomic_or
:
3340 case nir_intrinsic_deref_atomic_or
:
3341 op
= LLVMAtomicRMWBinOpOr
;
3343 case nir_intrinsic_shared_atomic_xor
:
3344 case nir_intrinsic_deref_atomic_xor
:
3345 op
= LLVMAtomicRMWBinOpXor
;
3347 case nir_intrinsic_shared_atomic_exchange
:
3348 case nir_intrinsic_deref_atomic_exchange
:
3349 op
= LLVMAtomicRMWBinOpXchg
;
3355 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
3358 if (ctx
->ac
.postponed_kill
)
3359 ac_build_endif(&ctx
->ac
, 7005);
3363 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3365 LLVMValueRef values
[2];
3366 LLVMValueRef pos
[2];
3368 pos
[0] = ac_to_float(&ctx
->ac
,
3369 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3370 pos
[1] = ac_to_float(&ctx
->ac
,
3371 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3373 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3374 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3375 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3378 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3379 enum glsl_interp_mode interp
, unsigned location
)
3382 case INTERP_MODE_FLAT
:
3385 case INTERP_MODE_SMOOTH
:
3386 case INTERP_MODE_NONE
:
3387 if (location
== INTERP_CENTER
)
3388 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3389 else if (location
== INTERP_CENTROID
)
3390 return ctx
->abi
->persp_centroid
;
3391 else if (location
== INTERP_SAMPLE
)
3392 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3394 case INTERP_MODE_NOPERSPECTIVE
:
3395 if (location
== INTERP_CENTER
)
3396 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3397 else if (location
== INTERP_CENTROID
)
3398 return ctx
->abi
->linear_centroid
;
3399 else if (location
== INTERP_SAMPLE
)
3400 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3406 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3409 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3410 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3413 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3415 LLVMValueRef offset
)
3417 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3418 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3419 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3421 LLVMValueRef ij_out
[2];
3422 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3425 * take the I then J parameters, and the DDX/Y for it, and
3426 * calculate the IJ inputs for the interpolator.
3427 * temp1 = ddx * offset/sample.x + I;
3428 * interp_param.I = ddy * offset/sample.y + temp1;
3429 * temp1 = ddx * offset/sample.x + J;
3430 * interp_param.J = ddy * offset/sample.y + temp1;
3432 for (unsigned i
= 0; i
< 2; i
++) {
3433 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3434 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3435 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3436 ddxy_out
, ix_ll
, "");
3437 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3438 ddxy_out
, iy_ll
, "");
3439 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3440 interp_param
, ix_ll
, "");
3441 LLVMValueRef temp1
, temp2
;
3443 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3446 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3447 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3449 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3450 temp2
, ctx
->ac
.i32
, "");
3452 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3453 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3456 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3459 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3460 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3463 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3465 LLVMValueRef sample_id
)
3467 if (ctx
->abi
->interp_at_sample_force_center
)
3468 return barycentric_center(ctx
, mode
);
3470 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3472 /* fetch sample ID */
3473 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3475 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3476 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3477 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3478 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3479 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3480 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3482 return barycentric_offset(ctx
, mode
, offset
);
3486 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3489 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3490 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3493 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3495 return LLVMBuildBitCast(ctx
->ac
.builder
,
3496 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3500 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3501 LLVMValueRef interp_param
,
3502 unsigned index
, unsigned comp_start
,
3503 unsigned num_components
,
3506 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3508 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3509 interp_param
, ctx
->ac
.v2f32
, "");
3510 LLVMValueRef i
= LLVMBuildExtractElement(
3511 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3512 LLVMValueRef j
= LLVMBuildExtractElement(
3513 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3515 LLVMValueRef values
[4];
3516 assert(bitsize
== 16 || bitsize
== 32);
3517 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3518 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3519 if (bitsize
== 16) {
3520 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3521 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3523 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3524 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3528 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3531 static LLVMValueRef
load_input(struct ac_nir_context
*ctx
,
3532 nir_intrinsic_instr
*instr
)
3534 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
3536 /* We only lower inputs for fragment shaders ATM */
3537 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
3539 assert(offset
[0].i32
== 0);
3541 unsigned component
= nir_intrinsic_component(instr
);
3542 unsigned index
= nir_intrinsic_base(instr
);
3543 unsigned vertex_id
= 2; /* P0 */
3545 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3546 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3548 switch (src0
[0].i32
) {
3559 unreachable("Invalid vertex index");
3563 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3564 LLVMValueRef values
[8];
3566 /* Each component of a 64-bit value takes up two GL-level channels. */
3567 unsigned num_components
= instr
->dest
.ssa
.num_components
;
3568 unsigned bit_size
= instr
->dest
.ssa
.bit_size
;
3570 bit_size
== 64 ? num_components
* 2 : num_components
;
3572 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3573 if (component
+ chan
> 4)
3574 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3575 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3576 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3577 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3580 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3581 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3582 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3583 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3586 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3587 if (bit_size
== 64) {
3588 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3589 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3590 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3595 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3596 nir_intrinsic_instr
*instr
)
3598 LLVMValueRef result
= NULL
;
3600 switch (instr
->intrinsic
) {
3601 case nir_intrinsic_ballot
:
3602 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3603 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3604 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3606 case nir_intrinsic_read_invocation
:
3607 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3608 get_src(ctx
, instr
->src
[1]));
3610 case nir_intrinsic_read_first_invocation
:
3611 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3613 case nir_intrinsic_load_subgroup_invocation
:
3614 result
= ac_get_thread_id(&ctx
->ac
);
3616 case nir_intrinsic_load_work_group_id
: {
3617 LLVMValueRef values
[3];
3619 for (int i
= 0; i
< 3; i
++) {
3620 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3621 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3624 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3627 case nir_intrinsic_load_base_vertex
:
3628 case nir_intrinsic_load_first_vertex
:
3629 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3631 case nir_intrinsic_load_local_group_size
:
3632 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3634 case nir_intrinsic_load_vertex_id
:
3635 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3636 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3637 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3639 case nir_intrinsic_load_vertex_id_zero_base
: {
3640 result
= ctx
->abi
->vertex_id
;
3643 case nir_intrinsic_load_local_invocation_id
: {
3644 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3647 case nir_intrinsic_load_base_instance
:
3648 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3650 case nir_intrinsic_load_draw_id
:
3651 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3653 case nir_intrinsic_load_view_index
:
3654 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3656 case nir_intrinsic_load_invocation_id
:
3657 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3658 result
= ac_unpack_param(&ctx
->ac
,
3659 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3662 if (ctx
->ac
.chip_class
>= GFX10
) {
3663 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3664 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3665 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3667 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3671 case nir_intrinsic_load_primitive_id
:
3672 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3673 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3674 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3675 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3676 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3677 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3679 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3681 case nir_intrinsic_load_sample_id
:
3682 result
= ac_unpack_param(&ctx
->ac
,
3683 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3686 case nir_intrinsic_load_sample_pos
:
3687 result
= load_sample_pos(ctx
);
3689 case nir_intrinsic_load_sample_mask_in
:
3690 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3692 case nir_intrinsic_load_frag_coord
: {
3693 LLVMValueRef values
[4] = {
3694 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3695 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3696 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3697 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3698 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3700 result
= ac_to_integer(&ctx
->ac
,
3701 ac_build_gather_values(&ctx
->ac
, values
, 4));
3704 case nir_intrinsic_load_layer_id
:
3705 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3707 case nir_intrinsic_load_front_face
:
3708 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3710 case nir_intrinsic_load_helper_invocation
:
3711 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3713 case nir_intrinsic_is_helper_invocation
:
3714 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3716 case nir_intrinsic_load_color0
:
3717 result
= ctx
->abi
->color0
;
3719 case nir_intrinsic_load_color1
:
3720 result
= ctx
->abi
->color1
;
3722 case nir_intrinsic_load_user_data_amd
:
3723 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3724 result
= ctx
->abi
->user_data
;
3726 case nir_intrinsic_load_instance_id
:
3727 result
= ctx
->abi
->instance_id
;
3729 case nir_intrinsic_load_num_work_groups
:
3730 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3732 case nir_intrinsic_load_local_invocation_index
:
3733 result
= visit_load_local_invocation_index(ctx
);
3735 case nir_intrinsic_load_subgroup_id
:
3736 result
= visit_load_subgroup_id(ctx
);
3738 case nir_intrinsic_load_num_subgroups
:
3739 result
= visit_load_num_subgroups(ctx
);
3741 case nir_intrinsic_first_invocation
:
3742 result
= visit_first_invocation(ctx
);
3744 case nir_intrinsic_load_push_constant
:
3745 result
= visit_load_push_constant(ctx
, instr
);
3747 case nir_intrinsic_vulkan_resource_index
: {
3748 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3749 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3750 unsigned binding
= nir_intrinsic_binding(instr
);
3752 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3756 case nir_intrinsic_vulkan_resource_reindex
:
3757 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3759 case nir_intrinsic_store_ssbo
:
3760 visit_store_ssbo(ctx
, instr
);
3762 case nir_intrinsic_load_ssbo
:
3763 result
= visit_load_buffer(ctx
, instr
);
3765 case nir_intrinsic_ssbo_atomic_add
:
3766 case nir_intrinsic_ssbo_atomic_imin
:
3767 case nir_intrinsic_ssbo_atomic_umin
:
3768 case nir_intrinsic_ssbo_atomic_imax
:
3769 case nir_intrinsic_ssbo_atomic_umax
:
3770 case nir_intrinsic_ssbo_atomic_and
:
3771 case nir_intrinsic_ssbo_atomic_or
:
3772 case nir_intrinsic_ssbo_atomic_xor
:
3773 case nir_intrinsic_ssbo_atomic_exchange
:
3774 case nir_intrinsic_ssbo_atomic_comp_swap
:
3775 result
= visit_atomic_ssbo(ctx
, instr
);
3777 case nir_intrinsic_load_ubo
:
3778 result
= visit_load_ubo_buffer(ctx
, instr
);
3780 case nir_intrinsic_get_buffer_size
:
3781 result
= visit_get_buffer_size(ctx
, instr
);
3783 case nir_intrinsic_load_deref
:
3784 result
= visit_load_var(ctx
, instr
);
3786 case nir_intrinsic_store_deref
:
3787 visit_store_var(ctx
, instr
);
3789 case nir_intrinsic_load_shared
:
3790 result
= visit_load_shared(ctx
, instr
);
3792 case nir_intrinsic_store_shared
:
3793 visit_store_shared(ctx
, instr
);
3795 case nir_intrinsic_bindless_image_samples
:
3796 case nir_intrinsic_image_deref_samples
:
3797 result
= visit_image_samples(ctx
, instr
);
3799 case nir_intrinsic_bindless_image_load
:
3800 result
= visit_image_load(ctx
, instr
, true);
3802 case nir_intrinsic_image_deref_load
:
3803 result
= visit_image_load(ctx
, instr
, false);
3805 case nir_intrinsic_bindless_image_store
:
3806 visit_image_store(ctx
, instr
, true);
3808 case nir_intrinsic_image_deref_store
:
3809 visit_image_store(ctx
, instr
, false);
3811 case nir_intrinsic_bindless_image_atomic_add
:
3812 case nir_intrinsic_bindless_image_atomic_imin
:
3813 case nir_intrinsic_bindless_image_atomic_umin
:
3814 case nir_intrinsic_bindless_image_atomic_imax
:
3815 case nir_intrinsic_bindless_image_atomic_umax
:
3816 case nir_intrinsic_bindless_image_atomic_and
:
3817 case nir_intrinsic_bindless_image_atomic_or
:
3818 case nir_intrinsic_bindless_image_atomic_xor
:
3819 case nir_intrinsic_bindless_image_atomic_exchange
:
3820 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3821 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3822 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3823 result
= visit_image_atomic(ctx
, instr
, true);
3825 case nir_intrinsic_image_deref_atomic_add
:
3826 case nir_intrinsic_image_deref_atomic_imin
:
3827 case nir_intrinsic_image_deref_atomic_umin
:
3828 case nir_intrinsic_image_deref_atomic_imax
:
3829 case nir_intrinsic_image_deref_atomic_umax
:
3830 case nir_intrinsic_image_deref_atomic_and
:
3831 case nir_intrinsic_image_deref_atomic_or
:
3832 case nir_intrinsic_image_deref_atomic_xor
:
3833 case nir_intrinsic_image_deref_atomic_exchange
:
3834 case nir_intrinsic_image_deref_atomic_comp_swap
:
3835 case nir_intrinsic_image_deref_atomic_inc_wrap
:
3836 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3837 result
= visit_image_atomic(ctx
, instr
, false);
3839 case nir_intrinsic_bindless_image_size
:
3840 result
= visit_image_size(ctx
, instr
, true);
3842 case nir_intrinsic_image_deref_size
:
3843 result
= visit_image_size(ctx
, instr
, false);
3845 case nir_intrinsic_shader_clock
:
3846 result
= ac_build_shader_clock(&ctx
->ac
);
3848 case nir_intrinsic_discard
:
3849 case nir_intrinsic_discard_if
:
3850 emit_discard(ctx
, instr
);
3852 case nir_intrinsic_demote
:
3853 case nir_intrinsic_demote_if
:
3854 emit_demote(ctx
, instr
);
3856 case nir_intrinsic_memory_barrier
:
3857 case nir_intrinsic_group_memory_barrier
:
3858 case nir_intrinsic_memory_barrier_buffer
:
3859 case nir_intrinsic_memory_barrier_image
:
3860 case nir_intrinsic_memory_barrier_shared
:
3861 emit_membar(&ctx
->ac
, instr
);
3863 case nir_intrinsic_memory_barrier_tcs_patch
:
3865 case nir_intrinsic_control_barrier
:
3866 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3868 case nir_intrinsic_shared_atomic_add
:
3869 case nir_intrinsic_shared_atomic_imin
:
3870 case nir_intrinsic_shared_atomic_umin
:
3871 case nir_intrinsic_shared_atomic_imax
:
3872 case nir_intrinsic_shared_atomic_umax
:
3873 case nir_intrinsic_shared_atomic_and
:
3874 case nir_intrinsic_shared_atomic_or
:
3875 case nir_intrinsic_shared_atomic_xor
:
3876 case nir_intrinsic_shared_atomic_exchange
:
3877 case nir_intrinsic_shared_atomic_comp_swap
: {
3878 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3879 instr
->src
[1].ssa
->bit_size
);
3880 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3883 case nir_intrinsic_deref_atomic_add
:
3884 case nir_intrinsic_deref_atomic_imin
:
3885 case nir_intrinsic_deref_atomic_umin
:
3886 case nir_intrinsic_deref_atomic_imax
:
3887 case nir_intrinsic_deref_atomic_umax
:
3888 case nir_intrinsic_deref_atomic_and
:
3889 case nir_intrinsic_deref_atomic_or
:
3890 case nir_intrinsic_deref_atomic_xor
:
3891 case nir_intrinsic_deref_atomic_exchange
:
3892 case nir_intrinsic_deref_atomic_comp_swap
: {
3893 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3894 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3897 case nir_intrinsic_load_barycentric_pixel
:
3898 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
3900 case nir_intrinsic_load_barycentric_centroid
:
3901 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
3903 case nir_intrinsic_load_barycentric_sample
:
3904 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
3906 case nir_intrinsic_load_barycentric_model
:
3907 result
= barycentric_model(ctx
);
3909 case nir_intrinsic_load_barycentric_at_offset
: {
3910 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3911 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
3914 case nir_intrinsic_load_barycentric_at_sample
: {
3915 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
3916 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
3919 case nir_intrinsic_load_interpolated_input
: {
3920 /* We assume any indirect loads have been lowered away */
3921 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
3923 assert(offset
[0].i32
== 0);
3925 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
3926 unsigned index
= nir_intrinsic_base(instr
);
3927 unsigned component
= nir_intrinsic_component(instr
);
3928 result
= load_interpolated_input(ctx
, interp_param
, index
,
3930 instr
->dest
.ssa
.num_components
,
3931 instr
->dest
.ssa
.bit_size
);
3934 case nir_intrinsic_load_input
:
3935 case nir_intrinsic_load_input_vertex
:
3936 result
= load_input(ctx
, instr
);
3938 case nir_intrinsic_emit_vertex
:
3939 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3941 case nir_intrinsic_emit_vertex_with_counter
: {
3942 unsigned stream
= nir_intrinsic_stream_id(instr
);
3943 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
3944 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
3949 case nir_intrinsic_end_primitive
:
3950 case nir_intrinsic_end_primitive_with_counter
:
3951 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3953 case nir_intrinsic_load_tess_coord
:
3954 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3956 case nir_intrinsic_load_tess_level_outer
:
3957 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
3959 case nir_intrinsic_load_tess_level_inner
:
3960 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
3962 case nir_intrinsic_load_tess_level_outer_default
:
3963 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
3965 case nir_intrinsic_load_tess_level_inner_default
:
3966 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
3968 case nir_intrinsic_load_patch_vertices_in
:
3969 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3971 case nir_intrinsic_vote_all
: {
3972 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3973 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3976 case nir_intrinsic_vote_any
: {
3977 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3978 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3981 case nir_intrinsic_shuffle
:
3982 if (ctx
->ac
.chip_class
== GFX8
||
3983 ctx
->ac
.chip_class
== GFX9
||
3984 (ctx
->ac
.chip_class
== GFX10
&& ctx
->ac
.wave_size
== 32)) {
3985 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3986 get_src(ctx
, instr
->src
[1]));
3988 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3989 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
3990 LLVMTypeRef type
= LLVMTypeOf(src
);
3991 struct waterfall_context wctx
;
3992 LLVMValueRef index_val
;
3994 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
3996 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
3999 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4001 (LLVMValueRef
[]) { src
, index_val
}, 2,
4002 AC_FUNC_ATTR_READNONE
|
4003 AC_FUNC_ATTR_CONVERGENT
);
4005 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4007 result
= exit_waterfall(ctx
, &wctx
, result
);
4010 case nir_intrinsic_reduce
:
4011 result
= ac_build_reduce(&ctx
->ac
,
4012 get_src(ctx
, instr
->src
[0]),
4013 instr
->const_index
[0],
4014 instr
->const_index
[1]);
4016 case nir_intrinsic_inclusive_scan
:
4017 result
= ac_build_inclusive_scan(&ctx
->ac
,
4018 get_src(ctx
, instr
->src
[0]),
4019 instr
->const_index
[0]);
4021 case nir_intrinsic_exclusive_scan
:
4022 result
= ac_build_exclusive_scan(&ctx
->ac
,
4023 get_src(ctx
, instr
->src
[0]),
4024 instr
->const_index
[0]);
4026 case nir_intrinsic_quad_broadcast
: {
4027 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4028 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4029 lane
, lane
, lane
, lane
);
4032 case nir_intrinsic_quad_swap_horizontal
:
4033 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4035 case nir_intrinsic_quad_swap_vertical
:
4036 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4038 case nir_intrinsic_quad_swap_diagonal
:
4039 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4041 case nir_intrinsic_quad_swizzle_amd
: {
4042 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4043 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4044 mask
& 0x3, (mask
>> 2) & 0x3,
4045 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4048 case nir_intrinsic_masked_swizzle_amd
: {
4049 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4050 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4053 case nir_intrinsic_write_invocation_amd
:
4054 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4055 get_src(ctx
, instr
->src
[1]),
4056 get_src(ctx
, instr
->src
[2]));
4058 case nir_intrinsic_mbcnt_amd
:
4059 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4061 case nir_intrinsic_load_scratch
: {
4062 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4063 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4065 LLVMTypeRef comp_type
=
4066 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4067 LLVMTypeRef vec_type
=
4068 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4069 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4070 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4071 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4072 LLVMPointerType(vec_type
, addr_space
), "");
4073 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4076 case nir_intrinsic_store_scratch
: {
4077 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4078 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4080 LLVMTypeRef comp_type
=
4081 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4082 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4083 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4084 LLVMPointerType(comp_type
, addr_space
), "");
4085 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4086 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4089 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4091 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4092 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4093 LLVMTypeRef vec_type
=
4094 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4095 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4097 LLVMPointerType(vec_type
, addr_space
),
4099 LLVMValueRef offset_src
=
4100 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4101 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4105 case nir_intrinsic_load_constant
: {
4106 unsigned base
= nir_intrinsic_base(instr
);
4107 unsigned range
= nir_intrinsic_range(instr
);
4109 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4110 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4111 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4113 /* Clamp the offset to avoid out-of-bound access because global
4114 * instructions can't handle them.
4116 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4117 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4119 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4121 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4123 LLVMTypeRef comp_type
=
4124 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4125 LLVMTypeRef vec_type
=
4126 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4127 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4128 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4129 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4130 LLVMPointerType(vec_type
, addr_space
), "");
4131 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4135 fprintf(stderr
, "Unknown intrinsic: ");
4136 nir_print_instr(&instr
->instr
, stderr
);
4137 fprintf(stderr
, "\n");
4141 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4145 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4146 unsigned base_index
,
4147 unsigned constant_index
,
4148 LLVMValueRef dynamic_index
)
4150 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4151 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4152 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4154 /* Bindless uniforms are 64bit so multiple index by 8 */
4155 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4156 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4158 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4160 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4161 NULL
, 0, 0, true, true);
4163 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4166 struct sampler_desc_address
{
4167 unsigned descriptor_set
;
4168 unsigned base_index
; /* binding in vulkan */
4169 unsigned constant_index
;
4170 LLVMValueRef dynamic_index
;
4175 static struct sampler_desc_address
4176 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4177 nir_deref_instr
*deref_instr
,
4178 const nir_instr
*instr
,
4181 LLVMValueRef index
= NULL
;
4182 unsigned constant_index
= 0;
4183 unsigned descriptor_set
;
4184 unsigned base_index
;
4185 bool bindless
= false;
4190 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4193 index
= get_src(ctx
, img_instr
->src
[0]);
4195 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4196 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4197 nir_tex_src_sampler_handle
);
4198 if (sampSrcIdx
!= -1) {
4201 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4203 assert(tex_instr
&& !image
);
4204 base_index
= tex_instr
->sampler_index
;
4208 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4209 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4210 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4214 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4215 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4217 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4219 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4220 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4225 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4228 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4229 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4230 unsigned sidx
= deref_instr
->strct
.index
;
4231 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4232 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4234 unreachable("Unsupported deref type");
4237 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4239 if (deref_instr
->var
->data
.bindless
) {
4240 /* For now just assert on unhandled variable types */
4241 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4243 base_index
= deref_instr
->var
->data
.driver_location
;
4246 index
= index
? index
: ctx
->ac
.i32_0
;
4247 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4248 constant_index
, index
);
4250 base_index
= deref_instr
->var
->data
.binding
;
4252 return (struct sampler_desc_address
) {
4253 .descriptor_set
= descriptor_set
,
4254 .base_index
= base_index
,
4255 .constant_index
= constant_index
,
4256 .dynamic_index
= index
,
4258 .bindless
= bindless
,
4262 /* Extract any possibly divergent index into a separate value that can be fed
4263 * into get_sampler_desc with the same arguments. */
4264 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4265 nir_deref_instr
*deref_instr
,
4266 const nir_instr
*instr
,
4269 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4270 return addr
.dynamic_index
;
4273 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4274 nir_deref_instr
*deref_instr
,
4275 enum ac_descriptor_type desc_type
,
4276 const nir_instr
*instr
,
4278 bool image
, bool write
)
4280 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4281 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4282 addr
.descriptor_set
,
4284 addr
.constant_index
, index
,
4285 desc_type
, addr
.image
, write
, addr
.bindless
);
4288 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4291 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4292 * filtering manually. The driver sets img7 to a mask clearing
4293 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4294 * s_and_b32 samp0, samp0, img7
4297 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4299 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4300 LLVMValueRef res
, LLVMValueRef samp
)
4302 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4303 LLVMValueRef img7
, samp0
;
4305 if (ctx
->ac
.chip_class
>= GFX8
)
4308 img7
= LLVMBuildExtractElement(builder
, res
,
4309 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4310 samp0
= LLVMBuildExtractElement(builder
, samp
,
4311 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4312 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4313 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4314 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4317 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4318 nir_tex_instr
*instr
,
4319 struct waterfall_context
*wctx
,
4320 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4321 LLVMValueRef
*fmask_ptr
)
4323 nir_deref_instr
*texture_deref_instr
= NULL
;
4324 nir_deref_instr
*sampler_deref_instr
= NULL
;
4327 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4328 switch (instr
->src
[i
].src_type
) {
4329 case nir_tex_src_texture_deref
:
4330 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4332 case nir_tex_src_sampler_deref
:
4333 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4335 case nir_tex_src_plane
:
4336 plane
= nir_src_as_int(instr
->src
[i
].src
);
4343 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4344 &instr
->instr
, false);
4345 if (!sampler_deref_instr
)
4346 sampler_deref_instr
= texture_deref_instr
;
4348 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4349 &instr
->instr
, false);
4350 if (instr
->texture_non_uniform
)
4351 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4353 if (instr
->sampler_non_uniform
)
4354 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4356 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4359 assert(instr
->op
!= nir_texop_txf_ms
&&
4360 instr
->op
!= nir_texop_samples_identical
);
4361 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4363 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4366 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4367 /* The fragment mask is fetched from the compressed
4368 * multisampled surface.
4370 main_descriptor
= AC_DESC_FMASK
;
4373 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4374 texture_dynamic_index
, false, false);
4377 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4378 sampler_dynamic_index
, false, false);
4379 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4380 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4382 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4383 instr
->op
== nir_texop_samples_identical
))
4384 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4385 &instr
->instr
, texture_dynamic_index
, false, false);
4388 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4391 coord
= ac_to_float(ctx
, coord
);
4392 coord
= ac_build_round(ctx
, coord
);
4393 coord
= ac_to_integer(ctx
, coord
);
4397 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4399 LLVMValueRef result
= NULL
;
4400 struct ac_image_args args
= { 0 };
4401 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4402 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4403 unsigned offset_src
= 0;
4404 struct waterfall_context wctx
[2] = {{{0}}};
4406 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4408 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4409 switch (instr
->src
[i
].src_type
) {
4410 case nir_tex_src_coord
: {
4411 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4412 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4413 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4416 case nir_tex_src_projector
:
4418 case nir_tex_src_comparator
:
4419 if (instr
->is_shadow
) {
4420 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4421 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4424 case nir_tex_src_offset
:
4425 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4428 case nir_tex_src_bias
:
4429 if (instr
->op
== nir_texop_txb
)
4430 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4432 case nir_tex_src_lod
: {
4433 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4434 args
.level_zero
= true;
4436 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4439 case nir_tex_src_ms_index
:
4440 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4442 case nir_tex_src_ms_mcs
:
4444 case nir_tex_src_ddx
:
4445 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4447 case nir_tex_src_ddy
:
4448 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4450 case nir_tex_src_texture_offset
:
4451 case nir_tex_src_sampler_offset
:
4452 case nir_tex_src_plane
:
4458 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4459 result
= get_buffer_size(ctx
, args
.resource
, true);
4463 if (instr
->op
== nir_texop_texture_samples
) {
4464 LLVMValueRef res
, samples
, is_msaa
;
4465 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4466 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4467 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4468 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4469 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4470 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4471 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4472 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4473 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4475 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4476 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4477 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4478 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4479 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4481 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4487 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4488 LLVMValueRef offset
[3], pack
;
4489 for (unsigned chan
= 0; chan
< 3; ++chan
)
4490 offset
[chan
] = ctx
->ac
.i32_0
;
4492 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4493 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4494 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4495 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4496 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4498 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4499 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4501 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4502 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4506 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4507 * OpenGL 4.5 spec says:
4509 * "If the texture’s internal format indicates a fixed-point
4510 * depth texture, then D_t and D_ref are clamped to the
4511 * range [0, 1]; otherwise no clamping is performed."
4513 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4514 * so the depth comparison value isn't clamped for Z16 and
4515 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4516 * an explicitly clamped 32-bit float format.
4519 ctx
->ac
.chip_class
>= GFX8
&&
4520 ctx
->ac
.chip_class
<= GFX9
&&
4521 ctx
->abi
->clamp_shadow_reference
) {
4522 LLVMValueRef upgraded
, clamped
;
4524 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4525 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4526 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4527 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4528 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4529 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4530 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4534 /* pack derivatives */
4536 int num_src_deriv_channels
, num_dest_deriv_channels
;
4537 switch (instr
->sampler_dim
) {
4538 case GLSL_SAMPLER_DIM_3D
:
4539 case GLSL_SAMPLER_DIM_CUBE
:
4540 num_src_deriv_channels
= 3;
4541 num_dest_deriv_channels
= 3;
4543 case GLSL_SAMPLER_DIM_2D
:
4545 num_src_deriv_channels
= 2;
4546 num_dest_deriv_channels
= 2;
4548 case GLSL_SAMPLER_DIM_1D
:
4549 num_src_deriv_channels
= 1;
4550 if (ctx
->ac
.chip_class
== GFX9
) {
4551 num_dest_deriv_channels
= 2;
4553 num_dest_deriv_channels
= 1;
4558 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4559 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4560 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4561 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4562 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4564 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4565 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4566 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4570 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4571 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4572 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4573 if (instr
->coord_components
== 3)
4574 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4575 ac_prepare_cube_coords(&ctx
->ac
,
4576 instr
->op
== nir_texop_txd
, instr
->is_array
,
4577 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4580 /* Texture coordinates fixups */
4581 if (instr
->coord_components
> 1 &&
4582 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4584 instr
->op
!= nir_texop_txf
) {
4585 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4588 if (instr
->coord_components
> 2 &&
4589 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4590 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4591 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4592 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4594 instr
->op
!= nir_texop_txf
&&
4595 instr
->op
!= nir_texop_txf_ms
&&
4596 instr
->op
!= nir_texop_fragment_fetch
&&
4597 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4598 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4601 if (ctx
->ac
.chip_class
== GFX9
&&
4602 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4603 instr
->op
!= nir_texop_lod
) {
4604 LLVMValueRef filler
;
4605 if (instr
->op
== nir_texop_txf
)
4606 filler
= ctx
->ac
.i32_0
;
4608 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4610 if (instr
->is_array
)
4611 args
.coords
[2] = args
.coords
[1];
4612 args
.coords
[1] = filler
;
4615 /* Pack sample index */
4616 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4617 instr
->op
== nir_texop_fragment_fetch
))
4618 args
.coords
[instr
->coord_components
] = sample_index
;
4620 if (instr
->op
== nir_texop_samples_identical
) {
4621 struct ac_image_args txf_args
= { 0 };
4622 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4624 txf_args
.dmask
= 0xf;
4625 txf_args
.resource
= fmask_ptr
;
4626 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4627 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4629 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4630 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4634 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4635 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4636 instr
->op
!= nir_texop_txs
&&
4637 instr
->op
!= nir_texop_fragment_fetch
&&
4638 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4639 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4640 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4641 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4642 instr
->is_array
? args
.coords
[2] : NULL
,
4643 args
.coords
[sample_chan
], fmask_ptr
);
4646 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4647 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4648 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4649 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4650 args
.coords
[i
] = LLVMBuildAdd(
4651 ctx
->ac
.builder
, args
.coords
[i
],
4652 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4657 /* DMASK was repurposed for GATHER4. 4 components are always
4658 * returned and DMASK works like a swizzle - it selects
4659 * the component to fetch. The only valid DMASK values are
4660 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4661 * (red,red,red,red) etc.) The ISA document doesn't mention
4665 if (instr
->op
== nir_texop_tg4
) {
4666 if (instr
->is_shadow
)
4669 args
.dmask
= 1 << instr
->component
;
4672 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4673 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4674 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4677 /* Adjust the number of coordinates because we only need (x,y) for 2D
4678 * multisampled images and (x,y,layer) for 2D multisampled layered
4679 * images or for multisampled input attachments.
4681 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4682 if (args
.dim
== ac_image_2dmsaa
) {
4683 args
.dim
= ac_image_2d
;
4685 assert(args
.dim
== ac_image_2darraymsaa
);
4686 args
.dim
= ac_image_2darray
;
4690 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4692 if (instr
->op
== nir_texop_query_levels
)
4693 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4694 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4695 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4696 instr
->op
!= nir_texop_tg4
)
4697 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4698 else if (instr
->op
== nir_texop_txs
&&
4699 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4701 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4702 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4703 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4704 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4705 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4706 } else if (ctx
->ac
.chip_class
== GFX9
&&
4707 instr
->op
== nir_texop_txs
&&
4708 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4710 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4711 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4712 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4714 } else if (instr
->dest
.ssa
.num_components
!= 4)
4715 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4719 assert(instr
->dest
.is_ssa
);
4720 result
= ac_to_integer(&ctx
->ac
, result
);
4722 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4723 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4726 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4730 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4732 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4733 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4735 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4736 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4739 static void visit_post_phi(struct ac_nir_context
*ctx
,
4740 nir_phi_instr
*instr
,
4741 LLVMValueRef llvm_phi
)
4743 nir_foreach_phi_src(src
, instr
) {
4744 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4745 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4747 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4751 static void phi_post_pass(struct ac_nir_context
*ctx
)
4753 hash_table_foreach(ctx
->phis
, entry
) {
4754 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4755 (LLVMValueRef
)entry
->data
);
4760 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4761 const nir_ssa_undef_instr
*instr
)
4763 unsigned num_components
= instr
->def
.num_components
;
4764 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4767 if (num_components
== 1)
4768 undef
= LLVMGetUndef(type
);
4770 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4772 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4775 static void visit_jump(struct ac_llvm_context
*ctx
,
4776 const nir_jump_instr
*instr
)
4778 switch (instr
->type
) {
4779 case nir_jump_break
:
4780 ac_build_break(ctx
);
4782 case nir_jump_continue
:
4783 ac_build_continue(ctx
);
4786 fprintf(stderr
, "Unknown NIR jump instr: ");
4787 nir_print_instr(&instr
->instr
, stderr
);
4788 fprintf(stderr
, "\n");
4794 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4795 enum glsl_base_type type
)
4799 case GLSL_TYPE_UINT
:
4800 case GLSL_TYPE_BOOL
:
4801 case GLSL_TYPE_SUBROUTINE
:
4803 case GLSL_TYPE_INT8
:
4804 case GLSL_TYPE_UINT8
:
4806 case GLSL_TYPE_INT16
:
4807 case GLSL_TYPE_UINT16
:
4809 case GLSL_TYPE_FLOAT
:
4811 case GLSL_TYPE_FLOAT16
:
4813 case GLSL_TYPE_INT64
:
4814 case GLSL_TYPE_UINT64
:
4816 case GLSL_TYPE_DOUBLE
:
4819 unreachable("unknown GLSL type");
4824 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4825 const struct glsl_type
*type
)
4827 if (glsl_type_is_scalar(type
)) {
4828 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4831 if (glsl_type_is_vector(type
)) {
4832 return LLVMVectorType(
4833 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4834 glsl_get_vector_elements(type
));
4837 if (glsl_type_is_matrix(type
)) {
4838 return LLVMArrayType(
4839 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4840 glsl_get_matrix_columns(type
));
4843 if (glsl_type_is_array(type
)) {
4844 return LLVMArrayType(
4845 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4846 glsl_get_length(type
));
4849 assert(glsl_type_is_struct_or_ifc(type
));
4851 LLVMTypeRef member_types
[glsl_get_length(type
)];
4853 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4855 glsl_to_llvm_type(ac
,
4856 glsl_get_struct_field(type
, i
));
4859 return LLVMStructTypeInContext(ac
->context
, member_types
,
4860 glsl_get_length(type
), false);
4863 static void visit_deref(struct ac_nir_context
*ctx
,
4864 nir_deref_instr
*instr
)
4866 if (instr
->mode
!= nir_var_mem_shared
&&
4867 instr
->mode
!= nir_var_mem_global
)
4870 LLVMValueRef result
= NULL
;
4871 switch(instr
->deref_type
) {
4872 case nir_deref_type_var
: {
4873 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4874 result
= entry
->data
;
4877 case nir_deref_type_struct
:
4878 if (instr
->mode
== nir_var_mem_global
) {
4879 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4880 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4881 instr
->strct
.index
);
4882 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4883 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4885 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4886 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4889 case nir_deref_type_array
:
4890 if (instr
->mode
== nir_var_mem_global
) {
4891 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4892 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4894 if ((glsl_type_is_matrix(parent
->type
) &&
4895 glsl_matrix_type_is_row_major(parent
->type
)) ||
4896 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4897 stride
= type_scalar_size_bytes(parent
->type
);
4900 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4901 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4902 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4904 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4906 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4908 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4909 get_src(ctx
, instr
->arr
.index
));
4912 case nir_deref_type_ptr_as_array
:
4913 if (instr
->mode
== nir_var_mem_global
) {
4914 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4916 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4917 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4918 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4920 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4922 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4924 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4925 get_src(ctx
, instr
->arr
.index
));
4928 case nir_deref_type_cast
: {
4929 result
= get_src(ctx
, instr
->parent
);
4931 /* We can't use the structs from LLVM because the shader
4932 * specifies its own offsets. */
4933 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4934 if (instr
->mode
== nir_var_mem_shared
)
4935 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4937 unsigned address_space
;
4939 switch(instr
->mode
) {
4940 case nir_var_mem_shared
:
4941 address_space
= AC_ADDR_SPACE_LDS
;
4943 case nir_var_mem_global
:
4944 address_space
= AC_ADDR_SPACE_GLOBAL
;
4947 unreachable("Unhandled address space");
4950 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4952 if (LLVMTypeOf(result
) != type
) {
4953 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4954 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4957 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4964 unreachable("Unhandled deref_instr deref type");
4967 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4970 static void visit_cf_list(struct ac_nir_context
*ctx
,
4971 struct exec_list
*list
);
4973 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4975 nir_foreach_instr(instr
, block
)
4977 switch (instr
->type
) {
4978 case nir_instr_type_alu
:
4979 visit_alu(ctx
, nir_instr_as_alu(instr
));
4981 case nir_instr_type_load_const
:
4982 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4984 case nir_instr_type_intrinsic
:
4985 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4987 case nir_instr_type_tex
:
4988 visit_tex(ctx
, nir_instr_as_tex(instr
));
4990 case nir_instr_type_phi
:
4991 visit_phi(ctx
, nir_instr_as_phi(instr
));
4993 case nir_instr_type_ssa_undef
:
4994 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4996 case nir_instr_type_jump
:
4997 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4999 case nir_instr_type_deref
:
5000 visit_deref(ctx
, nir_instr_as_deref(instr
));
5003 fprintf(stderr
, "Unknown NIR instr type: ");
5004 nir_print_instr(instr
, stderr
);
5005 fprintf(stderr
, "\n");
5010 _mesa_hash_table_insert(ctx
->defs
, block
,
5011 LLVMGetInsertBlock(ctx
->ac
.builder
));
5014 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5016 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5018 nir_block
*then_block
=
5019 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5021 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5023 visit_cf_list(ctx
, &if_stmt
->then_list
);
5025 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5026 nir_block
*else_block
=
5027 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5029 ac_build_else(&ctx
->ac
, else_block
->index
);
5030 visit_cf_list(ctx
, &if_stmt
->else_list
);
5033 ac_build_endif(&ctx
->ac
, then_block
->index
);
5036 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5038 nir_block
*first_loop_block
=
5039 (nir_block
*) exec_list_get_head(&loop
->body
);
5041 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5043 visit_cf_list(ctx
, &loop
->body
);
5045 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5048 static void visit_cf_list(struct ac_nir_context
*ctx
,
5049 struct exec_list
*list
)
5051 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5053 switch (node
->type
) {
5054 case nir_cf_node_block
:
5055 visit_block(ctx
, nir_cf_node_as_block(node
));
5058 case nir_cf_node_if
:
5059 visit_if(ctx
, nir_cf_node_as_if(node
));
5062 case nir_cf_node_loop
:
5063 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5073 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5074 struct ac_shader_abi
*abi
,
5075 struct nir_shader
*nir
,
5076 struct nir_variable
*variable
,
5077 gl_shader_stage stage
)
5079 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5080 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5082 /* tess ctrl has it's own load/store paths for outputs */
5083 if (stage
== MESA_SHADER_TESS_CTRL
)
5086 if (stage
== MESA_SHADER_VERTEX
||
5087 stage
== MESA_SHADER_TESS_EVAL
||
5088 stage
== MESA_SHADER_GEOMETRY
) {
5089 int idx
= variable
->data
.location
+ variable
->data
.index
;
5090 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5091 int length
= nir
->info
.clip_distance_array_size
+
5092 nir
->info
.cull_distance_array_size
;
5101 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5102 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5103 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5104 for (unsigned chan
= 0; chan
< 4; chan
++) {
5105 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5106 ac_build_alloca_undef(ctx
, type
, "");
5112 setup_locals(struct ac_nir_context
*ctx
,
5113 struct nir_function
*func
)
5116 ctx
->num_locals
= 0;
5117 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5118 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5119 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5120 variable
->data
.location_frac
= 0;
5121 ctx
->num_locals
+= attrib_count
;
5123 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5127 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5128 for (j
= 0; j
< 4; j
++) {
5129 ctx
->locals
[i
* 4 + j
] =
5130 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5136 setup_scratch(struct ac_nir_context
*ctx
,
5137 struct nir_shader
*shader
)
5139 if (shader
->scratch_size
== 0)
5142 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5143 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5148 setup_constant_data(struct ac_nir_context
*ctx
,
5149 struct nir_shader
*shader
)
5151 if (!shader
->constant_data
)
5155 LLVMConstStringInContext(ctx
->ac
.context
,
5156 shader
->constant_data
,
5157 shader
->constant_data_size
,
5159 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5161 /* We want to put the constant data in the CONST address space so that
5162 * we can use scalar loads. However, LLVM versions before 10 put these
5163 * variables in the same section as the code, which is unacceptable
5164 * for RadeonSI as it needs to relocate all the data sections after
5165 * the code sections. See https://reviews.llvm.org/D65813.
5167 unsigned address_space
=
5168 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5170 LLVMValueRef global
=
5171 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5175 LLVMSetInitializer(global
, data
);
5176 LLVMSetGlobalConstant(global
, true);
5177 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5178 ctx
->constant_data
= global
;
5182 setup_shared(struct ac_nir_context
*ctx
,
5183 struct nir_shader
*nir
)
5188 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5189 nir
->info
.cs
.shared_size
);
5192 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5195 LLVMSetAlignment(lds
, 64 * 1024);
5197 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5198 LLVMPointerType(ctx
->ac
.i8
,
5199 AC_ADDR_SPACE_LDS
), "");
5202 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5203 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5205 struct ac_nir_context ctx
= {};
5206 struct nir_function
*func
;
5212 ctx
.stage
= nir
->info
.stage
;
5213 ctx
.info
= &nir
->info
;
5215 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5217 nir_foreach_variable(variable
, &nir
->outputs
)
5218 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5221 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5222 _mesa_key_pointer_equal
);
5223 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5224 _mesa_key_pointer_equal
);
5225 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5226 _mesa_key_pointer_equal
);
5228 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5230 nir_index_ssa_defs(func
->impl
);
5231 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5233 setup_locals(&ctx
, func
);
5234 setup_scratch(&ctx
, nir
);
5235 setup_constant_data(&ctx
, nir
);
5237 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5238 setup_shared(&ctx
, nir
);
5240 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5241 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5242 /* true = don't kill. */
5243 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5246 visit_cf_list(&ctx
, &func
->impl
->body
);
5247 phi_post_pass(&ctx
);
5249 if (ctx
.ac
.postponed_kill
)
5250 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5251 ctx
.ac
.postponed_kill
, ""));
5253 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5254 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5259 ralloc_free(ctx
.defs
);
5260 ralloc_free(ctx
.phis
);
5261 ralloc_free(ctx
.vars
);
5265 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5267 bool progress
= false;
5269 /* Lower large variables to scratch first so that we won't bloat the
5270 * shader by generating large if ladders for them. We later lower
5271 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5273 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5274 nir_var_function_temp
,
5276 glsl_get_natural_size_align_bytes
);
5278 /* While it would be nice not to have this flag, we are constrained
5279 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5281 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5283 /* TODO: Indirect indexing of GS inputs is unimplemented.
5285 * TCS and TES load inputs directly from LDS or offchip memory, so
5286 * indirect indexing is trivial.
5288 nir_variable_mode indirect_mask
= 0;
5289 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5290 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5291 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5292 !llvm_has_working_vgpr_indexing
)) {
5293 indirect_mask
|= nir_var_shader_in
;
5295 if (!llvm_has_working_vgpr_indexing
&&
5296 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5297 indirect_mask
|= nir_var_shader_out
;
5299 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5300 * smart enough to handle indirects without causing excess spilling
5301 * causing the gpu to hang.
5303 * See the following thread for more details of the problem:
5304 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5306 indirect_mask
|= nir_var_function_temp
;
5308 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
);
5313 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5315 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
5319 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
5321 if (var
->data
.mode
!= nir_var_shader_out
)
5324 unsigned writemask
= 0;
5325 const int location
= var
->data
.location
;
5326 unsigned first_component
= var
->data
.location_frac
;
5327 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
5329 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5330 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
5331 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5332 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
5338 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5339 unsigned *cond_block_tf_writemask
,
5340 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5342 switch (cf_node
->type
) {
5343 case nir_cf_node_block
: {
5344 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5345 nir_foreach_instr(instr
, block
) {
5346 if (instr
->type
!= nir_instr_type_intrinsic
)
5349 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5350 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5352 /* If we find a barrier in nested control flow put this in the
5353 * too hard basket. In GLSL this is not possible but it is in
5357 *tessfactors_are_def_in_all_invocs
= false;
5361 /* The following case must be prevented:
5362 * gl_TessLevelInner = ...;
5364 * if (gl_InvocationID == 1)
5365 * gl_TessLevelInner = ...;
5367 * If you consider disjoint code segments separated by barriers, each
5368 * such segment that writes tess factor channels should write the same
5369 * channels in all codepaths within that segment.
5371 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5372 /* Accumulate the result: */
5373 *tessfactors_are_def_in_all_invocs
&=
5374 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5376 /* Analyze the next code segment from scratch. */
5377 *upper_block_tf_writemask
= 0;
5378 *cond_block_tf_writemask
= 0;
5381 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5386 case nir_cf_node_if
: {
5387 unsigned then_tessfactor_writemask
= 0;
5388 unsigned else_tessfactor_writemask
= 0;
5390 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5391 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5392 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5393 cond_block_tf_writemask
,
5394 tessfactors_are_def_in_all_invocs
, true);
5397 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5398 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5399 cond_block_tf_writemask
,
5400 tessfactors_are_def_in_all_invocs
, true);
5403 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5404 /* If both statements write the same tess factor channels,
5405 * we can say that the upper block writes them too.
5407 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5408 else_tessfactor_writemask
;
5409 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5410 else_tessfactor_writemask
;
5415 case nir_cf_node_loop
: {
5416 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5417 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5418 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5419 cond_block_tf_writemask
,
5420 tessfactors_are_def_in_all_invocs
, true);
5426 unreachable("unknown cf node type");
5431 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5433 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5435 /* The pass works as follows:
5436 * If all codepaths write tess factors, we can say that all
5437 * invocations define tess factors.
5439 * Each tess factor channel is tracked separately.
5441 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5442 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5444 /* Initial value = true. Here the pass will accumulate results from
5445 * multiple segments surrounded by barriers. If tess factors aren't
5446 * written at all, it's a shader bug and we don't care if this will be
5449 bool tessfactors_are_def_in_all_invocs
= true;
5451 nir_foreach_function(function
, nir
) {
5452 if (function
->impl
) {
5453 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5454 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5455 &cond_block_tf_writemask
,
5456 &tessfactors_are_def_in_all_invocs
,
5462 /* Accumulate the result for the last code segment separated by a
5465 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5466 tessfactors_are_def_in_all_invocs
&=
5467 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5470 return tessfactors_are_def_in_all_invocs
;