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
get_alu_src(struct ac_nir_context
*ctx
,
158 unsigned num_components
)
160 LLVMValueRef value
= get_src(ctx
, src
.src
);
161 bool need_swizzle
= false;
164 unsigned src_components
= ac_get_llvm_num_components(value
);
165 for (unsigned i
= 0; i
< num_components
; ++i
) {
166 assert(src
.swizzle
[i
] < src_components
);
167 if (src
.swizzle
[i
] != i
)
171 if (need_swizzle
|| num_components
!= src_components
) {
172 LLVMValueRef masks
[] = {
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
176 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
178 if (src_components
> 1 && num_components
== 1) {
179 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
181 } else if (src_components
== 1 && num_components
> 1) {
182 LLVMValueRef values
[] = {value
, value
, value
, value
};
183 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
185 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
186 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
195 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
196 LLVMIntPredicate pred
, LLVMValueRef src0
,
199 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
200 return LLVMBuildSelect(ctx
->builder
, result
,
201 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
205 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
206 LLVMRealPredicate pred
, LLVMValueRef src0
,
210 src0
= ac_to_float(ctx
, src0
);
211 src1
= ac_to_float(ctx
, src1
);
212 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
213 return LLVMBuildSelect(ctx
->builder
, result
,
214 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
218 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
220 LLVMTypeRef result_type
,
224 LLVMValueRef params
[] = {
225 ac_to_float(ctx
, src0
),
228 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
229 ac_get_elem_bits(ctx
, result_type
));
230 assert(length
< sizeof(name
));
231 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
234 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
236 LLVMTypeRef result_type
,
237 LLVMValueRef src0
, LLVMValueRef src1
)
240 LLVMValueRef params
[] = {
241 ac_to_float(ctx
, src0
),
242 ac_to_float(ctx
, src1
),
245 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
246 ac_get_elem_bits(ctx
, result_type
));
247 assert(length
< sizeof(name
));
248 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
251 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
253 LLVMTypeRef result_type
,
254 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
257 LLVMValueRef params
[] = {
258 ac_to_float(ctx
, src0
),
259 ac_to_float(ctx
, src1
),
260 ac_to_float(ctx
, src2
),
263 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
264 ac_get_elem_bits(ctx
, result_type
));
265 assert(length
< sizeof(name
));
266 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
269 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
270 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
272 assert(LLVMGetTypeKind(LLVMTypeOf(src0
)) != LLVMVectorTypeKind
);
274 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
276 return LLVMBuildSelect(ctx
->builder
, v
,
277 ac_to_integer_or_pointer(ctx
, src1
),
278 ac_to_integer_or_pointer(ctx
, src2
), "");
281 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
284 return ac_build_imax(ctx
, src0
, LLVMBuildNeg(ctx
->builder
, src0
, ""));
287 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
289 LLVMValueRef src0
, LLVMValueRef src1
)
291 LLVMTypeRef ret_type
;
292 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
294 LLVMValueRef params
[] = { src0
, src1
};
295 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
298 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
299 params
, 2, AC_FUNC_ATTR_READNONE
);
301 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
302 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
306 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
310 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
311 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
313 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
317 return LLVMBuildFPTrunc(ctx
->builder
, result
, ctx
->f16
, "");
321 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
323 unreachable("Unsupported bit size.");
327 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
330 src0
= ac_to_float(ctx
, src0
);
331 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
332 return LLVMBuildSExt(ctx
->builder
,
333 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
337 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
341 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
345 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i8
, "");
347 return LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i16
, "");
351 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
353 unreachable("Unsupported bit size.");
357 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
360 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
361 return LLVMBuildSExt(ctx
->builder
,
362 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
366 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
370 LLVMValueRef cond
= NULL
;
372 src0
= ac_to_float(ctx
, src0
);
373 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
375 if (ctx
->chip_class
>= GFX8
) {
376 LLVMValueRef args
[2];
377 /* Check if the result is a denormal - and flush to 0 if so. */
379 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
380 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
383 /* need to convert back up to f32 */
384 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
386 if (ctx
->chip_class
>= GFX8
)
387 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
390 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
391 * so compare the result and flush to 0 if it's smaller.
393 LLVMValueRef temp
, cond2
;
394 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
395 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
396 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
398 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
399 temp
, ctx
->f32_0
, "");
400 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
401 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
406 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
407 LLVMValueRef src0
, LLVMValueRef src1
)
409 LLVMValueRef dst64
, result
;
410 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
411 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
413 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
414 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
415 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
419 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
420 LLVMValueRef src0
, LLVMValueRef src1
)
422 LLVMValueRef dst64
, result
;
423 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
424 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
426 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
427 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
428 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
432 static LLVMValueRef
emit_bfm(struct ac_llvm_context
*ctx
,
433 LLVMValueRef bits
, LLVMValueRef offset
)
435 /* mask = ((1 << bits) - 1) << offset */
436 return LLVMBuildShl(ctx
->builder
,
437 LLVMBuildSub(ctx
->builder
,
438 LLVMBuildShl(ctx
->builder
,
445 static LLVMValueRef
emit_bitfield_select(struct ac_llvm_context
*ctx
,
446 LLVMValueRef mask
, LLVMValueRef insert
,
450 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
451 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
453 return LLVMBuildXor(ctx
->builder
, base
,
454 LLVMBuildAnd(ctx
->builder
, mask
,
455 LLVMBuildXor(ctx
->builder
, insert
, base
, ""), ""), "");
458 static LLVMValueRef
emit_pack_2x16(struct ac_llvm_context
*ctx
,
460 LLVMValueRef (*pack
)(struct ac_llvm_context
*ctx
,
461 LLVMValueRef args
[2]))
463 LLVMValueRef comp
[2];
465 src0
= ac_to_float(ctx
, src0
);
466 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
467 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
469 return LLVMBuildBitCast(ctx
->builder
, pack(ctx
, comp
), ctx
->i32
, "");
472 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
475 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
476 LLVMValueRef temps
[2], val
;
479 for (i
= 0; i
< 2; i
++) {
480 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
481 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
482 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
483 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
485 return ac_build_gather_values(ctx
, temps
, 2);
488 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
496 if (op
== nir_op_fddx_fine
)
497 mask
= AC_TID_MASK_LEFT
;
498 else if (op
== nir_op_fddy_fine
)
499 mask
= AC_TID_MASK_TOP
;
501 mask
= AC_TID_MASK_TOP_LEFT
;
503 /* for DDX we want to next X pixel, DDY next Y pixel. */
504 if (op
== nir_op_fddx_fine
||
505 op
== nir_op_fddx_coarse
||
511 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
515 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
517 LLVMValueRef src
[4], result
= NULL
;
518 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
519 unsigned src_components
;
520 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
522 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
529 case nir_op_pack_half_2x16
:
530 case nir_op_pack_snorm_2x16
:
531 case nir_op_pack_unorm_2x16
:
534 case nir_op_unpack_half_2x16
:
537 case nir_op_cube_face_coord
:
538 case nir_op_cube_face_index
:
542 src_components
= num_components
;
545 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
546 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
553 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
554 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
557 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
560 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
563 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
566 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
567 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
568 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
571 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
572 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
573 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
576 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
579 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
582 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
585 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
588 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
589 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
590 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
591 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
592 ac_to_float_type(&ctx
->ac
, def_type
), result
);
593 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
594 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
597 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
598 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
599 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
602 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
605 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
608 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
611 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
612 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
613 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
616 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
617 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
620 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
623 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
626 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
629 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
630 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
631 LLVMTypeOf(src
[0]), "");
632 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
633 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
634 LLVMTypeOf(src
[0]), "");
635 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0], src
[1], "");
638 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
639 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
640 LLVMTypeOf(src
[0]), "");
641 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
642 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
643 LLVMTypeOf(src
[0]), "");
644 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0], src
[1], "");
647 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) < ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
648 src
[1] = LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
649 LLVMTypeOf(src
[0]), "");
650 else if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[1])) > ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])))
651 src
[1] = LLVMBuildTrunc(ctx
->ac
.builder
, src
[1],
652 LLVMTypeOf(src
[0]), "");
653 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0], src
[1], "");
656 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
659 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
662 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
665 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
668 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
671 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
674 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
677 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
680 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
683 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
686 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
687 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
690 result
= emit_iabs(&ctx
->ac
, src
[0]);
693 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
696 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
699 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
702 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
705 result
= ac_build_isign(&ctx
->ac
, src
[0],
706 instr
->dest
.dest
.ssa
.bit_size
);
709 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
710 result
= ac_build_fsign(&ctx
->ac
, src
[0],
711 instr
->dest
.dest
.ssa
.bit_size
);
714 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
715 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
718 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
719 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
722 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
723 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
725 case nir_op_fround_even
:
726 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
727 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
730 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
731 result
= ac_build_fract(&ctx
->ac
, src
[0],
732 instr
->dest
.dest
.ssa
.bit_size
);
735 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
736 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
739 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
740 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
743 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
744 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
747 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
748 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
751 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
752 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
755 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
756 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
757 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
759 case nir_op_frexp_exp
:
760 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
761 result
= ac_build_frexp_exp(&ctx
->ac
, src
[0],
762 ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])));
763 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) == 16)
764 result
= LLVMBuildSExt(ctx
->ac
.builder
, result
,
767 case nir_op_frexp_sig
:
768 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
769 result
= ac_build_frexp_mant(&ctx
->ac
, src
[0],
770 instr
->dest
.dest
.ssa
.bit_size
);
773 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.pow",
774 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
777 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
779 if (ctx
->ac
.chip_class
< GFX9
&&
780 instr
->dest
.dest
.ssa
.bit_size
== 32) {
781 /* Only pre-GFX9 chips do not flush denorms. */
782 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
783 ac_to_float_type(&ctx
->ac
, def_type
),
788 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
789 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
790 if (ctx
->ac
.chip_class
< GFX9
&&
791 instr
->dest
.dest
.ssa
.bit_size
== 32) {
792 /* Only pre-GFX9 chips do not flush denorms. */
793 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
794 ac_to_float_type(&ctx
->ac
, def_type
),
799 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
800 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
803 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
804 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
805 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
806 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
807 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
809 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
812 result
= emit_bfm(&ctx
->ac
, src
[0], src
[1]);
814 case nir_op_bitfield_select
:
815 result
= emit_bitfield_select(&ctx
->ac
, src
[0], src
[1], src
[2]);
818 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], false);
821 result
= ac_build_bfe(&ctx
->ac
, src
[0], src
[1], src
[2], true);
823 case nir_op_bitfield_reverse
:
824 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
826 case nir_op_bit_count
:
827 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
832 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
833 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
834 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
840 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
841 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
847 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
848 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
853 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
858 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
860 case nir_op_f2f16_rtz
:
861 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
862 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
863 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
864 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
865 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
866 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
868 case nir_op_f2f16_rtne
:
872 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
873 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
874 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
876 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
882 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
883 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
885 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
891 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
892 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
894 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
897 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
899 case nir_op_find_lsb
:
900 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
902 case nir_op_ufind_msb
:
903 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
905 case nir_op_ifind_msb
:
906 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
908 case nir_op_uadd_carry
:
909 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
911 case nir_op_usub_borrow
:
912 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
917 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
920 result
= emit_f2b(&ctx
->ac
, src
[0]);
926 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
929 result
= emit_i2b(&ctx
->ac
, src
[0]);
931 case nir_op_fquantize2f16
:
932 result
= emit_f2f16(&ctx
->ac
, src
[0]);
934 case nir_op_umul_high
:
935 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
937 case nir_op_imul_high
:
938 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
940 case nir_op_pack_half_2x16
:
941 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pkrtz_f16
);
943 case nir_op_pack_snorm_2x16
:
944 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_i16
);
946 case nir_op_pack_unorm_2x16
:
947 result
= emit_pack_2x16(&ctx
->ac
, src
[0], ac_build_cvt_pknorm_u16
);
949 case nir_op_unpack_half_2x16
:
950 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
954 case nir_op_fddx_fine
:
955 case nir_op_fddy_fine
:
956 case nir_op_fddx_coarse
:
957 case nir_op_fddy_coarse
:
958 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
961 case nir_op_unpack_64_2x32_split_x
: {
962 assert(ac_get_llvm_num_components(src
[0]) == 1);
963 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
966 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
971 case nir_op_unpack_64_2x32_split_y
: {
972 assert(ac_get_llvm_num_components(src
[0]) == 1);
973 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
976 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
981 case nir_op_pack_64_2x32_split
: {
982 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
983 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
987 case nir_op_pack_32_2x16_split
: {
988 LLVMValueRef tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
989 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
993 case nir_op_unpack_32_2x16_split_x
: {
994 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
997 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1002 case nir_op_unpack_32_2x16_split_y
: {
1003 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1006 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1011 case nir_op_cube_face_coord
: {
1012 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1013 LLVMValueRef results
[2];
1015 for (unsigned chan
= 0; chan
< 3; chan
++)
1016 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1017 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1018 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1019 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1020 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1021 LLVMValueRef ma
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubema",
1022 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1023 results
[0] = ac_build_fdiv(&ctx
->ac
, results
[0], ma
);
1024 results
[1] = ac_build_fdiv(&ctx
->ac
, results
[1], ma
);
1025 LLVMValueRef offset
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
1026 results
[0] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[0], offset
, "");
1027 results
[1] = LLVMBuildFAdd(ctx
->ac
.builder
, results
[1], offset
, "");
1028 result
= ac_build_gather_values(&ctx
->ac
, results
, 2);
1032 case nir_op_cube_face_index
: {
1033 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1035 for (unsigned chan
= 0; chan
< 3; chan
++)
1036 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1037 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubeid",
1038 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1043 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1044 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1045 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1046 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1049 result
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1050 result
= ac_build_umin(&ctx
->ac
, result
, src
[2]);
1053 result
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1054 result
= ac_build_imin(&ctx
->ac
, result
, src
[2]);
1057 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1058 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1059 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1060 ac_to_float_type(&ctx
->ac
, def_type
), result
, src
[2]);
1063 result
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1064 result
= ac_build_umax(&ctx
->ac
, result
, src
[2]);
1067 result
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1068 result
= ac_build_imax(&ctx
->ac
, result
, src
[2]);
1070 case nir_op_fmed3
: {
1071 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1072 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
1073 src
[2] = ac_to_float(&ctx
->ac
, src
[2]);
1074 result
= ac_build_fmed3(&ctx
->ac
, src
[0], src
[1], src
[2],
1075 instr
->dest
.dest
.ssa
.bit_size
);
1078 case nir_op_imed3
: {
1079 LLVMValueRef tmp1
= ac_build_imin(&ctx
->ac
, src
[0], src
[1]);
1080 LLVMValueRef tmp2
= ac_build_imax(&ctx
->ac
, src
[0], src
[1]);
1081 tmp2
= ac_build_imin(&ctx
->ac
, tmp2
, src
[2]);
1082 result
= ac_build_imax(&ctx
->ac
, tmp1
, tmp2
);
1085 case nir_op_umed3
: {
1086 LLVMValueRef tmp1
= ac_build_umin(&ctx
->ac
, src
[0], src
[1]);
1087 LLVMValueRef tmp2
= ac_build_umax(&ctx
->ac
, src
[0], src
[1]);
1088 tmp2
= ac_build_umin(&ctx
->ac
, tmp2
, src
[2]);
1089 result
= ac_build_umax(&ctx
->ac
, tmp1
, tmp2
);
1094 fprintf(stderr
, "Unknown NIR alu instr: ");
1095 nir_print_instr(&instr
->instr
, stderr
);
1096 fprintf(stderr
, "\n");
1101 assert(instr
->dest
.dest
.is_ssa
);
1102 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1103 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1107 static void visit_load_const(struct ac_nir_context
*ctx
,
1108 const nir_load_const_instr
*instr
)
1110 LLVMValueRef values
[4], value
= NULL
;
1111 LLVMTypeRef element_type
=
1112 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1114 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1115 switch (instr
->def
.bit_size
) {
1117 values
[i
] = LLVMConstInt(element_type
,
1118 instr
->value
[i
].u8
, false);
1121 values
[i
] = LLVMConstInt(element_type
,
1122 instr
->value
[i
].u16
, false);
1125 values
[i
] = LLVMConstInt(element_type
,
1126 instr
->value
[i
].u32
, false);
1129 values
[i
] = LLVMConstInt(element_type
,
1130 instr
->value
[i
].u64
, false);
1134 "unsupported nir load_const bit_size: %d\n",
1135 instr
->def
.bit_size
);
1139 if (instr
->def
.num_components
> 1) {
1140 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1144 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1148 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1151 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1152 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1155 if (ctx
->ac
.chip_class
== GFX8
&& in_elements
) {
1156 /* On GFX8, the descriptor contains the size in bytes,
1157 * but TXQ must return the size in elements.
1158 * The stride is always non-zero for resources using TXQ.
1160 LLVMValueRef stride
=
1161 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1163 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1164 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1165 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1166 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1168 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1173 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1175 struct ac_image_args
*args
,
1176 const nir_tex_instr
*instr
)
1178 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1179 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1180 LLVMValueRef half_texel
[2];
1181 LLVMValueRef compare_cube_wa
= NULL
;
1182 LLVMValueRef result
;
1186 struct ac_image_args txq_args
= { 0 };
1188 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1189 txq_args
.opcode
= ac_image_get_resinfo
;
1190 txq_args
.dmask
= 0xf;
1191 txq_args
.lod
= ctx
->i32_0
;
1192 txq_args
.resource
= args
->resource
;
1193 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1194 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1196 for (unsigned c
= 0; c
< 2; c
++) {
1197 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1198 LLVMConstInt(ctx
->i32
, c
, false), "");
1199 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1200 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1201 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1202 LLVMConstReal(ctx
->f32
, -0.5), "");
1206 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1208 for (unsigned c
= 0; c
< 2; c
++) {
1210 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1211 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1215 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1216 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1217 * workaround by sampling using a scaled type and converting.
1218 * This is taken from amdgpu-pro shaders.
1220 /* NOTE this produces some ugly code compared to amdgpu-pro,
1221 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1222 * and then reads them back. -pro generates two selects,
1223 * one s_cmp for the descriptor rewriting
1224 * one v_cmp for the coordinate and result changes.
1226 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1227 LLVMValueRef tmp
, tmp2
;
1229 /* workaround 8/8/8/8 uint/sint cube gather bug */
1230 /* first detect it then change to a scaled read and f2i */
1231 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1234 /* extract the DATA_FORMAT */
1235 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1236 LLVMConstInt(ctx
->i32
, 6, false), false);
1238 /* is the DATA_FORMAT == 8_8_8_8 */
1239 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1241 if (stype
== GLSL_TYPE_UINT
)
1242 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1243 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1244 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1246 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1247 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1248 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1250 /* replace the NUM FORMAT in the descriptor */
1251 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false), "");
1252 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1254 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1256 /* don't modify the coordinates for this case */
1257 for (unsigned c
= 0; c
< 2; ++c
)
1258 args
->coords
[c
] = LLVMBuildSelect(
1259 ctx
->builder
, compare_cube_wa
,
1260 orig_coords
[c
], args
->coords
[c
], "");
1263 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1264 result
= ac_build_image_opcode(ctx
, args
);
1266 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1267 LLVMValueRef tmp
, tmp2
;
1269 /* if the cube workaround is in place, f2i the result. */
1270 for (unsigned c
= 0; c
< 4; c
++) {
1271 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1272 if (stype
== GLSL_TYPE_UINT
)
1273 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1275 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1276 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1277 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1278 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1279 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1280 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1286 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1288 nir_deref_instr
*texture_deref_instr
= NULL
;
1290 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1291 switch (instr
->src
[i
].src_type
) {
1292 case nir_tex_src_texture_deref
:
1293 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1299 return texture_deref_instr
;
1302 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1303 const nir_tex_instr
*instr
,
1304 struct ac_image_args
*args
)
1306 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1307 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1309 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1310 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1314 util_last_bit(mask
),
1317 return ac_build_buffer_load_format(&ctx
->ac
,
1321 util_last_bit(mask
),
1326 args
->opcode
= ac_image_sample
;
1328 switch (instr
->op
) {
1330 case nir_texop_txf_ms
:
1331 case nir_texop_samples_identical
:
1332 args
->opcode
= args
->level_zero
||
1333 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1334 ac_image_load
: ac_image_load_mip
;
1335 args
->level_zero
= false;
1338 case nir_texop_query_levels
:
1339 args
->opcode
= ac_image_get_resinfo
;
1341 args
->lod
= ctx
->ac
.i32_0
;
1342 args
->level_zero
= false;
1345 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1347 args
->level_zero
= true;
1351 args
->opcode
= ac_image_gather4
;
1352 args
->level_zero
= true;
1355 args
->opcode
= ac_image_get_lod
;
1361 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= GFX8
) {
1362 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1363 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1364 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1365 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1366 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1367 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1371 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1372 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
== GFX9
) {
1373 if ((args
->dim
== ac_image_2darray
||
1374 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1375 args
->coords
[1] = ctx
->ac
.i32_0
;
1379 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1380 bool cs_derivs
= ctx
->stage
== MESA_SHADER_COMPUTE
&&
1381 ctx
->info
->cs
.derivative_group
!= DERIVATIVE_GROUP_NONE
;
1382 if (ctx
->stage
== MESA_SHADER_FRAGMENT
|| cs_derivs
) {
1383 /* Prevent texture instructions with implicit derivatives from being
1384 * sinked into branches. */
1385 switch (instr
->op
) {
1389 args
->attributes
|= AC_FUNC_ATTR_CONVERGENT
;
1396 return ac_build_image_opcode(&ctx
->ac
, args
);
1399 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1400 nir_intrinsic_instr
*instr
)
1402 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1403 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1405 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1406 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1410 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1411 nir_intrinsic_instr
*instr
)
1413 LLVMValueRef ptr
, addr
;
1414 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1415 unsigned index
= nir_intrinsic_base(instr
);
1417 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1418 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1420 /* Load constant values from user SGPRS when possible, otherwise
1421 * fallback to the default path that loads directly from memory.
1423 if (LLVMIsConstant(src0
) &&
1424 instr
->dest
.ssa
.bit_size
== 32) {
1425 unsigned count
= instr
->dest
.ssa
.num_components
;
1426 unsigned offset
= index
;
1428 offset
+= LLVMConstIntGetZExtValue(src0
);
1431 offset
-= ctx
->abi
->base_inline_push_consts
;
1433 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1434 return ac_build_gather_values(&ctx
->ac
,
1435 ctx
->abi
->inline_push_consts
+ offset
,
1440 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->abi
->push_constants
, &addr
, 1, "");
1442 if (instr
->dest
.ssa
.bit_size
== 8) {
1443 unsigned load_dwords
= instr
->dest
.ssa
.num_components
> 1 ? 2 : 1;
1444 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), 4 * load_dwords
);
1445 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1446 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1448 LLVMValueRef params
[3];
1449 if (load_dwords
> 1) {
1450 LLVMValueRef res_vec
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(ctx
->ac
.i32
, 2), "");
1451 params
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 1, false), "");
1452 params
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, res_vec
, LLVMConstInt(ctx
->ac
.i32
, 0, false), "");
1454 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, ctx
->ac
.i32
, "");
1455 params
[0] = ctx
->ac
.i32_0
;
1459 res
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.alignbyte", ctx
->ac
.i32
, params
, 3, 0);
1461 res
= LLVMBuildTrunc(ctx
->ac
.builder
, res
, LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.num_components
* 8), "");
1462 if (instr
->dest
.ssa
.num_components
> 1)
1463 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, LLVMVectorType(LLVMInt8TypeInContext(ctx
->ac
.context
), instr
->dest
.ssa
.num_components
), "");
1465 } else if (instr
->dest
.ssa
.bit_size
== 16) {
1466 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1467 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1468 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1469 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1470 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1471 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1472 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1473 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1474 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1475 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1476 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1477 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1478 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1479 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1480 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1481 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1484 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1486 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1489 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1490 const nir_intrinsic_instr
*instr
)
1492 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1494 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1497 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1499 uint32_t new_mask
= 0;
1500 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1501 if (mask
& (1u << i
))
1502 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1506 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1507 unsigned start
, unsigned count
)
1509 LLVMValueRef mask
[] = {
1510 ctx
->i32_0
, ctx
->i32_1
,
1511 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1513 unsigned src_elements
= ac_get_llvm_num_components(src
);
1515 if (count
== src_elements
) {
1518 } else if (count
== 1) {
1519 assert(start
< src_elements
);
1520 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1522 assert(start
+ count
<= src_elements
);
1524 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1525 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1529 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1530 enum gl_access_qualifier access
,
1531 bool may_store_unaligned
,
1532 bool writeonly_memory
)
1534 unsigned cache_policy
= 0;
1536 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1537 * store opcodes not aligned to a dword are affected. The only way to
1538 * get unaligned stores is through shader images.
1540 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== GFX6
) ||
1541 /* If this is write-only, don't keep data in L1 to prevent
1542 * evicting L1 cache lines that may be needed by other
1546 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1547 cache_policy
|= ac_glc
;
1550 return cache_policy
;
1553 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1554 nir_intrinsic_instr
*instr
)
1556 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1557 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1558 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1559 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1560 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1561 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1563 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1564 get_src(ctx
, instr
->src
[1]), true);
1565 LLVMValueRef base_data
= src_data
;
1566 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1567 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1571 LLVMValueRef data
, offset
;
1572 LLVMTypeRef data_type
;
1574 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1576 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1577 * writes into a 2-element and a 1-element write. */
1579 (elem_size_bytes
!= 4 || !ac_has_vec3_support(ctx
->ac
.chip_class
, false))) {
1580 writemask
|= 1 << (start
+ 2);
1583 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1585 /* we can only store 4 DWords at the same time.
1586 * can only happen for 64 Bit vectors. */
1587 if (num_bytes
> 16) {
1588 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1593 /* check alignment of 16 Bit stores */
1594 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1595 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1599 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1601 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1602 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1604 if (num_bytes
== 1) {
1605 ac_build_tbuffer_store_byte(&ctx
->ac
, rsrc
, data
,
1606 offset
, ctx
->ac
.i32_0
,
1608 } else if (num_bytes
== 2) {
1609 ac_build_tbuffer_store_short(&ctx
->ac
, rsrc
, data
,
1610 offset
, ctx
->ac
.i32_0
,
1613 int num_channels
= num_bytes
/ 4;
1615 switch (num_bytes
) {
1616 case 16: /* v4f32 */
1617 data_type
= ctx
->ac
.v4f32
;
1619 case 12: /* v3f32 */
1620 data_type
= ctx
->ac
.v3f32
;
1623 data_type
= ctx
->ac
.v2f32
;
1626 data_type
= ctx
->ac
.f32
;
1629 unreachable("Malformed vector store.");
1631 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1633 ac_build_buffer_store_dword(&ctx
->ac
, rsrc
, data
,
1634 num_channels
, offset
,
1636 cache_policy
, false);
1641 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1642 const nir_intrinsic_instr
*instr
)
1644 LLVMTypeRef return_type
= LLVMTypeOf(get_src(ctx
, instr
->src
[2]));
1646 char name
[64], type
[8];
1647 LLVMValueRef params
[6];
1650 switch (instr
->intrinsic
) {
1651 case nir_intrinsic_ssbo_atomic_add
:
1654 case nir_intrinsic_ssbo_atomic_imin
:
1657 case nir_intrinsic_ssbo_atomic_umin
:
1660 case nir_intrinsic_ssbo_atomic_imax
:
1663 case nir_intrinsic_ssbo_atomic_umax
:
1666 case nir_intrinsic_ssbo_atomic_and
:
1669 case nir_intrinsic_ssbo_atomic_or
:
1672 case nir_intrinsic_ssbo_atomic_xor
:
1675 case nir_intrinsic_ssbo_atomic_exchange
:
1678 case nir_intrinsic_ssbo_atomic_comp_swap
:
1685 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1686 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1688 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1689 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1690 get_src(ctx
, instr
->src
[0]),
1693 if (HAVE_LLVM
>= 0x900) {
1694 /* XXX: The new raw/struct atomic intrinsics are buggy with
1695 * LLVM 8, see r358579.
1697 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1698 params
[arg_count
++] = ctx
->ac
.i32_0
; /* soffset */
1699 params
[arg_count
++] = ctx
->ac
.i32_0
; /* slc */
1701 ac_build_type_name_for_intr(return_type
, type
, sizeof(type
));
1702 snprintf(name
, sizeof(name
),
1703 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op
, type
);
1705 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1706 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1707 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1709 assert(return_type
== ctx
->ac
.i32
);
1710 snprintf(name
, sizeof(name
),
1711 "llvm.amdgcn.buffer.atomic.%s", op
);
1714 return ac_build_intrinsic(&ctx
->ac
, name
, return_type
, params
,
1718 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1719 const nir_intrinsic_instr
*instr
)
1721 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1722 int num_components
= instr
->num_components
;
1723 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1724 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1726 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1727 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1728 get_src(ctx
, instr
->src
[0]), false);
1729 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1731 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1732 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1734 LLVMValueRef results
[4];
1735 for (int i
= 0; i
< num_components
;) {
1736 int num_elems
= num_components
- i
;
1737 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1739 if (num_elems
* elem_size_bytes
> 16)
1740 num_elems
= 16 / elem_size_bytes
;
1741 int load_bytes
= num_elems
* elem_size_bytes
;
1743 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1747 if (load_bytes
== 1) {
1748 ret
= ac_build_tbuffer_load_byte(&ctx
->ac
,
1754 } else if (load_bytes
== 2) {
1755 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1762 int num_channels
= util_next_power_of_two(load_bytes
) / 4;
1763 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
1765 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_channels
,
1766 vindex
, offset
, immoffset
, 0,
1767 cache_policy
, can_speculate
, false);
1770 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1771 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1772 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1774 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1775 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1777 for (unsigned j
= 0; j
< num_elems
; j
++) {
1778 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1783 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1786 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1787 const nir_intrinsic_instr
*instr
)
1790 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1791 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1792 int num_components
= instr
->num_components
;
1794 if (ctx
->abi
->load_ubo
)
1795 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1797 if (instr
->dest
.ssa
.bit_size
== 64)
1798 num_components
*= 2;
1800 if (instr
->dest
.ssa
.bit_size
== 16 || instr
->dest
.ssa
.bit_size
== 8) {
1801 unsigned load_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1802 LLVMValueRef results
[num_components
];
1803 for (unsigned i
= 0; i
< num_components
; ++i
) {
1804 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
,
1807 if (load_bytes
== 1) {
1808 results
[i
] = ac_build_tbuffer_load_byte(&ctx
->ac
,
1815 assert(load_bytes
== 2);
1816 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1824 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1826 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1827 NULL
, 0, 0, true, true);
1829 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1832 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1833 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1837 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1838 bool vs_in
, unsigned *vertex_index_out
,
1839 LLVMValueRef
*vertex_index_ref
,
1840 unsigned *const_out
, LLVMValueRef
*indir_out
)
1842 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1843 nir_deref_path path
;
1844 unsigned idx_lvl
= 1;
1846 nir_deref_path_init(&path
, instr
, NULL
);
1848 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1849 if (vertex_index_ref
) {
1850 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1851 if (vertex_index_out
)
1852 *vertex_index_out
= 0;
1854 *vertex_index_out
= nir_src_as_uint(path
.path
[idx_lvl
]->arr
.index
);
1859 uint32_t const_offset
= 0;
1860 LLVMValueRef offset
= NULL
;
1862 if (var
->data
.compact
) {
1863 assert(instr
->deref_type
== nir_deref_type_array
);
1864 const_offset
= nir_src_as_uint(instr
->arr
.index
);
1868 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1869 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1870 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1871 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1873 for (unsigned i
= 0; i
< index
; i
++) {
1874 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1875 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1877 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1878 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1879 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1880 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1882 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1886 unreachable("Uhandled deref type in get_deref_instr_offset");
1890 nir_deref_path_finish(&path
);
1892 if (const_offset
&& offset
)
1893 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1894 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1897 *const_out
= const_offset
;
1898 *indir_out
= offset
;
1901 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1902 nir_intrinsic_instr
*instr
,
1905 LLVMValueRef result
;
1906 LLVMValueRef vertex_index
= NULL
;
1907 LLVMValueRef indir_index
= NULL
;
1908 unsigned const_index
= 0;
1910 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1912 unsigned location
= var
->data
.location
;
1913 unsigned driver_location
= var
->data
.driver_location
;
1914 const bool is_patch
= var
->data
.patch
;
1915 const bool is_compact
= var
->data
.compact
;
1917 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1918 false, NULL
, is_patch
? NULL
: &vertex_index
,
1919 &const_index
, &indir_index
);
1921 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1923 LLVMTypeRef src_component_type
;
1924 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1925 src_component_type
= LLVMGetElementType(dest_type
);
1927 src_component_type
= dest_type
;
1929 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1930 vertex_index
, indir_index
,
1931 const_index
, location
, driver_location
,
1932 var
->data
.location_frac
,
1933 instr
->num_components
,
1934 is_patch
, is_compact
, load_inputs
);
1935 if (instr
->dest
.ssa
.bit_size
== 16) {
1936 result
= ac_to_integer(&ctx
->ac
, result
);
1937 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1939 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1943 type_scalar_size_bytes(const struct glsl_type
*type
)
1945 assert(glsl_type_is_vector_or_scalar(type
) ||
1946 glsl_type_is_matrix(type
));
1947 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1950 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1951 nir_intrinsic_instr
*instr
)
1953 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1954 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1956 LLVMValueRef values
[8];
1958 int ve
= instr
->dest
.ssa
.num_components
;
1960 LLVMValueRef indir_index
;
1962 unsigned const_index
;
1963 unsigned stride
= 4;
1964 int mode
= deref
->mode
;
1967 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1968 var
->data
.mode
== nir_var_shader_in
;
1969 idx
= var
->data
.driver_location
;
1970 comp
= var
->data
.location_frac
;
1971 mode
= var
->data
.mode
;
1973 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1974 &const_index
, &indir_index
);
1976 if (var
->data
.compact
) {
1978 const_index
+= comp
;
1983 if (instr
->dest
.ssa
.bit_size
== 64 &&
1984 (deref
->mode
== nir_var_shader_in
||
1985 deref
->mode
== nir_var_shader_out
||
1986 deref
->mode
== nir_var_function_temp
))
1990 case nir_var_shader_in
:
1991 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1992 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1993 return load_tess_varyings(ctx
, instr
, true);
1996 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1997 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1998 LLVMValueRef indir_index
;
1999 unsigned const_index
, vertex_index
;
2000 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
2001 &const_index
, &indir_index
);
2003 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
2004 var
->data
.driver_location
,
2005 var
->data
.location_frac
,
2006 instr
->num_components
, vertex_index
, const_index
, type
);
2009 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2011 unsigned count
= glsl_count_attribute_slots(
2013 ctx
->stage
== MESA_SHADER_VERTEX
);
2015 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2016 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
2017 stride
, false, true);
2019 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2023 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
2026 case nir_var_function_temp
:
2027 for (unsigned chan
= 0; chan
< ve
; chan
++) {
2029 unsigned count
= glsl_count_attribute_slots(
2032 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2033 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2034 stride
, true, true);
2036 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2040 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2044 case nir_var_mem_shared
: {
2045 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2046 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2047 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2048 get_def_type(ctx
, &instr
->dest
.ssa
),
2051 case nir_var_shader_out
:
2052 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2053 return load_tess_varyings(ctx
, instr
, false);
2056 if (ctx
->stage
== MESA_SHADER_FRAGMENT
&&
2057 var
->data
.fb_fetch_output
&&
2058 ctx
->abi
->emit_fbfetch
)
2059 return ctx
->abi
->emit_fbfetch(ctx
->abi
);
2061 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2063 unsigned count
= glsl_count_attribute_slots(
2066 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2067 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2068 stride
, true, true);
2070 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2074 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2075 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2080 case nir_var_mem_global
: {
2081 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2082 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2083 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2084 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2086 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2087 if (stride
!= natural_stride
) {
2088 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2089 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2090 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2092 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2093 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2094 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2095 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2097 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2099 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2100 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2101 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2102 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2107 unreachable("unhandle variable mode");
2109 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2110 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2114 visit_store_var(struct ac_nir_context
*ctx
,
2115 nir_intrinsic_instr
*instr
)
2117 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2118 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2120 LLVMValueRef temp_ptr
, value
;
2123 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2124 int writemask
= instr
->const_index
[0];
2125 LLVMValueRef indir_index
;
2126 unsigned const_index
;
2129 get_deref_offset(ctx
, deref
, false,
2130 NULL
, NULL
, &const_index
, &indir_index
);
2131 idx
= var
->data
.driver_location
;
2132 comp
= var
->data
.location_frac
;
2134 if (var
->data
.compact
) {
2135 const_index
+= comp
;
2140 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2141 (deref
->mode
== nir_var_shader_out
||
2142 deref
->mode
== nir_var_function_temp
)) {
2144 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2145 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2148 writemask
= widen_mask(writemask
, 2);
2151 writemask
= writemask
<< comp
;
2153 switch (deref
->mode
) {
2154 case nir_var_shader_out
:
2156 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2157 LLVMValueRef vertex_index
= NULL
;
2158 LLVMValueRef indir_index
= NULL
;
2159 unsigned const_index
= 0;
2160 const bool is_patch
= var
->data
.patch
;
2162 get_deref_offset(ctx
, deref
, false, NULL
,
2163 is_patch
? NULL
: &vertex_index
,
2164 &const_index
, &indir_index
);
2166 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2167 vertex_index
, indir_index
,
2168 const_index
, src
, writemask
);
2172 for (unsigned chan
= 0; chan
< 8; chan
++) {
2174 if (!(writemask
& (1 << chan
)))
2177 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2179 if (var
->data
.compact
)
2182 unsigned count
= glsl_count_attribute_slots(
2185 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2186 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2187 stride
, true, true);
2189 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2190 value
, indir_index
, "");
2191 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2192 count
, stride
, tmp_vec
);
2195 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2197 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2201 case nir_var_function_temp
:
2202 for (unsigned chan
= 0; chan
< 8; chan
++) {
2203 if (!(writemask
& (1 << chan
)))
2206 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2208 unsigned count
= glsl_count_attribute_slots(
2211 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2212 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2215 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2216 value
, indir_index
, "");
2217 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2220 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2222 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2227 case nir_var_mem_global
:
2228 case nir_var_mem_shared
: {
2229 int writemask
= instr
->const_index
[0];
2230 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2231 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2233 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2234 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2235 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2237 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2238 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2239 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2241 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2242 stride
== natural_stride
) {
2243 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2244 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2245 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2247 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2248 LLVMGetElementType(LLVMTypeOf(address
)), "");
2249 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2251 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2252 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2253 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2254 for (unsigned chan
= 0; chan
< 4; chan
++) {
2255 if (!(writemask
& (1 << chan
)))
2258 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2260 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2261 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2263 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2264 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2265 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2276 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2279 case GLSL_SAMPLER_DIM_BUF
:
2281 case GLSL_SAMPLER_DIM_1D
:
2282 return array
? 2 : 1;
2283 case GLSL_SAMPLER_DIM_2D
:
2284 return array
? 3 : 2;
2285 case GLSL_SAMPLER_DIM_MS
:
2286 return array
? 4 : 3;
2287 case GLSL_SAMPLER_DIM_3D
:
2288 case GLSL_SAMPLER_DIM_CUBE
:
2290 case GLSL_SAMPLER_DIM_RECT
:
2291 case GLSL_SAMPLER_DIM_SUBPASS
:
2293 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2301 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2302 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2303 LLVMValueRef coord_z
,
2304 LLVMValueRef sample_index
,
2305 LLVMValueRef fmask_desc_ptr
)
2307 unsigned sample_chan
= coord_z
? 3 : 2;
2308 LLVMValueRef addr
[4] = {coord_x
, coord_y
, coord_z
};
2309 addr
[sample_chan
] = sample_index
;
2311 ac_apply_fmask_to_sample(ctx
, fmask_desc_ptr
, addr
, coord_z
!= NULL
);
2312 return addr
[sample_chan
];
2315 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2317 assert(instr
->src
[0].is_ssa
);
2318 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2321 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2322 const nir_intrinsic_instr
*instr
,
2323 enum ac_descriptor_type desc_type
,
2326 nir_deref_instr
*deref_instr
=
2327 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_deref
?
2328 nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
) : NULL
;
2330 return get_sampler_desc(ctx
, deref_instr
, desc_type
, &instr
->instr
, true, write
);
2333 static void get_image_coords(struct ac_nir_context
*ctx
,
2334 const nir_intrinsic_instr
*instr
,
2335 struct ac_image_args
*args
,
2336 enum glsl_sampler_dim dim
,
2339 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2340 LLVMValueRef masks
[] = {
2341 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2342 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2344 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2347 MAYBE_UNUSED
bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2348 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2349 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2350 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2351 bool gfx9_1d
= ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2352 assert(!add_frag_pos
&& "Input attachments should be lowered by this point.");
2353 count
= image_type_to_components_count(dim
, is_array
);
2355 if (is_ms
&& (instr
->intrinsic
== nir_intrinsic_image_deref_load
||
2356 instr
->intrinsic
== nir_intrinsic_bindless_image_load
)) {
2357 LLVMValueRef fmask_load_address
[3];
2359 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2360 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2362 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2364 fmask_load_address
[2] = NULL
;
2366 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2367 fmask_load_address
[0],
2368 fmask_load_address
[1],
2369 fmask_load_address
[2],
2371 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2372 AC_DESC_FMASK
, &instr
->instr
, false, false));
2374 if (count
== 1 && !gfx9_1d
) {
2375 if (instr
->src
[1].ssa
->num_components
)
2376 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2378 args
->coords
[0] = src0
;
2383 for (chan
= 0; chan
< count
; ++chan
) {
2384 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2389 args
->coords
[2] = args
->coords
[1];
2390 args
->coords
[1] = ctx
->ac
.i32_0
;
2392 args
->coords
[1] = ctx
->ac
.i32_0
;
2397 args
->coords
[count
] = sample_index
;
2403 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2404 const nir_intrinsic_instr
*instr
,
2405 bool write
, bool atomic
)
2407 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2408 if (ctx
->abi
->gfx9_stride_size_workaround
||
2409 (ctx
->abi
->gfx9_stride_size_workaround_for_atomic
&& atomic
)) {
2410 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2411 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2412 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2414 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2415 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2416 elem_count
, stride
, "");
2418 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2419 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2424 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2425 const nir_intrinsic_instr
*instr
,
2430 enum glsl_sampler_dim dim
;
2431 enum gl_access_qualifier access
;
2434 dim
= nir_intrinsic_image_dim(instr
);
2435 access
= nir_intrinsic_access(instr
);
2436 is_array
= nir_intrinsic_image_array(instr
);
2438 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2439 const struct glsl_type
*type
= image_deref
->type
;
2440 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2441 dim
= glsl_get_sampler_dim(type
);
2442 access
= var
->data
.image
.access
;
2443 is_array
= glsl_sampler_type_is_array(type
);
2446 struct ac_image_args args
= {};
2448 args
.cache_policy
= get_cache_policy(ctx
, access
, false, false);
2450 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2451 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2452 unsigned num_channels
= util_last_bit(mask
);
2453 LLVMValueRef rsrc
, vindex
;
2455 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false, false);
2456 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2459 bool can_speculate
= access
& ACCESS_CAN_REORDER
;
2460 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2461 ctx
->ac
.i32_0
, num_channels
,
2464 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2466 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2467 res
= ac_to_integer(&ctx
->ac
, res
);
2469 args
.opcode
= ac_image_load
;
2470 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2471 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2472 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2474 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2476 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2481 static void visit_image_store(struct ac_nir_context
*ctx
,
2482 nir_intrinsic_instr
*instr
,
2487 enum glsl_sampler_dim dim
;
2488 enum gl_access_qualifier access
;
2491 dim
= nir_intrinsic_image_dim(instr
);
2492 access
= nir_intrinsic_access(instr
);
2493 is_array
= nir_intrinsic_image_array(instr
);
2495 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2496 const struct glsl_type
*type
= image_deref
->type
;
2497 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2498 dim
= glsl_get_sampler_dim(type
);
2499 access
= var
->data
.image
.access
;
2500 is_array
= glsl_sampler_type_is_array(type
);
2503 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
2504 struct ac_image_args args
= {};
2506 args
.cache_policy
= get_cache_policy(ctx
, access
, true, writeonly_memory
);
2508 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2509 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true, false);
2510 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2511 unsigned src_channels
= ac_get_llvm_num_components(src
);
2512 LLVMValueRef vindex
;
2514 if (src_channels
== 3)
2515 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2517 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2518 get_src(ctx
, instr
->src
[1]),
2521 ac_build_buffer_store_format(&ctx
->ac
, rsrc
, src
, vindex
,
2522 ctx
->ac
.i32_0
, src_channels
,
2525 args
.opcode
= ac_image_store
;
2526 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2527 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2528 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2529 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2532 ac_build_image_opcode(&ctx
->ac
, &args
);
2537 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2538 const nir_intrinsic_instr
*instr
,
2541 LLVMValueRef params
[7];
2542 int param_count
= 0;
2544 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
||
2545 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_comp_swap
;
2546 const char *atomic_name
;
2547 char intrinsic_name
[64];
2548 enum ac_atomic_op atomic_subop
;
2549 MAYBE_UNUSED
int length
;
2551 enum glsl_sampler_dim dim
;
2552 bool is_unsigned
= false;
2555 if (instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_min
||
2556 instr
->intrinsic
== nir_intrinsic_bindless_image_atomic_max
) {
2557 const GLenum format
= nir_intrinsic_format(instr
);
2558 assert(format
== GL_R32UI
|| format
== GL_R32I
);
2559 is_unsigned
= format
== GL_R32UI
;
2561 dim
= nir_intrinsic_image_dim(instr
);
2562 is_array
= nir_intrinsic_image_array(instr
);
2564 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2565 is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2566 dim
= glsl_get_sampler_dim(type
);
2567 is_array
= glsl_sampler_type_is_array(type
);
2570 switch (instr
->intrinsic
) {
2571 case nir_intrinsic_bindless_image_atomic_add
:
2572 case nir_intrinsic_image_deref_atomic_add
:
2573 atomic_name
= "add";
2574 atomic_subop
= ac_atomic_add
;
2576 case nir_intrinsic_bindless_image_atomic_min
:
2577 case nir_intrinsic_image_deref_atomic_min
:
2578 atomic_name
= is_unsigned
? "umin" : "smin";
2579 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2581 case nir_intrinsic_bindless_image_atomic_max
:
2582 case nir_intrinsic_image_deref_atomic_max
:
2583 atomic_name
= is_unsigned
? "umax" : "smax";
2584 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2586 case nir_intrinsic_bindless_image_atomic_and
:
2587 case nir_intrinsic_image_deref_atomic_and
:
2588 atomic_name
= "and";
2589 atomic_subop
= ac_atomic_and
;
2591 case nir_intrinsic_bindless_image_atomic_or
:
2592 case nir_intrinsic_image_deref_atomic_or
:
2594 atomic_subop
= ac_atomic_or
;
2596 case nir_intrinsic_bindless_image_atomic_xor
:
2597 case nir_intrinsic_image_deref_atomic_xor
:
2598 atomic_name
= "xor";
2599 atomic_subop
= ac_atomic_xor
;
2601 case nir_intrinsic_bindless_image_atomic_exchange
:
2602 case nir_intrinsic_image_deref_atomic_exchange
:
2603 atomic_name
= "swap";
2604 atomic_subop
= ac_atomic_swap
;
2606 case nir_intrinsic_bindless_image_atomic_comp_swap
:
2607 case nir_intrinsic_image_deref_atomic_comp_swap
:
2608 atomic_name
= "cmpswap";
2609 atomic_subop
= 0; /* not used */
2616 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2617 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2619 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2620 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true, true);
2621 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2622 ctx
->ac
.i32_0
, ""); /* vindex */
2623 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2624 if (HAVE_LLVM
>= 0x900) {
2625 /* XXX: The new raw/struct atomic intrinsics are buggy
2626 * with LLVM 8, see r358579.
2628 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2629 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2631 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2632 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2634 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2636 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2637 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2640 assert(length
< sizeof(intrinsic_name
));
2641 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2642 params
, param_count
, 0);
2644 struct ac_image_args args
= {};
2645 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2646 args
.atomic
= atomic_subop
;
2647 args
.data
[0] = params
[0];
2649 args
.data
[1] = params
[1];
2650 get_image_coords(ctx
, instr
, &args
, dim
, is_array
);
2651 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2652 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2654 return ac_build_image_opcode(&ctx
->ac
, &args
);
2658 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2659 const nir_intrinsic_instr
*instr
,
2662 enum glsl_sampler_dim dim
;
2665 dim
= nir_intrinsic_image_dim(instr
);
2666 is_array
= nir_intrinsic_image_array(instr
);
2668 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2669 dim
= glsl_get_sampler_dim(type
);
2670 is_array
= glsl_sampler_type_is_array(type
);
2673 struct ac_image_args args
= { 0 };
2674 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, dim
, is_array
);
2676 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2677 args
.opcode
= ac_image_get_resinfo
;
2678 args
.lod
= ctx
->ac
.i32_0
;
2679 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2681 return ac_build_image_opcode(&ctx
->ac
, &args
);
2684 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2685 const nir_intrinsic_instr
*instr
,
2690 enum glsl_sampler_dim dim
;
2693 dim
= nir_intrinsic_image_dim(instr
);
2694 is_array
= nir_intrinsic_image_array(instr
);
2696 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2697 dim
= glsl_get_sampler_dim(type
);
2698 is_array
= glsl_sampler_type_is_array(type
);
2701 if (dim
== GLSL_SAMPLER_DIM_BUF
)
2702 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2704 struct ac_image_args args
= { 0 };
2706 args
.dim
= get_ac_image_dim(&ctx
->ac
, dim
, is_array
);
2708 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2709 args
.opcode
= ac_image_get_resinfo
;
2710 args
.lod
= ctx
->ac
.i32_0
;
2711 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2713 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2715 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2717 if (dim
== GLSL_SAMPLER_DIM_CUBE
&& is_array
) {
2718 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2719 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2720 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2721 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2723 if (ctx
->ac
.chip_class
== GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
&& is_array
) {
2724 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2725 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2732 static void emit_membar(struct ac_llvm_context
*ac
,
2733 const nir_intrinsic_instr
*instr
)
2735 unsigned wait_flags
= 0;
2737 switch (instr
->intrinsic
) {
2738 case nir_intrinsic_memory_barrier
:
2739 case nir_intrinsic_group_memory_barrier
:
2740 wait_flags
= AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2742 case nir_intrinsic_memory_barrier_atomic_counter
:
2743 case nir_intrinsic_memory_barrier_buffer
:
2744 case nir_intrinsic_memory_barrier_image
:
2745 wait_flags
= AC_WAIT_VLOAD
| AC_WAIT_VSTORE
;
2747 case nir_intrinsic_memory_barrier_shared
:
2748 wait_flags
= AC_WAIT_LGKM
;
2754 ac_build_waitcnt(ac
, wait_flags
);
2757 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2759 /* GFX6 only (thanks to a hw bug workaround):
2760 * The real barrier instruction isn’t needed, because an entire patch
2761 * always fits into a single wave.
2763 if (ac
->chip_class
== GFX6
&& stage
== MESA_SHADER_TESS_CTRL
) {
2764 ac_build_waitcnt(ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
2767 ac_build_s_barrier(ac
);
2770 static void emit_discard(struct ac_nir_context
*ctx
,
2771 const nir_intrinsic_instr
*instr
)
2775 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2776 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2777 get_src(ctx
, instr
->src
[0]),
2780 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2781 cond
= ctx
->ac
.i1false
;
2784 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2788 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2790 LLVMValueRef result
;
2791 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2792 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2793 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2795 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2799 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2801 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2802 LLVMValueRef result
;
2803 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2804 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2805 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2807 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2812 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2814 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2815 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2816 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2818 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2823 visit_first_invocation(struct ac_nir_context
*ctx
)
2825 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2826 const char *intr
= ctx
->ac
.wave_size
== 32 ? "llvm.cttz.i32" : "llvm.cttz.i64";
2828 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2829 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2830 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
, intr
,
2831 ctx
->ac
.iN_wavemask
, args
, 2,
2832 AC_FUNC_ATTR_NOUNWIND
|
2833 AC_FUNC_ATTR_READNONE
);
2835 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2839 visit_load_shared(struct ac_nir_context
*ctx
,
2840 const nir_intrinsic_instr
*instr
)
2842 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2844 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2846 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2847 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2848 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2849 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2852 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2853 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2857 visit_store_shared(struct ac_nir_context
*ctx
,
2858 const nir_intrinsic_instr
*instr
)
2860 LLVMValueRef derived_ptr
, data
,index
;
2861 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2863 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2864 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2866 int writemask
= nir_intrinsic_write_mask(instr
);
2867 for (int chan
= 0; chan
< 4; chan
++) {
2868 if (!(writemask
& (1 << chan
))) {
2871 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2872 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2873 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2874 LLVMBuildStore(builder
, data
, derived_ptr
);
2878 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2879 const nir_intrinsic_instr
*instr
,
2880 LLVMValueRef ptr
, int src_idx
)
2882 LLVMValueRef result
;
2883 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2885 const char *sync_scope
= HAVE_LLVM
>= 0x0900 ? "workgroup-one-as" : "workgroup";
2887 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2888 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2889 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2890 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, src
, src1
, sync_scope
);
2891 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2893 LLVMAtomicRMWBinOp op
;
2894 switch (instr
->intrinsic
) {
2895 case nir_intrinsic_shared_atomic_add
:
2896 case nir_intrinsic_deref_atomic_add
:
2897 op
= LLVMAtomicRMWBinOpAdd
;
2899 case nir_intrinsic_shared_atomic_umin
:
2900 case nir_intrinsic_deref_atomic_umin
:
2901 op
= LLVMAtomicRMWBinOpUMin
;
2903 case nir_intrinsic_shared_atomic_umax
:
2904 case nir_intrinsic_deref_atomic_umax
:
2905 op
= LLVMAtomicRMWBinOpUMax
;
2907 case nir_intrinsic_shared_atomic_imin
:
2908 case nir_intrinsic_deref_atomic_imin
:
2909 op
= LLVMAtomicRMWBinOpMin
;
2911 case nir_intrinsic_shared_atomic_imax
:
2912 case nir_intrinsic_deref_atomic_imax
:
2913 op
= LLVMAtomicRMWBinOpMax
;
2915 case nir_intrinsic_shared_atomic_and
:
2916 case nir_intrinsic_deref_atomic_and
:
2917 op
= LLVMAtomicRMWBinOpAnd
;
2919 case nir_intrinsic_shared_atomic_or
:
2920 case nir_intrinsic_deref_atomic_or
:
2921 op
= LLVMAtomicRMWBinOpOr
;
2923 case nir_intrinsic_shared_atomic_xor
:
2924 case nir_intrinsic_deref_atomic_xor
:
2925 op
= LLVMAtomicRMWBinOpXor
;
2927 case nir_intrinsic_shared_atomic_exchange
:
2928 case nir_intrinsic_deref_atomic_exchange
:
2929 op
= LLVMAtomicRMWBinOpXchg
;
2935 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
), sync_scope
);
2940 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2942 LLVMValueRef values
[2];
2943 LLVMValueRef pos
[2];
2945 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2946 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2948 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2949 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2950 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2953 static LLVMValueRef
barycentric_center(struct ac_nir_context
*ctx
,
2956 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTER
);
2957 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
2960 static LLVMValueRef
barycentric_offset(struct ac_nir_context
*ctx
,
2962 LLVMValueRef offset
)
2964 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTER
);
2965 LLVMValueRef src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_0
, ""));
2966 LLVMValueRef src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, offset
, ctx
->ac
.i32_1
, ""));
2968 LLVMValueRef ij_out
[2];
2969 LLVMValueRef ddxy_out
= ac_build_ddxy_interp(&ctx
->ac
, interp_param
);
2972 * take the I then J parameters, and the DDX/Y for it, and
2973 * calculate the IJ inputs for the interpolator.
2974 * temp1 = ddx * offset/sample.x + I;
2975 * interp_param.I = ddy * offset/sample.y + temp1;
2976 * temp1 = ddx * offset/sample.x + J;
2977 * interp_param.J = ddy * offset/sample.y + temp1;
2979 for (unsigned i
= 0; i
< 2; i
++) {
2980 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2981 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2982 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2983 ddxy_out
, ix_ll
, "");
2984 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2985 ddxy_out
, iy_ll
, "");
2986 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2987 interp_param
, ix_ll
, "");
2988 LLVMValueRef temp1
, temp2
;
2990 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
2993 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
2994 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
2996 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
2997 temp2
, ctx
->ac
.i32
, "");
2999 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3000 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3003 static LLVMValueRef
barycentric_centroid(struct ac_nir_context
*ctx
,
3006 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_CENTROID
);
3007 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3010 static LLVMValueRef
barycentric_at_sample(struct ac_nir_context
*ctx
,
3012 LLVMValueRef sample_id
)
3014 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
3016 /* fetch sample ID */
3017 LLVMValueRef sample_pos
= ctx
->abi
->load_sample_position(ctx
->abi
, sample_id
);
3019 LLVMValueRef src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_0
, "");
3020 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
3021 LLVMValueRef src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_pos
, ctx
->ac
.i32_1
, "");
3022 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
3023 LLVMValueRef coords
[] = { src_c0
, src_c1
};
3024 LLVMValueRef offset
= ac_build_gather_values(&ctx
->ac
, coords
, 2);
3026 return barycentric_offset(ctx
, mode
, offset
);
3030 static LLVMValueRef
barycentric_sample(struct ac_nir_context
*ctx
,
3033 LLVMValueRef interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, mode
, INTERP_SAMPLE
);
3034 return LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
, ctx
->ac
.v2i32
, "");
3037 static LLVMValueRef
load_interpolated_input(struct ac_nir_context
*ctx
,
3038 LLVMValueRef interp_param
,
3039 unsigned index
, unsigned comp_start
,
3040 unsigned num_components
,
3043 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3045 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3046 interp_param
, ctx
->ac
.v2f32
, "");
3047 LLVMValueRef i
= LLVMBuildExtractElement(
3048 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3049 LLVMValueRef j
= LLVMBuildExtractElement(
3050 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3052 LLVMValueRef values
[4];
3053 assert(bitsize
== 16 || bitsize
== 32);
3054 for (unsigned comp
= 0; comp
< num_components
; comp
++) {
3055 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, comp_start
+ comp
, false);
3056 if (bitsize
== 16) {
3057 values
[comp
] = ac_build_fs_interp_f16(&ctx
->ac
, llvm_chan
, attr_number
,
3058 ctx
->abi
->prim_mask
, i
, j
);
3060 values
[comp
] = ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3061 ctx
->abi
->prim_mask
, i
, j
);
3065 return ac_to_integer(&ctx
->ac
, ac_build_gather_values(&ctx
->ac
, values
, num_components
));
3068 static LLVMValueRef
load_flat_input(struct ac_nir_context
*ctx
,
3069 unsigned index
, unsigned comp_start
,
3070 unsigned num_components
,
3073 LLVMValueRef attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
, false);
3075 LLVMValueRef values
[8];
3077 /* Each component of a 64-bit value takes up two GL-level channels. */
3079 bit_size
== 64 ? num_components
* 2 : num_components
;
3081 for (unsigned chan
= 0; chan
< channels
; chan
++) {
3082 if (comp_start
+ chan
> 4)
3083 attr_number
= LLVMConstInt(ctx
->ac
.i32
, index
+ 1, false);
3084 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, (comp_start
+ chan
) % 4, false);
3085 values
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
3086 LLVMConstInt(ctx
->ac
.i32
, 2, false),
3089 ctx
->abi
->prim_mask
);
3090 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
3091 values
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, values
[chan
],
3092 bit_size
== 16 ? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
3095 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, channels
);
3096 if (bit_size
== 64) {
3097 LLVMTypeRef type
= num_components
== 1 ? ctx
->ac
.i64
:
3098 LLVMVectorType(ctx
->ac
.i64
, num_components
);
3099 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
3104 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3105 nir_intrinsic_instr
*instr
)
3107 LLVMValueRef result
= NULL
;
3109 switch (instr
->intrinsic
) {
3110 case nir_intrinsic_ballot
:
3111 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3113 case nir_intrinsic_read_invocation
:
3114 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3115 get_src(ctx
, instr
->src
[1]));
3117 case nir_intrinsic_read_first_invocation
:
3118 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3120 case nir_intrinsic_load_subgroup_invocation
:
3121 result
= ac_get_thread_id(&ctx
->ac
);
3123 case nir_intrinsic_load_work_group_id
: {
3124 LLVMValueRef values
[3];
3126 for (int i
= 0; i
< 3; i
++) {
3127 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3128 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3131 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3134 case nir_intrinsic_load_base_vertex
:
3135 case nir_intrinsic_load_first_vertex
:
3136 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3138 case nir_intrinsic_load_local_group_size
:
3139 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3141 case nir_intrinsic_load_vertex_id
:
3142 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3143 ctx
->abi
->base_vertex
, "");
3145 case nir_intrinsic_load_vertex_id_zero_base
: {
3146 result
= ctx
->abi
->vertex_id
;
3149 case nir_intrinsic_load_local_invocation_id
: {
3150 result
= ctx
->abi
->local_invocation_ids
;
3153 case nir_intrinsic_load_base_instance
:
3154 result
= ctx
->abi
->start_instance
;
3156 case nir_intrinsic_load_draw_id
:
3157 result
= ctx
->abi
->draw_id
;
3159 case nir_intrinsic_load_view_index
:
3160 result
= ctx
->abi
->view_index
;
3162 case nir_intrinsic_load_invocation_id
:
3163 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3164 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3166 if (ctx
->ac
.chip_class
>= GFX10
) {
3167 result
= LLVMBuildAnd(ctx
->ac
.builder
,
3168 ctx
->abi
->gs_invocation_id
,
3169 LLVMConstInt(ctx
->ac
.i32
, 127, 0), "");
3171 result
= ctx
->abi
->gs_invocation_id
;
3175 case nir_intrinsic_load_primitive_id
:
3176 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3177 result
= ctx
->abi
->gs_prim_id
;
3178 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3179 result
= ctx
->abi
->tcs_patch_id
;
3180 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3181 result
= ctx
->abi
->tes_patch_id
;
3183 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3185 case nir_intrinsic_load_sample_id
:
3186 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3188 case nir_intrinsic_load_sample_pos
:
3189 result
= load_sample_pos(ctx
);
3191 case nir_intrinsic_load_sample_mask_in
:
3192 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3194 case nir_intrinsic_load_frag_coord
: {
3195 LLVMValueRef values
[4] = {
3196 ctx
->abi
->frag_pos
[0],
3197 ctx
->abi
->frag_pos
[1],
3198 ctx
->abi
->frag_pos
[2],
3199 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3201 result
= ac_to_integer(&ctx
->ac
,
3202 ac_build_gather_values(&ctx
->ac
, values
, 4));
3205 case nir_intrinsic_load_layer_id
:
3206 result
= ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
3208 case nir_intrinsic_load_front_face
:
3209 result
= ctx
->abi
->front_face
;
3211 case nir_intrinsic_load_helper_invocation
:
3212 result
= ac_build_load_helper_invocation(&ctx
->ac
);
3214 case nir_intrinsic_load_color0
:
3215 result
= ctx
->abi
->color0
;
3217 case nir_intrinsic_load_color1
:
3218 result
= ctx
->abi
->color1
;
3220 case nir_intrinsic_load_instance_id
:
3221 result
= ctx
->abi
->instance_id
;
3223 case nir_intrinsic_load_num_work_groups
:
3224 result
= ctx
->abi
->num_work_groups
;
3226 case nir_intrinsic_load_local_invocation_index
:
3227 result
= visit_load_local_invocation_index(ctx
);
3229 case nir_intrinsic_load_subgroup_id
:
3230 result
= visit_load_subgroup_id(ctx
);
3232 case nir_intrinsic_load_num_subgroups
:
3233 result
= visit_load_num_subgroups(ctx
);
3235 case nir_intrinsic_first_invocation
:
3236 result
= visit_first_invocation(ctx
);
3238 case nir_intrinsic_load_push_constant
:
3239 result
= visit_load_push_constant(ctx
, instr
);
3241 case nir_intrinsic_vulkan_resource_index
: {
3242 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3243 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3244 unsigned binding
= nir_intrinsic_binding(instr
);
3246 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3250 case nir_intrinsic_vulkan_resource_reindex
:
3251 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3253 case nir_intrinsic_store_ssbo
:
3254 visit_store_ssbo(ctx
, instr
);
3256 case nir_intrinsic_load_ssbo
:
3257 result
= visit_load_buffer(ctx
, instr
);
3259 case nir_intrinsic_ssbo_atomic_add
:
3260 case nir_intrinsic_ssbo_atomic_imin
:
3261 case nir_intrinsic_ssbo_atomic_umin
:
3262 case nir_intrinsic_ssbo_atomic_imax
:
3263 case nir_intrinsic_ssbo_atomic_umax
:
3264 case nir_intrinsic_ssbo_atomic_and
:
3265 case nir_intrinsic_ssbo_atomic_or
:
3266 case nir_intrinsic_ssbo_atomic_xor
:
3267 case nir_intrinsic_ssbo_atomic_exchange
:
3268 case nir_intrinsic_ssbo_atomic_comp_swap
:
3269 result
= visit_atomic_ssbo(ctx
, instr
);
3271 case nir_intrinsic_load_ubo
:
3272 result
= visit_load_ubo_buffer(ctx
, instr
);
3274 case nir_intrinsic_get_buffer_size
:
3275 result
= visit_get_buffer_size(ctx
, instr
);
3277 case nir_intrinsic_load_deref
:
3278 result
= visit_load_var(ctx
, instr
);
3280 case nir_intrinsic_store_deref
:
3281 visit_store_var(ctx
, instr
);
3283 case nir_intrinsic_load_shared
:
3284 result
= visit_load_shared(ctx
, instr
);
3286 case nir_intrinsic_store_shared
:
3287 visit_store_shared(ctx
, instr
);
3289 case nir_intrinsic_bindless_image_samples
:
3290 result
= visit_image_samples(ctx
, instr
, true);
3292 case nir_intrinsic_image_deref_samples
:
3293 result
= visit_image_samples(ctx
, instr
, false);
3295 case nir_intrinsic_bindless_image_load
:
3296 result
= visit_image_load(ctx
, instr
, true);
3298 case nir_intrinsic_image_deref_load
:
3299 result
= visit_image_load(ctx
, instr
, false);
3301 case nir_intrinsic_bindless_image_store
:
3302 visit_image_store(ctx
, instr
, true);
3304 case nir_intrinsic_image_deref_store
:
3305 visit_image_store(ctx
, instr
, false);
3307 case nir_intrinsic_bindless_image_atomic_add
:
3308 case nir_intrinsic_bindless_image_atomic_min
:
3309 case nir_intrinsic_bindless_image_atomic_max
:
3310 case nir_intrinsic_bindless_image_atomic_and
:
3311 case nir_intrinsic_bindless_image_atomic_or
:
3312 case nir_intrinsic_bindless_image_atomic_xor
:
3313 case nir_intrinsic_bindless_image_atomic_exchange
:
3314 case nir_intrinsic_bindless_image_atomic_comp_swap
:
3315 result
= visit_image_atomic(ctx
, instr
, true);
3317 case nir_intrinsic_image_deref_atomic_add
:
3318 case nir_intrinsic_image_deref_atomic_min
:
3319 case nir_intrinsic_image_deref_atomic_max
:
3320 case nir_intrinsic_image_deref_atomic_and
:
3321 case nir_intrinsic_image_deref_atomic_or
:
3322 case nir_intrinsic_image_deref_atomic_xor
:
3323 case nir_intrinsic_image_deref_atomic_exchange
:
3324 case nir_intrinsic_image_deref_atomic_comp_swap
:
3325 result
= visit_image_atomic(ctx
, instr
, false);
3327 case nir_intrinsic_bindless_image_size
:
3328 result
= visit_image_size(ctx
, instr
, true);
3330 case nir_intrinsic_image_deref_size
:
3331 result
= visit_image_size(ctx
, instr
, false);
3333 case nir_intrinsic_shader_clock
:
3334 result
= ac_build_shader_clock(&ctx
->ac
);
3336 case nir_intrinsic_discard
:
3337 case nir_intrinsic_discard_if
:
3338 emit_discard(ctx
, instr
);
3340 case nir_intrinsic_memory_barrier
:
3341 case nir_intrinsic_group_memory_barrier
:
3342 case nir_intrinsic_memory_barrier_atomic_counter
:
3343 case nir_intrinsic_memory_barrier_buffer
:
3344 case nir_intrinsic_memory_barrier_image
:
3345 case nir_intrinsic_memory_barrier_shared
:
3346 emit_membar(&ctx
->ac
, instr
);
3348 case nir_intrinsic_barrier
:
3349 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3351 case nir_intrinsic_shared_atomic_add
:
3352 case nir_intrinsic_shared_atomic_imin
:
3353 case nir_intrinsic_shared_atomic_umin
:
3354 case nir_intrinsic_shared_atomic_imax
:
3355 case nir_intrinsic_shared_atomic_umax
:
3356 case nir_intrinsic_shared_atomic_and
:
3357 case nir_intrinsic_shared_atomic_or
:
3358 case nir_intrinsic_shared_atomic_xor
:
3359 case nir_intrinsic_shared_atomic_exchange
:
3360 case nir_intrinsic_shared_atomic_comp_swap
: {
3361 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3362 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3365 case nir_intrinsic_deref_atomic_add
:
3366 case nir_intrinsic_deref_atomic_imin
:
3367 case nir_intrinsic_deref_atomic_umin
:
3368 case nir_intrinsic_deref_atomic_imax
:
3369 case nir_intrinsic_deref_atomic_umax
:
3370 case nir_intrinsic_deref_atomic_and
:
3371 case nir_intrinsic_deref_atomic_or
:
3372 case nir_intrinsic_deref_atomic_xor
:
3373 case nir_intrinsic_deref_atomic_exchange
:
3374 case nir_intrinsic_deref_atomic_comp_swap
: {
3375 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3376 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3379 case nir_intrinsic_load_barycentric_pixel
:
3380 result
= barycentric_center(ctx
, nir_intrinsic_interp_mode(instr
));
3382 case nir_intrinsic_load_barycentric_centroid
:
3383 result
= barycentric_centroid(ctx
, nir_intrinsic_interp_mode(instr
));
3385 case nir_intrinsic_load_barycentric_sample
:
3386 result
= barycentric_sample(ctx
, nir_intrinsic_interp_mode(instr
));
3388 case nir_intrinsic_load_barycentric_at_offset
: {
3389 LLVMValueRef offset
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3390 result
= barycentric_offset(ctx
, nir_intrinsic_interp_mode(instr
), offset
);
3393 case nir_intrinsic_load_barycentric_at_sample
: {
3394 LLVMValueRef sample_id
= get_src(ctx
, instr
->src
[0]);
3395 result
= barycentric_at_sample(ctx
, nir_intrinsic_interp_mode(instr
), sample_id
);
3398 case nir_intrinsic_load_interpolated_input
: {
3399 /* We assume any indirect loads have been lowered away */
3400 MAYBE_UNUSED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[1]);
3402 assert(offset
[0].i32
== 0);
3404 LLVMValueRef interp_param
= get_src(ctx
, instr
->src
[0]);
3405 unsigned index
= nir_intrinsic_base(instr
);
3406 unsigned component
= nir_intrinsic_component(instr
);
3407 result
= load_interpolated_input(ctx
, interp_param
, index
,
3409 instr
->dest
.ssa
.num_components
,
3410 instr
->dest
.ssa
.bit_size
);
3413 case nir_intrinsic_load_input
: {
3414 /* We only lower inputs for fragment shaders ATM */
3415 MAYBE_UNUSED nir_const_value
*offset
= nir_src_as_const_value(instr
->src
[0]);
3417 assert(offset
[0].i32
== 0);
3419 unsigned index
= nir_intrinsic_base(instr
);
3420 unsigned component
= nir_intrinsic_component(instr
);
3421 result
= load_flat_input(ctx
, index
, component
,
3422 instr
->dest
.ssa
.num_components
,
3423 instr
->dest
.ssa
.bit_size
);
3426 case nir_intrinsic_emit_vertex
:
3427 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3429 case nir_intrinsic_end_primitive
:
3430 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3432 case nir_intrinsic_load_tess_coord
:
3433 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3435 case nir_intrinsic_load_tess_level_outer
:
3436 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3438 case nir_intrinsic_load_tess_level_inner
:
3439 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3441 case nir_intrinsic_load_patch_vertices_in
:
3442 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3444 case nir_intrinsic_vote_all
: {
3445 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3446 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3449 case nir_intrinsic_vote_any
: {
3450 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3451 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3454 case nir_intrinsic_shuffle
:
3455 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3456 get_src(ctx
, instr
->src
[1]));
3458 case nir_intrinsic_reduce
:
3459 result
= ac_build_reduce(&ctx
->ac
,
3460 get_src(ctx
, instr
->src
[0]),
3461 instr
->const_index
[0],
3462 instr
->const_index
[1]);
3464 case nir_intrinsic_inclusive_scan
:
3465 result
= ac_build_inclusive_scan(&ctx
->ac
,
3466 get_src(ctx
, instr
->src
[0]),
3467 instr
->const_index
[0]);
3469 case nir_intrinsic_exclusive_scan
:
3470 result
= ac_build_exclusive_scan(&ctx
->ac
,
3471 get_src(ctx
, instr
->src
[0]),
3472 instr
->const_index
[0]);
3474 case nir_intrinsic_quad_broadcast
: {
3475 unsigned lane
= nir_src_as_uint(instr
->src
[1]);
3476 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3477 lane
, lane
, lane
, lane
);
3480 case nir_intrinsic_quad_swap_horizontal
:
3481 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3483 case nir_intrinsic_quad_swap_vertical
:
3484 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3486 case nir_intrinsic_quad_swap_diagonal
:
3487 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3489 case nir_intrinsic_quad_swizzle_amd
: {
3490 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3491 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3492 mask
& 0x3, (mask
>> 2) & 0x3,
3493 (mask
>> 4) & 0x3, (mask
>> 6) & 0x3);
3496 case nir_intrinsic_masked_swizzle_amd
: {
3497 uint32_t mask
= nir_intrinsic_swizzle_mask(instr
);
3498 result
= ac_build_ds_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), mask
);
3501 case nir_intrinsic_write_invocation_amd
:
3502 result
= ac_build_writelane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3503 get_src(ctx
, instr
->src
[1]),
3504 get_src(ctx
, instr
->src
[2]));
3506 case nir_intrinsic_mbcnt_amd
:
3507 result
= ac_build_mbcnt(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3510 fprintf(stderr
, "Unknown intrinsic: ");
3511 nir_print_instr(&instr
->instr
, stderr
);
3512 fprintf(stderr
, "\n");
3516 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3520 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3521 unsigned base_index
,
3522 unsigned constant_index
,
3523 LLVMValueRef dynamic_index
)
3525 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3526 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3527 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3529 /* Bindless uniforms are 64bit so multiple index by 8 */
3530 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3531 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3533 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3535 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3536 NULL
, 0, 0, true, true);
3538 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3541 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3542 nir_deref_instr
*deref_instr
,
3543 enum ac_descriptor_type desc_type
,
3544 const nir_instr
*instr
,
3545 bool image
, bool write
)
3547 LLVMValueRef index
= NULL
;
3548 unsigned constant_index
= 0;
3549 unsigned descriptor_set
;
3550 unsigned base_index
;
3551 bool bindless
= false;
3556 nir_intrinsic_instr
*img_instr
= nir_instr_as_intrinsic(instr
);
3559 index
= get_src(ctx
, img_instr
->src
[0]);
3561 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
3562 int sampSrcIdx
= nir_tex_instr_src_index(tex_instr
,
3563 nir_tex_src_sampler_handle
);
3564 if (sampSrcIdx
!= -1) {
3567 index
= get_src(ctx
, tex_instr
->src
[sampSrcIdx
].src
);
3569 assert(tex_instr
&& !image
);
3570 base_index
= tex_instr
->sampler_index
;
3574 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3575 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3576 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3580 if (nir_src_is_const(deref_instr
->arr
.index
)) {
3581 constant_index
+= array_size
* nir_src_as_uint(deref_instr
->arr
.index
);
3583 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3585 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3586 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3591 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3594 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3595 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3596 unsigned sidx
= deref_instr
->strct
.index
;
3597 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3598 constant_index
+= glsl_get_struct_location_offset(deref_instr
->type
, sidx
);
3600 unreachable("Unsupported deref type");
3603 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3605 if (deref_instr
->var
->data
.bindless
) {
3606 /* For now just assert on unhandled variable types */
3607 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3609 base_index
= deref_instr
->var
->data
.driver_location
;
3612 index
= index
? index
: ctx
->ac
.i32_0
;
3613 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3614 constant_index
, index
);
3616 base_index
= deref_instr
->var
->data
.binding
;
3619 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3622 constant_index
, index
,
3623 desc_type
, image
, write
, bindless
);
3626 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3629 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3630 * filtering manually. The driver sets img7 to a mask clearing
3631 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3632 * s_and_b32 samp0, samp0, img7
3635 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3637 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3638 LLVMValueRef res
, LLVMValueRef samp
)
3640 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3641 LLVMValueRef img7
, samp0
;
3643 if (ctx
->ac
.chip_class
>= GFX8
)
3646 img7
= LLVMBuildExtractElement(builder
, res
,
3647 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3648 samp0
= LLVMBuildExtractElement(builder
, samp
,
3649 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3650 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3651 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3652 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3655 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3656 nir_tex_instr
*instr
,
3657 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3658 LLVMValueRef
*fmask_ptr
)
3660 nir_deref_instr
*texture_deref_instr
= NULL
;
3661 nir_deref_instr
*sampler_deref_instr
= NULL
;
3664 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3665 switch (instr
->src
[i
].src_type
) {
3666 case nir_tex_src_texture_deref
:
3667 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3669 case nir_tex_src_sampler_deref
:
3670 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3672 case nir_tex_src_plane
:
3673 plane
= nir_src_as_int(instr
->src
[i
].src
);
3680 if (!sampler_deref_instr
)
3681 sampler_deref_instr
= texture_deref_instr
;
3683 enum ac_descriptor_type main_descriptor
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
? AC_DESC_BUFFER
: AC_DESC_IMAGE
;
3686 assert(instr
->op
!= nir_texop_txf_ms
&&
3687 instr
->op
!= nir_texop_samples_identical
);
3688 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
);
3690 main_descriptor
= AC_DESC_PLANE_0
+ plane
;
3693 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, main_descriptor
, &instr
->instr
, false, false);
3696 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, &instr
->instr
, false, false);
3697 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3698 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3700 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3701 instr
->op
== nir_texop_samples_identical
))
3702 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, &instr
->instr
, false, false);
3705 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3708 coord
= ac_to_float(ctx
, coord
);
3709 coord
= ac_build_round(ctx
, coord
);
3710 coord
= ac_to_integer(ctx
, coord
);
3714 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3716 LLVMValueRef result
= NULL
;
3717 struct ac_image_args args
= { 0 };
3718 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3719 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3720 unsigned offset_src
= 0;
3722 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3724 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3725 switch (instr
->src
[i
].src_type
) {
3726 case nir_tex_src_coord
: {
3727 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3728 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3729 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3732 case nir_tex_src_projector
:
3734 case nir_tex_src_comparator
:
3735 if (instr
->is_shadow
)
3736 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3738 case nir_tex_src_offset
:
3739 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3742 case nir_tex_src_bias
:
3743 if (instr
->op
== nir_texop_txb
)
3744 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3746 case nir_tex_src_lod
: {
3747 if (nir_src_is_const(instr
->src
[i
].src
) && nir_src_as_uint(instr
->src
[i
].src
) == 0)
3748 args
.level_zero
= true;
3750 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3753 case nir_tex_src_ms_index
:
3754 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3756 case nir_tex_src_ms_mcs
:
3758 case nir_tex_src_ddx
:
3759 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3761 case nir_tex_src_ddy
:
3762 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3764 case nir_tex_src_texture_offset
:
3765 case nir_tex_src_sampler_offset
:
3766 case nir_tex_src_plane
:
3772 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3773 result
= get_buffer_size(ctx
, args
.resource
, true);
3777 if (instr
->op
== nir_texop_texture_samples
) {
3778 LLVMValueRef res
, samples
, is_msaa
;
3779 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3780 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3781 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3782 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3783 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3784 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3785 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3786 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3787 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3789 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3790 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3791 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3792 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3793 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3795 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3801 if (args
.offset
&& instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3802 LLVMValueRef offset
[3], pack
;
3803 for (unsigned chan
= 0; chan
< 3; ++chan
)
3804 offset
[chan
] = ctx
->ac
.i32_0
;
3806 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3807 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3808 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3809 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3810 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3812 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3813 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3815 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3816 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3820 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3821 * so the depth comparison value isn't clamped for Z16 and
3822 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has an explicitly
3823 * clamped 32-bit float format.
3825 * It's unnecessary if the original texture format was
3826 * Z32_FLOAT, but we don't know that here.
3829 ctx
->ac
.chip_class
>= GFX8
&&
3830 ctx
->ac
.chip_class
<= GFX9
&&
3831 ctx
->abi
->clamp_shadow_reference
)
3832 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3834 /* pack derivatives */
3836 int num_src_deriv_channels
, num_dest_deriv_channels
;
3837 switch (instr
->sampler_dim
) {
3838 case GLSL_SAMPLER_DIM_3D
:
3839 case GLSL_SAMPLER_DIM_CUBE
:
3840 num_src_deriv_channels
= 3;
3841 num_dest_deriv_channels
= 3;
3843 case GLSL_SAMPLER_DIM_2D
:
3845 num_src_deriv_channels
= 2;
3846 num_dest_deriv_channels
= 2;
3848 case GLSL_SAMPLER_DIM_1D
:
3849 num_src_deriv_channels
= 1;
3850 if (ctx
->ac
.chip_class
== GFX9
) {
3851 num_dest_deriv_channels
= 2;
3853 num_dest_deriv_channels
= 1;
3858 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3859 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3860 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3861 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3862 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3864 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3865 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3866 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3870 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3871 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3872 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3873 if (instr
->coord_components
== 3)
3874 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3875 ac_prepare_cube_coords(&ctx
->ac
,
3876 instr
->op
== nir_texop_txd
, instr
->is_array
,
3877 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3880 /* Texture coordinates fixups */
3881 if (instr
->coord_components
> 1 &&
3882 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3884 instr
->op
!= nir_texop_txf
) {
3885 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3888 if (instr
->coord_components
> 2 &&
3889 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3890 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3891 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3892 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3894 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3895 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3898 if (ctx
->ac
.chip_class
== GFX9
&&
3899 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3900 instr
->op
!= nir_texop_lod
) {
3901 LLVMValueRef filler
;
3902 if (instr
->op
== nir_texop_txf
)
3903 filler
= ctx
->ac
.i32_0
;
3905 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3907 if (instr
->is_array
)
3908 args
.coords
[2] = args
.coords
[1];
3909 args
.coords
[1] = filler
;
3912 /* Pack sample index */
3913 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3914 args
.coords
[instr
->coord_components
] = sample_index
;
3916 if (instr
->op
== nir_texop_samples_identical
) {
3917 struct ac_image_args txf_args
= { 0 };
3918 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3920 txf_args
.dmask
= 0xf;
3921 txf_args
.resource
= fmask_ptr
;
3922 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3923 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3925 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3926 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3930 if ((instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
||
3931 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
) &&
3932 instr
->op
!= nir_texop_txs
) {
3933 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3934 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3935 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3936 instr
->is_array
? args
.coords
[2] : NULL
,
3937 args
.coords
[sample_chan
], fmask_ptr
);
3940 if (args
.offset
&& (instr
->op
== nir_texop_txf
|| instr
->op
== nir_texop_txf_ms
)) {
3941 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3942 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3943 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3944 args
.coords
[i
] = LLVMBuildAdd(
3945 ctx
->ac
.builder
, args
.coords
[i
],
3946 LLVMConstInt(ctx
->ac
.i32
, nir_src_comp_as_uint(instr
->src
[offset_src
].src
, i
), false), "");
3951 /* DMASK was repurposed for GATHER4. 4 components are always
3952 * returned and DMASK works like a swizzle - it selects
3953 * the component to fetch. The only valid DMASK values are
3954 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3955 * (red,red,red,red) etc.) The ISA document doesn't mention
3959 if (instr
->op
== nir_texop_tg4
) {
3960 if (instr
->is_shadow
)
3963 args
.dmask
= 1 << instr
->component
;
3966 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3967 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3968 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3970 if (instr
->op
== nir_texop_query_levels
)
3971 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3972 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3973 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3974 instr
->op
!= nir_texop_tg4
)
3975 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3976 else if (instr
->op
== nir_texop_txs
&&
3977 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3979 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3980 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3981 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3982 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3983 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3984 } else if (ctx
->ac
.chip_class
== GFX9
&&
3985 instr
->op
== nir_texop_txs
&&
3986 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3988 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3989 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3990 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3992 } else if (instr
->dest
.ssa
.num_components
!= 4)
3993 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3997 assert(instr
->dest
.is_ssa
);
3998 result
= ac_to_integer(&ctx
->ac
, result
);
3999 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4004 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
4006 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
4007 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
4009 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4010 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
4013 static void visit_post_phi(struct ac_nir_context
*ctx
,
4014 nir_phi_instr
*instr
,
4015 LLVMValueRef llvm_phi
)
4017 nir_foreach_phi_src(src
, instr
) {
4018 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
4019 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
4021 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
4025 static void phi_post_pass(struct ac_nir_context
*ctx
)
4027 hash_table_foreach(ctx
->phis
, entry
) {
4028 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
4029 (LLVMValueRef
)entry
->data
);
4034 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
4035 const nir_ssa_undef_instr
*instr
)
4037 unsigned num_components
= instr
->def
.num_components
;
4038 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
4041 if (num_components
== 1)
4042 undef
= LLVMGetUndef(type
);
4044 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
4046 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
4049 static void visit_jump(struct ac_llvm_context
*ctx
,
4050 const nir_jump_instr
*instr
)
4052 switch (instr
->type
) {
4053 case nir_jump_break
:
4054 ac_build_break(ctx
);
4056 case nir_jump_continue
:
4057 ac_build_continue(ctx
);
4060 fprintf(stderr
, "Unknown NIR jump instr: ");
4061 nir_print_instr(&instr
->instr
, stderr
);
4062 fprintf(stderr
, "\n");
4068 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
4069 enum glsl_base_type type
)
4073 case GLSL_TYPE_UINT
:
4074 case GLSL_TYPE_BOOL
:
4075 case GLSL_TYPE_SUBROUTINE
:
4077 case GLSL_TYPE_INT8
:
4078 case GLSL_TYPE_UINT8
:
4080 case GLSL_TYPE_INT16
:
4081 case GLSL_TYPE_UINT16
:
4083 case GLSL_TYPE_FLOAT
:
4085 case GLSL_TYPE_FLOAT16
:
4087 case GLSL_TYPE_INT64
:
4088 case GLSL_TYPE_UINT64
:
4090 case GLSL_TYPE_DOUBLE
:
4093 unreachable("unknown GLSL type");
4098 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
4099 const struct glsl_type
*type
)
4101 if (glsl_type_is_scalar(type
)) {
4102 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
4105 if (glsl_type_is_vector(type
)) {
4106 return LLVMVectorType(
4107 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
4108 glsl_get_vector_elements(type
));
4111 if (glsl_type_is_matrix(type
)) {
4112 return LLVMArrayType(
4113 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
4114 glsl_get_matrix_columns(type
));
4117 if (glsl_type_is_array(type
)) {
4118 return LLVMArrayType(
4119 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
4120 glsl_get_length(type
));
4123 assert(glsl_type_is_struct_or_ifc(type
));
4125 LLVMTypeRef member_types
[glsl_get_length(type
)];
4127 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
4129 glsl_to_llvm_type(ac
,
4130 glsl_get_struct_field(type
, i
));
4133 return LLVMStructTypeInContext(ac
->context
, member_types
,
4134 glsl_get_length(type
), false);
4137 static void visit_deref(struct ac_nir_context
*ctx
,
4138 nir_deref_instr
*instr
)
4140 if (instr
->mode
!= nir_var_mem_shared
&&
4141 instr
->mode
!= nir_var_mem_global
)
4144 LLVMValueRef result
= NULL
;
4145 switch(instr
->deref_type
) {
4146 case nir_deref_type_var
: {
4147 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
4148 result
= entry
->data
;
4151 case nir_deref_type_struct
:
4152 if (instr
->mode
== nir_var_mem_global
) {
4153 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4154 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
4155 instr
->strct
.index
);
4156 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4157 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4159 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4160 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4163 case nir_deref_type_array
:
4164 if (instr
->mode
== nir_var_mem_global
) {
4165 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4166 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4168 if ((glsl_type_is_matrix(parent
->type
) &&
4169 glsl_matrix_type_is_row_major(parent
->type
)) ||
4170 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4171 stride
= type_scalar_size_bytes(parent
->type
);
4174 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4175 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4176 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4178 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4180 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4182 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4183 get_src(ctx
, instr
->arr
.index
));
4186 case nir_deref_type_ptr_as_array
:
4187 if (instr
->mode
== nir_var_mem_global
) {
4188 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4190 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4191 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4192 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4194 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4196 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4198 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4199 get_src(ctx
, instr
->arr
.index
));
4202 case nir_deref_type_cast
: {
4203 result
= get_src(ctx
, instr
->parent
);
4205 /* We can't use the structs from LLVM because the shader
4206 * specifies its own offsets. */
4207 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4208 if (instr
->mode
== nir_var_mem_shared
)
4209 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4211 unsigned address_space
;
4213 switch(instr
->mode
) {
4214 case nir_var_mem_shared
:
4215 address_space
= AC_ADDR_SPACE_LDS
;
4217 case nir_var_mem_global
:
4218 address_space
= AC_ADDR_SPACE_GLOBAL
;
4221 unreachable("Unhandled address space");
4224 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4226 if (LLVMTypeOf(result
) != type
) {
4227 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4228 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4231 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4238 unreachable("Unhandled deref_instr deref type");
4241 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4244 static void visit_cf_list(struct ac_nir_context
*ctx
,
4245 struct exec_list
*list
);
4247 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4249 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4250 nir_foreach_instr(instr
, block
)
4252 switch (instr
->type
) {
4253 case nir_instr_type_alu
:
4254 visit_alu(ctx
, nir_instr_as_alu(instr
));
4256 case nir_instr_type_load_const
:
4257 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4259 case nir_instr_type_intrinsic
:
4260 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4262 case nir_instr_type_tex
:
4263 visit_tex(ctx
, nir_instr_as_tex(instr
));
4265 case nir_instr_type_phi
:
4266 visit_phi(ctx
, nir_instr_as_phi(instr
));
4268 case nir_instr_type_ssa_undef
:
4269 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4271 case nir_instr_type_jump
:
4272 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4274 case nir_instr_type_deref
:
4275 visit_deref(ctx
, nir_instr_as_deref(instr
));
4278 fprintf(stderr
, "Unknown NIR instr type: ");
4279 nir_print_instr(instr
, stderr
);
4280 fprintf(stderr
, "\n");
4285 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4288 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4290 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4292 nir_block
*then_block
=
4293 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4295 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4297 visit_cf_list(ctx
, &if_stmt
->then_list
);
4299 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4300 nir_block
*else_block
=
4301 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4303 ac_build_else(&ctx
->ac
, else_block
->index
);
4304 visit_cf_list(ctx
, &if_stmt
->else_list
);
4307 ac_build_endif(&ctx
->ac
, then_block
->index
);
4310 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4312 nir_block
*first_loop_block
=
4313 (nir_block
*) exec_list_get_head(&loop
->body
);
4315 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4317 visit_cf_list(ctx
, &loop
->body
);
4319 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4322 static void visit_cf_list(struct ac_nir_context
*ctx
,
4323 struct exec_list
*list
)
4325 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4327 switch (node
->type
) {
4328 case nir_cf_node_block
:
4329 visit_block(ctx
, nir_cf_node_as_block(node
));
4332 case nir_cf_node_if
:
4333 visit_if(ctx
, nir_cf_node_as_if(node
));
4336 case nir_cf_node_loop
:
4337 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4347 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4348 struct ac_shader_abi
*abi
,
4349 struct nir_shader
*nir
,
4350 struct nir_variable
*variable
,
4351 gl_shader_stage stage
)
4353 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4354 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4356 /* tess ctrl has it's own load/store paths for outputs */
4357 if (stage
== MESA_SHADER_TESS_CTRL
)
4360 if (stage
== MESA_SHADER_VERTEX
||
4361 stage
== MESA_SHADER_TESS_EVAL
||
4362 stage
== MESA_SHADER_GEOMETRY
) {
4363 int idx
= variable
->data
.location
+ variable
->data
.index
;
4364 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4365 int length
= nir
->info
.clip_distance_array_size
+
4366 nir
->info
.cull_distance_array_size
;
4375 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4376 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4377 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4378 for (unsigned chan
= 0; chan
< 4; chan
++) {
4379 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4380 ac_build_alloca_undef(ctx
, type
, "");
4386 setup_locals(struct ac_nir_context
*ctx
,
4387 struct nir_function
*func
)
4390 ctx
->num_locals
= 0;
4391 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4392 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4393 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4394 variable
->data
.location_frac
= 0;
4395 ctx
->num_locals
+= attrib_count
;
4397 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4401 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4402 for (j
= 0; j
< 4; j
++) {
4403 ctx
->locals
[i
* 4 + j
] =
4404 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4410 setup_shared(struct ac_nir_context
*ctx
,
4411 struct nir_shader
*nir
)
4413 nir_foreach_variable(variable
, &nir
->shared
) {
4414 LLVMValueRef shared
=
4415 LLVMAddGlobalInAddressSpace(
4416 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4417 variable
->name
? variable
->name
: "",
4419 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4423 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4424 struct nir_shader
*nir
)
4426 struct ac_nir_context ctx
= {};
4427 struct nir_function
*func
;
4432 ctx
.stage
= nir
->info
.stage
;
4433 ctx
.info
= &nir
->info
;
4435 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4437 nir_foreach_variable(variable
, &nir
->outputs
)
4438 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4441 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4442 _mesa_key_pointer_equal
);
4443 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4444 _mesa_key_pointer_equal
);
4445 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4446 _mesa_key_pointer_equal
);
4448 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4450 nir_index_ssa_defs(func
->impl
);
4451 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4453 setup_locals(&ctx
, func
);
4455 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4456 setup_shared(&ctx
, nir
);
4458 visit_cf_list(&ctx
, &func
->impl
->body
);
4459 phi_post_pass(&ctx
);
4461 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4462 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4467 ralloc_free(ctx
.defs
);
4468 ralloc_free(ctx
.phis
);
4469 ralloc_free(ctx
.vars
);
4473 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4475 /* While it would be nice not to have this flag, we are constrained
4476 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
4478 bool llvm_has_working_vgpr_indexing
= chip_class
!= GFX9
;
4480 /* TODO: Indirect indexing of GS inputs is unimplemented.
4482 * TCS and TES load inputs directly from LDS or offchip memory, so
4483 * indirect indexing is trivial.
4485 nir_variable_mode indirect_mask
= 0;
4486 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4487 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4488 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4489 !llvm_has_working_vgpr_indexing
)) {
4490 indirect_mask
|= nir_var_shader_in
;
4492 if (!llvm_has_working_vgpr_indexing
&&
4493 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4494 indirect_mask
|= nir_var_shader_out
;
4496 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4497 * smart enough to handle indirects without causing excess spilling
4498 * causing the gpu to hang.
4500 * See the following thread for more details of the problem:
4501 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4503 indirect_mask
|= nir_var_function_temp
;
4505 nir_lower_indirect_derefs(nir
, indirect_mask
);
4509 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4511 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4515 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4517 if (var
->data
.mode
!= nir_var_shader_out
)
4520 unsigned writemask
= 0;
4521 const int location
= var
->data
.location
;
4522 unsigned first_component
= var
->data
.location_frac
;
4523 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4525 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4526 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4527 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4528 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4534 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4535 unsigned *cond_block_tf_writemask
,
4536 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4538 switch (cf_node
->type
) {
4539 case nir_cf_node_block
: {
4540 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4541 nir_foreach_instr(instr
, block
) {
4542 if (instr
->type
!= nir_instr_type_intrinsic
)
4545 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4546 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4548 /* If we find a barrier in nested control flow put this in the
4549 * too hard basket. In GLSL this is not possible but it is in
4553 *tessfactors_are_def_in_all_invocs
= false;
4557 /* The following case must be prevented:
4558 * gl_TessLevelInner = ...;
4560 * if (gl_InvocationID == 1)
4561 * gl_TessLevelInner = ...;
4563 * If you consider disjoint code segments separated by barriers, each
4564 * such segment that writes tess factor channels should write the same
4565 * channels in all codepaths within that segment.
4567 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4568 /* Accumulate the result: */
4569 *tessfactors_are_def_in_all_invocs
&=
4570 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4572 /* Analyze the next code segment from scratch. */
4573 *upper_block_tf_writemask
= 0;
4574 *cond_block_tf_writemask
= 0;
4577 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4582 case nir_cf_node_if
: {
4583 unsigned then_tessfactor_writemask
= 0;
4584 unsigned else_tessfactor_writemask
= 0;
4586 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4587 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4588 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4589 cond_block_tf_writemask
,
4590 tessfactors_are_def_in_all_invocs
, true);
4593 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4594 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4595 cond_block_tf_writemask
,
4596 tessfactors_are_def_in_all_invocs
, true);
4599 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4600 /* If both statements write the same tess factor channels,
4601 * we can say that the upper block writes them too.
4603 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4604 else_tessfactor_writemask
;
4605 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4606 else_tessfactor_writemask
;
4611 case nir_cf_node_loop
: {
4612 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4613 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4614 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4615 cond_block_tf_writemask
,
4616 tessfactors_are_def_in_all_invocs
, true);
4622 unreachable("unknown cf node type");
4627 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4629 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4631 /* The pass works as follows:
4632 * If all codepaths write tess factors, we can say that all
4633 * invocations define tess factors.
4635 * Each tess factor channel is tracked separately.
4637 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4638 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4640 /* Initial value = true. Here the pass will accumulate results from
4641 * multiple segments surrounded by barriers. If tess factors aren't
4642 * written at all, it's a shader bug and we don't care if this will be
4645 bool tessfactors_are_def_in_all_invocs
= true;
4647 nir_foreach_function(function
, nir
) {
4648 if (function
->impl
) {
4649 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4650 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4651 &cond_block_tf_writemask
,
4652 &tessfactors_are_def_in_all_invocs
,
4658 /* Accumulate the result for the last code segment separated by a
4661 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4662 tessfactors_are_def_in_all_invocs
&=
4663 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4666 return tessfactors_are_def_in_all_invocs
;