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
)) != LLVMFixedVectorTypeKind
);
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
);
592 bool saved_inexact
= false;
595 saved_inexact
= ac_disable_inexact_math(ctx
->ac
.builder
);
597 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
604 case nir_op_pack_half_2x16
:
605 case nir_op_pack_snorm_2x16
:
606 case nir_op_pack_unorm_2x16
:
609 case nir_op_unpack_half_2x16
:
612 case nir_op_cube_face_coord
:
613 case nir_op_cube_face_index
:
617 src_components
= num_components
;
620 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
621 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
628 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
629 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
630 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
631 /* fneg will be optimized by backend compiler with sign
632 * bit removed via XOR. This is probably a LLVM bug.
634 result
= ac_build_canonicalize(&ctx
->ac
, result
,
635 instr
->dest
.dest
.ssa
.bit_size
);
639 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
642 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
645 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
648 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
649 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
650 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
653 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
654 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
655 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
658 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
661 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
664 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
667 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
670 /* lower_fmod only lower 16-bit and 32-bit fmod */
671 assert(instr
->dest
.dest
.ssa
.bit_size
== 64);
672 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
673 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
674 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
675 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
676 ac_to_float_type(&ctx
->ac
, def_type
), result
);
677 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
678 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
681 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
684 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
687 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
690 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
691 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
692 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
695 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rcp",
696 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
699 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
702 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
705 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
708 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
709 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
710 LLVMTypeOf(src
[0]), "");
711 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
712 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
713 LLVMTypeOf(src
[0]), "");
714 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
717 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
718 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
719 LLVMTypeOf(src
[0]), "");
720 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
721 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
722 LLVMTypeOf(src
[0]), "");
723 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
726 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
727 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
728 LLVMTypeOf(src
[0]), "");
729 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
730 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
731 LLVMTypeOf(src
[0]), "");
732 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
735 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
738 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
741 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
744 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
747 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
750 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
753 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
756 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
759 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
762 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
765 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
766 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
767 if (ctx
->ac
.float_mode
== AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO
) {
768 /* fabs will be optimized by backend compiler with sign
769 * bit removed via AND.
771 result
= ac_build_canonicalize(&ctx
->ac
, result
,
772 instr
->dest
.dest
.ssa
.bit_size
);
776 result
= emit_iabs(&ctx
->ac
, src
[0]);
779 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
782 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
785 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
788 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
791 result
= ac_build_isign(&ctx
->ac
, src
[0],
792 instr
->dest
.dest
.ssa
.bit_size
);
795 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
796 result
= ac_build_fsign(&ctx
->ac
, src
[0],
797 instr
->dest
.dest
.ssa
.bit_size
);
800 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
801 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
804 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
805 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
808 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
809 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
811 case nir_op_fround_even
:
812 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
813 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
816 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
817 result
= ac_build_fract(&ctx
->ac
, src
[0],
818 instr
->dest
.dest
.ssa
.bit_size
);
821 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
822 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
825 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
826 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
829 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
830 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
833 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
834 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
837 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
838 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
841 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.amdgcn.rsq",
842 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
844 case nir_op_frexp_exp
:
845 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
846 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
847 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
848 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
849 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
852 case nir_op_frexp_sig
:
853 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
854 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
855 instr
->dest
.dest
.ssa
.bit_size
);
858 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
859 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
862 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
863 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
864 if (ctx
->ac
.chip_class
< GFX9
&&
865 instr
->dest
.dest
.ssa
.bit_size
== 32) {
866 /* Only pre-GFX9 chips do not flush denorms. */
867 result
= ac_build_canonicalize(&ctx
->ac
, result
,
868 instr
->dest
.dest
.ssa
.bit_size
);
872 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
873 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
874 if (ctx
->ac
.chip_class
< GFX9
&&
875 instr
->dest
.dest
.ssa
.bit_size
== 32) {
876 /* Only pre-GFX9 chips do not flush denorms. */
877 result
= ac_build_canonicalize(&ctx
->ac
, result
,
878 instr
->dest
.dest
.ssa
.bit_size
);
882 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
883 result
= emit_intrin_3f_param(&ctx
->ac
, ctx
->ac
.chip_class
>= GFX10
? "llvm.fma" : "llvm.fmuladd",
884 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
887 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
888 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
889 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
890 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
891 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
893 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
896 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
898 case nir_op_bitfield_select
:
899 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
902 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
905 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
907 case nir_op_bitfield_reverse
:
908 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
910 case nir_op_bit_count
:
911 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
916 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
917 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
918 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
924 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
925 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
931 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
932 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
937 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
942 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
944 case nir_op_f2f16_rtz
:
945 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
946 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
947 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
948 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
949 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
950 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
952 case nir_op_f2f16_rtne
:
956 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
957 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
958 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
960 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
966 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
967 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
969 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
975 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
976 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
978 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
981 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
983 case nir_op_find_lsb
:
984 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
986 case nir_op_ufind_msb
:
987 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
989 case nir_op_ifind_msb
:
990 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
992 case nir_op_uadd_carry
:
993 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
995 case nir_op_usub_borrow
:
996 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
1001 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1004 result
= emit_f2b(&ctx
->ac
, src
[0]);
1010 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
1013 result
= emit_i2b(&ctx
->ac
, src
[0]);
1015 case nir_op_fquantize2f16
:
1016 result
= emit_f2f16(&ctx
->ac
, src
[0]);
1018 case nir_op_umul_high
:
1019 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
1021 case nir_op_imul_high
:
1022 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
1024 case nir_op_pack_half_2x16
:
1025 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
1027 case nir_op_pack_snorm_2x16
:
1028 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
1030 case nir_op_pack_unorm_2x16
:
1031 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
1033 case nir_op_unpack_half_2x16
:
1034 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
1038 case nir_op_fddx_fine
:
1039 case nir_op_fddy_fine
:
1040 case nir_op_fddx_coarse
:
1041 case nir_op_fddy_coarse
:
1042 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
1045 case nir_op_unpack_64_2x32_split_x
: {
1046 assert(ac_get_llvm_num_components(src
[0]) == 1);
1047 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1050 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1055 case nir_op_unpack_64_2x32_split_y
: {
1056 assert(ac_get_llvm_num_components(src
[0]) == 1);
1057 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1060 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1065 case nir_op_pack_64_2x32_split
: {
1066 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1067 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1071 case nir_op_pack_32_2x16_split
: {
1072 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1073 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1077 case nir_op_unpack_32_2x16_split_x
: {
1078 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1081 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1086 case nir_op_unpack_32_2x16_split_y
: {
1087 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1090 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1095 case nir_op_cube_face_coord
: {
1096 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1097 LLVMValueRef results
[2];
1099 for (unsigned chan
= 0; chan
< 3; chan
++)
1100 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1101 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1102 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1103 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1104 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1105 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1106 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1107 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1108 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1109 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1110 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1111 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1112 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1116 case nir_op_cube_face_index
: {
1117 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1119 for (unsigned chan
= 0; chan
< 3; chan
++)
1120 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1121 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1122 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1127 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1128 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1129 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1130 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1133 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1134 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1137 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1138 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1141 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1142 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1143 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1144 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1147 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1148 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1151 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1152 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1154 case nir_op_fmed3
: {
1155 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1156 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1157 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1158 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1159 instr
->dest
.dest
.ssa
.bit_size
);
1162 case nir_op_imed3
: {
1163 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1164 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1165 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1166 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1169 case nir_op_umed3
: {
1170 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1171 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1172 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1173 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1178 fprintf(stderr
, "Unknown NIR alu instr: ");
1179 nir_print_instr(&instr
->instr
, stderr
);
1180 fprintf(stderr
, "\n");
1185 assert(instr
->dest
.dest
.is_ssa
);
1186 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1187 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1191 ac_restore_inexact_math(ctx
->ac
.builder
, saved_inexact
);
1194 static void visit_load_const(struct ac_nir_context
*ctx
,
1195 const nir_load_const_instr
*instr
)
1197 LLVMValueRef values
[4], value
= NULL
;
1198 LLVMTypeRef element_type
=
1199 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1201 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1202 switch (instr
->def
.bit_size
) {
1204 values
[i
] = LLVMConstInt(element_type
,
1205 instr
->value
[i
].u8
, false);
1208 values
[i
] = LLVMConstInt(element_type
,
1209 instr
->value
[i
].u16
, false);
1212 values
[i
] = LLVMConstInt(element_type
,
1213 instr
->value
[i
].u32
, false);
1216 values
[i
] = LLVMConstInt(element_type
,
1217 instr
->value
[i
].u64
, false);
1221 "unsupported nir load_const bit_size: %d\n",
1222 instr
->def
.bit_size
);
1226 if (instr
->def
.num_components
> 1) {
1227 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1231 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1235 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1238 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1239 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1242 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1243 /* On GFX8, the descriptor contains the size in bytes,
1244 * but TXQ must return the size in elements.
1245 * The stride is always non-zero for resources using TXQ.
1247 LLVMValueRef stride
=
1248 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1250 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1251 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1252 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1253 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1255 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1260 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1261 * incorrectly forces nearest filtering if the texture format is integer.
1262 * The only effect it has on Gather4, which always returns 4 texels for
1263 * bilinear filtering, is that the final coordinates are off by 0.5 of
1266 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1267 * or (0.5 / size) from the normalized coordinates.
1269 * However, cube textures with 8_8_8_8 data formats require a different
1270 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1271 * precision in 32-bit data formats, so it needs to be applied dynamically at
1272 * runtime. In this case, return an i1 value that indicates whether the
1273 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1275 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1277 struct ac_image_args
*args
,
1278 const nir_tex_instr
*instr
)
1280 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1281 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1282 LLVMValueRef wa_8888
= NULL
;
1283 LLVMValueRef half_texel
[2];
1284 LLVMValueRef result
;
1286 assert(stype
== GLSL_TYPE_INT
|| stype
== GLSL_TYPE_UINT
);
1288 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1289 LLVMValueRef formats
;
1290 LLVMValueRef data_format
;
1291 LLVMValueRef wa_formats
;
1293 formats
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1295 data_format
= LLVMBuildLShr(ctx
->builder
, formats
,
1296 LLVMConstInt(ctx
->i32
, 20, false), "");
1297 data_format
= LLVMBuildAnd(ctx
->builder
, data_format
,
1298 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1299 wa_8888
= LLVMBuildICmp(
1300 ctx
->builder
, LLVMIntEQ
, data_format
,
1301 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1304 uint32_t wa_num_format
=
1305 stype
== GLSL_TYPE_UINT
?
1306 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1307 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1308 wa_formats
= LLVMBuildAnd(ctx
->builder
, formats
,
1309 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1311 wa_formats
= LLVMBuildOr(ctx
->builder
, wa_formats
,
1312 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1314 formats
= LLVMBuildSelect(ctx
->builder
, wa_8888
, wa_formats
, formats
, "");
1315 args
->resource
= LLVMBuildInsertElement(
1316 ctx
->builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1319 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
1321 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1323 struct ac_image_args resinfo
= {};
1324 LLVMBasicBlockRef bbs
[2];
1326 LLVMValueRef unnorm
= NULL
;
1327 LLVMValueRef default_offset
= ctx
->f32_0
;
1328 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
&&
1330 /* In vulkan, whether the sampler uses unnormalized
1331 * coordinates or not is a dynamic property of the
1332 * sampler. Hence, to figure out whether or not we
1333 * need to divide by the texture size, we need to test
1334 * the sampler at runtime. This tests the bit set by
1335 * radv_init_sampler().
1337 LLVMValueRef sampler0
=
1338 LLVMBuildExtractElement(ctx
->builder
, args
->sampler
, ctx
->i32_0
, "");
1339 sampler0
= LLVMBuildLShr(ctx
->builder
, sampler0
,
1340 LLVMConstInt(ctx
->i32
, 15, false), "");
1341 sampler0
= LLVMBuildAnd(ctx
->builder
, sampler0
, ctx
->i32_1
, "");
1342 unnorm
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, sampler0
, ctx
->i32_1
, "");
1343 default_offset
= LLVMConstReal(ctx
->f32
, -0.5);
1346 bbs
[0] = LLVMGetInsertBlock(ctx
->builder
);
1347 if (wa_8888
|| unnorm
) {
1348 assert(!(wa_8888
&& unnorm
));
1349 LLVMValueRef not_needed
= wa_8888
? wa_8888
: unnorm
;
1350 /* Skip the texture size query entirely if we don't need it. */
1351 ac_build_ifcc(ctx
, LLVMBuildNot(ctx
->builder
, not_needed
, ""), 2000);
1352 bbs
[1] = LLVMGetInsertBlock(ctx
->builder
);
1355 /* Query the texture size. */
1356 resinfo
.dim
= ac_get_sampler_dim(ctx
->chip_class
, instr
->sampler_dim
, instr
->is_array
);
1357 resinfo
.opcode
= ac_image_get_resinfo
;
1358 resinfo
.dmask
= 0xf;
1359 resinfo
.lod
= ctx
->i32_0
;
1360 resinfo
.resource
= args
->resource
;
1361 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1362 LLVMValueRef size
= ac_build_image_opcode(ctx
, &resinfo
);
1364 /* Compute -0.5 / size. */
1365 for (unsigned c
= 0; c
< 2; c
++) {
1367 LLVMBuildExtractElement(ctx
->builder
, size
,
1368 LLVMConstInt(ctx
->i32
, c
, 0), "");
1369 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1370 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1371 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1372 LLVMConstReal(ctx
->f32
, -0.5), "");
1375 if (wa_8888
|| unnorm
) {
1376 ac_build_endif(ctx
, 2000);
1378 for (unsigned c
= 0; c
< 2; c
++) {
1379 LLVMValueRef values
[2] = { default_offset
, half_texel
[c
] };
1380 half_texel
[c
] = ac_build_phi(ctx
, ctx
->f32
, 2,
1386 for (unsigned c
= 0; c
< 2; c
++) {
1388 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1389 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1392 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1393 result
= ac_build_image_opcode(ctx
, args
);
1395 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1396 LLVMValueRef tmp
, tmp2
;
1398 /* if the cube workaround is in place, f2i the result. */
1399 for (unsigned c
= 0; c
< 4; c
++) {
1400 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1401 if (stype
== GLSL_TYPE_UINT
)
1402 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1404 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1405 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1406 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1407 tmp
= LLVMBuildSelect(ctx
->builder
, wa_8888
, tmp2
, tmp
, "");
1408 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1409 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1415 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1417 nir_deref_instr
*texture_deref_instr
= NULL
;
1419 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1420 switch (instr
->src
[i
].src_type
) {
1421 case nir_tex_src_texture_deref
:
1422 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1428 return texture_deref_instr
;
1431 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1432 const nir_tex_instr
*instr
,
1433 struct ac_image_args
*args
)
1435 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1436 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1438 return ac_build_buffer_load_format(&ctx
->ac
,
1442 util_last_bit(mask
),
1446 args
->opcode
= ac_image_sample
;
1448 switch (instr
->op
) {
1450 case nir_texop_txf_ms
:
1451 case nir_texop_samples_identical
:
1452 args
->opcode
= args
->level_zero
||
1453 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1454 ac_image_load
: ac_image_load_mip
;
1455 args
->level_zero
= false;
1458 case nir_texop_query_levels
:
1459 args
->opcode
= ac_image_get_resinfo
;
1461 args
->lod
= ctx
->ac
.i32_0
;
1462 args
->level_zero
= false;
1465 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1467 args
->level_zero
= true;
1471 args
->opcode
= ac_image_gather4
;
1472 args
->level_zero
= true;
1475 args
->opcode
= ac_image_get_lod
;
1477 case nir_texop_fragment_fetch
:
1478 case nir_texop_fragment_mask_fetch
:
1479 args
->opcode
= ac_image_load
;
1480 args
->level_zero
= false;
1486 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1487 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1488 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1489 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1490 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1491 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1492 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1496 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1497 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1498 if ((args
->dim
== ac_image_2darray
||
1499 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1500 args
->coords
[1] = ctx
->ac
.i32_0
;
1504 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1505 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1506 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1507 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1508 /* Prevent texture instructions with implicit derivatives from being
1509 * sinked into branches. */
1510 switch (instr
->op
) {
1514 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1521 return ac_build_image_opcode(&ctx
->ac
, args
);
1524 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1525 nir_intrinsic_instr
*instr
)
1527 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1528 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1530 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1531 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1535 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1536 nir_intrinsic_instr
*instr
)
1538 LLVMValueRef ptr
, addr
;
1539 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1540 unsigned index
= nir_intrinsic_base(instr
);
1542 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1543 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1545 /* Load constant values from user SGPRS when possible, otherwise
1546 * fallback to the default path that loads directly from memory.
1548 if (LLVMIsConstant(src0
) &&
1549 instr
->dest
.ssa
.bit_size
== 32) {
1550 unsigned count
= instr
->dest
.ssa
.num_components
;
1551 unsigned offset
= index
;
1553 offset
+= LLVMConstIntGetZExtValue(src0
);
1556 offset
-= ctx
->args
->base_inline_push_consts
;
1558 unsigned num_inline_push_consts
= ctx
->args
->num_inline_push_consts
;
1559 if (offset
+ count
<= num_inline_push_consts
) {
1560 LLVMValueRef push_constants
[num_inline_push_consts
];
1561 for (unsigned i
= 0; i
< num_inline_push_consts
; i
++)
1562 push_constants
[i
] = ac_get_arg(&ctx
->ac
,
1563 ctx
->args
->inline_push_consts
[i
]);
1564 return ac_build_gather_values(&ctx
->ac
,
1565 push_constants
+ offset
,
1570 ptr
= LLVMBuildGEP(ctx
->ac
.builder
,
1571 ac_get_arg(&ctx
->ac
, ctx
->args
->push_constants
), &addr
, 1, "");
1573 if (instr
->dest
.ssa
.bit_size
== 8) {
1574 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1575 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1576 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1577 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1579 LLVMValueRef params
[3];
1580 if (load_dwords
> 1) {
1581 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1582 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1583 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1585 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1586 params
[0] = ctx
->ac
.i32_0
;
1590 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1592 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1593 if (instr
->dest
.ssa
.num_components
> 1)
1594 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1596 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1597 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1598 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1599 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1600 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1601 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1602 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1603 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1604 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1605 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1606 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1607 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1608 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1609 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1610 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1611 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1612 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1615 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1617 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1620 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1621 const nir_intrinsic_instr
*instr
)
1623 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1625 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1628 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1630 uint32_t new_mask
= 0;
1631 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1632 if (mask
& (1u << i
))
1633 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1637 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1638 unsigned start
, unsigned count
)
1640 LLVMValueRef mask
[] = {
1641 ctx
->i32_0
, ctx
->i32_1
,
1642 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1644 unsigned src_elements
= ac_get_llvm_num_components(src
);
1646 if (count
== src_elements
) {
1649 } else if (count
== 1) {
1650 assert(start
< src_elements
);
1651 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1653 assert(start
+ count
<= src_elements
);
1655 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1656 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1660 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1661 enum gl_access_qualifier access
,
1662 bool may_store_unaligned
,
1663 bool writeonly_memory
)
1665 unsigned cache_policy
= 0;
1667 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1668 * store opcodes not aligned to a dword are affected. The only way to
1669 * get unaligned stores is through shader images.
1671 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1672 /* If this is write-only, don't keep data in L1 to prevent
1673 * evicting L1 cache lines that may be needed by other
1677 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1678 cache_policy
|= ac_glc
;
1681 if (access
& ACCESS_STREAM_CACHE_POLICY
)
1682 cache_policy
|= ac_slc
;
1684 return cache_policy
;
1687 static LLVMValueRef
enter_waterfall_ssbo(struct ac_nir_context
*ctx
,
1688 struct waterfall_context
*wctx
,
1689 const nir_intrinsic_instr
*instr
,
1692 return enter_waterfall(ctx
, wctx
, get_src(ctx
, src
),
1693 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
1696 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1697 nir_intrinsic_instr
*instr
)
1699 if (ctx
->ac
.postponed_kill
) {
1700 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1701 ctx
->ac
.postponed_kill
, "");
1702 ac_build_ifcc(&ctx
->ac
, cond
, 7000);
1705 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1706 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1707 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1708 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1709 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1710 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1712 struct waterfall_context wctx
;
1713 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[1]);
1715 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, true);
1716 LLVMValueRef base_data
= src_data
;
1717 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1718 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1722 LLVMValueRef data
, offset
;
1723 LLVMTypeRef data_type
;
1725 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1727 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1728 * writes into a 2-element and a 1-element write. */
1730 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1731 writemask
|= 1 << (start
+ 2);
1734 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1736 /* we can only store 4 DWords at the same time.
1737 * can only happen for 64 Bit vectors. */
1738 if (num_bytes
> 16) {
1739 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1744 /* check alignment of 16 Bit stores */
1745 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1746 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1751 /* Due to alignment issues, split stores of 8-bit/16-bit
1754 if (ctx
->ac
.chip_class
== GFX6
&& count
> 1 && elem_size_bytes
< 4) {
1755 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1757 num_bytes
= elem_size_bytes
;
1760 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1762 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1763 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1765 if (num_bytes
== 1) {
1766 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1767 offset
, ctx
->ac
.i32_0
,
1769 } else if (num_bytes
== 2) {
1770 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1771 offset
, ctx
->ac
.i32_0
,
1774 int num_channels
= num_bytes
/ 4;
1776 switch (num_bytes
) {
1777 case 16: /* v4f32 */
1778 data_type
= ctx
->ac
.v4f32
;
1780 case 12: /* v3f32 */
1781 data_type
= ctx
->ac
.v3f32
;
1784 data_type
= ctx
->ac
.v2f32
;
1787 data_type
= ctx
->ac
.f32
;
1790 unreachable("Malformed vector store.");
1792 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1794 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1795 num_channels
, offset
,
1801 exit_waterfall(ctx
, &wctx
, NULL
);
1803 if (ctx
->ac
.postponed_kill
)
1804 ac_build_endif(&ctx
->ac
, 7000);
1807 static LLVMValueRef
emit_ssbo_comp_swap_64(struct ac_nir_context
*ctx
,
1808 LLVMValueRef descriptor
,
1809 LLVMValueRef offset
,
1810 LLVMValueRef compare
,
1811 LLVMValueRef exchange
)
1813 LLVMBasicBlockRef start_block
= NULL
, then_block
= NULL
;
1814 if (ctx
->abi
->robust_buffer_access
) {
1815 LLVMValueRef size
= ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 2);
1817 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, offset
, size
, "");
1818 start_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1820 ac_build_ifcc(&ctx
->ac
, cond
, -1);
1822 then_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
1825 LLVMValueRef ptr_parts
[2] = {
1826 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 0),
1827 LLVMBuildAnd(ctx
->ac
.builder
,
1828 ac_llvm_extract_elem(&ctx
->ac
, descriptor
, 1),
1829 LLVMConstInt(ctx
->ac
.i32
, 65535, 0), "")
1832 ptr_parts
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i16
, "");
1833 ptr_parts
[1] = LLVMBuildSExt(ctx
->ac
.builder
, ptr_parts
[1], ctx
->ac
.i32
, "");
1835 offset
= LLVMBuildZExt(ctx
->ac
.builder
, offset
, ctx
->ac
.i64
, "");
1837 LLVMValueRef ptr
= ac_build_gather_values(&ctx
->ac
, ptr_parts
, 2);
1838 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->ac
.i64
, "");
1839 ptr
= LLVMBuildAdd(ctx
->ac
.builder
, ptr
, offset
, "");
1840 ptr
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
, LLVMPointerType(ctx
->ac
.i64
, AC_ADDR_SPACE_GLOBAL
), "");
1842 LLVMValueRef result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, compare
, exchange
, "singlethread-one-as");
1843 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
1845 if (ctx
->abi
->robust_buffer_access
) {
1846 ac_build_endif(&ctx
->ac
, -1);
1848 LLVMBasicBlockRef incoming_blocks
[2] = {
1853 LLVMValueRef incoming_values
[2] = {
1854 LLVMConstInt(ctx
->ac
.i64
, 0, 0),
1857 LLVMValueRef ret
= LLVMBuildPhi(ctx
->ac
.builder
, ctx
->ac
.i64
, "");
1858 LLVMAddIncoming(ret
, incoming_values
, incoming_blocks
, 2);
1865 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1866 nir_intrinsic_instr
*instr
)
1868 if (ctx
->ac
.postponed_kill
) {
1869 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
1870 ctx
->ac
.postponed_kill
, "");
1871 ac_build_ifcc(&ctx
->ac
, cond
, 7001);
1874 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1876 char name
[64], type
[8];
1877 LLVMValueRef params
[6], descriptor
;
1878 LLVMValueRef result
;
1881 struct waterfall_context wctx
;
1882 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1884 switch (instr
->intrinsic
) {
1885 case nir_intrinsic_ssbo_atomic_add
:
1888 case nir_intrinsic_ssbo_atomic_imin
:
1891 case nir_intrinsic_ssbo_atomic_umin
:
1894 case nir_intrinsic_ssbo_atomic_imax
:
1897 case nir_intrinsic_ssbo_atomic_umax
:
1900 case nir_intrinsic_ssbo_atomic_and
:
1903 case nir_intrinsic_ssbo_atomic_or
:
1906 case nir_intrinsic_ssbo_atomic_xor
:
1909 case nir_intrinsic_ssbo_atomic_exchange
:
1912 case nir_intrinsic_ssbo_atomic_comp_swap
:
1919 descriptor
= ctx
->abi
->load_ssbo(ctx
->abi
,
1923 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
&&
1924 return_type
== ctx
->ac
.i64
) {
1925 result
= emit_ssbo_comp_swap_64(ctx
, descriptor
,
1926 get_src(ctx
, instr
->src
[1]),
1927 get_src(ctx
, instr
->src
[2]),
1928 get_src(ctx
, instr
->src
[3]));
1930 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1931 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1933 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1934 params
[arg_count
++] = descriptor
;
1936 if (LLVM_VERSION_MAJOR
>= 9) {
1937 /* XXX: The new raw/struct atomic intrinsics are buggy with
1938 * LLVM 8, see r358579.
1940 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1941 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1942 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1944 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1945 snprintf(name
, sizeof(name
),
1946 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1948 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1949 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1950 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1952 assert(return_type
== ctx
->ac
.i32
);
1953 snprintf(name
, sizeof(name
),
1954 "llvm.amdgcn.buffer.atomic.%s", op
);
1957 result
= ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1961 result
= exit_waterfall(ctx
, &wctx
, result
);
1962 if (ctx
->ac
.postponed_kill
)
1963 ac_build_endif(&ctx
->ac
, 7001);
1967 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1968 nir_intrinsic_instr
*instr
)
1970 struct waterfall_context wctx
;
1971 LLVMValueRef rsrc_base
= enter_waterfall_ssbo(ctx
, &wctx
, instr
, instr
->src
[0]);
1973 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1974 int num_components
= instr
->num_components
;
1975 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1976 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1978 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1979 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
, rsrc_base
, false);
1980 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1982 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1983 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1985 LLVMValueRef results
[4];
1986 for (int i
= 0; i
< num_components
;) {
1987 int num_elems
= num_components
- i
;
1988 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1990 if (num_elems
* elem_size_bytes
> 16)
1991 num_elems
= 16 / elem_size_bytes
;
1992 int load_bytes
= num_elems
* elem_size_bytes
;
1994 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1998 if (load_bytes
== 1) {
1999 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
2005 } else if (load_bytes
== 2) {
2006 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
2013 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
2014 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2016 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
2017 vindex
, offset
, immoffset
, 0,
2018 cache_policy
, can_speculate
, false);
2021 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
2022 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
2023 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
2025 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
2026 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
2028 for (unsigned j
= 0; j
< num_elems
; j
++) {
2029 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
2034 LLVMValueRef ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2035 return exit_waterfall(ctx
, &wctx
, ret
);
2038 static LLVMValueRef
enter_waterfall_ubo(struct ac_nir_context
*ctx
,
2039 struct waterfall_context
*wctx
,
2040 const nir_intrinsic_instr
*instr
)
2042 return enter_waterfall(ctx
, wctx
, get_src(ctx
, instr
->src
[0]),
2043 nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2046 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
2047 nir_intrinsic_instr
*instr
)
2049 struct waterfall_context wctx
;
2050 LLVMValueRef rsrc_base
= enter_waterfall_ubo(ctx
, &wctx
, instr
);
2053 LLVMValueRef rsrc
= rsrc_base
;
2054 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
2055 int num_components
= instr
->num_components
;
2057 if (ctx
->abi
->load_ubo
)
2058 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
2060 if (instr
->dest
.ssa
.bit_size
== 64)
2061 num_components
*= 2;
2063 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
2064 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
2065 LLVMValueRef results
[num_components
];
2066 for (unsigned i
= 0; i
< num_components
; ++i
) {
2067 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
2070 if (load_bytes
== 1) {
2071 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
2078 assert(load_bytes
== 2);
2079 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
2087 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
2089 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
2090 NULL
, 0, 0, true, true);
2092 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
2095 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
2096 get_def_type(ctx
, &instr
->dest
.ssa
), "");
2098 return exit_waterfall(ctx
, &wctx
, ret
);
2102 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
2103 bool vs_in
, unsigned *vertex_index_out
,
2104 LLVMValueRef
*vertex_index_ref
,
2105 unsigned *const_out
, LLVMValueRef
*indir_out
)
2107 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
2108 nir_deref_path path
;
2109 unsigned idx_lvl
= 1;
2111 nir_deref_path_init(&path
, instr
, NULL
);
2113 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
2114 if (vertex_index_ref
) {
2115 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
2116 if (vertex_index_out
)
2117 *vertex_index_out
= 0;
2119 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2124 uint32_t const_offset
= 0;
2125 LLVMValueRef offset
= NULL
;
2127 if (var
->data
.compact
) {
2128 assert(instr
->deref_type
== nir_deref_type_array
);
2129 const_offset
= nir_src_as_uint(instr
->arr
.index
);
2133 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
2134 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
2135 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
2136 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
2138 for (unsigned i
= 0; i
< index
; i
++) {
2139 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
2140 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
2142 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
2143 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
2144 if (nir_src_is_const(path
.path
[idx_lvl
]->arr
.index
)) {
2145 const_offset
+= size
*
2146 nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
2148 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
2149 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
2151 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
2156 unreachable("Uhandled deref type in get_deref_instr_offset");
2160 nir_deref_path_finish(&path
);
2162 if (const_offset
&& offset
)
2163 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
2164 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
2167 *const_out
= const_offset
;
2168 *indir_out
= offset
;
2171 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
2172 nir_intrinsic_instr
*instr
,
2175 LLVMValueRef result
;
2176 LLVMValueRef vertex_index
= NULL
;
2177 LLVMValueRef indir_index
= NULL
;
2178 unsigned const_index
= 0;
2180 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
2182 unsigned location
= var
->data
.location
;
2183 unsigned driver_location
= var
->data
.driver_location
;
2184 const bool is_patch
= var
->data
.patch
||
2185 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2186 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2187 const bool is_compact
= var
->data
.compact
;
2189 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2190 false, NULL
, is_patch
? NULL
: &vertex_index
,
2191 &const_index
, &indir_index
);
2193 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2195 LLVMTypeRef src_component_type
;
2196 if (LLVMGetTypeKind(dest_type
) == LLVMFixedVectorTypeKind
)
2197 src_component_type
= LLVMGetElementType(dest_type
);
2199 src_component_type
= dest_type
;
2201 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
2202 vertex_index
, indir_index
,
2203 const_index
, location
, driver_location
,
2204 var
->data
.location_frac
,
2205 instr
->num_components
,
2206 is_patch
, is_compact
, load_inputs
);
2207 if (instr
->dest
.ssa
.bit_size
== 16) {
2208 result
= ac_to_integer(&ctx
->ac
, result
);
2209 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
2211 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
2215 type_scalar_size_bytes(const struct glsl_type
*type
)
2217 assert(glsl_type_is_vector_or_scalar(type
) ||
2218 glsl_type_is_matrix(type
));
2219 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
2222 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
2223 nir_intrinsic_instr
*instr
)
2225 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2226 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2228 LLVMValueRef values
[8];
2230 int ve
= instr
->dest
.ssa
.num_components
;
2232 LLVMValueRef indir_index
;
2234 unsigned const_index
;
2235 unsigned stride
= 4;
2236 int mode
= deref
->mode
;
2239 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
2240 var
->data
.mode
== nir_var_shader_in
;
2241 idx
= var
->data
.driver_location
;
2242 comp
= var
->data
.location_frac
;
2243 mode
= var
->data
.mode
;
2245 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2246 &const_index
, &indir_index
);
2248 if (var
->data
.compact
) {
2250 const_index
+= comp
;
2255 if (instr
->dest
.ssa
.bit_size
== 64 &&
2256 (deref
->mode
== nir_var_shader_in
||
2257 deref
->mode
== nir_var_shader_out
||
2258 deref
->mode
== nir_var_function_temp
))
2262 case nir_var_shader_in
:
2263 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2264 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2265 return load_tess_varyings(ctx
, instr
, true);
2268 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2269 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2270 LLVMValueRef indir_index
;
2271 unsigned const_index
, vertex_index
;
2272 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2273 &const_index
, &indir_index
);
2274 assert(indir_index
== NULL
);
2276 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2277 var
->data
.driver_location
,
2278 var
->data
.location_frac
,
2279 instr
->num_components
, vertex_index
, const_index
, type
);
2282 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2284 unsigned count
= glsl_count_attribute_slots(
2286 ctx
->stage
== MESA_SHADER_VERTEX
);
2288 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2289 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2290 stride
, false, true);
2292 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2296 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2299 case nir_var_function_temp
:
2300 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2302 unsigned count
= glsl_count_attribute_slots(
2305 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2306 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2307 stride
, true, true);
2309 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2313 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2317 case nir_var_shader_out
:
2318 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2319 return load_tess_varyings(ctx
, instr
, false);
2322 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2323 var
->data
.fb_fetch_output
&&
2324 ctx
->abi
->emit_fbfetch
)
2325 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2327 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2329 unsigned count
= glsl_count_attribute_slots(
2332 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2333 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2334 stride
, true, true);
2336 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2340 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2341 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2346 case nir_var_mem_global
: {
2347 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2348 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2349 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2350 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2351 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2352 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, result_type
) / 8;
2353 bool split_loads
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2355 if (stride
!= natural_stride
|| split_loads
) {
2356 if (LLVMGetTypeKind(result_type
) == LLVMFixedVectorTypeKind
)
2357 result_type
= LLVMGetElementType(result_type
);
2359 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2360 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2361 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2363 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2364 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2365 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2366 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2368 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2370 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2371 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2372 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2373 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2378 unreachable("unhandle variable mode");
2380 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2381 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2385 visit_store_var(struct ac_nir_context
*ctx
,
2386 nir_intrinsic_instr
*instr
)
2388 if (ctx
->ac
.postponed_kill
) {
2389 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2390 ctx
->ac
.postponed_kill
, "");
2391 ac_build_ifcc(&ctx
->ac
, cond
, 7002);
2394 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2395 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2397 LLVMValueRef temp_ptr
, value
;
2400 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2401 int writemask
= instr
->const_index
[0];
2402 LLVMValueRef indir_index
;
2403 unsigned const_index
;
2406 get_deref_offset(ctx
, deref
, false,
2407 NULL
, NULL
, &const_index
, &indir_index
);
2408 idx
= var
->data
.driver_location
;
2409 comp
= var
->data
.location_frac
;
2411 if (var
->data
.compact
) {
2412 const_index
+= comp
;
2417 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2418 (deref
->mode
== nir_var_shader_out
||
2419 deref
->mode
== nir_var_function_temp
)) {
2421 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2422 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2425 writemask
= widen_mask(writemask
, 2);
2428 writemask
= writemask
<< comp
;
2430 switch (deref
->mode
) {
2431 case nir_var_shader_out
:
2433 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2434 LLVMValueRef vertex_index
= NULL
;
2435 LLVMValueRef indir_index
= NULL
;
2436 unsigned const_index
= 0;
2437 const bool is_patch
= var
->data
.patch
||
2438 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
2439 var
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
;
2441 get_deref_offset(ctx
, deref
, false, NULL
,
2442 is_patch
? NULL
: &vertex_index
,
2443 &const_index
, &indir_index
);
2445 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2446 vertex_index
, indir_index
,
2447 const_index
, src
, writemask
);
2451 for (unsigned chan
= 0; chan
< 8; chan
++) {
2453 if (!(writemask
& (1 << chan
)))
2456 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2458 if (var
->data
.compact
)
2461 unsigned count
= glsl_count_attribute_slots(
2464 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2465 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2466 stride
, true, true);
2468 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2469 value
, indir_index
, "");
2470 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2471 count
, stride
, tmp_vec
);
2474 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2476 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2480 case nir_var_function_temp
:
2481 for (unsigned chan
= 0; chan
< 8; chan
++) {
2482 if (!(writemask
& (1 << chan
)))
2485 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2487 unsigned count
= glsl_count_attribute_slots(
2490 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2491 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2494 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2495 value
, indir_index
, "");
2496 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2499 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2501 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2506 case nir_var_mem_global
: {
2507 int writemask
= instr
->const_index
[0];
2508 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2509 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2511 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2512 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2513 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2514 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(val
)) / 8;
2515 bool split_stores
= ctx
->ac
.chip_class
== GFX6
&& elem_size_bytes
< 4;
2517 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2518 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2519 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2521 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2522 stride
== natural_stride
&& !split_stores
) {
2523 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2524 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2525 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2527 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2528 LLVMGetElementType(LLVMTypeOf(address
)), "");
2529 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2531 LLVMTypeRef val_type
= LLVMTypeOf(val
);
2532 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMFixedVectorTypeKind
)
2533 val_type
= LLVMGetElementType(val_type
);
2535 LLVMTypeRef ptr_type
= LLVMPointerType(val_type
,
2536 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2537 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2538 for (unsigned chan
= 0; chan
< 4; chan
++) {
2539 if (!(writemask
& (1 << chan
)))
2542 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2544 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2545 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2547 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2548 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2549 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2559 if (ctx
->ac
.postponed_kill
)
2560 ac_build_endif(&ctx
->ac
, 7002);
2563 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2566 case GLSL_SAMPLER_DIM_BUF
:
2568 case GLSL_SAMPLER_DIM_1D
:
2569 return array
? 2 : 1;
2570 case GLSL_SAMPLER_DIM_2D
:
2571 return array
? 3 : 2;
2572 case GLSL_SAMPLER_DIM_MS
:
2573 return array
? 4 : 3;
2574 case GLSL_SAMPLER_DIM_3D
:
2575 case GLSL_SAMPLER_DIM_CUBE
:
2577 case GLSL_SAMPLER_DIM_RECT
:
2578 case GLSL_SAMPLER_DIM_SUBPASS
:
2580 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2588 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2589 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2590 LLVMValueRef coord_z
,
2591 LLVMValueRef sample_index
,
2592 LLVMValueRef fmask_desc_ptr
)
2594 unsigned sample_chan
= coord_z
? 3 : 2;
2595 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2596 addr
[sample_chan
] = sample_index
;
2598 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2599 return addr
[sample_chan
];
2602 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2604 assert(instr
->src
[0].is_ssa
);
2605 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2608 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2609 const nir_intrinsic_instr
*instr
,
2610 LLVMValueRef dynamic_index
,
2611 enum ac_descriptor_type desc_type
,
2614 nir_deref_instr
*deref_instr
=
2615 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2616 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2618 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, dynamic_index
, true, write
);
2621 static void get_image_coords(struct ac_nir_context
*ctx
,
2622 const nir_intrinsic_instr
*instr
,
2623 LLVMValueRef dynamic_desc_index
,
2624 struct ac_image_args
*args
,
2625 enum glsl_sampler_dim dim
,
2628 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2629 LLVMValueRef masks
[] = {
2630 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2631 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2633 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2636 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2637 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2638 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2639 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2640 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2641 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2642 count
= image_type_to_components_count(dim
, is_array
);
2644 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2645 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2646 LLVMValueRef fmask_load_address
[3];
2648 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2649 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2651 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2653 fmask_load_address
[2] = NULL
;
2655 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2656 fmask_load_address
[0],
2657 fmask_load_address
[1],
2658 fmask_load_address
[2],
2660 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2661 AC_DESC_FMASK
, &instr
->instr
, dynamic_desc_index
, true, false));
2663 if (count
== 1 && !gfx9_1d
) {
2664 if (instr
->src
[1].ssa
->num_components
)
2665 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2667 args
->coords
[0] = src0
;
2672 for (chan
= 0; chan
< count
; ++chan
) {
2673 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2678 args
->coords
[2] = args
->coords
[1];
2679 args
->coords
[1] = ctx
->ac
.i32_0
;
2681 args
->coords
[1] = ctx
->ac
.i32_0
;
2684 if (ctx
->ac
.chip_class
== GFX9
&&
2685 dim
== GLSL_SAMPLER_DIM_2D
&&
2687 /* The hw can't bind a slice of a 3D image as a 2D
2688 * image, because it ignores BASE_ARRAY if the target
2689 * is 3D. The workaround is to read BASE_ARRAY and set
2690 * it as the 3rd address operand for all 2D images.
2692 LLVMValueRef first_layer
, const5
, mask
;
2694 const5
= LLVMConstInt(ctx
->ac
.i32
, 5, 0);
2695 mask
= LLVMConstInt(ctx
->ac
.i32
, S_008F24_BASE_ARRAY(~0), 0);
2696 first_layer
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
->resource
, const5
, "");
2697 first_layer
= LLVMBuildAnd(ctx
->ac
.builder
, first_layer
, mask
, "");
2699 args
->coords
[count
] = first_layer
;
2705 args
->coords
[count
] = sample_index
;
2711 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2712 const nir_intrinsic_instr
*instr
,
2713 LLVMValueRef dynamic_index
,
2714 bool write
, bool atomic
)
2716 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, write
);
2717 if (ctx
->ac
.chip_class
== GFX9
&& LLVM_VERSION_MAJOR
< 9 && atomic
) {
2718 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2719 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2720 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2722 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2723 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2724 elem_count
, stride
, "");
2726 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2727 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2732 static LLVMValueRef
enter_waterfall_image(struct ac_nir_context
*ctx
,
2733 struct waterfall_context
*wctx
,
2734 const nir_intrinsic_instr
*instr
)
2736 nir_deref_instr
*deref_instr
= NULL
;
2738 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
)
2739 deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2741 LLVMValueRef value
= get_sampler_desc_index(ctx
, deref_instr
, &instr
->instr
, true);
2742 return enter_waterfall(ctx
, wctx
, value
, nir_intrinsic_access(instr
) & ACCESS_NON_UNIFORM
);
2745 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2746 const nir_intrinsic_instr
*instr
,
2751 enum glsl_sampler_dim dim
;
2752 enum gl_access_qualifier access
;
2755 dim
= nir_intrinsic_image_dim(instr
);
2756 access
= nir_intrinsic_access(instr
);
2757 is_array
= nir_intrinsic_image_array(instr
);
2759 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2760 const struct glsl_type
*type
= image_deref
->type
;
2761 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2762 dim
= glsl_get_sampler_dim(type
);
2763 access
= var
->data
.access
;
2764 is_array
= glsl_sampler_type_is_array(type
);
2767 struct waterfall_context wctx
;
2768 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2770 struct ac_image_args args
= {};
2772 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2774 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2775 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2776 unsigned num_channels
= util_last_bit(mask
);
2777 LLVMValueRef rsrc
, vindex
;
2779 rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, false, false);
2780 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2783 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2784 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2785 ctx
->ac
.i32_0
, num_channels
,
2788 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2790 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2791 res
= ac_to_integer(&ctx
->ac
, res
);
2793 bool level_zero
= nir_src_is_const(instr
->src
[3]) && nir_src_as_uint(instr
->src
[3]) == 0;
2795 args
.opcode
= level_zero
? ac_image_load
: ac_image_load_mip
;
2796 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
2797 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2798 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2800 args
.lod
= get_src(ctx
, instr
->src
[3]);
2802 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2804 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2806 return exit_waterfall(ctx
, &wctx
, res
);
2809 static void visit_image_store(struct ac_nir_context
*ctx
,
2810 const nir_intrinsic_instr
*instr
,
2813 if (ctx
->ac
.postponed_kill
) {
2814 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2815 ctx
->ac
.postponed_kill
, "");
2816 ac_build_ifcc(&ctx
->ac
, cond
, 7003);
2819 enum glsl_sampler_dim dim
;
2820 enum gl_access_qualifier access
;
2824 dim
= nir_intrinsic_image_dim(instr
);
2825 access
= nir_intrinsic_access(instr
);
2826 is_array
= nir_intrinsic_image_array(instr
);
2828 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2829 const struct glsl_type
*type
= image_deref
->type
;
2830 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2831 dim
= glsl_get_sampler_dim(type
);
2832 access
= var
->data
.access
;
2833 is_array
= glsl_sampler_type_is_array(type
);
2836 struct waterfall_context wctx
;
2837 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2839 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2840 struct ac_image_args args
= {};
2842 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2844 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2845 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, false);
2846 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2847 unsigned src_channels
= ac_get_llvm_num_components(src
);
2848 LLVMValueRef vindex
;
2850 if (src_channels
== 3)
2851 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2853 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2854 get_src(ctx
, instr
->src
[1]),
2857 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2858 ctx
->ac
.i32_0
, src_channels
,
2861 bool level_zero
= nir_src_is_const(instr
->src
[4]) && nir_src_as_uint(instr
->src
[4]) == 0;
2863 args
.opcode
= level_zero
? ac_image_store
: ac_image_store_mip
;
2864 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2865 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
2866 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
2867 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
2869 args
.lod
= get_src(ctx
, instr
->src
[4]);
2872 ac_build_image_opcode(&ctx
->ac
, &args
);
2875 exit_waterfall(ctx
, &wctx
, NULL
);
2876 if (ctx
->ac
.postponed_kill
)
2877 ac_build_endif(&ctx
->ac
, 7003);
2880 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2881 const nir_intrinsic_instr
*instr
,
2884 if (ctx
->ac
.postponed_kill
) {
2885 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
2886 ctx
->ac
.postponed_kill
, "");
2887 ac_build_ifcc(&ctx
->ac
, cond
, 7004);
2890 LLVMValueRef params
[7];
2891 int param_count
= 0;
2893 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2894 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2895 const char *atomic_name
;
2896 char intrinsic_name
[64];
2897 enum ac_atomic_op atomic_subop
;
2898 ASSERTED
int length
;
2900 enum glsl_sampler_dim dim
;
2903 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imin
||
2904 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umin
||
2905 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_imax
||
2906 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_umax
) {
2907 ASSERTED
const GLenum format
= nir_intrinsic_format(instr
);
2908 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2910 dim
= nir_intrinsic_image_dim(instr
);
2911 is_array
= nir_intrinsic_image_array(instr
);
2913 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2914 dim
= glsl_get_sampler_dim(type
);
2915 is_array
= glsl_sampler_type_is_array(type
);
2918 struct waterfall_context wctx
;
2919 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
2921 switch (instr
->intrinsic
) {
2922 case nir_intrinsic_bindless_image_atomic_add
:
2923 case nir_intrinsic_image_deref_atomic_add
:
2924 atomic_name
= "add";
2925 atomic_subop
= ac_atomic_add
;
2927 case nir_intrinsic_bindless_image_atomic_imin
:
2928 case nir_intrinsic_image_deref_atomic_imin
:
2929 atomic_name
= "smin";
2930 atomic_subop
= ac_atomic_smin
;
2932 case nir_intrinsic_bindless_image_atomic_umin
:
2933 case nir_intrinsic_image_deref_atomic_umin
:
2934 atomic_name
= "umin";
2935 atomic_subop
= ac_atomic_umin
;
2937 case nir_intrinsic_bindless_image_atomic_imax
:
2938 case nir_intrinsic_image_deref_atomic_imax
:
2939 atomic_name
= "smax";
2940 atomic_subop
= ac_atomic_smax
;
2942 case nir_intrinsic_bindless_image_atomic_umax
:
2943 case nir_intrinsic_image_deref_atomic_umax
:
2944 atomic_name
= "umax";
2945 atomic_subop
= ac_atomic_umax
;
2947 case nir_intrinsic_bindless_image_atomic_and
:
2948 case nir_intrinsic_image_deref_atomic_and
:
2949 atomic_name
= "and";
2950 atomic_subop
= ac_atomic_and
;
2952 case nir_intrinsic_bindless_image_atomic_or
:
2953 case nir_intrinsic_image_deref_atomic_or
:
2955 atomic_subop
= ac_atomic_or
;
2957 case nir_intrinsic_bindless_image_atomic_xor
:
2958 case nir_intrinsic_image_deref_atomic_xor
:
2959 atomic_name
= "xor";
2960 atomic_subop
= ac_atomic_xor
;
2962 case nir_intrinsic_bindless_image_atomic_exchange
:
2963 case nir_intrinsic_image_deref_atomic_exchange
:
2964 atomic_name
= "swap";
2965 atomic_subop
= ac_atomic_swap
;
2967 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2968 case nir_intrinsic_image_deref_atomic_comp_swap
:
2969 atomic_name
= "cmpswap";
2970 atomic_subop
= 0; /* not used */
2972 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
2973 case nir_intrinsic_image_deref_atomic_inc_wrap
: {
2974 atomic_name
= "inc";
2975 atomic_subop
= ac_atomic_inc_wrap
;
2976 /* ATOMIC_INC instruction does:
2977 * value = (value + 1) % (data + 1)
2979 * value = (value + 1) % data
2980 * So replace 'data' by 'data - 1'.
2982 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
] =
2983 LLVMBuildSub(ctx
->ac
.builder
,
2984 ctx
->ssa_defs
[instr
->src
[3].ssa
->index
],
2988 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
2989 case nir_intrinsic_image_deref_atomic_dec_wrap
:
2990 atomic_name
= "dec";
2991 atomic_subop
= ac_atomic_dec_wrap
;
2998 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2999 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
3001 LLVMValueRef result
;
3002 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3003 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, dynamic_index
, true, true);
3004 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
3005 ctx
->ac
.i32_0
, ""); /* vindex */
3006 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
3007 if (LLVM_VERSION_MAJOR
>= 9) {
3008 /* XXX: The new raw/struct atomic intrinsics are buggy
3009 * with LLVM 8, see r358579.
3011 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
3012 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
3014 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3015 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
3017 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
3019 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3020 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
3023 assert(length
< sizeof(intrinsic_name
));
3024 result
= ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
3025 params
, param_count
, 0);
3027 struct ac_image_args args
= {};
3028 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
3029 args
.atomic
= atomic_subop
;
3030 args
.data
[0] = params
[0];
3032 args
.data
[1] = params
[1];
3033 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, true);
3034 get_image_coords(ctx
, instr
, dynamic_index
, &args
, dim
, is_array
);
3035 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3037 result
= ac_build_image_opcode(&ctx
->ac
, &args
);
3040 result
= exit_waterfall(ctx
, &wctx
, result
);
3041 if (ctx
->ac
.postponed_kill
)
3042 ac_build_endif(&ctx
->ac
, 7004);
3046 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
3047 nir_intrinsic_instr
*instr
)
3049 struct waterfall_context wctx
;
3050 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3051 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3053 LLVMValueRef ret
= ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
3055 return exit_waterfall(ctx
, &wctx
, ret
);
3058 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
3059 const nir_intrinsic_instr
*instr
,
3064 enum glsl_sampler_dim dim
;
3067 dim
= nir_intrinsic_image_dim(instr
);
3068 is_array
= nir_intrinsic_image_array(instr
);
3070 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
3071 dim
= glsl_get_sampler_dim(type
);
3072 is_array
= glsl_sampler_type_is_array(type
);
3075 struct waterfall_context wctx
;
3076 LLVMValueRef dynamic_index
= enter_waterfall_image(ctx
, &wctx
, instr
);
3078 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
3079 res
= get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_BUFFER
, false), true);
3082 struct ac_image_args args
= { 0 };
3084 args
.dim
= ac_get_image_dim(ctx
->ac
.chip_class
, dim
, is_array
);
3086 args
.resource
= get_image_descriptor(ctx
, instr
, dynamic_index
, AC_DESC_IMAGE
, false);
3087 args
.opcode
= ac_image_get_resinfo
;
3088 args
.lod
= ctx
->ac
.i32_0
;
3089 args
.attributes
= AC_FUNC_ATTR_READNONE
;
3091 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
3093 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3095 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
3096 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3097 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3098 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3099 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
3102 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
3103 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
3104 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
3108 return exit_waterfall(ctx
, &wctx
, res
);
3111 static void emit_membar(struct ac_llvm_context
*ac
,
3112 const nir_intrinsic_instr
*instr
)
3114 unsigned wait_flags
= 0;
3116 switch (instr
->intrinsic
) {
3117 case nir_intrinsic_memory_barrier
:
3118 case nir_intrinsic_group_memory_barrier
:
3119 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3121 case nir_intrinsic_memory_barrier_buffer
:
3122 case nir_intrinsic_memory_barrier_image
:
3123 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
3125 case nir_intrinsic_memory_barrier_shared
:
3126 wait_flags
= AC_WAIT_LGKM
;
3132 ac_build_waitcnt(ac
, wait_flags
);
3135 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
3137 /* GFX6 only (thanks to a hw bug workaround):
3138 * The real barrier instruction isn’t needed, because an entire patch
3139 * always fits into a single wave.
3141 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
3142 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
3145 ac_build_s_barrier(ac
);
3148 static void emit_discard(struct ac_nir_context
*ctx
,
3149 const nir_intrinsic_instr
*instr
)
3153 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3154 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3155 get_src(ctx
, instr
->src
[0]),
3158 assert(instr
->intrinsic
== nir_intrinsic_discard
);
3159 cond
= ctx
->ac
.i1false
;
3162 ac_build_kill_if_false(&ctx
->ac
, cond
);
3165 static void emit_demote(struct ac_nir_context
*ctx
,
3166 const nir_intrinsic_instr
*instr
)
3170 if (instr
->intrinsic
== nir_intrinsic_demote_if
) {
3171 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3172 get_src(ctx
, instr
->src
[0]),
3175 assert(instr
->intrinsic
== nir_intrinsic_demote
);
3176 cond
= ctx
->ac
.i1false
;
3179 /* Kill immediately while maintaining WQM. */
3180 ac_build_kill_if_false(&ctx
->ac
, ac_build_wqm_vote(&ctx
->ac
, cond
));
3182 LLVMValueRef mask
= LLVMBuildLoad(ctx
->ac
.builder
, ctx
->ac
.postponed_kill
, "");
3183 mask
= LLVMBuildAnd(ctx
->ac
.builder
, mask
, cond
, "");
3184 LLVMBuildStore(ctx
->ac
.builder
, mask
, ctx
->ac
.postponed_kill
);
3189 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
3191 LLVMValueRef result
;
3192 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
3193 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3194 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3195 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3197 if (ctx
->ac
.wave_size
== 32)
3198 result
= LLVMBuildLShr(ctx
->ac
.builder
, result
,
3199 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
3201 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
3205 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
3207 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3208 LLVMValueRef result
;
3209 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3210 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3211 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
3212 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
3214 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
3219 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
3221 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
3222 return LLVMBuildAnd(ctx
->ac
.builder
,
3223 ac_get_arg(&ctx
->ac
, ctx
->args
->tg_size
),
3224 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3226 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
3231 visit_first_invocation(struct ac_nir_context
*ctx
)
3233 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
3234 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
3236 /* The second argument is whether cttz(0) should be defined, but we do not care. */
3237 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
3238 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
3239 ctx
->ac
.iN_wavemask
, args
, 2,
3240 AC_FUNC_ATTR_NOUNWIND
|
3241 AC_FUNC_ATTR_READNONE
);
3243 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
3247 visit_load_shared(struct ac_nir_context
*ctx
,
3248 const nir_intrinsic_instr
*instr
)
3250 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
3252 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3253 instr
->dest
.ssa
.bit_size
);
3255 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
3256 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3257 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
3258 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
3261 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
3262 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
3266 visit_store_shared(struct ac_nir_context
*ctx
,
3267 const nir_intrinsic_instr
*instr
)
3269 LLVMValueRef derived_ptr
, data
,index
;
3270 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3272 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1],
3273 instr
->src
[0].ssa
->bit_size
);
3274 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3276 int writemask
= nir_intrinsic_write_mask(instr
);
3277 for (int chan
= 0; chan
< 4; chan
++) {
3278 if (!(writemask
& (1 << chan
))) {
3281 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
3282 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
3283 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3284 LLVMBuildStore(builder
, data
, derived_ptr
);
3288 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
3289 const nir_intrinsic_instr
*instr
,
3290 LLVMValueRef ptr
, int src_idx
)
3292 if (ctx
->ac
.postponed_kill
) {
3293 LLVMValueRef cond
= LLVMBuildLoad(ctx
->ac
.builder
,
3294 ctx
->ac
.postponed_kill
, "");
3295 ac_build_ifcc(&ctx
->ac
, cond
, 7005);
3298 LLVMValueRef result
;
3299 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
3301 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
3303 if (instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
) {
3304 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
3305 if (deref
->mode
== nir_var_mem_global
) {
3306 /* use "singlethread" sync scope to implement relaxed ordering */
3307 sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "singlethread-one-as" : "singlethread";
3309 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(src
), LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
)));
3310 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ptr_type
, "");
3314 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
3315 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
3316 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
3317 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
3318 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
3320 LLVMAtomicRMWBinOp op
;
3321 switch (instr
->intrinsic
) {
3322 case nir_intrinsic_shared_atomic_add
:
3323 case nir_intrinsic_deref_atomic_add
:
3324 op
= LLVMAtomicRMWBinOpAdd
;
3326 case nir_intrinsic_shared_atomic_umin
:
3327 case nir_intrinsic_deref_atomic_umin
:
3328 op
= LLVMAtomicRMWBinOpUMin
;
3330 case nir_intrinsic_shared_atomic_umax
:
3331 case nir_intrinsic_deref_atomic_umax
:
3332 op
= LLVMAtomicRMWBinOpUMax
;
3334 case nir_intrinsic_shared_atomic_imin
:
3335 case nir_intrinsic_deref_atomic_imin
:
3336 op
= LLVMAtomicRMWBinOpMin
;
3338 case nir_intrinsic_shared_atomic_imax
:
3339 case nir_intrinsic_deref_atomic_imax
:
3340 op
= LLVMAtomicRMWBinOpMax
;
3342 case nir_intrinsic_shared_atomic_and
:
3343 case nir_intrinsic_deref_atomic_and
:
3344 op
= LLVMAtomicRMWBinOpAnd
;
3346 case nir_intrinsic_shared_atomic_or
:
3347 case nir_intrinsic_deref_atomic_or
:
3348 op
= LLVMAtomicRMWBinOpOr
;
3350 case nir_intrinsic_shared_atomic_xor
:
3351 case nir_intrinsic_deref_atomic_xor
:
3352 op
= LLVMAtomicRMWBinOpXor
;
3354 case nir_intrinsic_shared_atomic_exchange
:
3355 case nir_intrinsic_deref_atomic_exchange
:
3356 op
= LLVMAtomicRMWBinOpXchg
;
3362 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
3365 if (ctx
->ac
.postponed_kill
)
3366 ac_build_endif(&ctx
->ac
, 7005);
3370 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
3372 LLVMValueRef values
[2];
3373 LLVMValueRef pos
[2];
3375 pos
[0] = ac_to_float(&ctx
->ac
,
3376 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]));
3377 pos
[1] = ac_to_float(&ctx
->ac
,
3378 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]));
3380 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
3381 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
3382 return ac_build_gather_values(&ctx
->ac
, values
, 2);
3385 static LLVMValueRef
lookup_interp_param(struct ac_nir_context
*ctx
,
3386 enum glsl_interp_mode interp
, unsigned location
)
3389 case INTERP_MODE_FLAT
:
3392 case INTERP_MODE_SMOOTH
:
3393 case INTERP_MODE_NONE
:
3394 if (location
== INTERP_CENTER
)
3395 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_center
);
3396 else if (location
== INTERP_CENTROID
)
3397 return ctx
->abi
->persp_centroid
;
3398 else if (location
== INTERP_SAMPLE
)
3399 return ac_get_arg(&ctx
->ac
, ctx
->args
->persp_sample
);
3401 case INTERP_MODE_NOPERSPECTIVE
:
3402 if (location
== INTERP_CENTER
)
3403 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_center
);
3404 else if (location
== INTERP_CENTROID
)
3405 return ctx
->abi
->linear_centroid
;
3406 else if (location
== INTERP_SAMPLE
)
3407 return ac_get_arg(&ctx
->ac
, ctx
->args
->linear_sample
);
3413 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
3416 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3417 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3420 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
3422 LLVMValueRef offset
)
3424 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTER
);
3425 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
3426 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
3428 LLVMValueRef ij_out
[2];
3429 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3432 * take the I then J parameters, and the DDX/Y for it, and
3433 * calculate the IJ inputs for the interpolator.
3434 * temp1 = ddx * offset/sample.x + I;
3435 * interp_param.I = ddy * offset/sample.y + temp1;
3436 * temp1 = ddx * offset/sample.x + J;
3437 * interp_param.J = ddy * offset/sample.y + temp1;
3439 for (unsigned i
= 0; i
< 2; i
++) {
3440 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3441 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3442 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3443 ddxy_out
, ix_ll
, "");
3444 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3445 ddxy_out
, iy_ll
, "");
3446 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3447 interp_param
, ix_ll
, "");
3448 LLVMValueRef temp1
, temp2
;
3450 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3453 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3454 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3456 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3457 temp2
, ctx
->ac
.i32
, "");
3459 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3460 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3463 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3466 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_CENTROID
);
3467 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3470 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3472 LLVMValueRef sample_id
)
3474 if (ctx
->abi
->interp_at_sample_force_center
)
3475 return barycentric_center(ctx
, mode
);
3477 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3479 /* fetch sample ID */
3480 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3482 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3483 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3484 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3485 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3486 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3487 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3489 return barycentric_offset(ctx
, mode
, offset
);
3493 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3496 LLVMValueRef interp_param
= lookup_interp_param(ctx
, mode
, INTERP_SAMPLE
);
3497 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3500 static LLVMValueRef
barycentric_model(struct ac_nir_context
*ctx
)
3502 return LLVMBuildBitCast(ctx
->ac
.builder
,
3503 ac_get_arg(&ctx
->ac
, ctx
->args
->pull_model
),
3507 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3508 LLVMValueRef interp_param
,
3509 unsigned index
, unsigned comp_start
,
3510 unsigned num_components
,
3513 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3515 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3516 interp_param
, ctx
->ac
.v2f32
, "");
3517 LLVMValueRef i
= LLVMBuildExtractElement(
3518 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3519 LLVMValueRef j
= LLVMBuildExtractElement(
3520 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3522 LLVMValueRef values
[4];
3523 assert(bitsize
== 16 || bitsize
== 32);
3524 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3525 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3526 if (bitsize
== 16) {
3527 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3528 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3530 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3531 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
), i
, j
);
3535 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3538 static LLVMValueRef
load_input(struct ac_nir_context
*ctx
,
3539 nir_intrinsic_instr
*instr
)
3541 unsigned offset_idx
= instr
->intrinsic
== nir_intrinsic_load_input
? 0 : 1;
3543 /* We only lower inputs for fragment shaders ATM */
3544 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[offset_idx
]);
3546 assert(offset
[0].i32
== 0);
3548 unsigned component
= nir_intrinsic_component(instr
);
3549 unsigned index
= nir_intrinsic_base(instr
);
3550 unsigned vertex_id
= 2; /* P0 */
3552 if (instr
->intrinsic
== nir_intrinsic_load_input_vertex
) {
3553 nir_const_value
*src0
= nir_src_as_const_value(instr
->src
[0]);
3555 switch (src0
[0].i32
) {
3566 unreachable("Invalid vertex index");
3570 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3571 LLVMValueRef values
[8];
3573 /* Each component of a 64-bit value takes up two GL-level channels. */
3574 unsigned num_components
= instr
->dest
.ssa
.num_components
;
3575 unsigned bit_size
= instr
->dest
.ssa
.bit_size
;
3577 bit_size
== 64 ? num_components
* 2 : num_components
;
3579 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3580 if (component
+ chan
> 4)
3581 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3582 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (component
+ chan
) % 4, false);
3583 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3584 LLVMConstInt(ctx
->ac
.i32
, vertex_id
, false),
3587 ac_get_arg(&ctx
->ac
, ctx
->args
->prim_mask
));
3588 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3589 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3590 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3593 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3594 if (bit_size
== 64) {
3595 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3596 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3597 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3602 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3603 nir_intrinsic_instr
*instr
)
3605 LLVMValueRef result
= NULL
;
3607 switch (instr
->intrinsic
) {
3608 case nir_intrinsic_ballot
:
3609 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3610 if (ctx
->ac
.ballot_mask_bits
> ctx
->ac
.wave_size
)
3611 result
= LLVMBuildZExt(ctx
->ac
.builder
, result
, ctx
->ac
.iN_ballotmask
, "");
3613 case nir_intrinsic_read_invocation
:
3614 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3615 get_src(ctx
, instr
->src
[1]));
3617 case nir_intrinsic_read_first_invocation
:
3618 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3620 case nir_intrinsic_load_subgroup_invocation
:
3621 result
= ac_get_thread_id(&ctx
->ac
);
3623 case nir_intrinsic_load_work_group_id
: {
3624 LLVMValueRef values
[3];
3626 for (int i
= 0; i
< 3; i
++) {
3627 values
[i
] = ctx
->args
->workgroup_ids
[i
].used
?
3628 ac_get_arg(&ctx
->ac
, ctx
->args
->workgroup_ids
[i
]) : ctx
->ac
.i32_0
;
3631 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3634 case nir_intrinsic_load_base_vertex
:
3635 case nir_intrinsic_load_first_vertex
:
3636 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3638 case nir_intrinsic_load_local_group_size
:
3639 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3641 case nir_intrinsic_load_vertex_id
:
3642 result
= LLVMBuildAdd(ctx
->ac
.builder
,
3643 ac_get_arg(&ctx
->ac
, ctx
->args
->vertex_id
),
3644 ac_get_arg(&ctx
->ac
, ctx
->args
->base_vertex
), "");
3646 case nir_intrinsic_load_vertex_id_zero_base
: {
3647 result
= ctx
->abi
->vertex_id
;
3650 case nir_intrinsic_load_local_invocation_id
: {
3651 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->local_invocation_ids
);
3654 case nir_intrinsic_load_base_instance
:
3655 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->start_instance
);
3657 case nir_intrinsic_load_draw_id
:
3658 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->draw_id
);
3660 case nir_intrinsic_load_view_index
:
3661 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->view_index
);
3663 case nir_intrinsic_load_invocation_id
:
3664 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3665 result
= ac_unpack_param(&ctx
->ac
,
3666 ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_rel_ids
),
3669 if (ctx
->ac
.chip_class
>= GFX10
) {
3670 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3671 ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
),
3672 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3674 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_invocation_id
);
3678 case nir_intrinsic_load_primitive_id
:
3679 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3680 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->gs_prim_id
);
3681 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3682 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tcs_patch_id
);
3683 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3684 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->tes_patch_id
);
3686 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3688 case nir_intrinsic_load_sample_id
:
3689 result
= ac_unpack_param(&ctx
->ac
,
3690 ac_get_arg(&ctx
->ac
, ctx
->args
->ancillary
),
3693 case nir_intrinsic_load_sample_pos
:
3694 result
= load_sample_pos(ctx
);
3696 case nir_intrinsic_load_sample_mask_in
:
3697 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3699 case nir_intrinsic_load_frag_coord
: {
3700 LLVMValueRef values
[4] = {
3701 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[0]),
3702 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[1]),
3703 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[2]),
3704 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
3705 ac_get_arg(&ctx
->ac
, ctx
->args
->frag_pos
[3]))
3707 result
= ac_to_integer(&ctx
->ac
,
3708 ac_build_gather_values(&ctx
->ac
, values
, 4));
3711 case nir_intrinsic_load_layer_id
:
3712 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3714 case nir_intrinsic_load_front_face
:
3715 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->front_face
);
3717 case nir_intrinsic_load_helper_invocation
:
3718 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3720 case nir_intrinsic_is_helper_invocation
:
3721 result
= ac_build_is_helper_invocation(&ctx
->ac
);
3723 case nir_intrinsic_load_color0
:
3724 result
= ctx
->abi
->color0
;
3726 case nir_intrinsic_load_color1
:
3727 result
= ctx
->abi
->color1
;
3729 case nir_intrinsic_load_user_data_amd
:
3730 assert(LLVMTypeOf(ctx
->abi
->user_data
) == ctx
->ac
.v4i32
);
3731 result
= ctx
->abi
->user_data
;
3733 case nir_intrinsic_load_instance_id
:
3734 result
= ctx
->abi
->instance_id
;
3736 case nir_intrinsic_load_num_work_groups
:
3737 result
= ac_get_arg(&ctx
->ac
, ctx
->args
->num_work_groups
);
3739 case nir_intrinsic_load_local_invocation_index
:
3740 result
= visit_load_local_invocation_index(ctx
);
3742 case nir_intrinsic_load_subgroup_id
:
3743 result
= visit_load_subgroup_id(ctx
);
3745 case nir_intrinsic_load_num_subgroups
:
3746 result
= visit_load_num_subgroups(ctx
);
3748 case nir_intrinsic_first_invocation
:
3749 result
= visit_first_invocation(ctx
);
3751 case nir_intrinsic_load_push_constant
:
3752 result
= visit_load_push_constant(ctx
, instr
);
3754 case nir_intrinsic_vulkan_resource_index
: {
3755 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3756 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3757 unsigned binding
= nir_intrinsic_binding(instr
);
3759 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3763 case nir_intrinsic_vulkan_resource_reindex
:
3764 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3766 case nir_intrinsic_store_ssbo
:
3767 visit_store_ssbo(ctx
, instr
);
3769 case nir_intrinsic_load_ssbo
:
3770 result
= visit_load_buffer(ctx
, instr
);
3772 case nir_intrinsic_ssbo_atomic_add
:
3773 case nir_intrinsic_ssbo_atomic_imin
:
3774 case nir_intrinsic_ssbo_atomic_umin
:
3775 case nir_intrinsic_ssbo_atomic_imax
:
3776 case nir_intrinsic_ssbo_atomic_umax
:
3777 case nir_intrinsic_ssbo_atomic_and
:
3778 case nir_intrinsic_ssbo_atomic_or
:
3779 case nir_intrinsic_ssbo_atomic_xor
:
3780 case nir_intrinsic_ssbo_atomic_exchange
:
3781 case nir_intrinsic_ssbo_atomic_comp_swap
:
3782 result
= visit_atomic_ssbo(ctx
, instr
);
3784 case nir_intrinsic_load_ubo
:
3785 result
= visit_load_ubo_buffer(ctx
, instr
);
3787 case nir_intrinsic_get_buffer_size
:
3788 result
= visit_get_buffer_size(ctx
, instr
);
3790 case nir_intrinsic_load_deref
:
3791 result
= visit_load_var(ctx
, instr
);
3793 case nir_intrinsic_store_deref
:
3794 visit_store_var(ctx
, instr
);
3796 case nir_intrinsic_load_shared
:
3797 result
= visit_load_shared(ctx
, instr
);
3799 case nir_intrinsic_store_shared
:
3800 visit_store_shared(ctx
, instr
);
3802 case nir_intrinsic_bindless_image_samples
:
3803 case nir_intrinsic_image_deref_samples
:
3804 result
= visit_image_samples(ctx
, instr
);
3806 case nir_intrinsic_bindless_image_load
:
3807 result
= visit_image_load(ctx
, instr
, true);
3809 case nir_intrinsic_image_deref_load
:
3810 result
= visit_image_load(ctx
, instr
, false);
3812 case nir_intrinsic_bindless_image_store
:
3813 visit_image_store(ctx
, instr
, true);
3815 case nir_intrinsic_image_deref_store
:
3816 visit_image_store(ctx
, instr
, false);
3818 case nir_intrinsic_bindless_image_atomic_add
:
3819 case nir_intrinsic_bindless_image_atomic_imin
:
3820 case nir_intrinsic_bindless_image_atomic_umin
:
3821 case nir_intrinsic_bindless_image_atomic_imax
:
3822 case nir_intrinsic_bindless_image_atomic_umax
:
3823 case nir_intrinsic_bindless_image_atomic_and
:
3824 case nir_intrinsic_bindless_image_atomic_or
:
3825 case nir_intrinsic_bindless_image_atomic_xor
:
3826 case nir_intrinsic_bindless_image_atomic_exchange
:
3827 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3828 case nir_intrinsic_bindless_image_atomic_inc_wrap
:
3829 case nir_intrinsic_bindless_image_atomic_dec_wrap
:
3830 result
= visit_image_atomic(ctx
, instr
, true);
3832 case nir_intrinsic_image_deref_atomic_add
:
3833 case nir_intrinsic_image_deref_atomic_imin
:
3834 case nir_intrinsic_image_deref_atomic_umin
:
3835 case nir_intrinsic_image_deref_atomic_imax
:
3836 case nir_intrinsic_image_deref_atomic_umax
:
3837 case nir_intrinsic_image_deref_atomic_and
:
3838 case nir_intrinsic_image_deref_atomic_or
:
3839 case nir_intrinsic_image_deref_atomic_xor
:
3840 case nir_intrinsic_image_deref_atomic_exchange
:
3841 case nir_intrinsic_image_deref_atomic_comp_swap
:
3842 case nir_intrinsic_image_deref_atomic_inc_wrap
:
3843 case nir_intrinsic_image_deref_atomic_dec_wrap
:
3844 result
= visit_image_atomic(ctx
, instr
, false);
3846 case nir_intrinsic_bindless_image_size
:
3847 result
= visit_image_size(ctx
, instr
, true);
3849 case nir_intrinsic_image_deref_size
:
3850 result
= visit_image_size(ctx
, instr
, false);
3852 case nir_intrinsic_shader_clock
:
3853 result
= ac_build_shader_clock(&ctx
->ac
);
3855 case nir_intrinsic_discard
:
3856 case nir_intrinsic_discard_if
:
3857 emit_discard(ctx
, instr
);
3859 case nir_intrinsic_demote
:
3860 case nir_intrinsic_demote_if
:
3861 emit_demote(ctx
, instr
);
3863 case nir_intrinsic_memory_barrier
:
3864 case nir_intrinsic_group_memory_barrier
:
3865 case nir_intrinsic_memory_barrier_buffer
:
3866 case nir_intrinsic_memory_barrier_image
:
3867 case nir_intrinsic_memory_barrier_shared
:
3868 emit_membar(&ctx
->ac
, instr
);
3870 case nir_intrinsic_memory_barrier_tcs_patch
:
3872 case nir_intrinsic_control_barrier
:
3873 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3875 case nir_intrinsic_shared_atomic_add
:
3876 case nir_intrinsic_shared_atomic_imin
:
3877 case nir_intrinsic_shared_atomic_umin
:
3878 case nir_intrinsic_shared_atomic_imax
:
3879 case nir_intrinsic_shared_atomic_umax
:
3880 case nir_intrinsic_shared_atomic_and
:
3881 case nir_intrinsic_shared_atomic_or
:
3882 case nir_intrinsic_shared_atomic_xor
:
3883 case nir_intrinsic_shared_atomic_exchange
:
3884 case nir_intrinsic_shared_atomic_comp_swap
: {
3885 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0],
3886 instr
->src
[1].ssa
->bit_size
);
3887 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3890 case nir_intrinsic_deref_atomic_add
:
3891 case nir_intrinsic_deref_atomic_imin
:
3892 case nir_intrinsic_deref_atomic_umin
:
3893 case nir_intrinsic_deref_atomic_imax
:
3894 case nir_intrinsic_deref_atomic_umax
:
3895 case nir_intrinsic_deref_atomic_and
:
3896 case nir_intrinsic_deref_atomic_or
:
3897 case nir_intrinsic_deref_atomic_xor
:
3898 case nir_intrinsic_deref_atomic_exchange
:
3899 case nir_intrinsic_deref_atomic_comp_swap
: {
3900 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3901 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3904 case nir_intrinsic_load_barycentric_pixel
:
3905 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
3907 case nir_intrinsic_load_barycentric_centroid
:
3908 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
3910 case nir_intrinsic_load_barycentric_sample
:
3911 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
3913 case nir_intrinsic_load_barycentric_model
:
3914 result
= barycentric_model(ctx
);
3916 case nir_intrinsic_load_barycentric_at_offset
: {
3917 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3918 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
3921 case nir_intrinsic_load_barycentric_at_sample
: {
3922 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
3923 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
3926 case nir_intrinsic_load_interpolated_input
: {
3927 /* We assume any indirect loads have been lowered away */
3928 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
3930 assert(offset
[0].i32
== 0);
3932 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
3933 unsigned index
= nir_intrinsic_base(instr
);
3934 unsigned component
= nir_intrinsic_component(instr
);
3935 result
= load_interpolated_input(ctx
, interp_param
, index
,
3937 instr
->dest
.ssa
.num_components
,
3938 instr
->dest
.ssa
.bit_size
);
3941 case nir_intrinsic_load_input
:
3942 case nir_intrinsic_load_input_vertex
:
3943 result
= load_input(ctx
, instr
);
3945 case nir_intrinsic_emit_vertex
:
3946 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3948 case nir_intrinsic_emit_vertex_with_counter
: {
3949 unsigned stream
= nir_intrinsic_stream_id(instr
);
3950 LLVMValueRef next_vertex
= get_src(ctx
, instr
->src
[0]);
3951 ctx
->abi
->emit_vertex_with_counter(ctx
->abi
, stream
,
3956 case nir_intrinsic_end_primitive
:
3957 case nir_intrinsic_end_primitive_with_counter
:
3958 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3960 case nir_intrinsic_load_tess_coord
:
3961 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3963 case nir_intrinsic_load_tess_level_outer
:
3964 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, false);
3966 case nir_intrinsic_load_tess_level_inner
:
3967 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, false);
3969 case nir_intrinsic_load_tess_level_outer_default
:
3970 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
, true);
3972 case nir_intrinsic_load_tess_level_inner_default
:
3973 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
, true);
3975 case nir_intrinsic_load_patch_vertices_in
:
3976 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3978 case nir_intrinsic_vote_all
: {
3979 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3980 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3983 case nir_intrinsic_vote_any
: {
3984 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3985 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3988 case nir_intrinsic_shuffle
:
3989 if (ctx
->ac
.chip_class
== GFX8
||
3990 ctx
->ac
.chip_class
== GFX9
||
3991 (ctx
->ac
.chip_class
== GFX10
&& ctx
->ac
.wave_size
== 32)) {
3992 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3993 get_src(ctx
, instr
->src
[1]));
3995 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
3996 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
3997 LLVMTypeRef type
= LLVMTypeOf(src
);
3998 struct waterfall_context wctx
;
3999 LLVMValueRef index_val
;
4001 index_val
= enter_waterfall(ctx
, &wctx
, index
, true);
4003 src
= LLVMBuildZExt(ctx
->ac
.builder
, src
,
4006 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.readlane",
4008 (LLVMValueRef
[]) { src
, index_val
}, 2,
4009 AC_FUNC_ATTR_READNONE
|
4010 AC_FUNC_ATTR_CONVERGENT
);
4012 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, type
, "");
4014 result
= exit_waterfall(ctx
, &wctx
, result
);
4017 case nir_intrinsic_reduce
:
4018 result
= ac_build_reduce(&ctx
->ac
,
4019 get_src(ctx
, instr
->src
[0]),
4020 instr
->const_index
[0],
4021 instr
->const_index
[1]);
4023 case nir_intrinsic_inclusive_scan
:
4024 result
= ac_build_inclusive_scan(&ctx
->ac
,
4025 get_src(ctx
, instr
->src
[0]),
4026 instr
->const_index
[0]);
4028 case nir_intrinsic_exclusive_scan
:
4029 result
= ac_build_exclusive_scan(&ctx
->ac
,
4030 get_src(ctx
, instr
->src
[0]),
4031 instr
->const_index
[0]);
4033 case nir_intrinsic_quad_broadcast
: {
4034 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
4035 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4036 lane
, lane
, lane
, lane
);
4039 case nir_intrinsic_quad_swap_horizontal
:
4040 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
4042 case nir_intrinsic_quad_swap_vertical
:
4043 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
4045 case nir_intrinsic_quad_swap_diagonal
:
4046 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
4048 case nir_intrinsic_quad_swizzle_amd
: {
4049 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4050 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4051 mask
& 0x3, (mask
>> 2) & 0x3,
4052 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
4055 case nir_intrinsic_masked_swizzle_amd
: {
4056 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
4057 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
4060 case nir_intrinsic_write_invocation_amd
:
4061 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
4062 get_src(ctx
, instr
->src
[1]),
4063 get_src(ctx
, instr
->src
[2]));
4065 case nir_intrinsic_mbcnt_amd
:
4066 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
4068 case nir_intrinsic_load_scratch
: {
4069 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4070 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4072 LLVMTypeRef comp_type
=
4073 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4074 LLVMTypeRef vec_type
=
4075 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4076 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4077 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4078 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4079 LLVMPointerType(vec_type
, addr_space
), "");
4080 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4083 case nir_intrinsic_store_scratch
: {
4084 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
4085 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->scratch
,
4087 LLVMTypeRef comp_type
=
4088 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->src
[0].ssa
->bit_size
);
4089 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4090 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4091 LLVMPointerType(comp_type
, addr_space
), "");
4092 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
4093 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
4096 u_bit_scan_consecutive_range(&wrmask
, &start
, &count
);
4098 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, start
, false);
4099 LLVMValueRef offset_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &offset
, 1, "");
4100 LLVMTypeRef vec_type
=
4101 count
== 1 ? comp_type
: LLVMVectorType(comp_type
, count
);
4102 offset_ptr
= LLVMBuildBitCast(ctx
->ac
.builder
,
4104 LLVMPointerType(vec_type
, addr_space
),
4106 LLVMValueRef offset_src
=
4107 ac_extract_components(&ctx
->ac
, src
, start
, count
);
4108 LLVMBuildStore(ctx
->ac
.builder
, offset_src
, offset_ptr
);
4112 case nir_intrinsic_load_constant
: {
4113 unsigned base
= nir_intrinsic_base(instr
);
4114 unsigned range
= nir_intrinsic_range(instr
);
4116 LLVMValueRef offset
= get_src(ctx
, instr
->src
[0]);
4117 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
4118 LLVMConstInt(ctx
->ac
.i32
, base
, false), "");
4120 /* Clamp the offset to avoid out-of-bound access because global
4121 * instructions can't handle them.
4123 LLVMValueRef size
= LLVMConstInt(ctx
->ac
.i32
, base
+ range
, false);
4124 LLVMValueRef cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
4126 offset
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, offset
, size
, "");
4128 LLVMValueRef ptr
= ac_build_gep0(&ctx
->ac
, ctx
->constant_data
,
4130 LLVMTypeRef comp_type
=
4131 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
4132 LLVMTypeRef vec_type
=
4133 instr
->dest
.ssa
.num_components
== 1 ? comp_type
:
4134 LLVMVectorType(comp_type
, instr
->dest
.ssa
.num_components
);
4135 unsigned addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
4136 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
4137 LLVMPointerType(vec_type
, addr_space
), "");
4138 result
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
4142 fprintf(stderr
, "Unknown intrinsic: ");
4143 nir_print_instr(&instr
->instr
, stderr
);
4144 fprintf(stderr
, "\n");
4148 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4152 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
4153 unsigned base_index
,
4154 unsigned constant_index
,
4155 LLVMValueRef dynamic_index
)
4157 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
4158 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
4159 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
4161 /* Bindless uniforms are 64bit so multiple index by 8 */
4162 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
4163 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
4165 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
4167 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
4168 NULL
, 0, 0, true, true);
4170 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
4173 struct sampler_desc_address
{
4174 unsigned descriptor_set
;
4175 unsigned base_index
; /* binding in vulkan */
4176 unsigned constant_index
;
4177 LLVMValueRef dynamic_index
;
4182 static struct sampler_desc_address
4183 get_sampler_desc_internal(struct ac_nir_context
*ctx
,
4184 nir_deref_instr
*deref_instr
,
4185 const nir_instr
*instr
,
4188 LLVMValueRef index
= NULL
;
4189 unsigned constant_index
= 0;
4190 unsigned descriptor_set
;
4191 unsigned base_index
;
4192 bool bindless
= false;
4197 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
4200 index
= get_src(ctx
, img_instr
->src
[0]);
4202 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
4203 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
4204 nir_tex_src_sampler_handle
);
4205 if (sampSrcIdx
!= -1) {
4208 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
4210 assert(tex_instr
&& !image
);
4211 base_index
= tex_instr
->sampler_index
;
4215 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
4216 if (deref_instr
->deref_type
== nir_deref_type_array
) {
4217 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
4221 if (nir_src_is_const(deref_instr
->arr
.index
)) {
4222 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
4224 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
4226 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
4227 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
4232 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
4235 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4236 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
4237 unsigned sidx
= deref_instr
->strct
.index
;
4238 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
4239 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
4241 unreachable("Unsupported deref type");
4244 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
4246 if (deref_instr
->var
->data
.bindless
) {
4247 /* For now just assert on unhandled variable types */
4248 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
4250 base_index
= deref_instr
->var
->data
.driver_location
;
4253 index
= index
? index
: ctx
->ac
.i32_0
;
4254 index
= get_bindless_index_from_uniform(ctx
, base_index
,
4255 constant_index
, index
);
4257 base_index
= deref_instr
->var
->data
.binding
;
4259 return (struct sampler_desc_address
) {
4260 .descriptor_set
= descriptor_set
,
4261 .base_index
= base_index
,
4262 .constant_index
= constant_index
,
4263 .dynamic_index
= index
,
4265 .bindless
= bindless
,
4269 /* Extract any possibly divergent index into a separate value that can be fed
4270 * into get_sampler_desc with the same arguments. */
4271 static LLVMValueRef
get_sampler_desc_index(struct ac_nir_context
*ctx
,
4272 nir_deref_instr
*deref_instr
,
4273 const nir_instr
*instr
,
4276 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4277 return addr
.dynamic_index
;
4280 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
4281 nir_deref_instr
*deref_instr
,
4282 enum ac_descriptor_type desc_type
,
4283 const nir_instr
*instr
,
4285 bool image
, bool write
)
4287 struct sampler_desc_address addr
= get_sampler_desc_internal(ctx
, deref_instr
, instr
, image
);
4288 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
4289 addr
.descriptor_set
,
4291 addr
.constant_index
, index
,
4292 desc_type
, addr
.image
, write
, addr
.bindless
);
4295 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4298 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4299 * filtering manually. The driver sets img7 to a mask clearing
4300 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4301 * s_and_b32 samp0, samp0, img7
4304 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4306 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
4307 LLVMValueRef res
, LLVMValueRef samp
)
4309 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4310 LLVMValueRef img7
, samp0
;
4312 if (ctx
->ac
.chip_class
>= GFX8
)
4315 img7
= LLVMBuildExtractElement(builder
, res
,
4316 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
4317 samp0
= LLVMBuildExtractElement(builder
, samp
,
4318 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4319 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4320 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4321 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
4324 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
4325 nir_tex_instr
*instr
,
4326 struct waterfall_context
*wctx
,
4327 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
4328 LLVMValueRef
*fmask_ptr
)
4330 nir_deref_instr
*texture_deref_instr
= NULL
;
4331 nir_deref_instr
*sampler_deref_instr
= NULL
;
4334 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4335 switch (instr
->src
[i
].src_type
) {
4336 case nir_tex_src_texture_deref
:
4337 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4339 case nir_tex_src_sampler_deref
:
4340 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
4342 case nir_tex_src_plane
:
4343 plane
= nir_src_as_int(instr
->src
[i
].src
);
4350 LLVMValueRef texture_dynamic_index
= get_sampler_desc_index(ctx
, texture_deref_instr
,
4351 &instr
->instr
, false);
4352 if (!sampler_deref_instr
)
4353 sampler_deref_instr
= texture_deref_instr
;
4355 LLVMValueRef sampler_dynamic_index
= get_sampler_desc_index(ctx
, sampler_deref_instr
,
4356 &instr
->instr
, false);
4357 if (instr
->texture_non_uniform
)
4358 texture_dynamic_index
= enter_waterfall(ctx
, wctx
+ 0, texture_dynamic_index
, true);
4360 if (instr
->sampler_non_uniform
)
4361 sampler_dynamic_index
= enter_waterfall(ctx
, wctx
+ 1, sampler_dynamic_index
, true);
4363 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
4366 assert(instr
->op
!= nir_texop_txf_ms
&&
4367 instr
->op
!= nir_texop_samples_identical
);
4368 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
4370 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
4373 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4374 /* The fragment mask is fetched from the compressed
4375 * multisampled surface.
4377 main_descriptor
= AC_DESC_FMASK
;
4380 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
,
4381 texture_dynamic_index
, false, false);
4384 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
,
4385 sampler_dynamic_index
, false, false);
4386 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
4387 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4389 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
4390 instr
->op
== nir_texop_samples_identical
))
4391 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
,
4392 &instr
->instr
, texture_dynamic_index
, false, false);
4395 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
4398 coord
= ac_to_float(ctx
, coord
);
4399 coord
= ac_build_round(ctx
, coord
);
4400 coord
= ac_to_integer(ctx
, coord
);
4404 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
4406 LLVMValueRef result
= NULL
;
4407 struct ac_image_args args
= { 0 };
4408 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
4409 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
4410 unsigned offset_src
= 0;
4411 struct waterfall_context wctx
[2] = {{{0}}};
4413 tex_fetch_ptrs(ctx
, instr
, wctx
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
4415 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4416 switch (instr
->src
[i
].src_type
) {
4417 case nir_tex_src_coord
: {
4418 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
4419 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
4420 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
4423 case nir_tex_src_projector
:
4425 case nir_tex_src_comparator
:
4426 if (instr
->is_shadow
) {
4427 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
4428 args
.compare
= ac_to_float(&ctx
->ac
, args
.compare
);
4431 case nir_tex_src_offset
:
4432 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
4435 case nir_tex_src_bias
:
4436 if (instr
->op
== nir_texop_txb
)
4437 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
4439 case nir_tex_src_lod
: {
4440 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
4441 args
.level_zero
= true;
4443 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
4446 case nir_tex_src_ms_index
:
4447 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
4449 case nir_tex_src_ms_mcs
:
4451 case nir_tex_src_ddx
:
4452 ddx
= get_src(ctx
, instr
->src
[i
].src
);
4454 case nir_tex_src_ddy
:
4455 ddy
= get_src(ctx
, instr
->src
[i
].src
);
4457 case nir_tex_src_texture_offset
:
4458 case nir_tex_src_sampler_offset
:
4459 case nir_tex_src_plane
:
4465 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
4466 result
= get_buffer_size(ctx
, args
.resource
, true);
4470 if (instr
->op
== nir_texop_texture_samples
) {
4471 LLVMValueRef res
, samples
, is_msaa
;
4472 LLVMValueRef default_sample
;
4474 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
4475 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4476 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4477 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4478 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
4479 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
4480 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4481 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
4482 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
4484 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
4485 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
4486 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
4487 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
4488 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
4491 if (ctx
->abi
->robust_buffer_access
) {
4492 LLVMValueRef dword1
, is_null_descriptor
;
4494 /* Extract the second dword of the descriptor, if it's
4495 * all zero, then it's a null descriptor.
4497 dword1
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
4498 LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
4499 is_null_descriptor
=
4500 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, dword1
,
4501 LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
4503 LLVMBuildSelect(ctx
->ac
.builder
, is_null_descriptor
,
4504 ctx
->ac
.i32_0
, ctx
->ac
.i32_1
, "");
4506 default_sample
= ctx
->ac
.i32_1
;
4509 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
4510 default_sample
, "");
4515 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
4516 LLVMValueRef offset
[3], pack
;
4517 for (unsigned chan
= 0; chan
< 3; ++chan
)
4518 offset
[chan
] = ctx
->ac
.i32_0
;
4520 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
4521 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
4522 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
4523 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
4524 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
4526 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
4527 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
4529 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
4530 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
4534 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
4535 * OpenGL 4.5 spec says:
4537 * "If the texture’s internal format indicates a fixed-point
4538 * depth texture, then D_t and D_ref are clamped to the
4539 * range [0, 1]; otherwise no clamping is performed."
4541 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4542 * so the depth comparison value isn't clamped for Z16 and
4543 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
4544 * an explicitly clamped 32-bit float format.
4547 ctx
->ac
.chip_class
>= GFX8
&&
4548 ctx
->ac
.chip_class
<= GFX9
&&
4549 ctx
->abi
->clamp_shadow_reference
) {
4550 LLVMValueRef upgraded
, clamped
;
4552 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
4553 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4554 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
4555 LLVMConstInt(ctx
->ac
.i32
, 29, false), "");
4556 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->ac
.i1
, "");
4557 clamped
= ac_build_clamp(&ctx
->ac
, args
.compare
);
4558 args
.compare
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
,
4562 /* pack derivatives */
4564 int num_src_deriv_channels
, num_dest_deriv_channels
;
4565 switch (instr
->sampler_dim
) {
4566 case GLSL_SAMPLER_DIM_3D
:
4567 case GLSL_SAMPLER_DIM_CUBE
:
4568 num_src_deriv_channels
= 3;
4569 num_dest_deriv_channels
= 3;
4571 case GLSL_SAMPLER_DIM_2D
:
4573 num_src_deriv_channels
= 2;
4574 num_dest_deriv_channels
= 2;
4576 case GLSL_SAMPLER_DIM_1D
:
4577 num_src_deriv_channels
= 1;
4578 if (ctx
->ac
.chip_class
== GFX9
) {
4579 num_dest_deriv_channels
= 2;
4581 num_dest_deriv_channels
= 1;
4586 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
4587 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
4588 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
4589 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
4590 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
4592 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
4593 args
.derivs
[i
] = ctx
->ac
.f32_0
;
4594 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
4598 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
4599 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
4600 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
4601 if (instr
->coord_components
== 3)
4602 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
4603 ac_prepare_cube_coords(&ctx
->ac
,
4604 instr
->op
== nir_texop_txd
, instr
->is_array
,
4605 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
4608 /* Texture coordinates fixups */
4609 if (instr
->coord_components
> 1 &&
4610 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4612 instr
->op
!= nir_texop_txf
) {
4613 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
4616 if (instr
->coord_components
> 2 &&
4617 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
4618 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
4619 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
4620 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
4622 instr
->op
!= nir_texop_txf
&&
4623 instr
->op
!= nir_texop_txf_ms
&&
4624 instr
->op
!= nir_texop_fragment_fetch
&&
4625 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4626 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
4629 if (ctx
->ac
.chip_class
== GFX9
&&
4630 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4631 instr
->op
!= nir_texop_lod
) {
4632 LLVMValueRef filler
;
4633 if (instr
->op
== nir_texop_txf
)
4634 filler
= ctx
->ac
.i32_0
;
4636 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
4638 if (instr
->is_array
)
4639 args
.coords
[2] = args
.coords
[1];
4640 args
.coords
[1] = filler
;
4643 /* Pack sample index */
4644 if (sample_index
&& (instr
->op
== nir_texop_txf_ms
||
4645 instr
->op
== nir_texop_fragment_fetch
))
4646 args
.coords
[instr
->coord_components
] = sample_index
;
4648 if (instr
->op
== nir_texop_samples_identical
) {
4649 struct ac_image_args txf_args
= { 0 };
4650 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
4652 txf_args
.dmask
= 0xf;
4653 txf_args
.resource
= fmask_ptr
;
4654 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
4655 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
4657 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4658 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
4662 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
4663 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
4664 instr
->op
!= nir_texop_txs
&&
4665 instr
->op
!= nir_texop_fragment_fetch
&&
4666 instr
->op
!= nir_texop_fragment_mask_fetch
) {
4667 unsigned sample_chan
= instr
->is_array
? 3 : 2;
4668 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
4669 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
4670 instr
->is_array
? args
.coords
[2] : NULL
,
4671 args
.coords
[sample_chan
], fmask_ptr
);
4674 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
4675 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
4676 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
4677 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
4678 args
.coords
[i
] = LLVMBuildAdd(
4679 ctx
->ac
.builder
, args
.coords
[i
],
4680 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
4685 /* DMASK was repurposed for GATHER4. 4 components are always
4686 * returned and DMASK works like a swizzle - it selects
4687 * the component to fetch. The only valid DMASK values are
4688 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4689 * (red,red,red,red) etc.) The ISA document doesn't mention
4693 if (instr
->op
== nir_texop_tg4
) {
4694 if (instr
->is_shadow
)
4697 args
.dmask
= 1 << instr
->component
;
4700 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
) {
4701 args
.dim
= ac_get_sampler_dim(ctx
->ac
.chip_class
, instr
->sampler_dim
, instr
->is_array
);
4702 args
.unorm
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
4705 /* Adjust the number of coordinates because we only need (x,y) for 2D
4706 * multisampled images and (x,y,layer) for 2D multisampled layered
4707 * images or for multisampled input attachments.
4709 if (instr
->op
== nir_texop_fragment_mask_fetch
) {
4710 if (args
.dim
== ac_image_2dmsaa
) {
4711 args
.dim
= ac_image_2d
;
4713 assert(args
.dim
== ac_image_2darraymsaa
);
4714 args
.dim
= ac_image_2darray
;
4718 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4720 if (instr
->op
== nir_texop_query_levels
)
4721 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4722 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4723 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4724 instr
->op
!= nir_texop_tg4
)
4725 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4726 else if (instr
->op
== nir_texop_txs
&&
4727 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4729 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4730 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4731 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4732 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4733 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4734 } else if (ctx
->ac
.chip_class
== GFX9
&&
4735 instr
->op
== nir_texop_txs
&&
4736 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4738 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4739 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4740 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4742 } else if (instr
->dest
.ssa
.num_components
!= 4)
4743 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4747 assert(instr
->dest
.is_ssa
);
4748 result
= ac_to_integer(&ctx
->ac
, result
);
4750 for (int i
= ARRAY_SIZE(wctx
); --i
>= 0;) {
4751 result
= exit_waterfall(ctx
, wctx
+ i
, result
);
4754 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4758 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4760 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4761 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4763 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4764 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4767 static void visit_post_phi(struct ac_nir_context
*ctx
,
4768 nir_phi_instr
*instr
,
4769 LLVMValueRef llvm_phi
)
4771 nir_foreach_phi_src(src
, instr
) {
4772 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4773 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4775 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4779 static void phi_post_pass(struct ac_nir_context
*ctx
)
4781 hash_table_foreach(ctx
->phis
, entry
) {
4782 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4783 (LLVMValueRef
)entry
->data
);
4788 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4789 const nir_ssa_undef_instr
*instr
)
4791 unsigned num_components
= instr
->def
.num_components
;
4792 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4795 if (num_components
== 1)
4796 undef
= LLVMGetUndef(type
);
4798 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4800 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4803 static void visit_jump(struct ac_llvm_context
*ctx
,
4804 const nir_jump_instr
*instr
)
4806 switch (instr
->type
) {
4807 case nir_jump_break
:
4808 ac_build_break(ctx
);
4810 case nir_jump_continue
:
4811 ac_build_continue(ctx
);
4814 fprintf(stderr
, "Unknown NIR jump instr: ");
4815 nir_print_instr(&instr
->instr
, stderr
);
4816 fprintf(stderr
, "\n");
4822 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4823 enum glsl_base_type type
)
4827 case GLSL_TYPE_UINT
:
4828 case GLSL_TYPE_BOOL
:
4829 case GLSL_TYPE_SUBROUTINE
:
4831 case GLSL_TYPE_INT8
:
4832 case GLSL_TYPE_UINT8
:
4834 case GLSL_TYPE_INT16
:
4835 case GLSL_TYPE_UINT16
:
4837 case GLSL_TYPE_FLOAT
:
4839 case GLSL_TYPE_FLOAT16
:
4841 case GLSL_TYPE_INT64
:
4842 case GLSL_TYPE_UINT64
:
4844 case GLSL_TYPE_DOUBLE
:
4847 unreachable("unknown GLSL type");
4852 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4853 const struct glsl_type
*type
)
4855 if (glsl_type_is_scalar(type
)) {
4856 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4859 if (glsl_type_is_vector(type
)) {
4860 return LLVMVectorType(
4861 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4862 glsl_get_vector_elements(type
));
4865 if (glsl_type_is_matrix(type
)) {
4866 return LLVMArrayType(
4867 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4868 glsl_get_matrix_columns(type
));
4871 if (glsl_type_is_array(type
)) {
4872 return LLVMArrayType(
4873 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4874 glsl_get_length(type
));
4877 assert(glsl_type_is_struct_or_ifc(type
));
4879 LLVMTypeRef member_types
[glsl_get_length(type
)];
4881 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4883 glsl_to_llvm_type(ac
,
4884 glsl_get_struct_field(type
, i
));
4887 return LLVMStructTypeInContext(ac
->context
, member_types
,
4888 glsl_get_length(type
), false);
4891 static void visit_deref(struct ac_nir_context
*ctx
,
4892 nir_deref_instr
*instr
)
4894 if (instr
->mode
!= nir_var_mem_shared
&&
4895 instr
->mode
!= nir_var_mem_global
)
4898 LLVMValueRef result
= NULL
;
4899 switch(instr
->deref_type
) {
4900 case nir_deref_type_var
: {
4901 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4902 result
= entry
->data
;
4905 case nir_deref_type_struct
:
4906 if (instr
->mode
== nir_var_mem_global
) {
4907 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4908 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4909 instr
->strct
.index
);
4910 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4911 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4913 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4914 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4917 case nir_deref_type_array
:
4918 if (instr
->mode
== nir_var_mem_global
) {
4919 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4920 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4922 if ((glsl_type_is_matrix(parent
->type
) &&
4923 glsl_matrix_type_is_row_major(parent
->type
)) ||
4924 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4925 stride
= type_scalar_size_bytes(parent
->type
);
4928 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4929 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4930 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4932 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4934 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4936 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4937 get_src(ctx
, instr
->arr
.index
));
4940 case nir_deref_type_ptr_as_array
:
4941 if (instr
->mode
== nir_var_mem_global
) {
4942 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4944 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4945 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4946 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4948 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4950 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4952 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4953 get_src(ctx
, instr
->arr
.index
));
4956 case nir_deref_type_cast
: {
4957 result
= get_src(ctx
, instr
->parent
);
4959 /* We can't use the structs from LLVM because the shader
4960 * specifies its own offsets. */
4961 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4962 if (instr
->mode
== nir_var_mem_shared
)
4963 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4965 unsigned address_space
;
4967 switch(instr
->mode
) {
4968 case nir_var_mem_shared
:
4969 address_space
= AC_ADDR_SPACE_LDS
;
4971 case nir_var_mem_global
:
4972 address_space
= AC_ADDR_SPACE_GLOBAL
;
4975 unreachable("Unhandled address space");
4978 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4980 if (LLVMTypeOf(result
) != type
) {
4981 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMFixedVectorTypeKind
) {
4982 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4985 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4992 unreachable("Unhandled deref_instr deref type");
4995 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4998 static void visit_cf_list(struct ac_nir_context
*ctx
,
4999 struct exec_list
*list
);
5001 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
5003 nir_foreach_instr(instr
, block
)
5005 switch (instr
->type
) {
5006 case nir_instr_type_alu
:
5007 visit_alu(ctx
, nir_instr_as_alu(instr
));
5009 case nir_instr_type_load_const
:
5010 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
5012 case nir_instr_type_intrinsic
:
5013 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
5015 case nir_instr_type_tex
:
5016 visit_tex(ctx
, nir_instr_as_tex(instr
));
5018 case nir_instr_type_phi
:
5019 visit_phi(ctx
, nir_instr_as_phi(instr
));
5021 case nir_instr_type_ssa_undef
:
5022 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
5024 case nir_instr_type_jump
:
5025 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
5027 case nir_instr_type_deref
:
5028 visit_deref(ctx
, nir_instr_as_deref(instr
));
5031 fprintf(stderr
, "Unknown NIR instr type: ");
5032 nir_print_instr(instr
, stderr
);
5033 fprintf(stderr
, "\n");
5038 _mesa_hash_table_insert(ctx
->defs
, block
,
5039 LLVMGetInsertBlock(ctx
->ac
.builder
));
5042 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
5044 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
5046 nir_block
*then_block
=
5047 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
5049 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
5051 visit_cf_list(ctx
, &if_stmt
->then_list
);
5053 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
5054 nir_block
*else_block
=
5055 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
5057 ac_build_else(&ctx
->ac
, else_block
->index
);
5058 visit_cf_list(ctx
, &if_stmt
->else_list
);
5061 ac_build_endif(&ctx
->ac
, then_block
->index
);
5064 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
5066 nir_block
*first_loop_block
=
5067 (nir_block
*) exec_list_get_head(&loop
->body
);
5069 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
5071 visit_cf_list(ctx
, &loop
->body
);
5073 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
5076 static void visit_cf_list(struct ac_nir_context
*ctx
,
5077 struct exec_list
*list
)
5079 foreach_list_typed(nir_cf_node
, node
, node
, list
)
5081 switch (node
->type
) {
5082 case nir_cf_node_block
:
5083 visit_block(ctx
, nir_cf_node_as_block(node
));
5086 case nir_cf_node_if
:
5087 visit_if(ctx
, nir_cf_node_as_if(node
));
5090 case nir_cf_node_loop
:
5091 visit_loop(ctx
, nir_cf_node_as_loop(node
));
5101 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
5102 struct ac_shader_abi
*abi
,
5103 struct nir_shader
*nir
,
5104 struct nir_variable
*variable
,
5105 gl_shader_stage stage
)
5107 unsigned output_loc
= variable
->data
.driver_location
/ 4;
5108 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5110 /* tess ctrl has it's own load/store paths for outputs */
5111 if (stage
== MESA_SHADER_TESS_CTRL
)
5114 if (stage
== MESA_SHADER_VERTEX
||
5115 stage
== MESA_SHADER_TESS_EVAL
||
5116 stage
== MESA_SHADER_GEOMETRY
) {
5117 int idx
= variable
->data
.location
+ variable
->data
.index
;
5118 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
5119 int length
= nir
->info
.clip_distance_array_size
+
5120 nir
->info
.cull_distance_array_size
;
5129 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
5130 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
5131 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
5132 for (unsigned chan
= 0; chan
< 4; chan
++) {
5133 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
5134 ac_build_alloca_undef(ctx
, type
, "");
5140 setup_locals(struct ac_nir_context
*ctx
,
5141 struct nir_function
*func
)
5144 ctx
->num_locals
= 0;
5145 nir_foreach_variable(variable
, &func
->impl
->locals
) {
5146 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
5147 variable
->data
.driver_location
= ctx
->num_locals
* 4;
5148 variable
->data
.location_frac
= 0;
5149 ctx
->num_locals
+= attrib_count
;
5151 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
5155 for (i
= 0; i
< ctx
->num_locals
; i
++) {
5156 for (j
= 0; j
< 4; j
++) {
5157 ctx
->locals
[i
* 4 + j
] =
5158 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
5164 setup_scratch(struct ac_nir_context
*ctx
,
5165 struct nir_shader
*shader
)
5167 if (shader
->scratch_size
== 0)
5170 ctx
->scratch
= ac_build_alloca_undef(&ctx
->ac
,
5171 LLVMArrayType(ctx
->ac
.i8
, shader
->scratch_size
),
5176 setup_constant_data(struct ac_nir_context
*ctx
,
5177 struct nir_shader
*shader
)
5179 if (!shader
->constant_data
)
5183 LLVMConstStringInContext(ctx
->ac
.context
,
5184 shader
->constant_data
,
5185 shader
->constant_data_size
,
5187 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
, shader
->constant_data_size
);
5189 /* We want to put the constant data in the CONST address space so that
5190 * we can use scalar loads. However, LLVM versions before 10 put these
5191 * variables in the same section as the code, which is unacceptable
5192 * for RadeonSI as it needs to relocate all the data sections after
5193 * the code sections. See https://reviews.llvm.org/D65813.
5195 unsigned address_space
=
5196 LLVM_VERSION_MAJOR
< 10 ? AC_ADDR_SPACE_GLOBAL
: AC_ADDR_SPACE_CONST
;
5198 LLVMValueRef global
=
5199 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5203 LLVMSetInitializer(global
, data
);
5204 LLVMSetGlobalConstant(global
, true);
5205 LLVMSetVisibility(global
, LLVMHiddenVisibility
);
5206 ctx
->constant_data
= global
;
5210 setup_shared(struct ac_nir_context
*ctx
,
5211 struct nir_shader
*nir
)
5216 LLVMTypeRef type
= LLVMArrayType(ctx
->ac
.i8
,
5217 nir
->info
.cs
.shared_size
);
5220 LLVMAddGlobalInAddressSpace(ctx
->ac
.module
, type
,
5223 LLVMSetAlignment(lds
, 64 * 1024);
5225 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, lds
,
5226 LLVMPointerType(ctx
->ac
.i8
,
5227 AC_ADDR_SPACE_LDS
), "");
5230 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
5231 const struct ac_shader_args
*args
, struct nir_shader
*nir
)
5233 struct ac_nir_context ctx
= {};
5234 struct nir_function
*func
;
5240 ctx
.stage
= nir
->info
.stage
;
5241 ctx
.info
= &nir
->info
;
5243 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
5245 nir_foreach_variable(variable
, &nir
->outputs
)
5246 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
5249 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5250 _mesa_key_pointer_equal
);
5251 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5252 _mesa_key_pointer_equal
);
5253 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
5254 _mesa_key_pointer_equal
);
5256 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
5258 nir_index_ssa_defs(func
->impl
);
5259 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
5261 setup_locals(&ctx
, func
);
5262 setup_scratch(&ctx
, nir
);
5263 setup_constant_data(&ctx
, nir
);
5265 if (gl_shader_stage_is_compute(nir
->info
.stage
))
5266 setup_shared(&ctx
, nir
);
5268 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&& nir
->info
.fs
.uses_demote
) {
5269 ctx
.ac
.postponed_kill
= ac_build_alloca_undef(&ctx
.ac
, ac
->i1
, "");
5270 /* true = don't kill. */
5271 LLVMBuildStore(ctx
.ac
.builder
, ctx
.ac
.i1true
, ctx
.ac
.postponed_kill
);
5274 visit_cf_list(&ctx
, &func
->impl
->body
);
5275 phi_post_pass(&ctx
);
5277 if (ctx
.ac
.postponed_kill
)
5278 ac_build_kill_if_false(&ctx
.ac
, LLVMBuildLoad(ctx
.ac
.builder
,
5279 ctx
.ac
.postponed_kill
, ""));
5281 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
5282 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
5287 ralloc_free(ctx
.defs
);
5288 ralloc_free(ctx
.phis
);
5289 ralloc_free(ctx
.vars
);
5293 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
5295 bool progress
= false;
5297 /* Lower large variables to scratch first so that we won't bloat the
5298 * shader by generating large if ladders for them. We later lower
5299 * scratch to alloca's, assuming LLVM won't generate VGPR indexing.
5301 NIR_PASS(progress
, nir
, nir_lower_vars_to_scratch
,
5302 nir_var_function_temp
,
5304 glsl_get_natural_size_align_bytes
);
5306 /* While it would be nice not to have this flag, we are constrained
5307 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
5309 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
5311 /* TODO: Indirect indexing of GS inputs is unimplemented.
5313 * TCS and TES load inputs directly from LDS or offchip memory, so
5314 * indirect indexing is trivial.
5316 nir_variable_mode indirect_mask
= 0;
5317 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
5318 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
5319 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
5320 !llvm_has_working_vgpr_indexing
)) {
5321 indirect_mask
|= nir_var_shader_in
;
5323 if (!llvm_has_working_vgpr_indexing
&&
5324 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
5325 indirect_mask
|= nir_var_shader_out
;
5327 /* TODO: We shouldn't need to do this, however LLVM isn't currently
5328 * smart enough to handle indirects without causing excess spilling
5329 * causing the gpu to hang.
5331 * See the following thread for more details of the problem:
5332 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
5334 indirect_mask
|= nir_var_function_temp
;
5336 progress
|= nir_lower_indirect_derefs(nir
, indirect_mask
);
5341 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
5343 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
5347 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
5349 if (var
->data
.mode
!= nir_var_shader_out
)
5352 unsigned writemask
= 0;
5353 const int location
= var
->data
.location
;
5354 unsigned first_component
= var
->data
.location_frac
;
5355 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
5357 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
5358 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
5359 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
5360 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
5366 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
5367 unsigned *cond_block_tf_writemask
,
5368 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
5370 switch (cf_node
->type
) {
5371 case nir_cf_node_block
: {
5372 nir_block
*block
= nir_cf_node_as_block(cf_node
);
5373 nir_foreach_instr(instr
, block
) {
5374 if (instr
->type
!= nir_instr_type_intrinsic
)
5377 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
5378 if (intrin
->intrinsic
== nir_intrinsic_control_barrier
) {
5380 /* If we find a barrier in nested control flow put this in the
5381 * too hard basket. In GLSL this is not possible but it is in
5385 *tessfactors_are_def_in_all_invocs
= false;
5389 /* The following case must be prevented:
5390 * gl_TessLevelInner = ...;
5392 * if (gl_InvocationID == 1)
5393 * gl_TessLevelInner = ...;
5395 * If you consider disjoint code segments separated by barriers, each
5396 * such segment that writes tess factor channels should write the same
5397 * channels in all codepaths within that segment.
5399 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
5400 /* Accumulate the result: */
5401 *tessfactors_are_def_in_all_invocs
&=
5402 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
5404 /* Analyze the next code segment from scratch. */
5405 *upper_block_tf_writemask
= 0;
5406 *cond_block_tf_writemask
= 0;
5409 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
5414 case nir_cf_node_if
: {
5415 unsigned then_tessfactor_writemask
= 0;
5416 unsigned else_tessfactor_writemask
= 0;
5418 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
5419 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
5420 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
5421 cond_block_tf_writemask
,
5422 tessfactors_are_def_in_all_invocs
, true);
5425 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
5426 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
5427 cond_block_tf_writemask
,
5428 tessfactors_are_def_in_all_invocs
, true);
5431 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
5432 /* If both statements write the same tess factor channels,
5433 * we can say that the upper block writes them too.
5435 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
5436 else_tessfactor_writemask
;
5437 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
5438 else_tessfactor_writemask
;
5443 case nir_cf_node_loop
: {
5444 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
5445 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
5446 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
5447 cond_block_tf_writemask
,
5448 tessfactors_are_def_in_all_invocs
, true);
5454 unreachable("unknown cf node type");
5459 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
5461 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
5463 /* The pass works as follows:
5464 * If all codepaths write tess factors, we can say that all
5465 * invocations define tess factors.
5467 * Each tess factor channel is tracked separately.
5469 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
5470 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
5472 /* Initial value = true. Here the pass will accumulate results from
5473 * multiple segments surrounded by barriers. If tess factors aren't
5474 * written at all, it's a shader bug and we don't care if this will be
5477 bool tessfactors_are_def_in_all_invocs
= true;
5479 nir_foreach_function(function
, nir
) {
5480 if (function
->impl
) {
5481 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
5482 scan_tess_ctrl(node
, &main_block_tf_writemask
,
5483 &cond_block_tf_writemask
,
5484 &tessfactors_are_def_in_all_invocs
,
5490 /* Accumulate the result for the last code segment separated by a
5493 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
5494 tessfactors_are_def_in_all_invocs
&=
5495 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
5498 return tessfactors_are_def_in_all_invocs
;