2 * Copyright © 2016 Bas Nieuwenhuizen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
36 struct ac_nir_context
{
37 struct ac_llvm_context ac
;
38 struct ac_shader_abi
*abi
;
40 gl_shader_stage stage
;
43 LLVMValueRef
*ssa_defs
;
45 struct hash_table
*defs
;
46 struct hash_table
*phis
;
47 struct hash_table
*vars
;
49 LLVMValueRef main_function
;
50 LLVMBasicBlockRef continue_block
;
51 LLVMBasicBlockRef break_block
;
57 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
58 nir_deref_instr
*deref_instr
,
59 enum ac_descriptor_type desc_type
,
60 const nir_instr
*instr
,
61 bool image
, bool write
);
64 build_store_values_extended(struct ac_llvm_context
*ac
,
67 unsigned value_stride
,
70 LLVMBuilderRef builder
= ac
->builder
;
73 for (i
= 0; i
< value_count
; i
++) {
74 LLVMValueRef ptr
= values
[i
* value_stride
];
75 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
76 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
77 LLVMBuildStore(builder
, value
, ptr
);
81 static enum ac_image_dim
82 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
86 case GLSL_SAMPLER_DIM_1D
:
87 if (ctx
->chip_class
== GFX9
)
88 return is_array
? ac_image_2darray
: ac_image_2d
;
89 return is_array
? ac_image_1darray
: ac_image_1d
;
90 case GLSL_SAMPLER_DIM_2D
:
91 case GLSL_SAMPLER_DIM_RECT
:
92 case GLSL_SAMPLER_DIM_EXTERNAL
:
93 return is_array
? ac_image_2darray
: ac_image_2d
;
94 case GLSL_SAMPLER_DIM_3D
:
96 case GLSL_SAMPLER_DIM_CUBE
:
98 case GLSL_SAMPLER_DIM_MS
:
99 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
100 case GLSL_SAMPLER_DIM_SUBPASS
:
101 return ac_image_2darray
;
102 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
103 return ac_image_2darraymsaa
;
105 unreachable("bad sampler dim");
109 static enum ac_image_dim
110 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
113 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
115 if (dim
== ac_image_cube
||
116 (ctx
->chip_class
<= GFX8
&& dim
== ac_image_3d
))
117 dim
= ac_image_2darray
;
122 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
123 const nir_ssa_def
*def
)
125 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
126 if (def
->num_components
> 1) {
127 type
= LLVMVectorType(type
, def
->num_components
);
132 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
135 return nir
->ssa_defs
[src
.ssa
->index
];
139 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
141 LLVMValueRef ptr
= get_src(ctx
, src
);
142 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
143 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
145 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
146 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
149 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
150 const struct nir_block
*b
)
152 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
153 return (LLVMBasicBlockRef
)entry
->data
;
156 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
159 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
162 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
164 unsigned num_components
)
166 LLVMValueRef value
= get_src(ctx
, src
.src
);
167 bool need_swizzle
= false;
170 unsigned src_components
= ac_get_llvm_num_components(value
);
171 for (unsigned i
= 0; i
< num_components
; ++i
) {
172 assert(src
.swizzle
[i
] < src_components
);
173 if (src
.swizzle
[i
] != i
)
177 if (need_swizzle
|| num_components
!= src_components
) {
178 LLVMValueRef masks
[] = {
179 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
180 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
181 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
182 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
184 if (src_components
> 1 && num_components
== 1) {
185 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
187 } else if (src_components
== 1 && num_components
> 1) {
188 LLVMValueRef values
[] = {value
, value
, value
, value
};
189 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
191 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
192 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
197 LLVMTypeRef type
= LLVMTypeOf(value
);
198 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
199 type
= LLVMGetElementType(type
);
202 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
) {
203 value
= emit_iabs(&ctx
->ac
, value
);
206 unsigned fsize
= type
== ctx
->ac
.f16
? 16 :
207 type
== ctx
->ac
.f32
? 32 : 64;
209 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
210 snprintf(name
, sizeof(name
), "llvm.fabs.v%uf%u",
211 LLVMGetVectorSize(LLVMTypeOf(value
)), fsize
);
213 snprintf(name
, sizeof(name
), "llvm.fabs.f%u", fsize
);
216 value
= ac_build_intrinsic(&ctx
->ac
, name
, LLVMTypeOf(value
),
217 &value
, 1, AC_FUNC_ATTR_READNONE
);
222 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
223 value
= LLVMBuildNeg(ctx
->ac
.builder
, value
, "");
225 value
= LLVMBuildFNeg(ctx
->ac
.builder
, value
, "");
231 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
232 LLVMIntPredicate pred
, LLVMValueRef src0
,
235 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
236 return LLVMBuildSelect(ctx
->builder
, result
,
237 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
241 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
242 LLVMRealPredicate pred
, LLVMValueRef src0
,
246 src0
= ac_to_float(ctx
, src0
);
247 src1
= ac_to_float(ctx
, src1
);
248 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
249 return LLVMBuildSelect(ctx
->builder
, result
,
250 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
254 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
256 LLVMTypeRef result_type
,
260 LLVMValueRef params
[] = {
261 ac_to_float(ctx
, src0
),
264 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
265 ac_get_elem_bits(ctx
, result_type
));
266 assert(length
< sizeof(name
));
267 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
270 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
272 LLVMTypeRef result_type
,
273 LLVMValueRef src0
, LLVMValueRef src1
)
276 LLVMValueRef params
[] = {
277 ac_to_float(ctx
, src0
),
278 ac_to_float(ctx
, src1
),
281 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
282 ac_get_elem_bits(ctx
, result_type
));
283 assert(length
< sizeof(name
));
284 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
287 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
289 LLVMTypeRef result_type
,
290 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
293 LLVMValueRef params
[] = {
294 ac_to_float(ctx
, src0
),
295 ac_to_float(ctx
, src1
),
296 ac_to_float(ctx
, src2
),
299 ASSERTED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
300 ac_get_elem_bits(ctx
, result_type
));
301 assert(length
< sizeof(name
));
302 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
305 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
306 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
308 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
310 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
312 return LLVMBuildSelect(ctx
->builder
, v
,
313 ac_to_integer_or_pointer(ctx
, src1
),
314 ac_to_integer_or_pointer(ctx
, src2
), "");
317 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
319 LLVMValueRef src0
, LLVMValueRef src1
)
321 LLVMTypeRef ret_type
;
322 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
324 LLVMValueRef params
[] = { src0
, src1
};
325 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
328 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
329 params
, 2, AC_FUNC_ATTR_READNONE
);
331 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
332 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
336 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
340 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
341 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
343 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
347 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
351 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
353 unreachable("Unsupported bit size.");
357 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
360 src0
= ac_to_float(ctx
, src0
);
361 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
362 return LLVMBuildSExt(ctx
->builder
,
363 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
367 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
371 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
375 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
377 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
381 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
383 unreachable("Unsupported bit size.");
387 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
390 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
391 return LLVMBuildSExt(ctx
->builder
,
392 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
396 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
400 LLVMValueRef cond
= NULL
;
402 src0
= ac_to_float(ctx
, src0
);
403 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
405 if (ctx
->chip_class
>= GFX8
) {
406 LLVMValueRef args
[2];
407 /* Check if the result is a denormal - and flush to 0 if so. */
409 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
410 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
413 /* need to convert back up to f32 */
414 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
416 if (ctx
->chip_class
>= GFX8
)
417 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
420 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
421 * so compare the result and flush to 0 if it's smaller.
423 LLVMValueRef temp
, cond2
;
424 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
425 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
426 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
428 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
429 temp
, ctx
->f32_0
, "");
430 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
431 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
436 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
437 LLVMValueRef src0
, LLVMValueRef src1
)
439 LLVMValueRef dst64
, result
;
440 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
441 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
443 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
444 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
445 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
449 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
450 LLVMValueRef src0
, LLVMValueRef src1
)
452 LLVMValueRef dst64
, result
;
453 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
454 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
456 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
457 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
458 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
462 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
463 LLVMValueRef bits
, LLVMValueRef offset
)
465 /* mask = ((1 << bits) - 1) << offset */
466 return LLVMBuildShl(ctx
->builder
,
467 LLVMBuildSub(ctx
->builder
,
468 LLVMBuildShl(ctx
->builder
,
475 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
476 LLVMValueRef mask
, LLVMValueRef insert
,
480 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
481 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
483 return LLVMBuildXor(ctx
->builder
, base
,
484 LLVMBuildAnd(ctx
->builder
, mask
,
485 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
488 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
490 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
491 LLVMValueRef args
[2]))
493 LLVMValueRef comp
[2];
495 src0
= ac_to_float(ctx
, src0
);
496 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
497 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
499 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
502 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
505 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
506 LLVMValueRef temps
[2], val
;
509 for (i
= 0; i
< 2; i
++) {
510 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
511 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
512 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
513 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
515 return ac_build_gather_values(ctx
, temps
, 2);
518 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
526 if (op
== nir_op_fddx_fine
)
527 mask
= AC_TID_MASK_LEFT
;
528 else if (op
== nir_op_fddy_fine
)
529 mask
= AC_TID_MASK_TOP
;
531 mask
= AC_TID_MASK_TOP_LEFT
;
533 /* for DDX we want to next X pixel, DDY next Y pixel. */
534 if (op
== nir_op_fddx_fine
||
535 op
== nir_op_fddx_coarse
||
541 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
545 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
547 LLVMValueRef src
[4], result
= NULL
;
548 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
549 unsigned src_components
;
550 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
552 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
559 case nir_op_pack_half_2x16
:
560 case nir_op_pack_snorm_2x16
:
561 case nir_op_pack_unorm_2x16
:
564 case nir_op_unpack_half_2x16
:
567 case nir_op_cube_face_coord
:
568 case nir_op_cube_face_index
:
572 src_components
= num_components
;
575 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
576 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
583 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
584 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
587 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
590 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
593 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
597 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
598 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
601 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
602 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
603 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
606 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
609 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
612 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
615 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
618 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
619 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
620 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
621 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
622 ac_to_float_type(&ctx
->ac
, def_type
), result
);
623 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
624 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
627 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
628 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
629 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
632 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
635 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
638 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
641 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
642 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
643 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
646 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
647 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
650 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
653 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
656 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
659 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
660 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
661 LLVMTypeOf(src
[0]), "");
662 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
663 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
664 LLVMTypeOf(src
[0]), "");
665 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
668 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
669 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
670 LLVMTypeOf(src
[0]), "");
671 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
672 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
673 LLVMTypeOf(src
[0]), "");
674 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
677 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
678 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
679 LLVMTypeOf(src
[0]), "");
680 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
681 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
682 LLVMTypeOf(src
[0]), "");
683 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
686 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
689 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
692 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
695 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
698 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
701 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
704 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
707 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
710 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
713 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
716 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
717 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
720 result
= emit_iabs(&ctx
->ac
, src
[0]);
723 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
726 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
729 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
732 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
735 result
= ac_build_isign(&ctx
->ac
, src
[0],
736 instr
->dest
.dest
.ssa
.bit_size
);
739 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
740 result
= ac_build_fsign(&ctx
->ac
, src
[0],
741 instr
->dest
.dest
.ssa
.bit_size
);
744 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
745 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
749 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
752 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
753 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
755 case nir_op_fround_even
:
756 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
757 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
760 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
761 result
= ac_build_fract(&ctx
->ac
, src
[0],
762 instr
->dest
.dest
.ssa
.bit_size
);
765 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
766 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
769 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
773 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
774 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
777 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
781 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
782 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
785 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
786 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
787 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
789 case nir_op_frexp_exp
:
790 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
791 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
792 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
793 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
794 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
797 case nir_op_frexp_sig
:
798 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
799 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
800 instr
->dest
.dest
.ssa
.bit_size
);
803 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
804 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
807 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
808 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
809 if (ctx
->ac
.chip_class
< GFX9
&&
810 instr
->dest
.dest
.ssa
.bit_size
== 32) {
811 /* Only pre-GFX9 chips do not flush denorms. */
812 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
813 ac_to_float_type(&ctx
->ac
, def_type
),
818 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
819 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
820 if (ctx
->ac
.chip_class
< GFX9
&&
821 instr
->dest
.dest
.ssa
.bit_size
== 32) {
822 /* Only pre-GFX9 chips do not flush denorms. */
823 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
824 ac_to_float_type(&ctx
->ac
, def_type
),
829 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
830 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
833 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
834 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
835 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
836 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
837 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
839 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
842 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
844 case nir_op_bitfield_select
:
845 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
848 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
851 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
853 case nir_op_bitfield_reverse
:
854 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
856 case nir_op_bit_count
:
857 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
862 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
863 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
864 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
877 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
878 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
883 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
888 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
890 case nir_op_f2f16_rtz
:
891 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
892 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
893 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
894 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
895 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
896 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
898 case nir_op_f2f16_rtne
:
902 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
903 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
904 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
906 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
912 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
913 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
915 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
921 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
922 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
924 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
927 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
929 case nir_op_find_lsb
:
930 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
932 case nir_op_ufind_msb
:
933 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
935 case nir_op_ifind_msb
:
936 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
938 case nir_op_uadd_carry
:
939 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
941 case nir_op_usub_borrow
:
942 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
947 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
950 result
= emit_f2b(&ctx
->ac
, src
[0]);
956 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
959 result
= emit_i2b(&ctx
->ac
, src
[0]);
961 case nir_op_fquantize2f16
:
962 result
= emit_f2f16(&ctx
->ac
, src
[0]);
964 case nir_op_umul_high
:
965 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
967 case nir_op_imul_high
:
968 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
970 case nir_op_pack_half_2x16
:
971 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
973 case nir_op_pack_snorm_2x16
:
974 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
976 case nir_op_pack_unorm_2x16
:
977 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
979 case nir_op_unpack_half_2x16
:
980 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
984 case nir_op_fddx_fine
:
985 case nir_op_fddy_fine
:
986 case nir_op_fddx_coarse
:
987 case nir_op_fddy_coarse
:
988 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
991 case nir_op_unpack_64_2x32_split_x
: {
992 assert(ac_get_llvm_num_components(src
[0]) == 1);
993 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
996 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1001 case nir_op_unpack_64_2x32_split_y
: {
1002 assert(ac_get_llvm_num_components(src
[0]) == 1);
1003 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1006 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1011 case nir_op_pack_64_2x32_split
: {
1012 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1013 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1017 case nir_op_pack_32_2x16_split
: {
1018 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1019 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
1023 case nir_op_unpack_32_2x16_split_x
: {
1024 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1027 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1032 case nir_op_unpack_32_2x16_split_y
: {
1033 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1036 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1041 case nir_op_cube_face_coord
: {
1042 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1043 LLVMValueRef results
[2];
1045 for (unsigned chan
= 0; chan
< 3; chan
++)
1046 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1047 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1048 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1049 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1050 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1051 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1052 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1053 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1054 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1055 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1056 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1057 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1058 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1062 case nir_op_cube_face_index
: {
1063 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1065 for (unsigned chan
= 0; chan
< 3; chan
++)
1066 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1067 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1068 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1073 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1074 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1075 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1076 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1079 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1080 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1083 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1084 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1087 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1088 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1089 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1090 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1093 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1094 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1097 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1098 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1100 case nir_op_fmed3
: {
1101 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1102 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1103 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1104 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1105 instr
->dest
.dest
.ssa
.bit_size
);
1108 case nir_op_imed3
: {
1109 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1110 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1111 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1112 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1115 case nir_op_umed3
: {
1116 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1117 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1118 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1119 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1124 fprintf(stderr
, "Unknown NIR alu instr: ");
1125 nir_print_instr(&instr
->instr
, stderr
);
1126 fprintf(stderr
, "\n");
1131 assert(instr
->dest
.dest
.is_ssa
);
1132 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1133 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1137 static void visit_load_const(struct ac_nir_context
*ctx
,
1138 const nir_load_const_instr
*instr
)
1140 LLVMValueRef values
[4], value
= NULL
;
1141 LLVMTypeRef element_type
=
1142 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1144 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1145 switch (instr
->def
.bit_size
) {
1147 values
[i
] = LLVMConstInt(element_type
,
1148 instr
->value
[i
].u8
, false);
1151 values
[i
] = LLVMConstInt(element_type
,
1152 instr
->value
[i
].u16
, false);
1155 values
[i
] = LLVMConstInt(element_type
,
1156 instr
->value
[i
].u32
, false);
1159 values
[i
] = LLVMConstInt(element_type
,
1160 instr
->value
[i
].u64
, false);
1164 "unsupported nir load_const bit_size: %d\n",
1165 instr
->def
.bit_size
);
1169 if (instr
->def
.num_components
> 1) {
1170 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1174 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1178 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1181 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1182 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1185 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1186 /* On GFX8, the descriptor contains the size in bytes,
1187 * but TXQ must return the size in elements.
1188 * The stride is always non-zero for resources using TXQ.
1190 LLVMValueRef stride
=
1191 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1193 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1194 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1195 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1196 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1198 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1203 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1205 struct ac_image_args
*args
,
1206 const nir_tex_instr
*instr
)
1208 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1209 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1210 LLVMValueRef half_texel
[2];
1211 LLVMValueRef compare_cube_wa
= NULL
;
1212 LLVMValueRef result
;
1216 struct ac_image_args txq_args
= { 0 };
1218 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1219 txq_args
.opcode
= ac_image_get_resinfo
;
1220 txq_args
.dmask
= 0xf;
1221 txq_args
.lod
= ctx
->i32_0
;
1222 txq_args
.resource
= args
->resource
;
1223 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1224 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1226 for (unsigned c
= 0; c
< 2; c
++) {
1227 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1228 LLVMConstInt(ctx
->i32
, c
, false), "");
1229 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1230 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1231 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1232 LLVMConstReal(ctx
->f32
, -0.5), "");
1236 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1238 for (unsigned c
= 0; c
< 2; c
++) {
1240 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1241 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1245 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1246 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1247 * workaround by sampling using a scaled type and converting.
1248 * This is taken from amdgpu-pro shaders.
1250 /* NOTE this produces some ugly code compared to amdgpu-pro,
1251 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1252 * and then reads them back. -pro generates two selects,
1253 * one s_cmp for the descriptor rewriting
1254 * one v_cmp for the coordinate and result changes.
1256 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1257 LLVMValueRef tmp
, tmp2
;
1259 /* workaround 8/8/8/8 uint/sint cube gather bug */
1260 /* first detect it then change to a scaled read and f2i */
1261 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1264 /* extract the DATA_FORMAT */
1265 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1266 LLVMConstInt(ctx
->i32
, 6, false), false);
1268 /* is the DATA_FORMAT == 8_8_8_8 */
1269 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1271 if (stype
== GLSL_TYPE_UINT
)
1272 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1273 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1274 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1276 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1277 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1278 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1280 /* replace the NUM FORMAT in the descriptor */
1281 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false), "");
1282 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1284 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1286 /* don't modify the coordinates for this case */
1287 for (unsigned c
= 0; c
< 2; ++c
)
1288 args
->coords
[c
] = LLVMBuildSelect(
1289 ctx
->builder
, compare_cube_wa
,
1290 orig_coords
[c
], args
->coords
[c
], "");
1293 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1294 result
= ac_build_image_opcode(ctx
, args
);
1296 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1297 LLVMValueRef tmp
, tmp2
;
1299 /* if the cube workaround is in place, f2i the result. */
1300 for (unsigned c
= 0; c
< 4; c
++) {
1301 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1302 if (stype
== GLSL_TYPE_UINT
)
1303 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1305 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1306 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1307 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1308 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1309 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1310 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1316 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1318 nir_deref_instr
*texture_deref_instr
= NULL
;
1320 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1321 switch (instr
->src
[i
].src_type
) {
1322 case nir_tex_src_texture_deref
:
1323 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1329 return texture_deref_instr
;
1332 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1333 const nir_tex_instr
*instr
,
1334 struct ac_image_args
*args
)
1336 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1337 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1339 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1340 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1344 util_last_bit(mask
),
1347 return ac_build_buffer_load_format(&ctx
->ac
,
1351 util_last_bit(mask
),
1356 args
->opcode
= ac_image_sample
;
1358 switch (instr
->op
) {
1360 case nir_texop_txf_ms
:
1361 case nir_texop_samples_identical
:
1362 args
->opcode
= args
->level_zero
||
1363 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1364 ac_image_load
: ac_image_load_mip
;
1365 args
->level_zero
= false;
1368 case nir_texop_query_levels
:
1369 args
->opcode
= ac_image_get_resinfo
;
1371 args
->lod
= ctx
->ac
.i32_0
;
1372 args
->level_zero
= false;
1375 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1377 args
->level_zero
= true;
1381 args
->opcode
= ac_image_gather4
;
1382 args
->level_zero
= true;
1385 args
->opcode
= ac_image_get_lod
;
1391 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1392 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1393 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1394 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1395 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1396 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1397 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1401 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1402 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1403 if ((args
->dim
== ac_image_2darray
||
1404 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1405 args
->coords
[1] = ctx
->ac
.i32_0
;
1409 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1410 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1411 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1412 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1413 /* Prevent texture instructions with implicit derivatives from being
1414 * sinked into branches. */
1415 switch (instr
->op
) {
1419 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1426 return ac_build_image_opcode(&ctx
->ac
, args
);
1429 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1430 nir_intrinsic_instr
*instr
)
1432 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1433 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1435 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1436 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1440 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1441 nir_intrinsic_instr
*instr
)
1443 LLVMValueRef ptr
, addr
;
1444 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1445 unsigned index
= nir_intrinsic_base(instr
);
1447 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1448 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1450 /* Load constant values from user SGPRS when possible, otherwise
1451 * fallback to the default path that loads directly from memory.
1453 if (LLVMIsConstant(src0
) &&
1454 instr
->dest
.ssa
.bit_size
== 32) {
1455 unsigned count
= instr
->dest
.ssa
.num_components
;
1456 unsigned offset
= index
;
1458 offset
+= LLVMConstIntGetZExtValue(src0
);
1461 offset
-= ctx
->abi
->base_inline_push_consts
;
1463 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1464 return ac_build_gather_values(&ctx
->ac
,
1465 ctx
->abi
->inline_push_consts
+ offset
,
1470 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->abi
->push_constants
, &addr
, 1, "");
1472 if (instr
->dest
.ssa
.bit_size
== 8) {
1473 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1474 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1475 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1476 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1478 LLVMValueRef params
[3];
1479 if (load_dwords
> 1) {
1480 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1481 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1482 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1484 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1485 params
[0] = ctx
->ac
.i32_0
;
1489 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1491 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1492 if (instr
->dest
.ssa
.num_components
> 1)
1493 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1495 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1496 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1497 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1498 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1499 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1500 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1501 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1502 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1503 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1504 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1505 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1506 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1507 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1508 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1509 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1510 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1511 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1514 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1516 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1519 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1520 const nir_intrinsic_instr
*instr
)
1522 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1524 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1527 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1529 uint32_t new_mask
= 0;
1530 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1531 if (mask
& (1u << i
))
1532 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1536 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1537 unsigned start
, unsigned count
)
1539 LLVMValueRef mask
[] = {
1540 ctx
->i32_0
, ctx
->i32_1
,
1541 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1543 unsigned src_elements
= ac_get_llvm_num_components(src
);
1545 if (count
== src_elements
) {
1548 } else if (count
== 1) {
1549 assert(start
< src_elements
);
1550 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1552 assert(start
+ count
<= src_elements
);
1554 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1555 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1559 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1560 enum gl_access_qualifier access
,
1561 bool may_store_unaligned
,
1562 bool writeonly_memory
)
1564 unsigned cache_policy
= 0;
1566 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1567 * store opcodes not aligned to a dword are affected. The only way to
1568 * get unaligned stores is through shader images.
1570 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1571 /* If this is write-only, don't keep data in L1 to prevent
1572 * evicting L1 cache lines that may be needed by other
1576 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1577 cache_policy
|= ac_glc
;
1580 return cache_policy
;
1583 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1584 nir_intrinsic_instr
*instr
)
1586 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1587 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1588 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1589 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1590 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1591 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1593 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1594 get_src(ctx
, instr
->src
[1]), true);
1595 LLVMValueRef base_data
= src_data
;
1596 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1597 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1601 LLVMValueRef data
, offset
;
1602 LLVMTypeRef data_type
;
1604 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1606 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1607 * writes into a 2-element and a 1-element write. */
1609 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1610 writemask
|= 1 << (start
+ 2);
1613 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1615 /* we can only store 4 DWords at the same time.
1616 * can only happen for 64 Bit vectors. */
1617 if (num_bytes
> 16) {
1618 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1623 /* check alignment of 16 Bit stores */
1624 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1625 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1629 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1631 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1632 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1634 if (num_bytes
== 1) {
1635 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1636 offset
, ctx
->ac
.i32_0
,
1638 } else if (num_bytes
== 2) {
1639 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1640 offset
, ctx
->ac
.i32_0
,
1643 int num_channels
= num_bytes
/ 4;
1645 switch (num_bytes
) {
1646 case 16: /* v4f32 */
1647 data_type
= ctx
->ac
.v4f32
;
1649 case 12: /* v3f32 */
1650 data_type
= ctx
->ac
.v3f32
;
1653 data_type
= ctx
->ac
.v2f32
;
1656 data_type
= ctx
->ac
.f32
;
1659 unreachable("Malformed vector store.");
1661 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1663 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1664 num_channels
, offset
,
1666 cache_policy
, false);
1671 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1672 const nir_intrinsic_instr
*instr
)
1674 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1676 char name
[64], type
[8];
1677 LLVMValueRef params
[6];
1680 switch (instr
->intrinsic
) {
1681 case nir_intrinsic_ssbo_atomic_add
:
1684 case nir_intrinsic_ssbo_atomic_imin
:
1687 case nir_intrinsic_ssbo_atomic_umin
:
1690 case nir_intrinsic_ssbo_atomic_imax
:
1693 case nir_intrinsic_ssbo_atomic_umax
:
1696 case nir_intrinsic_ssbo_atomic_and
:
1699 case nir_intrinsic_ssbo_atomic_or
:
1702 case nir_intrinsic_ssbo_atomic_xor
:
1705 case nir_intrinsic_ssbo_atomic_exchange
:
1708 case nir_intrinsic_ssbo_atomic_comp_swap
:
1715 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1716 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1718 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1719 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1720 get_src(ctx
, instr
->src
[0]),
1723 if (HAVE_LLVM
>= 0x900) {
1724 /* XXX: The new raw/struct atomic intrinsics are buggy with
1725 * LLVM 8, see r358579.
1727 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1728 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1729 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1731 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1732 snprintf(name
, sizeof(name
),
1733 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1735 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1736 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1737 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1739 assert(return_type
== ctx
->ac
.i32
);
1740 snprintf(name
, sizeof(name
),
1741 "llvm.amdgcn.buffer.atomic.%s", op
);
1744 return ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1748 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1749 const nir_intrinsic_instr
*instr
)
1751 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1752 int num_components
= instr
->num_components
;
1753 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1754 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1756 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1757 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1758 get_src(ctx
, instr
->src
[0]), false);
1759 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1761 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1762 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1764 LLVMValueRef results
[4];
1765 for (int i
= 0; i
< num_components
;) {
1766 int num_elems
= num_components
- i
;
1767 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1769 if (num_elems
* elem_size_bytes
> 16)
1770 num_elems
= 16 / elem_size_bytes
;
1771 int load_bytes
= num_elems
* elem_size_bytes
;
1773 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1777 if (load_bytes
== 1) {
1778 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1784 } else if (load_bytes
== 2) {
1785 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1792 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1793 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
1795 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1796 vindex
, offset
, immoffset
, 0,
1797 cache_policy
, can_speculate
, false);
1800 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1801 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1802 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1804 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1805 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1807 for (unsigned j
= 0; j
< num_elems
; j
++) {
1808 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1813 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1816 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1817 const nir_intrinsic_instr
*instr
)
1820 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1821 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1822 int num_components
= instr
->num_components
;
1824 if (ctx
->abi
->load_ubo
)
1825 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1827 if (instr
->dest
.ssa
.bit_size
== 64)
1828 num_components
*= 2;
1830 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1831 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1832 LLVMValueRef results
[num_components
];
1833 for (unsigned i
= 0; i
< num_components
; ++i
) {
1834 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1837 if (load_bytes
== 1) {
1838 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1845 assert(load_bytes
== 2);
1846 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1854 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1856 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1857 NULL
, 0, 0, true, true);
1859 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1862 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1863 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1867 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1868 bool vs_in
, unsigned *vertex_index_out
,
1869 LLVMValueRef
*vertex_index_ref
,
1870 unsigned *const_out
, LLVMValueRef
*indir_out
)
1872 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1873 nir_deref_path path
;
1874 unsigned idx_lvl
= 1;
1876 nir_deref_path_init(&path
, instr
, NULL
);
1878 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1879 if (vertex_index_ref
) {
1880 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1881 if (vertex_index_out
)
1882 *vertex_index_out
= 0;
1884 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1889 uint32_t const_offset
= 0;
1890 LLVMValueRef offset
= NULL
;
1892 if (var
->data
.compact
) {
1893 assert(instr
->deref_type
== nir_deref_type_array
);
1894 const_offset
= nir_src_as_uint(instr
->arr
.index
);
1898 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1899 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1900 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1901 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1903 for (unsigned i
= 0; i
< index
; i
++) {
1904 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1905 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1907 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1908 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1909 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1910 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1912 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1916 unreachable("Uhandled deref type in get_deref_instr_offset");
1920 nir_deref_path_finish(&path
);
1922 if (const_offset
&& offset
)
1923 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1924 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1927 *const_out
= const_offset
;
1928 *indir_out
= offset
;
1931 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1932 nir_intrinsic_instr
*instr
,
1935 LLVMValueRef result
;
1936 LLVMValueRef vertex_index
= NULL
;
1937 LLVMValueRef indir_index
= NULL
;
1938 unsigned const_index
= 0;
1940 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1942 unsigned location
= var
->data
.location
;
1943 unsigned driver_location
= var
->data
.driver_location
;
1944 const bool is_patch
= var
->data
.patch
;
1945 const bool is_compact
= var
->data
.compact
;
1947 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1948 false, NULL
, is_patch
? NULL
: &vertex_index
,
1949 &const_index
, &indir_index
);
1951 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1953 LLVMTypeRef src_component_type
;
1954 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1955 src_component_type
= LLVMGetElementType(dest_type
);
1957 src_component_type
= dest_type
;
1959 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1960 vertex_index
, indir_index
,
1961 const_index
, location
, driver_location
,
1962 var
->data
.location_frac
,
1963 instr
->num_components
,
1964 is_patch
, is_compact
, load_inputs
);
1965 if (instr
->dest
.ssa
.bit_size
== 16) {
1966 result
= ac_to_integer(&ctx
->ac
, result
);
1967 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1969 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1973 type_scalar_size_bytes(const struct glsl_type
*type
)
1975 assert(glsl_type_is_vector_or_scalar(type
) ||
1976 glsl_type_is_matrix(type
));
1977 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1980 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1981 nir_intrinsic_instr
*instr
)
1983 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1984 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1986 LLVMValueRef values
[8];
1988 int ve
= instr
->dest
.ssa
.num_components
;
1990 LLVMValueRef indir_index
;
1992 unsigned const_index
;
1993 unsigned stride
= 4;
1994 int mode
= deref
->mode
;
1997 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1998 var
->data
.mode
== nir_var_shader_in
;
1999 idx
= var
->data
.driver_location
;
2000 comp
= var
->data
.location_frac
;
2001 mode
= var
->data
.mode
;
2003 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
2004 &const_index
, &indir_index
);
2006 if (var
->data
.compact
) {
2008 const_index
+= comp
;
2013 if (instr
->dest
.ssa
.bit_size
== 64 &&
2014 (deref
->mode
== nir_var_shader_in
||
2015 deref
->mode
== nir_var_shader_out
||
2016 deref
->mode
== nir_var_function_temp
))
2020 case nir_var_shader_in
:
2021 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
2022 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2023 return load_tess_varyings(ctx
, instr
, true);
2026 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
2027 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
2028 LLVMValueRef indir_index
;
2029 unsigned const_index
, vertex_index
;
2030 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2031 &const_index
, &indir_index
);
2033 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2034 var
->data
.driver_location
,
2035 var
->data
.location_frac
,
2036 instr
->num_components
, vertex_index
, const_index
, type
);
2039 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2041 unsigned count
= glsl_count_attribute_slots(
2043 ctx
->stage
== MESA_SHADER_VERTEX
);
2045 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2046 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2047 stride
, false, true);
2049 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2053 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2056 case nir_var_function_temp
:
2057 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2059 unsigned count
= glsl_count_attribute_slots(
2062 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2063 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2064 stride
, true, true);
2066 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2070 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2074 case nir_var_mem_shared
: {
2075 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2076 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2077 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2078 get_def_type(ctx
, &instr
->dest
.ssa
),
2081 case nir_var_shader_out
:
2082 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2083 return load_tess_varyings(ctx
, instr
, false);
2086 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2087 var
->data
.fb_fetch_output
&&
2088 ctx
->abi
->emit_fbfetch
)
2089 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2091 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2093 unsigned count
= glsl_count_attribute_slots(
2096 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2097 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2098 stride
, true, true);
2100 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2104 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2105 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2110 case nir_var_mem_global
: {
2111 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2112 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2113 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2114 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2116 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2117 if (stride
!= natural_stride
) {
2118 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2119 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2120 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2122 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2123 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2124 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2125 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2127 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2129 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2130 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2131 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2132 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2137 unreachable("unhandle variable mode");
2139 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2140 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2144 visit_store_var(struct ac_nir_context
*ctx
,
2145 nir_intrinsic_instr
*instr
)
2147 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2148 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2150 LLVMValueRef temp_ptr
, value
;
2153 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2154 int writemask
= instr
->const_index
[0];
2155 LLVMValueRef indir_index
;
2156 unsigned const_index
;
2159 get_deref_offset(ctx
, deref
, false,
2160 NULL
, NULL
, &const_index
, &indir_index
);
2161 idx
= var
->data
.driver_location
;
2162 comp
= var
->data
.location_frac
;
2164 if (var
->data
.compact
) {
2165 const_index
+= comp
;
2170 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2171 (deref
->mode
== nir_var_shader_out
||
2172 deref
->mode
== nir_var_function_temp
)) {
2174 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2175 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2178 writemask
= widen_mask(writemask
, 2);
2181 writemask
= writemask
<< comp
;
2183 switch (deref
->mode
) {
2184 case nir_var_shader_out
:
2186 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2187 LLVMValueRef vertex_index
= NULL
;
2188 LLVMValueRef indir_index
= NULL
;
2189 unsigned const_index
= 0;
2190 const bool is_patch
= var
->data
.patch
;
2192 get_deref_offset(ctx
, deref
, false, NULL
,
2193 is_patch
? NULL
: &vertex_index
,
2194 &const_index
, &indir_index
);
2196 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2197 vertex_index
, indir_index
,
2198 const_index
, src
, writemask
);
2202 for (unsigned chan
= 0; chan
< 8; chan
++) {
2204 if (!(writemask
& (1 << chan
)))
2207 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2209 if (var
->data
.compact
)
2212 unsigned count
= glsl_count_attribute_slots(
2215 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2216 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2217 stride
, true, true);
2219 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2220 value
, indir_index
, "");
2221 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2222 count
, stride
, tmp_vec
);
2225 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2227 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2231 case nir_var_function_temp
:
2232 for (unsigned chan
= 0; chan
< 8; chan
++) {
2233 if (!(writemask
& (1 << chan
)))
2236 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2238 unsigned count
= glsl_count_attribute_slots(
2241 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2242 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2245 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2246 value
, indir_index
, "");
2247 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2250 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2252 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2257 case nir_var_mem_global
:
2258 case nir_var_mem_shared
: {
2259 int writemask
= instr
->const_index
[0];
2260 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2261 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2263 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2264 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2265 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2267 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2268 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2269 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2271 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2272 stride
== natural_stride
) {
2273 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2274 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2275 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2277 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2278 LLVMGetElementType(LLVMTypeOf(address
)), "");
2279 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2281 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2282 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2283 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2284 for (unsigned chan
= 0; chan
< 4; chan
++) {
2285 if (!(writemask
& (1 << chan
)))
2288 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2290 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2291 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2293 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2294 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2295 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2306 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2309 case GLSL_SAMPLER_DIM_BUF
:
2311 case GLSL_SAMPLER_DIM_1D
:
2312 return array
? 2 : 1;
2313 case GLSL_SAMPLER_DIM_2D
:
2314 return array
? 3 : 2;
2315 case GLSL_SAMPLER_DIM_MS
:
2316 return array
? 4 : 3;
2317 case GLSL_SAMPLER_DIM_3D
:
2318 case GLSL_SAMPLER_DIM_CUBE
:
2320 case GLSL_SAMPLER_DIM_RECT
:
2321 case GLSL_SAMPLER_DIM_SUBPASS
:
2323 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2331 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2332 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2333 LLVMValueRef coord_z
,
2334 LLVMValueRef sample_index
,
2335 LLVMValueRef fmask_desc_ptr
)
2337 unsigned sample_chan
= coord_z
? 3 : 2;
2338 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2339 addr
[sample_chan
] = sample_index
;
2341 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2342 return addr
[sample_chan
];
2345 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2347 assert(instr
->src
[0].is_ssa
);
2348 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2351 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2352 const nir_intrinsic_instr
*instr
,
2353 enum ac_descriptor_type desc_type
,
2356 nir_deref_instr
*deref_instr
=
2357 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2358 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2360 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2363 static void get_image_coords(struct ac_nir_context
*ctx
,
2364 const nir_intrinsic_instr
*instr
,
2365 struct ac_image_args
*args
,
2366 enum glsl_sampler_dim dim
,
2369 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2370 LLVMValueRef masks
[] = {
2371 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2372 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2374 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2377 ASSERTED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2378 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2379 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2380 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2381 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2382 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2383 count
= image_type_to_components_count(dim
, is_array
);
2385 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2386 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2387 LLVMValueRef fmask_load_address
[3];
2389 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2390 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2392 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2394 fmask_load_address
[2] = NULL
;
2396 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2397 fmask_load_address
[0],
2398 fmask_load_address
[1],
2399 fmask_load_address
[2],
2401 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2402 AC_DESC_FMASK
, &instr
->instr
, false, false));
2404 if (count
== 1 && !gfx9_1d
) {
2405 if (instr
->src
[1].ssa
->num_components
)
2406 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2408 args
->coords
[0] = src0
;
2413 for (chan
= 0; chan
< count
; ++chan
) {
2414 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2419 args
->coords
[2] = args
->coords
[1];
2420 args
->coords
[1] = ctx
->ac
.i32_0
;
2422 args
->coords
[1] = ctx
->ac
.i32_0
;
2427 args
->coords
[count
] = sample_index
;
2433 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2434 const nir_intrinsic_instr
*instr
,
2435 bool write
, bool atomic
)
2437 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2438 if (ctx
->abi
->gfx9_stride_size_workaround
||
2439 (ctx
->abi
->gfx9_stride_size_workaround_for_atomic
&& atomic
)) {
2440 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2441 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2442 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2444 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2445 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2446 elem_count
, stride
, "");
2448 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2449 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2454 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2455 const nir_intrinsic_instr
*instr
,
2460 enum glsl_sampler_dim dim
;
2461 enum gl_access_qualifier access
;
2464 dim
= nir_intrinsic_image_dim(instr
);
2465 access
= nir_intrinsic_access(instr
);
2466 is_array
= nir_intrinsic_image_array(instr
);
2468 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2469 const struct glsl_type
*type
= image_deref
->type
;
2470 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2471 dim
= glsl_get_sampler_dim(type
);
2472 access
= var
->data
.image
.access
;
2473 is_array
= glsl_sampler_type_is_array(type
);
2476 struct ac_image_args args
= {};
2478 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2480 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2481 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2482 unsigned num_channels
= util_last_bit(mask
);
2483 LLVMValueRef rsrc
, vindex
;
2485 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false, false);
2486 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2489 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2490 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2491 ctx
->ac
.i32_0
, num_channels
,
2494 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2496 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2497 res
= ac_to_integer(&ctx
->ac
, res
);
2499 args
.opcode
= ac_image_load
;
2500 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2501 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2502 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2504 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2506 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2511 static void visit_image_store(struct ac_nir_context
*ctx
,
2512 nir_intrinsic_instr
*instr
,
2517 enum glsl_sampler_dim dim
;
2518 enum gl_access_qualifier access
;
2521 dim
= nir_intrinsic_image_dim(instr
);
2522 access
= nir_intrinsic_access(instr
);
2523 is_array
= nir_intrinsic_image_array(instr
);
2525 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2526 const struct glsl_type
*type
= image_deref
->type
;
2527 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2528 dim
= glsl_get_sampler_dim(type
);
2529 access
= var
->data
.image
.access
;
2530 is_array
= glsl_sampler_type_is_array(type
);
2533 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2534 struct ac_image_args args
= {};
2536 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2538 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2539 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true, false);
2540 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2541 unsigned src_channels
= ac_get_llvm_num_components(src
);
2542 LLVMValueRef vindex
;
2544 if (src_channels
== 3)
2545 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2547 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2548 get_src(ctx
, instr
->src
[1]),
2551 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2552 ctx
->ac
.i32_0
, src_channels
,
2555 args
.opcode
= ac_image_store
;
2556 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2557 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2558 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2559 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2562 ac_build_image_opcode(&ctx
->ac
, &args
);
2567 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2568 const nir_intrinsic_instr
*instr
,
2571 LLVMValueRef params
[7];
2572 int param_count
= 0;
2574 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2575 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2576 const char *atomic_name
;
2577 char intrinsic_name
[64];
2578 enum ac_atomic_op atomic_subop
;
2579 ASSERTED
int length
;
2581 enum glsl_sampler_dim dim
;
2582 bool is_unsigned
= false;
2585 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_min
||
2586 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_max
) {
2587 const GLenum format
= nir_intrinsic_format(instr
);
2588 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2589 is_unsigned
= format
== GL_R32UI
;
2591 dim
= nir_intrinsic_image_dim(instr
);
2592 is_array
= nir_intrinsic_image_array(instr
);
2594 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2595 is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2596 dim
= glsl_get_sampler_dim(type
);
2597 is_array
= glsl_sampler_type_is_array(type
);
2600 switch (instr
->intrinsic
) {
2601 case nir_intrinsic_bindless_image_atomic_add
:
2602 case nir_intrinsic_image_deref_atomic_add
:
2603 atomic_name
= "add";
2604 atomic_subop
= ac_atomic_add
;
2606 case nir_intrinsic_bindless_image_atomic_min
:
2607 case nir_intrinsic_image_deref_atomic_min
:
2608 atomic_name
= is_unsigned
? "umin" : "smin";
2609 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2611 case nir_intrinsic_bindless_image_atomic_max
:
2612 case nir_intrinsic_image_deref_atomic_max
:
2613 atomic_name
= is_unsigned
? "umax" : "smax";
2614 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2616 case nir_intrinsic_bindless_image_atomic_and
:
2617 case nir_intrinsic_image_deref_atomic_and
:
2618 atomic_name
= "and";
2619 atomic_subop
= ac_atomic_and
;
2621 case nir_intrinsic_bindless_image_atomic_or
:
2622 case nir_intrinsic_image_deref_atomic_or
:
2624 atomic_subop
= ac_atomic_or
;
2626 case nir_intrinsic_bindless_image_atomic_xor
:
2627 case nir_intrinsic_image_deref_atomic_xor
:
2628 atomic_name
= "xor";
2629 atomic_subop
= ac_atomic_xor
;
2631 case nir_intrinsic_bindless_image_atomic_exchange
:
2632 case nir_intrinsic_image_deref_atomic_exchange
:
2633 atomic_name
= "swap";
2634 atomic_subop
= ac_atomic_swap
;
2636 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2637 case nir_intrinsic_image_deref_atomic_comp_swap
:
2638 atomic_name
= "cmpswap";
2639 atomic_subop
= 0; /* not used */
2646 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2647 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2649 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2650 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true, true);
2651 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2652 ctx
->ac
.i32_0
, ""); /* vindex */
2653 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2654 if (HAVE_LLVM
>= 0x900) {
2655 /* XXX: The new raw/struct atomic intrinsics are buggy
2656 * with LLVM 8, see r358579.
2658 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2659 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2661 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2662 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2664 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2666 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2667 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2670 assert(length
< sizeof(intrinsic_name
));
2671 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2672 params
, param_count
, 0);
2674 struct ac_image_args args
= {};
2675 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2676 args
.atomic
= atomic_subop
;
2677 args
.data
[0] = params
[0];
2679 args
.data
[1] = params
[1];
2680 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2681 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2682 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2684 return ac_build_image_opcode(&ctx
->ac
, &args
);
2688 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2689 const nir_intrinsic_instr
*instr
,
2692 enum glsl_sampler_dim dim
;
2695 dim
= nir_intrinsic_image_dim(instr
);
2696 is_array
= nir_intrinsic_image_array(instr
);
2698 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2699 dim
= glsl_get_sampler_dim(type
);
2700 is_array
= glsl_sampler_type_is_array(type
);
2703 struct ac_image_args args
= { 0 };
2704 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, dim
, is_array
);
2706 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2707 args
.opcode
= ac_image_get_resinfo
;
2708 args
.lod
= ctx
->ac
.i32_0
;
2709 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2711 return ac_build_image_opcode(&ctx
->ac
, &args
);
2714 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2715 const nir_intrinsic_instr
*instr
,
2720 enum glsl_sampler_dim dim
;
2723 dim
= nir_intrinsic_image_dim(instr
);
2724 is_array
= nir_intrinsic_image_array(instr
);
2726 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2727 dim
= glsl_get_sampler_dim(type
);
2728 is_array
= glsl_sampler_type_is_array(type
);
2731 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2732 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2734 struct ac_image_args args
= { 0 };
2736 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2738 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2739 args
.opcode
= ac_image_get_resinfo
;
2740 args
.lod
= ctx
->ac
.i32_0
;
2741 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2743 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2745 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2747 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2748 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2749 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2750 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2751 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2753 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2754 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2755 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2762 static void emit_membar(struct ac_llvm_context
*ac
,
2763 const nir_intrinsic_instr
*instr
)
2765 unsigned wait_flags
= 0;
2767 switch (instr
->intrinsic
) {
2768 case nir_intrinsic_memory_barrier
:
2769 case nir_intrinsic_group_memory_barrier
:
2770 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2772 case nir_intrinsic_memory_barrier_atomic_counter
:
2773 case nir_intrinsic_memory_barrier_buffer
:
2774 case nir_intrinsic_memory_barrier_image
:
2775 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2777 case nir_intrinsic_memory_barrier_shared
:
2778 wait_flags
= AC_WAIT_LGKM
;
2784 ac_build_waitcnt(ac
, wait_flags
);
2787 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2789 /* GFX6 only (thanks to a hw bug workaround):
2790 * The real barrier instruction isn’t needed, because an entire patch
2791 * always fits into a single wave.
2793 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
2794 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
2797 ac_build_s_barrier(ac
);
2800 static void emit_discard(struct ac_nir_context
*ctx
,
2801 const nir_intrinsic_instr
*instr
)
2805 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2806 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2807 get_src(ctx
, instr
->src
[0]),
2810 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2811 cond
= ctx
->ac
.i1false
;
2814 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2818 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2820 LLVMValueRef result
;
2821 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2822 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2823 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2825 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2829 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2831 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2832 LLVMValueRef result
;
2833 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2834 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2835 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2837 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2842 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2844 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2845 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2846 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2848 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2853 visit_first_invocation(struct ac_nir_context
*ctx
)
2855 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2856 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
2858 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2859 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2860 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
2861 ctx
->ac
.iN_wavemask
, args
, 2,
2862 AC_FUNC_ATTR_NOUNWIND
|
2863 AC_FUNC_ATTR_READNONE
);
2865 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2869 visit_load_shared(struct ac_nir_context
*ctx
,
2870 const nir_intrinsic_instr
*instr
)
2872 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2874 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2876 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2877 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2878 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2879 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2882 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2883 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2887 visit_store_shared(struct ac_nir_context
*ctx
,
2888 const nir_intrinsic_instr
*instr
)
2890 LLVMValueRef derived_ptr
, data
,index
;
2891 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2893 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2894 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2896 int writemask
= nir_intrinsic_write_mask(instr
);
2897 for (int chan
= 0; chan
< 4; chan
++) {
2898 if (!(writemask
& (1 << chan
))) {
2901 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2902 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2903 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2904 LLVMBuildStore(builder
, data
, derived_ptr
);
2908 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2909 const nir_intrinsic_instr
*instr
,
2910 LLVMValueRef ptr
, int src_idx
)
2912 LLVMValueRef result
;
2913 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2915 const char *sync_scope
= HAVE_LLVM
>= 0x0900 ? "workgroup-one-as" : "workgroup";
2917 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2918 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2919 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2920 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
2921 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2923 LLVMAtomicRMWBinOp op
;
2924 switch (instr
->intrinsic
) {
2925 case nir_intrinsic_shared_atomic_add
:
2926 case nir_intrinsic_deref_atomic_add
:
2927 op
= LLVMAtomicRMWBinOpAdd
;
2929 case nir_intrinsic_shared_atomic_umin
:
2930 case nir_intrinsic_deref_atomic_umin
:
2931 op
= LLVMAtomicRMWBinOpUMin
;
2933 case nir_intrinsic_shared_atomic_umax
:
2934 case nir_intrinsic_deref_atomic_umax
:
2935 op
= LLVMAtomicRMWBinOpUMax
;
2937 case nir_intrinsic_shared_atomic_imin
:
2938 case nir_intrinsic_deref_atomic_imin
:
2939 op
= LLVMAtomicRMWBinOpMin
;
2941 case nir_intrinsic_shared_atomic_imax
:
2942 case nir_intrinsic_deref_atomic_imax
:
2943 op
= LLVMAtomicRMWBinOpMax
;
2945 case nir_intrinsic_shared_atomic_and
:
2946 case nir_intrinsic_deref_atomic_and
:
2947 op
= LLVMAtomicRMWBinOpAnd
;
2949 case nir_intrinsic_shared_atomic_or
:
2950 case nir_intrinsic_deref_atomic_or
:
2951 op
= LLVMAtomicRMWBinOpOr
;
2953 case nir_intrinsic_shared_atomic_xor
:
2954 case nir_intrinsic_deref_atomic_xor
:
2955 op
= LLVMAtomicRMWBinOpXor
;
2957 case nir_intrinsic_shared_atomic_exchange
:
2958 case nir_intrinsic_deref_atomic_exchange
:
2959 op
= LLVMAtomicRMWBinOpXchg
;
2965 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
2970 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2972 LLVMValueRef values
[2];
2973 LLVMValueRef pos
[2];
2975 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2976 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2978 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2979 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2980 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2983 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
2986 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTER
);
2987 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
2990 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
2992 LLVMValueRef offset
)
2994 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTER
);
2995 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
2996 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
2998 LLVMValueRef ij_out
[2];
2999 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
3002 * take the I then J parameters, and the DDX/Y for it, and
3003 * calculate the IJ inputs for the interpolator.
3004 * temp1 = ddx * offset/sample.x + I;
3005 * interp_param.I = ddy * offset/sample.y + temp1;
3006 * temp1 = ddx * offset/sample.x + J;
3007 * interp_param.J = ddy * offset/sample.y + temp1;
3009 for (unsigned i
= 0; i
< 2; i
++) {
3010 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
3011 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
3012 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3013 ddxy_out
, ix_ll
, "");
3014 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3015 ddxy_out
, iy_ll
, "");
3016 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3017 interp_param
, ix_ll
, "");
3018 LLVMValueRef temp1
, temp2
;
3020 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3023 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3024 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3026 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3027 temp2
, ctx
->ac
.i32
, "");
3029 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3030 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3033 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3036 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTROID
);
3037 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3040 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3042 LLVMValueRef sample_id
)
3044 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3046 /* fetch sample ID */
3047 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3049 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3050 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3051 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3052 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3053 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3054 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3056 return barycentric_offset(ctx
, mode
, offset
);
3060 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3063 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_SAMPLE
);
3064 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3067 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3068 LLVMValueRef interp_param
,
3069 unsigned index
, unsigned comp_start
,
3070 unsigned num_components
,
3073 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3075 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3076 interp_param
, ctx
->ac
.v2f32
, "");
3077 LLVMValueRef i
= LLVMBuildExtractElement(
3078 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3079 LLVMValueRef j
= LLVMBuildExtractElement(
3080 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3082 LLVMValueRef values
[4];
3083 assert(bitsize
== 16 || bitsize
== 32);
3084 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3085 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3086 if (bitsize
== 16) {
3087 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3088 ctx
->abi
->prim_mask
, i
, j
);
3090 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3091 ctx
->abi
->prim_mask
, i
, j
);
3095 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3098 static LLVMValueRef
load_flat_input(struct ac_nir_context
*ctx
,
3099 unsigned index
, unsigned comp_start
,
3100 unsigned num_components
,
3103 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3105 LLVMValueRef values
[8];
3107 /* Each component of a 64-bit value takes up two GL-level channels. */
3109 bit_size
== 64 ? num_components
* 2 : num_components
;
3111 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3112 if (comp_start
+ chan
> 4)
3113 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3114 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (comp_start
+ chan
) % 4, false);
3115 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3116 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3119 ctx
->abi
->prim_mask
);
3120 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3121 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3122 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3125 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3126 if (bit_size
== 64) {
3127 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3128 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3129 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3134 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3135 nir_intrinsic_instr
*instr
)
3137 LLVMValueRef result
= NULL
;
3139 switch (instr
->intrinsic
) {
3140 case nir_intrinsic_ballot
:
3141 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3143 case nir_intrinsic_read_invocation
:
3144 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3145 get_src(ctx
, instr
->src
[1]));
3147 case nir_intrinsic_read_first_invocation
:
3148 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3150 case nir_intrinsic_load_subgroup_invocation
:
3151 result
= ac_get_thread_id(&ctx
->ac
);
3153 case nir_intrinsic_load_work_group_id
: {
3154 LLVMValueRef values
[3];
3156 for (int i
= 0; i
< 3; i
++) {
3157 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3158 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3161 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3164 case nir_intrinsic_load_base_vertex
:
3165 case nir_intrinsic_load_first_vertex
:
3166 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3168 case nir_intrinsic_load_local_group_size
:
3169 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3171 case nir_intrinsic_load_vertex_id
:
3172 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3173 ctx
->abi
->base_vertex
, "");
3175 case nir_intrinsic_load_vertex_id_zero_base
: {
3176 result
= ctx
->abi
->vertex_id
;
3179 case nir_intrinsic_load_local_invocation_id
: {
3180 result
= ctx
->abi
->local_invocation_ids
;
3183 case nir_intrinsic_load_base_instance
:
3184 result
= ctx
->abi
->start_instance
;
3186 case nir_intrinsic_load_draw_id
:
3187 result
= ctx
->abi
->draw_id
;
3189 case nir_intrinsic_load_view_index
:
3190 result
= ctx
->abi
->view_index
;
3192 case nir_intrinsic_load_invocation_id
:
3193 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3194 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3196 if (ctx
->ac
.chip_class
>= GFX10
) {
3197 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3198 ctx
->abi
->gs_invocation_id
,
3199 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3201 result
= ctx
->abi
->gs_invocation_id
;
3205 case nir_intrinsic_load_primitive_id
:
3206 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3207 result
= ctx
->abi
->gs_prim_id
;
3208 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3209 result
= ctx
->abi
->tcs_patch_id
;
3210 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3211 result
= ctx
->abi
->tes_patch_id
;
3213 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3215 case nir_intrinsic_load_sample_id
:
3216 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3218 case nir_intrinsic_load_sample_pos
:
3219 result
= load_sample_pos(ctx
);
3221 case nir_intrinsic_load_sample_mask_in
:
3222 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3224 case nir_intrinsic_load_frag_coord
: {
3225 LLVMValueRef values
[4] = {
3226 ctx
->abi
->frag_pos
[0],
3227 ctx
->abi
->frag_pos
[1],
3228 ctx
->abi
->frag_pos
[2],
3229 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3231 result
= ac_to_integer(&ctx
->ac
,
3232 ac_build_gather_values(&ctx
->ac
, values
, 4));
3235 case nir_intrinsic_load_layer_id
:
3236 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3238 case nir_intrinsic_load_front_face
:
3239 result
= ctx
->abi
->front_face
;
3241 case nir_intrinsic_load_helper_invocation
:
3242 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3244 case nir_intrinsic_load_color0
:
3245 result
= ctx
->abi
->color0
;
3247 case nir_intrinsic_load_color1
:
3248 result
= ctx
->abi
->color1
;
3250 case nir_intrinsic_load_instance_id
:
3251 result
= ctx
->abi
->instance_id
;
3253 case nir_intrinsic_load_num_work_groups
:
3254 result
= ctx
->abi
->num_work_groups
;
3256 case nir_intrinsic_load_local_invocation_index
:
3257 result
= visit_load_local_invocation_index(ctx
);
3259 case nir_intrinsic_load_subgroup_id
:
3260 result
= visit_load_subgroup_id(ctx
);
3262 case nir_intrinsic_load_num_subgroups
:
3263 result
= visit_load_num_subgroups(ctx
);
3265 case nir_intrinsic_first_invocation
:
3266 result
= visit_first_invocation(ctx
);
3268 case nir_intrinsic_load_push_constant
:
3269 result
= visit_load_push_constant(ctx
, instr
);
3271 case nir_intrinsic_vulkan_resource_index
: {
3272 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3273 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3274 unsigned binding
= nir_intrinsic_binding(instr
);
3276 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3280 case nir_intrinsic_vulkan_resource_reindex
:
3281 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3283 case nir_intrinsic_store_ssbo
:
3284 visit_store_ssbo(ctx
, instr
);
3286 case nir_intrinsic_load_ssbo
:
3287 result
= visit_load_buffer(ctx
, instr
);
3289 case nir_intrinsic_ssbo_atomic_add
:
3290 case nir_intrinsic_ssbo_atomic_imin
:
3291 case nir_intrinsic_ssbo_atomic_umin
:
3292 case nir_intrinsic_ssbo_atomic_imax
:
3293 case nir_intrinsic_ssbo_atomic_umax
:
3294 case nir_intrinsic_ssbo_atomic_and
:
3295 case nir_intrinsic_ssbo_atomic_or
:
3296 case nir_intrinsic_ssbo_atomic_xor
:
3297 case nir_intrinsic_ssbo_atomic_exchange
:
3298 case nir_intrinsic_ssbo_atomic_comp_swap
:
3299 result
= visit_atomic_ssbo(ctx
, instr
);
3301 case nir_intrinsic_load_ubo
:
3302 result
= visit_load_ubo_buffer(ctx
, instr
);
3304 case nir_intrinsic_get_buffer_size
:
3305 result
= visit_get_buffer_size(ctx
, instr
);
3307 case nir_intrinsic_load_deref
:
3308 result
= visit_load_var(ctx
, instr
);
3310 case nir_intrinsic_store_deref
:
3311 visit_store_var(ctx
, instr
);
3313 case nir_intrinsic_load_shared
:
3314 result
= visit_load_shared(ctx
, instr
);
3316 case nir_intrinsic_store_shared
:
3317 visit_store_shared(ctx
, instr
);
3319 case nir_intrinsic_bindless_image_samples
:
3320 result
= visit_image_samples(ctx
, instr
, true);
3322 case nir_intrinsic_image_deref_samples
:
3323 result
= visit_image_samples(ctx
, instr
, false);
3325 case nir_intrinsic_bindless_image_load
:
3326 result
= visit_image_load(ctx
, instr
, true);
3328 case nir_intrinsic_image_deref_load
:
3329 result
= visit_image_load(ctx
, instr
, false);
3331 case nir_intrinsic_bindless_image_store
:
3332 visit_image_store(ctx
, instr
, true);
3334 case nir_intrinsic_image_deref_store
:
3335 visit_image_store(ctx
, instr
, false);
3337 case nir_intrinsic_bindless_image_atomic_add
:
3338 case nir_intrinsic_bindless_image_atomic_min
:
3339 case nir_intrinsic_bindless_image_atomic_max
:
3340 case nir_intrinsic_bindless_image_atomic_and
:
3341 case nir_intrinsic_bindless_image_atomic_or
:
3342 case nir_intrinsic_bindless_image_atomic_xor
:
3343 case nir_intrinsic_bindless_image_atomic_exchange
:
3344 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3345 result
= visit_image_atomic(ctx
, instr
, true);
3347 case nir_intrinsic_image_deref_atomic_add
:
3348 case nir_intrinsic_image_deref_atomic_min
:
3349 case nir_intrinsic_image_deref_atomic_max
:
3350 case nir_intrinsic_image_deref_atomic_and
:
3351 case nir_intrinsic_image_deref_atomic_or
:
3352 case nir_intrinsic_image_deref_atomic_xor
:
3353 case nir_intrinsic_image_deref_atomic_exchange
:
3354 case nir_intrinsic_image_deref_atomic_comp_swap
:
3355 result
= visit_image_atomic(ctx
, instr
, false);
3357 case nir_intrinsic_bindless_image_size
:
3358 result
= visit_image_size(ctx
, instr
, true);
3360 case nir_intrinsic_image_deref_size
:
3361 result
= visit_image_size(ctx
, instr
, false);
3363 case nir_intrinsic_shader_clock
:
3364 result
= ac_build_shader_clock(&ctx
->ac
);
3366 case nir_intrinsic_discard
:
3367 case nir_intrinsic_discard_if
:
3368 emit_discard(ctx
, instr
);
3370 case nir_intrinsic_memory_barrier
:
3371 case nir_intrinsic_group_memory_barrier
:
3372 case nir_intrinsic_memory_barrier_atomic_counter
:
3373 case nir_intrinsic_memory_barrier_buffer
:
3374 case nir_intrinsic_memory_barrier_image
:
3375 case nir_intrinsic_memory_barrier_shared
:
3376 emit_membar(&ctx
->ac
, instr
);
3378 case nir_intrinsic_barrier
:
3379 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3381 case nir_intrinsic_shared_atomic_add
:
3382 case nir_intrinsic_shared_atomic_imin
:
3383 case nir_intrinsic_shared_atomic_umin
:
3384 case nir_intrinsic_shared_atomic_imax
:
3385 case nir_intrinsic_shared_atomic_umax
:
3386 case nir_intrinsic_shared_atomic_and
:
3387 case nir_intrinsic_shared_atomic_or
:
3388 case nir_intrinsic_shared_atomic_xor
:
3389 case nir_intrinsic_shared_atomic_exchange
:
3390 case nir_intrinsic_shared_atomic_comp_swap
: {
3391 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3392 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3395 case nir_intrinsic_deref_atomic_add
:
3396 case nir_intrinsic_deref_atomic_imin
:
3397 case nir_intrinsic_deref_atomic_umin
:
3398 case nir_intrinsic_deref_atomic_imax
:
3399 case nir_intrinsic_deref_atomic_umax
:
3400 case nir_intrinsic_deref_atomic_and
:
3401 case nir_intrinsic_deref_atomic_or
:
3402 case nir_intrinsic_deref_atomic_xor
:
3403 case nir_intrinsic_deref_atomic_exchange
:
3404 case nir_intrinsic_deref_atomic_comp_swap
: {
3405 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3406 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3409 case nir_intrinsic_load_barycentric_pixel
:
3410 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
3412 case nir_intrinsic_load_barycentric_centroid
:
3413 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
3415 case nir_intrinsic_load_barycentric_sample
:
3416 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
3418 case nir_intrinsic_load_barycentric_at_offset
: {
3419 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3420 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
3423 case nir_intrinsic_load_barycentric_at_sample
: {
3424 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
3425 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
3428 case nir_intrinsic_load_interpolated_input
: {
3429 /* We assume any indirect loads have been lowered away */
3430 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
3432 assert(offset
[0].i32
== 0);
3434 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
3435 unsigned index
= nir_intrinsic_base(instr
);
3436 unsigned component
= nir_intrinsic_component(instr
);
3437 result
= load_interpolated_input(ctx
, interp_param
, index
,
3439 instr
->dest
.ssa
.num_components
,
3440 instr
->dest
.ssa
.bit_size
);
3443 case nir_intrinsic_load_input
: {
3444 /* We only lower inputs for fragment shaders ATM */
3445 ASSERTED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[0]);
3447 assert(offset
[0].i32
== 0);
3449 unsigned index
= nir_intrinsic_base(instr
);
3450 unsigned component
= nir_intrinsic_component(instr
);
3451 result
= load_flat_input(ctx
, index
, component
,
3452 instr
->dest
.ssa
.num_components
,
3453 instr
->dest
.ssa
.bit_size
);
3456 case nir_intrinsic_emit_vertex
:
3457 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3459 case nir_intrinsic_end_primitive
:
3460 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3462 case nir_intrinsic_load_tess_coord
:
3463 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3465 case nir_intrinsic_load_tess_level_outer
:
3466 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3468 case nir_intrinsic_load_tess_level_inner
:
3469 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3471 case nir_intrinsic_load_patch_vertices_in
:
3472 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3474 case nir_intrinsic_vote_all
: {
3475 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3476 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3479 case nir_intrinsic_vote_any
: {
3480 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3481 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3484 case nir_intrinsic_shuffle
:
3485 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3486 get_src(ctx
, instr
->src
[1]));
3488 case nir_intrinsic_reduce
:
3489 result
= ac_build_reduce(&ctx
->ac
,
3490 get_src(ctx
, instr
->src
[0]),
3491 instr
->const_index
[0],
3492 instr
->const_index
[1]);
3494 case nir_intrinsic_inclusive_scan
:
3495 result
= ac_build_inclusive_scan(&ctx
->ac
,
3496 get_src(ctx
, instr
->src
[0]),
3497 instr
->const_index
[0]);
3499 case nir_intrinsic_exclusive_scan
:
3500 result
= ac_build_exclusive_scan(&ctx
->ac
,
3501 get_src(ctx
, instr
->src
[0]),
3502 instr
->const_index
[0]);
3504 case nir_intrinsic_quad_broadcast
: {
3505 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
3506 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3507 lane
, lane
, lane
, lane
);
3510 case nir_intrinsic_quad_swap_horizontal
:
3511 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3513 case nir_intrinsic_quad_swap_vertical
:
3514 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3516 case nir_intrinsic_quad_swap_diagonal
:
3517 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3519 case nir_intrinsic_quad_swizzle_amd
: {
3520 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3521 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3522 mask
& 0x3, (mask
>> 2) & 0x3,
3523 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
3526 case nir_intrinsic_masked_swizzle_amd
: {
3527 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3528 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
3531 case nir_intrinsic_write_invocation_amd
:
3532 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3533 get_src(ctx
, instr
->src
[1]),
3534 get_src(ctx
, instr
->src
[2]));
3536 case nir_intrinsic_mbcnt_amd
:
3537 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3540 fprintf(stderr
, "Unknown intrinsic: ");
3541 nir_print_instr(&instr
->instr
, stderr
);
3542 fprintf(stderr
, "\n");
3546 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3550 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3551 unsigned base_index
,
3552 unsigned constant_index
,
3553 LLVMValueRef dynamic_index
)
3555 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3556 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3557 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3559 /* Bindless uniforms are 64bit so multiple index by 8 */
3560 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3561 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3563 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3565 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3566 NULL
, 0, 0, true, true);
3568 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3571 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3572 nir_deref_instr
*deref_instr
,
3573 enum ac_descriptor_type desc_type
,
3574 const nir_instr
*instr
,
3575 bool image
, bool write
)
3577 LLVMValueRef index
= NULL
;
3578 unsigned constant_index
= 0;
3579 unsigned descriptor_set
;
3580 unsigned base_index
;
3581 bool bindless
= false;
3586 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3589 index
= get_src(ctx
, img_instr
->src
[0]);
3591 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3592 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3593 nir_tex_src_sampler_handle
);
3594 if (sampSrcIdx
!= -1) {
3597 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3599 assert(tex_instr
&& !image
);
3600 base_index
= tex_instr
->sampler_index
;
3604 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3605 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3606 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3610 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3611 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
3613 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3615 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3616 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3621 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3624 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3625 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3626 unsigned sidx
= deref_instr
->strct
.index
;
3627 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3628 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3630 unreachable("Unsupported deref type");
3633 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3635 if (deref_instr
->var
->data
.bindless
) {
3636 /* For now just assert on unhandled variable types */
3637 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3639 base_index
= deref_instr
->var
->data
.driver_location
;
3642 index
= index
? index
: ctx
->ac
.i32_0
;
3643 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3644 constant_index
, index
);
3646 base_index
= deref_instr
->var
->data
.binding
;
3649 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3652 constant_index
, index
,
3653 desc_type
, image
, write
, bindless
);
3656 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3659 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3660 * filtering manually. The driver sets img7 to a mask clearing
3661 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3662 * s_and_b32 samp0, samp0, img7
3665 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3667 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3668 LLVMValueRef res
, LLVMValueRef samp
)
3670 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3671 LLVMValueRef img7
, samp0
;
3673 if (ctx
->ac
.chip_class
>= GFX8
)
3676 img7
= LLVMBuildExtractElement(builder
, res
,
3677 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3678 samp0
= LLVMBuildExtractElement(builder
, samp
,
3679 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3680 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3681 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3682 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3685 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3686 nir_tex_instr
*instr
,
3687 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3688 LLVMValueRef
*fmask_ptr
)
3690 nir_deref_instr
*texture_deref_instr
= NULL
;
3691 nir_deref_instr
*sampler_deref_instr
= NULL
;
3694 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3695 switch (instr
->src
[i
].src_type
) {
3696 case nir_tex_src_texture_deref
:
3697 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3699 case nir_tex_src_sampler_deref
:
3700 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3702 case nir_tex_src_plane
:
3703 plane
= nir_src_as_int(instr
->src
[i
].src
);
3710 if (!sampler_deref_instr
)
3711 sampler_deref_instr
= texture_deref_instr
;
3713 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
3716 assert(instr
->op
!= nir_texop_txf_ms
&&
3717 instr
->op
!= nir_texop_samples_identical
);
3718 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
3720 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
3723 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
, false, false);
3726 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3727 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3728 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3730 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3731 instr
->op
== nir_texop_samples_identical
))
3732 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3735 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3738 coord
= ac_to_float(ctx
, coord
);
3739 coord
= ac_build_round(ctx
, coord
);
3740 coord
= ac_to_integer(ctx
, coord
);
3744 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3746 LLVMValueRef result
= NULL
;
3747 struct ac_image_args args
= { 0 };
3748 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3749 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3750 unsigned offset_src
= 0;
3752 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3754 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3755 switch (instr
->src
[i
].src_type
) {
3756 case nir_tex_src_coord
: {
3757 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3758 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3759 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3762 case nir_tex_src_projector
:
3764 case nir_tex_src_comparator
:
3765 if (instr
->is_shadow
)
3766 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3768 case nir_tex_src_offset
:
3769 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3772 case nir_tex_src_bias
:
3773 if (instr
->op
== nir_texop_txb
)
3774 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3776 case nir_tex_src_lod
: {
3777 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
3778 args
.level_zero
= true;
3780 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3783 case nir_tex_src_ms_index
:
3784 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3786 case nir_tex_src_ms_mcs
:
3788 case nir_tex_src_ddx
:
3789 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3791 case nir_tex_src_ddy
:
3792 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3794 case nir_tex_src_texture_offset
:
3795 case nir_tex_src_sampler_offset
:
3796 case nir_tex_src_plane
:
3802 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3803 result
= get_buffer_size(ctx
, args
.resource
, true);
3807 if (instr
->op
== nir_texop_texture_samples
) {
3808 LLVMValueRef res
, samples
, is_msaa
;
3809 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3810 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3811 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3812 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3813 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3814 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3815 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3816 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3817 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3819 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3820 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3821 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3822 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3823 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3825 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3831 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3832 LLVMValueRef offset
[3], pack
;
3833 for (unsigned chan
= 0; chan
< 3; ++chan
)
3834 offset
[chan
] = ctx
->ac
.i32_0
;
3836 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3837 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3838 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3839 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3840 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3842 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3843 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3845 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3846 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3850 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3851 * so the depth comparison value isn't clamped for Z16 and
3852 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has an explicitly
3853 * clamped 32-bit float format.
3855 * It's unnecessary if the original texture format was
3856 * Z32_FLOAT, but we don't know that here.
3859 ctx
->ac
.chip_class
>= GFX8
&&
3860 ctx
->ac
.chip_class
<= GFX9
&&
3861 ctx
->abi
->clamp_shadow_reference
)
3862 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3864 /* pack derivatives */
3866 int num_src_deriv_channels
, num_dest_deriv_channels
;
3867 switch (instr
->sampler_dim
) {
3868 case GLSL_SAMPLER_DIM_3D
:
3869 case GLSL_SAMPLER_DIM_CUBE
:
3870 num_src_deriv_channels
= 3;
3871 num_dest_deriv_channels
= 3;
3873 case GLSL_SAMPLER_DIM_2D
:
3875 num_src_deriv_channels
= 2;
3876 num_dest_deriv_channels
= 2;
3878 case GLSL_SAMPLER_DIM_1D
:
3879 num_src_deriv_channels
= 1;
3880 if (ctx
->ac
.chip_class
== GFX9
) {
3881 num_dest_deriv_channels
= 2;
3883 num_dest_deriv_channels
= 1;
3888 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3889 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3890 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3891 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3892 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3894 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3895 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3896 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3900 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3901 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3902 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3903 if (instr
->coord_components
== 3)
3904 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3905 ac_prepare_cube_coords(&ctx
->ac
,
3906 instr
->op
== nir_texop_txd
, instr
->is_array
,
3907 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3910 /* Texture coordinates fixups */
3911 if (instr
->coord_components
> 1 &&
3912 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3914 instr
->op
!= nir_texop_txf
) {
3915 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3918 if (instr
->coord_components
> 2 &&
3919 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3920 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3921 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3922 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3924 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3925 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3928 if (ctx
->ac
.chip_class
== GFX9
&&
3929 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3930 instr
->op
!= nir_texop_lod
) {
3931 LLVMValueRef filler
;
3932 if (instr
->op
== nir_texop_txf
)
3933 filler
= ctx
->ac
.i32_0
;
3935 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3937 if (instr
->is_array
)
3938 args
.coords
[2] = args
.coords
[1];
3939 args
.coords
[1] = filler
;
3942 /* Pack sample index */
3943 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3944 args
.coords
[instr
->coord_components
] = sample_index
;
3946 if (instr
->op
== nir_texop_samples_identical
) {
3947 struct ac_image_args txf_args
= { 0 };
3948 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3950 txf_args
.dmask
= 0xf;
3951 txf_args
.resource
= fmask_ptr
;
3952 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3953 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3955 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3956 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3960 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
3961 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
3962 instr
->op
!= nir_texop_txs
) {
3963 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3964 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3965 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3966 instr
->is_array
? args
.coords
[2] : NULL
,
3967 args
.coords
[sample_chan
], fmask_ptr
);
3970 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
3971 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3972 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3973 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3974 args
.coords
[i
] = LLVMBuildAdd(
3975 ctx
->ac
.builder
, args
.coords
[i
],
3976 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
3981 /* DMASK was repurposed for GATHER4. 4 components are always
3982 * returned and DMASK works like a swizzle - it selects
3983 * the component to fetch. The only valid DMASK values are
3984 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3985 * (red,red,red,red) etc.) The ISA document doesn't mention
3989 if (instr
->op
== nir_texop_tg4
) {
3990 if (instr
->is_shadow
)
3993 args
.dmask
= 1 << instr
->component
;
3996 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3997 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3998 result
= build_tex_intrinsic(ctx
, instr
, &args
);
4000 if (instr
->op
== nir_texop_query_levels
)
4001 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
4002 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
4003 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
4004 instr
->op
!= nir_texop_tg4
)
4005 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
4006 else if (instr
->op
== nir_texop_txs
&&
4007 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
4009 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4010 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
4011 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4012 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
4013 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
4014 } else if (ctx
->ac
.chip_class
== GFX9
&&
4015 instr
->op
== nir_texop_txs
&&
4016 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
4018 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
4019 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
4020 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
4022 } else if (instr
->dest
.ssa
.num_components
!= 4)
4023 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
4027 assert(instr
->dest
.is_ssa
);
4028 result
= ac_to_integer(&ctx
->ac
, result
);
4029 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4034 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4036 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4037 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4039 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4040 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4043 static void visit_post_phi(struct ac_nir_context
*ctx
,
4044 nir_phi_instr
*instr
,
4045 LLVMValueRef llvm_phi
)
4047 nir_foreach_phi_src(src
, instr
) {
4048 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4049 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4051 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4055 static void phi_post_pass(struct ac_nir_context
*ctx
)
4057 hash_table_foreach(ctx
->phis
, entry
) {
4058 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4059 (LLVMValueRef
)entry
->data
);
4064 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4065 const nir_ssa_undef_instr
*instr
)
4067 unsigned num_components
= instr
->def
.num_components
;
4068 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4071 if (num_components
== 1)
4072 undef
= LLVMGetUndef(type
);
4074 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4076 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4079 static void visit_jump(struct ac_llvm_context
*ctx
,
4080 const nir_jump_instr
*instr
)
4082 switch (instr
->type
) {
4083 case nir_jump_break
:
4084 ac_build_break(ctx
);
4086 case nir_jump_continue
:
4087 ac_build_continue(ctx
);
4090 fprintf(stderr
, "Unknown NIR jump instr: ");
4091 nir_print_instr(&instr
->instr
, stderr
);
4092 fprintf(stderr
, "\n");
4098 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4099 enum glsl_base_type type
)
4103 case GLSL_TYPE_UINT
:
4104 case GLSL_TYPE_BOOL
:
4105 case GLSL_TYPE_SUBROUTINE
:
4107 case GLSL_TYPE_INT8
:
4108 case GLSL_TYPE_UINT8
:
4110 case GLSL_TYPE_INT16
:
4111 case GLSL_TYPE_UINT16
:
4113 case GLSL_TYPE_FLOAT
:
4115 case GLSL_TYPE_FLOAT16
:
4117 case GLSL_TYPE_INT64
:
4118 case GLSL_TYPE_UINT64
:
4120 case GLSL_TYPE_DOUBLE
:
4123 unreachable("unknown GLSL type");
4128 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4129 const struct glsl_type
*type
)
4131 if (glsl_type_is_scalar(type
)) {
4132 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4135 if (glsl_type_is_vector(type
)) {
4136 return LLVMVectorType(
4137 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4138 glsl_get_vector_elements(type
));
4141 if (glsl_type_is_matrix(type
)) {
4142 return LLVMArrayType(
4143 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4144 glsl_get_matrix_columns(type
));
4147 if (glsl_type_is_array(type
)) {
4148 return LLVMArrayType(
4149 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4150 glsl_get_length(type
));
4153 assert(glsl_type_is_struct_or_ifc(type
));
4155 LLVMTypeRef member_types
[glsl_get_length(type
)];
4157 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4159 glsl_to_llvm_type(ac
,
4160 glsl_get_struct_field(type
, i
));
4163 return LLVMStructTypeInContext(ac
->context
, member_types
,
4164 glsl_get_length(type
), false);
4167 static void visit_deref(struct ac_nir_context
*ctx
,
4168 nir_deref_instr
*instr
)
4170 if (instr
->mode
!= nir_var_mem_shared
&&
4171 instr
->mode
!= nir_var_mem_global
)
4174 LLVMValueRef result
= NULL
;
4175 switch(instr
->deref_type
) {
4176 case nir_deref_type_var
: {
4177 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4178 result
= entry
->data
;
4181 case nir_deref_type_struct
:
4182 if (instr
->mode
== nir_var_mem_global
) {
4183 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4184 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4185 instr
->strct
.index
);
4186 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4187 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4189 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4190 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4193 case nir_deref_type_array
:
4194 if (instr
->mode
== nir_var_mem_global
) {
4195 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4196 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4198 if ((glsl_type_is_matrix(parent
->type
) &&
4199 glsl_matrix_type_is_row_major(parent
->type
)) ||
4200 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4201 stride
= type_scalar_size_bytes(parent
->type
);
4204 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4205 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4206 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4208 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4210 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4212 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4213 get_src(ctx
, instr
->arr
.index
));
4216 case nir_deref_type_ptr_as_array
:
4217 if (instr
->mode
== nir_var_mem_global
) {
4218 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4220 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4221 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4222 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4224 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4226 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4228 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4229 get_src(ctx
, instr
->arr
.index
));
4232 case nir_deref_type_cast
: {
4233 result
= get_src(ctx
, instr
->parent
);
4235 /* We can't use the structs from LLVM because the shader
4236 * specifies its own offsets. */
4237 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4238 if (instr
->mode
== nir_var_mem_shared
)
4239 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4241 unsigned address_space
;
4243 switch(instr
->mode
) {
4244 case nir_var_mem_shared
:
4245 address_space
= AC_ADDR_SPACE_LDS
;
4247 case nir_var_mem_global
:
4248 address_space
= AC_ADDR_SPACE_GLOBAL
;
4251 unreachable("Unhandled address space");
4254 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4256 if (LLVMTypeOf(result
) != type
) {
4257 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4258 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4261 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4268 unreachable("Unhandled deref_instr deref type");
4271 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4274 static void visit_cf_list(struct ac_nir_context
*ctx
,
4275 struct exec_list
*list
);
4277 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4279 nir_foreach_instr(instr
, block
)
4281 switch (instr
->type
) {
4282 case nir_instr_type_alu
:
4283 visit_alu(ctx
, nir_instr_as_alu(instr
));
4285 case nir_instr_type_load_const
:
4286 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4288 case nir_instr_type_intrinsic
:
4289 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4291 case nir_instr_type_tex
:
4292 visit_tex(ctx
, nir_instr_as_tex(instr
));
4294 case nir_instr_type_phi
:
4295 visit_phi(ctx
, nir_instr_as_phi(instr
));
4297 case nir_instr_type_ssa_undef
:
4298 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4300 case nir_instr_type_jump
:
4301 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4303 case nir_instr_type_deref
:
4304 visit_deref(ctx
, nir_instr_as_deref(instr
));
4307 fprintf(stderr
, "Unknown NIR instr type: ");
4308 nir_print_instr(instr
, stderr
);
4309 fprintf(stderr
, "\n");
4314 _mesa_hash_table_insert(ctx
->defs
, block
,
4315 LLVMGetInsertBlock(ctx
->ac
.builder
));
4318 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4320 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4322 nir_block
*then_block
=
4323 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4325 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4327 visit_cf_list(ctx
, &if_stmt
->then_list
);
4329 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4330 nir_block
*else_block
=
4331 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4333 ac_build_else(&ctx
->ac
, else_block
->index
);
4334 visit_cf_list(ctx
, &if_stmt
->else_list
);
4337 ac_build_endif(&ctx
->ac
, then_block
->index
);
4340 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4342 nir_block
*first_loop_block
=
4343 (nir_block
*) exec_list_get_head(&loop
->body
);
4345 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4347 visit_cf_list(ctx
, &loop
->body
);
4349 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4352 static void visit_cf_list(struct ac_nir_context
*ctx
,
4353 struct exec_list
*list
)
4355 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4357 switch (node
->type
) {
4358 case nir_cf_node_block
:
4359 visit_block(ctx
, nir_cf_node_as_block(node
));
4362 case nir_cf_node_if
:
4363 visit_if(ctx
, nir_cf_node_as_if(node
));
4366 case nir_cf_node_loop
:
4367 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4377 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4378 struct ac_shader_abi
*abi
,
4379 struct nir_shader
*nir
,
4380 struct nir_variable
*variable
,
4381 gl_shader_stage stage
)
4383 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4384 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4386 /* tess ctrl has it's own load/store paths for outputs */
4387 if (stage
== MESA_SHADER_TESS_CTRL
)
4390 if (stage
== MESA_SHADER_VERTEX
||
4391 stage
== MESA_SHADER_TESS_EVAL
||
4392 stage
== MESA_SHADER_GEOMETRY
) {
4393 int idx
= variable
->data
.location
+ variable
->data
.index
;
4394 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4395 int length
= nir
->info
.clip_distance_array_size
+
4396 nir
->info
.cull_distance_array_size
;
4405 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4406 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4407 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4408 for (unsigned chan
= 0; chan
< 4; chan
++) {
4409 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4410 ac_build_alloca_undef(ctx
, type
, "");
4416 setup_locals(struct ac_nir_context
*ctx
,
4417 struct nir_function
*func
)
4420 ctx
->num_locals
= 0;
4421 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4422 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4423 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4424 variable
->data
.location_frac
= 0;
4425 ctx
->num_locals
+= attrib_count
;
4427 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4431 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4432 for (j
= 0; j
< 4; j
++) {
4433 ctx
->locals
[i
* 4 + j
] =
4434 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4440 setup_shared(struct ac_nir_context
*ctx
,
4441 struct nir_shader
*nir
)
4443 nir_foreach_variable(variable
, &nir
->shared
) {
4444 LLVMValueRef shared
=
4445 LLVMAddGlobalInAddressSpace(
4446 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4447 variable
->name
? variable
->name
: "",
4449 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4453 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4454 struct nir_shader
*nir
)
4456 struct ac_nir_context ctx
= {};
4457 struct nir_function
*func
;
4462 ctx
.stage
= nir
->info
.stage
;
4463 ctx
.info
= &nir
->info
;
4465 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4467 nir_foreach_variable(variable
, &nir
->outputs
)
4468 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4471 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4472 _mesa_key_pointer_equal
);
4473 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4474 _mesa_key_pointer_equal
);
4475 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4476 _mesa_key_pointer_equal
);
4478 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4480 nir_index_ssa_defs(func
->impl
);
4481 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4483 setup_locals(&ctx
, func
);
4485 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4486 setup_shared(&ctx
, nir
);
4488 visit_cf_list(&ctx
, &func
->impl
->body
);
4489 phi_post_pass(&ctx
);
4491 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4492 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4497 ralloc_free(ctx
.defs
);
4498 ralloc_free(ctx
.phis
);
4499 ralloc_free(ctx
.vars
);
4503 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4505 /* While it would be nice not to have this flag, we are constrained
4506 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
4508 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
4510 /* TODO: Indirect indexing of GS inputs is unimplemented.
4512 * TCS and TES load inputs directly from LDS or offchip memory, so
4513 * indirect indexing is trivial.
4515 nir_variable_mode indirect_mask
= 0;
4516 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4517 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4518 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4519 !llvm_has_working_vgpr_indexing
)) {
4520 indirect_mask
|= nir_var_shader_in
;
4522 if (!llvm_has_working_vgpr_indexing
&&
4523 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4524 indirect_mask
|= nir_var_shader_out
;
4526 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4527 * smart enough to handle indirects without causing excess spilling
4528 * causing the gpu to hang.
4530 * See the following thread for more details of the problem:
4531 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4533 indirect_mask
|= nir_var_function_temp
;
4535 nir_lower_indirect_derefs(nir
, indirect_mask
);
4539 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4541 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4545 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4547 if (var
->data
.mode
!= nir_var_shader_out
)
4550 unsigned writemask
= 0;
4551 const int location
= var
->data
.location
;
4552 unsigned first_component
= var
->data
.location_frac
;
4553 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4555 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4556 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4557 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4558 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4564 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4565 unsigned *cond_block_tf_writemask
,
4566 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4568 switch (cf_node
->type
) {
4569 case nir_cf_node_block
: {
4570 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4571 nir_foreach_instr(instr
, block
) {
4572 if (instr
->type
!= nir_instr_type_intrinsic
)
4575 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4576 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4578 /* If we find a barrier in nested control flow put this in the
4579 * too hard basket. In GLSL this is not possible but it is in
4583 *tessfactors_are_def_in_all_invocs
= false;
4587 /* The following case must be prevented:
4588 * gl_TessLevelInner = ...;
4590 * if (gl_InvocationID == 1)
4591 * gl_TessLevelInner = ...;
4593 * If you consider disjoint code segments separated by barriers, each
4594 * such segment that writes tess factor channels should write the same
4595 * channels in all codepaths within that segment.
4597 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4598 /* Accumulate the result: */
4599 *tessfactors_are_def_in_all_invocs
&=
4600 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4602 /* Analyze the next code segment from scratch. */
4603 *upper_block_tf_writemask
= 0;
4604 *cond_block_tf_writemask
= 0;
4607 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4612 case nir_cf_node_if
: {
4613 unsigned then_tessfactor_writemask
= 0;
4614 unsigned else_tessfactor_writemask
= 0;
4616 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4617 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4618 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4619 cond_block_tf_writemask
,
4620 tessfactors_are_def_in_all_invocs
, true);
4623 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4624 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4625 cond_block_tf_writemask
,
4626 tessfactors_are_def_in_all_invocs
, true);
4629 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4630 /* If both statements write the same tess factor channels,
4631 * we can say that the upper block writes them too.
4633 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4634 else_tessfactor_writemask
;
4635 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4636 else_tessfactor_writemask
;
4641 case nir_cf_node_loop
: {
4642 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4643 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4644 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4645 cond_block_tf_writemask
,
4646 tessfactors_are_def_in_all_invocs
, true);
4652 unreachable("unknown cf node type");
4657 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4659 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4661 /* The pass works as follows:
4662 * If all codepaths write tess factors, we can say that all
4663 * invocations define tess factors.
4665 * Each tess factor channel is tracked separately.
4667 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4668 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4670 /* Initial value = true. Here the pass will accumulate results from
4671 * multiple segments surrounded by barriers. If tess factors aren't
4672 * written at all, it's a shader bug and we don't care if this will be
4675 bool tessfactors_are_def_in_all_invocs
= true;
4677 nir_foreach_function(function
, nir
) {
4678 if (function
->impl
) {
4679 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4680 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4681 &cond_block_tf_writemask
,
4682 &tessfactors_are_def_in_all_invocs
,
4688 /* Accumulate the result for the last code segment separated by a
4691 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4692 tessfactors_are_def_in_all_invocs
&=
4693 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4696 return tessfactors_are_def_in_all_invocs
;