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
;
42 LLVMValueRef
*ssa_defs
;
44 struct hash_table
*defs
;
45 struct hash_table
*phis
;
46 struct hash_table
*vars
;
48 LLVMValueRef main_function
;
49 LLVMBasicBlockRef continue_block
;
50 LLVMBasicBlockRef break_block
;
56 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
57 nir_deref_instr
*deref_instr
,
58 enum ac_descriptor_type desc_type
,
59 const nir_tex_instr
*instr
,
60 bool image
, bool write
);
63 build_store_values_extended(struct ac_llvm_context
*ac
,
66 unsigned value_stride
,
69 LLVMBuilderRef builder
= ac
->builder
;
72 for (i
= 0; i
< value_count
; i
++) {
73 LLVMValueRef ptr
= values
[i
* value_stride
];
74 LLVMValueRef index
= LLVMConstInt(ac
->i32
, i
, false);
75 LLVMValueRef value
= LLVMBuildExtractElement(builder
, vec
, index
, "");
76 LLVMBuildStore(builder
, value
, ptr
);
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim dim
,
85 case GLSL_SAMPLER_DIM_1D
:
86 if (ctx
->chip_class
>= GFX9
)
87 return is_array
? ac_image_2darray
: ac_image_2d
;
88 return is_array
? ac_image_1darray
: ac_image_1d
;
89 case GLSL_SAMPLER_DIM_2D
:
90 case GLSL_SAMPLER_DIM_RECT
:
91 case GLSL_SAMPLER_DIM_EXTERNAL
:
92 return is_array
? ac_image_2darray
: ac_image_2d
;
93 case GLSL_SAMPLER_DIM_3D
:
95 case GLSL_SAMPLER_DIM_CUBE
:
97 case GLSL_SAMPLER_DIM_MS
:
98 return is_array
? ac_image_2darraymsaa
: ac_image_2dmsaa
;
99 case GLSL_SAMPLER_DIM_SUBPASS
:
100 return ac_image_2darray
;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
102 return ac_image_2darraymsaa
;
104 unreachable("bad sampler dim");
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context
*ctx
, enum glsl_sampler_dim sdim
,
112 enum ac_image_dim dim
= get_ac_sampler_dim(ctx
, sdim
, is_array
);
114 if (dim
== ac_image_cube
||
115 (ctx
->chip_class
<= VI
&& dim
== ac_image_3d
))
116 dim
= ac_image_2darray
;
121 static LLVMTypeRef
get_def_type(struct ac_nir_context
*ctx
,
122 const nir_ssa_def
*def
)
124 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, def
->bit_size
);
125 if (def
->num_components
> 1) {
126 type
= LLVMVectorType(type
, def
->num_components
);
131 static LLVMValueRef
get_src(struct ac_nir_context
*nir
, nir_src src
)
134 return nir
->ssa_defs
[src
.ssa
->index
];
138 get_memory_ptr(struct ac_nir_context
*ctx
, nir_src src
)
140 LLVMValueRef ptr
= get_src(ctx
, src
);
141 ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ac
.lds
, &ptr
, 1, "");
142 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
144 return LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
145 LLVMPointerType(ctx
->ac
.i32
, addr_space
), "");
148 static LLVMBasicBlockRef
get_block(struct ac_nir_context
*nir
,
149 const struct nir_block
*b
)
151 struct hash_entry
*entry
= _mesa_hash_table_search(nir
->defs
, b
);
152 return (LLVMBasicBlockRef
)entry
->data
;
155 static LLVMValueRef
get_alu_src(struct ac_nir_context
*ctx
,
157 unsigned num_components
)
159 LLVMValueRef value
= get_src(ctx
, src
.src
);
160 bool need_swizzle
= false;
163 unsigned src_components
= ac_get_llvm_num_components(value
);
164 for (unsigned i
= 0; i
< num_components
; ++i
) {
165 assert(src
.swizzle
[i
] < src_components
);
166 if (src
.swizzle
[i
] != i
)
170 if (need_swizzle
|| num_components
!= src_components
) {
171 LLVMValueRef masks
[] = {
172 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[0], false),
173 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[1], false),
174 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[2], false),
175 LLVMConstInt(ctx
->ac
.i32
, src
.swizzle
[3], false)};
177 if (src_components
> 1 && num_components
== 1) {
178 value
= LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
180 } else if (src_components
== 1 && num_components
> 1) {
181 LLVMValueRef values
[] = {value
, value
, value
, value
};
182 value
= ac_build_gather_values(&ctx
->ac
, values
, num_components
);
184 LLVMValueRef swizzle
= LLVMConstVector(masks
, num_components
);
185 value
= LLVMBuildShuffleVector(ctx
->ac
.builder
, value
, value
,
194 static LLVMValueRef
emit_int_cmp(struct ac_llvm_context
*ctx
,
195 LLVMIntPredicate pred
, LLVMValueRef src0
,
198 LLVMValueRef result
= LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, "");
199 return LLVMBuildSelect(ctx
->builder
, result
,
200 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
204 static LLVMValueRef
emit_float_cmp(struct ac_llvm_context
*ctx
,
205 LLVMRealPredicate pred
, LLVMValueRef src0
,
209 src0
= ac_to_float(ctx
, src0
);
210 src1
= ac_to_float(ctx
, src1
);
211 result
= LLVMBuildFCmp(ctx
->builder
, pred
, src0
, src1
, "");
212 return LLVMBuildSelect(ctx
->builder
, result
,
213 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false),
217 static LLVMValueRef
emit_intrin_1f_param(struct ac_llvm_context
*ctx
,
219 LLVMTypeRef result_type
,
223 LLVMValueRef params
[] = {
224 ac_to_float(ctx
, src0
),
227 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
228 ac_get_elem_bits(ctx
, result_type
));
229 assert(length
< sizeof(name
));
230 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 1, AC_FUNC_ATTR_READNONE
);
233 static LLVMValueRef
emit_intrin_2f_param(struct ac_llvm_context
*ctx
,
235 LLVMTypeRef result_type
,
236 LLVMValueRef src0
, LLVMValueRef src1
)
239 LLVMValueRef params
[] = {
240 ac_to_float(ctx
, src0
),
241 ac_to_float(ctx
, src1
),
244 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
245 ac_get_elem_bits(ctx
, result_type
));
246 assert(length
< sizeof(name
));
247 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 2, AC_FUNC_ATTR_READNONE
);
250 static LLVMValueRef
emit_intrin_3f_param(struct ac_llvm_context
*ctx
,
252 LLVMTypeRef result_type
,
253 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
256 LLVMValueRef params
[] = {
257 ac_to_float(ctx
, src0
),
258 ac_to_float(ctx
, src1
),
259 ac_to_float(ctx
, src2
),
262 MAYBE_UNUSED
const int length
= snprintf(name
, sizeof(name
), "%s.f%d", intrin
,
263 ac_get_elem_bits(ctx
, result_type
));
264 assert(length
< sizeof(name
));
265 return ac_build_intrinsic(ctx
, name
, result_type
, params
, 3, AC_FUNC_ATTR_READNONE
);
268 static LLVMValueRef
emit_bcsel(struct ac_llvm_context
*ctx
,
269 LLVMValueRef src0
, LLVMValueRef src1
, LLVMValueRef src2
)
271 LLVMValueRef v
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
,
273 return LLVMBuildSelect(ctx
->builder
, v
,
274 ac_to_integer_or_pointer(ctx
, src1
),
275 ac_to_integer_or_pointer(ctx
, src2
), "");
278 static LLVMValueRef
emit_minmax_int(struct ac_llvm_context
*ctx
,
279 LLVMIntPredicate pred
,
280 LLVMValueRef src0
, LLVMValueRef src1
)
282 return LLVMBuildSelect(ctx
->builder
,
283 LLVMBuildICmp(ctx
->builder
, pred
, src0
, src1
, ""),
288 static LLVMValueRef
emit_iabs(struct ac_llvm_context
*ctx
,
291 return emit_minmax_int(ctx
, LLVMIntSGT
, src0
,
292 LLVMBuildNeg(ctx
->builder
, src0
, ""));
295 static LLVMValueRef
emit_uint_carry(struct ac_llvm_context
*ctx
,
297 LLVMValueRef src0
, LLVMValueRef src1
)
299 LLVMTypeRef ret_type
;
300 LLVMTypeRef types
[] = { ctx
->i32
, ctx
->i1
};
302 LLVMValueRef params
[] = { src0
, src1
};
303 ret_type
= LLVMStructTypeInContext(ctx
->context
, types
,
306 res
= ac_build_intrinsic(ctx
, intrin
, ret_type
,
307 params
, 2, AC_FUNC_ATTR_READNONE
);
309 res
= LLVMBuildExtractValue(ctx
->builder
, res
, 1, "");
310 res
= LLVMBuildZExt(ctx
->builder
, res
, ctx
->i32
, "");
314 static LLVMValueRef
emit_b2f(struct ac_llvm_context
*ctx
,
318 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
,
319 LLVMBuildBitCast(ctx
->builder
, LLVMConstReal(ctx
->f32
, 1.0), ctx
->i32
, ""),
321 result
= LLVMBuildBitCast(ctx
->builder
, result
, ctx
->f32
, "");
326 return LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f64
, "");
329 static LLVMValueRef
emit_f2b(struct ac_llvm_context
*ctx
,
332 src0
= ac_to_float(ctx
, src0
);
333 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
334 return LLVMBuildSExt(ctx
->builder
,
335 LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
, src0
, zero
, ""),
339 static LLVMValueRef
emit_b2i(struct ac_llvm_context
*ctx
,
343 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, src0
, ctx
->i32_1
, "");
348 return LLVMBuildZExt(ctx
->builder
, result
, ctx
->i64
, "");
351 static LLVMValueRef
emit_i2b(struct ac_llvm_context
*ctx
,
354 LLVMValueRef zero
= LLVMConstNull(LLVMTypeOf(src0
));
355 return LLVMBuildSExt(ctx
->builder
,
356 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src0
, zero
, ""),
360 static LLVMValueRef
emit_f2f16(struct ac_llvm_context
*ctx
,
364 LLVMValueRef cond
= NULL
;
366 src0
= ac_to_float(ctx
, src0
);
367 result
= LLVMBuildFPTrunc(ctx
->builder
, src0
, ctx
->f16
, "");
369 if (ctx
->chip_class
>= VI
) {
370 LLVMValueRef args
[2];
371 /* Check if the result is a denormal - and flush to 0 if so. */
373 args
[1] = LLVMConstInt(ctx
->i32
, N_SUBNORMAL
| P_SUBNORMAL
, false);
374 cond
= ac_build_intrinsic(ctx
, "llvm.amdgcn.class.f16", ctx
->i1
, args
, 2, AC_FUNC_ATTR_READNONE
);
377 /* need to convert back up to f32 */
378 result
= LLVMBuildFPExt(ctx
->builder
, result
, ctx
->f32
, "");
380 if (ctx
->chip_class
>= VI
)
381 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
384 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
385 * so compare the result and flush to 0 if it's smaller.
387 LLVMValueRef temp
, cond2
;
388 temp
= emit_intrin_1f_param(ctx
, "llvm.fabs", ctx
->f32
, result
);
389 cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUGT
,
390 LLVMBuildBitCast(ctx
->builder
, LLVMConstInt(ctx
->i32
, 0x38800000, false), ctx
->f32
, ""),
392 cond2
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
393 temp
, ctx
->f32_0
, "");
394 cond
= LLVMBuildAnd(ctx
->builder
, cond
, cond2
, "");
395 result
= LLVMBuildSelect(ctx
->builder
, cond
, ctx
->f32_0
, result
, "");
400 static LLVMValueRef
emit_umul_high(struct ac_llvm_context
*ctx
,
401 LLVMValueRef src0
, LLVMValueRef src1
)
403 LLVMValueRef dst64
, result
;
404 src0
= LLVMBuildZExt(ctx
->builder
, src0
, ctx
->i64
, "");
405 src1
= LLVMBuildZExt(ctx
->builder
, src1
, ctx
->i64
, "");
407 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
408 dst64
= LLVMBuildLShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
409 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
413 static LLVMValueRef
emit_imul_high(struct ac_llvm_context
*ctx
,
414 LLVMValueRef src0
, LLVMValueRef src1
)
416 LLVMValueRef dst64
, result
;
417 src0
= LLVMBuildSExt(ctx
->builder
, src0
, ctx
->i64
, "");
418 src1
= LLVMBuildSExt(ctx
->builder
, src1
, ctx
->i64
, "");
420 dst64
= LLVMBuildMul(ctx
->builder
, src0
, src1
, "");
421 dst64
= LLVMBuildAShr(ctx
->builder
, dst64
, LLVMConstInt(ctx
->i64
, 32, false), "");
422 result
= LLVMBuildTrunc(ctx
->builder
, dst64
, ctx
->i32
, "");
426 static LLVMValueRef
emit_bitfield_extract(struct ac_llvm_context
*ctx
,
428 const LLVMValueRef srcs
[3])
432 if (HAVE_LLVM
>= 0x0800) {
433 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
434 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
435 result
= LLVMBuildSelect(ctx
->builder
, icond
, srcs
[0], result
, "");
437 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
438 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
440 LLVMValueRef zero
= ctx
->i32_0
;
441 LLVMValueRef icond1
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], LLVMConstInt(ctx
->i32
, 32, false), "");
442 LLVMValueRef icond2
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, srcs
[2], zero
, "");
444 result
= ac_build_bfe(ctx
, srcs
[0], srcs
[1], srcs
[2], is_signed
);
445 result
= LLVMBuildSelect(ctx
->builder
, icond1
, srcs
[0], result
, "");
446 result
= LLVMBuildSelect(ctx
->builder
, icond2
, zero
, result
, "");
452 static LLVMValueRef
emit_bitfield_insert(struct ac_llvm_context
*ctx
,
453 LLVMValueRef src0
, LLVMValueRef src1
,
454 LLVMValueRef src2
, LLVMValueRef src3
)
456 LLVMValueRef bfi_args
[3], result
;
458 bfi_args
[0] = LLVMBuildShl(ctx
->builder
,
459 LLVMBuildSub(ctx
->builder
,
460 LLVMBuildShl(ctx
->builder
,
465 bfi_args
[1] = LLVMBuildShl(ctx
->builder
, src1
, src2
, "");
468 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, src3
, LLVMConstInt(ctx
->i32
, 32, false), "");
471 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
472 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
474 result
= LLVMBuildXor(ctx
->builder
, bfi_args
[2],
475 LLVMBuildAnd(ctx
->builder
, bfi_args
[0],
476 LLVMBuildXor(ctx
->builder
, bfi_args
[1], bfi_args
[2], ""), ""), "");
478 result
= LLVMBuildSelect(ctx
->builder
, icond
, src1
, result
, "");
482 static LLVMValueRef
emit_pack_half_2x16(struct ac_llvm_context
*ctx
,
485 LLVMValueRef comp
[2];
487 src0
= ac_to_float(ctx
, src0
);
488 comp
[0] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_0
, "");
489 comp
[1] = LLVMBuildExtractElement(ctx
->builder
, src0
, ctx
->i32_1
, "");
491 return LLVMBuildBitCast(ctx
->builder
, ac_build_cvt_pkrtz_f16(ctx
, comp
),
495 static LLVMValueRef
emit_unpack_half_2x16(struct ac_llvm_context
*ctx
,
498 LLVMValueRef const16
= LLVMConstInt(ctx
->i32
, 16, false);
499 LLVMValueRef temps
[2], val
;
502 for (i
= 0; i
< 2; i
++) {
503 val
= i
== 1 ? LLVMBuildLShr(ctx
->builder
, src0
, const16
, "") : src0
;
504 val
= LLVMBuildTrunc(ctx
->builder
, val
, ctx
->i16
, "");
505 val
= LLVMBuildBitCast(ctx
->builder
, val
, ctx
->f16
, "");
506 temps
[i
] = LLVMBuildFPExt(ctx
->builder
, val
, ctx
->f32
, "");
508 return ac_build_gather_values(ctx
, temps
, 2);
511 static LLVMValueRef
emit_ddxy(struct ac_nir_context
*ctx
,
519 if (op
== nir_op_fddx_fine
)
520 mask
= AC_TID_MASK_LEFT
;
521 else if (op
== nir_op_fddy_fine
)
522 mask
= AC_TID_MASK_TOP
;
524 mask
= AC_TID_MASK_TOP_LEFT
;
526 /* for DDX we want to next X pixel, DDY next Y pixel. */
527 if (op
== nir_op_fddx_fine
||
528 op
== nir_op_fddx_coarse
||
534 result
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, src0
);
539 * this takes an I,J coordinate pair,
540 * and works out the X and Y derivatives.
541 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
543 static LLVMValueRef
emit_ddxy_interp(
544 struct ac_nir_context
*ctx
,
545 LLVMValueRef interp_ij
)
547 LLVMValueRef result
[4], a
;
550 for (i
= 0; i
< 2; i
++) {
551 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
552 LLVMConstInt(ctx
->ac
.i32
, i
, false), "");
553 result
[i
] = emit_ddxy(ctx
, nir_op_fddx
, a
);
554 result
[2+i
] = emit_ddxy(ctx
, nir_op_fddy
, a
);
556 return ac_build_gather_values(&ctx
->ac
, result
, 4);
559 static void visit_alu(struct ac_nir_context
*ctx
, const nir_alu_instr
*instr
)
561 LLVMValueRef src
[4], result
= NULL
;
562 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
563 unsigned src_components
;
564 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.dest
.ssa
);
566 assert(nir_op_infos
[instr
->op
].num_inputs
<= ARRAY_SIZE(src
));
573 case nir_op_pack_half_2x16
:
576 case nir_op_unpack_half_2x16
:
579 case nir_op_cube_face_coord
:
580 case nir_op_cube_face_index
:
584 src_components
= num_components
;
587 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
588 src
[i
] = get_alu_src(ctx
, instr
->src
[i
], src_components
);
596 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
597 result
= LLVMBuildFNeg(ctx
->ac
.builder
, src
[0], "");
600 result
= LLVMBuildNeg(ctx
->ac
.builder
, src
[0], "");
603 result
= LLVMBuildNot(ctx
->ac
.builder
, src
[0], "");
606 result
= LLVMBuildAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
609 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
610 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
611 result
= LLVMBuildFAdd(ctx
->ac
.builder
, src
[0], src
[1], "");
614 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
615 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
616 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], src
[1], "");
619 result
= LLVMBuildSub(ctx
->ac
.builder
, src
[0], src
[1], "");
622 result
= LLVMBuildMul(ctx
->ac
.builder
, src
[0], src
[1], "");
625 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
628 result
= LLVMBuildURem(ctx
->ac
.builder
, src
[0], src
[1], "");
631 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
632 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
633 result
= ac_build_fdiv(&ctx
->ac
, src
[0], src
[1]);
634 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
635 ac_to_float_type(&ctx
->ac
, def_type
), result
);
636 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[1] , result
, "");
637 result
= LLVMBuildFSub(ctx
->ac
.builder
, src
[0], result
, "");
640 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
641 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
642 result
= LLVMBuildFRem(ctx
->ac
.builder
, src
[0], src
[1], "");
645 result
= LLVMBuildSRem(ctx
->ac
.builder
, src
[0], src
[1], "");
648 result
= LLVMBuildSDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
651 result
= LLVMBuildUDiv(ctx
->ac
.builder
, src
[0], src
[1], "");
654 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
655 src
[1] = ac_to_float(&ctx
->ac
, src
[1]);
656 result
= LLVMBuildFMul(ctx
->ac
.builder
, src
[0], src
[1], "");
659 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
660 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(src
[0]), 1.0), src
[0]);
663 result
= LLVMBuildAnd(ctx
->ac
.builder
, src
[0], src
[1], "");
666 result
= LLVMBuildOr(ctx
->ac
.builder
, src
[0], src
[1], "");
669 result
= LLVMBuildXor(ctx
->ac
.builder
, src
[0], src
[1], "");
672 result
= LLVMBuildShl(ctx
->ac
.builder
, src
[0],
673 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
674 LLVMTypeOf(src
[0]), ""),
678 result
= LLVMBuildAShr(ctx
->ac
.builder
, src
[0],
679 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
680 LLVMTypeOf(src
[0]), ""),
684 result
= LLVMBuildLShr(ctx
->ac
.builder
, src
[0],
685 LLVMBuildZExt(ctx
->ac
.builder
, src
[1],
686 LLVMTypeOf(src
[0]), ""),
690 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
693 result
= emit_int_cmp(&ctx
->ac
, LLVMIntNE
, src
[0], src
[1]);
696 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, src
[0], src
[1]);
699 result
= emit_int_cmp(&ctx
->ac
, LLVMIntSGE
, src
[0], src
[1]);
702 result
= emit_int_cmp(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
705 result
= emit_int_cmp(&ctx
->ac
, LLVMIntUGE
, src
[0], src
[1]);
708 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOEQ
, src
[0], src
[1]);
711 result
= emit_float_cmp(&ctx
->ac
, LLVMRealUNE
, src
[0], src
[1]);
714 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOLT
, src
[0], src
[1]);
717 result
= emit_float_cmp(&ctx
->ac
, LLVMRealOGE
, src
[0], src
[1]);
720 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.fabs",
721 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
724 result
= emit_iabs(&ctx
->ac
, src
[0]);
727 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
730 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
733 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
736 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
739 result
= ac_build_isign(&ctx
->ac
, src
[0],
740 instr
->dest
.dest
.ssa
.bit_size
);
743 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
744 result
= ac_build_fsign(&ctx
->ac
, src
[0],
745 instr
->dest
.dest
.ssa
.bit_size
);
748 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.floor",
749 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
752 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.trunc",
753 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
756 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.ceil",
757 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
759 case nir_op_fround_even
:
760 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.rint",
761 ac_to_float_type(&ctx
->ac
, def_type
),src
[0]);
764 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
765 result
= ac_build_fract(&ctx
->ac
, src
[0],
766 instr
->dest
.dest
.ssa
.bit_size
);
769 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sin",
770 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
773 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.cos",
774 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
777 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
778 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
781 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.exp2",
782 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
785 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.log2",
786 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
789 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.sqrt",
790 ac_to_float_type(&ctx
->ac
, def_type
), src
[0]);
791 result
= ac_build_fdiv(&ctx
->ac
, LLVMConstReal(LLVMTypeOf(result
), 1.0), result
);
793 case nir_op_frexp_exp
:
794 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
795 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.exp.i32.f64",
796 ctx
->ac
.i32
, src
, 1, AC_FUNC_ATTR_READNONE
);
799 case nir_op_frexp_sig
:
800 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
801 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.frexp.mant.f64",
802 ctx
->ac
.f64
, src
, 1, AC_FUNC_ATTR_READNONE
);
805 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
806 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
807 if (ctx
->ac
.chip_class
< GFX9
&&
808 instr
->dest
.dest
.ssa
.bit_size
== 32) {
809 /* Only pre-GFX9 chips do not flush denorms. */
810 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
811 ac_to_float_type(&ctx
->ac
, def_type
),
816 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
817 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
818 if (ctx
->ac
.chip_class
< GFX9
&&
819 instr
->dest
.dest
.ssa
.bit_size
== 32) {
820 /* Only pre-GFX9 chips do not flush denorms. */
821 result
= emit_intrin_1f_param(&ctx
->ac
, "llvm.canonicalize",
822 ac_to_float_type(&ctx
->ac
, def_type
),
827 result
= emit_intrin_3f_param(&ctx
->ac
, "llvm.fmuladd",
828 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1], src
[2]);
831 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
832 if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 32)
833 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f32", ctx
->ac
.f32
, src
, 2, AC_FUNC_ATTR_READNONE
);
834 else if (ac_get_elem_bits(&ctx
->ac
, def_type
) == 16)
835 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f16", ctx
->ac
.f16
, src
, 2, AC_FUNC_ATTR_READNONE
);
837 result
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.ldexp.f64", ctx
->ac
.f64
, src
, 2, AC_FUNC_ATTR_READNONE
);
839 case nir_op_ibitfield_extract
:
840 result
= emit_bitfield_extract(&ctx
->ac
, true, src
);
842 case nir_op_ubitfield_extract
:
843 result
= emit_bitfield_extract(&ctx
->ac
, false, src
);
845 case nir_op_bitfield_insert
:
846 result
= emit_bitfield_insert(&ctx
->ac
, src
[0], src
[1], src
[2], src
[3]);
848 case nir_op_bitfield_reverse
:
849 result
= ac_build_bitfield_reverse(&ctx
->ac
, src
[0]);
851 case nir_op_bit_count
:
852 result
= ac_build_bit_count(&ctx
->ac
, src
[0]);
857 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
858 src
[i
] = ac_to_integer(&ctx
->ac
, src
[i
]);
859 result
= ac_build_gather_values(&ctx
->ac
, src
, num_components
);
864 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
865 result
= LLVMBuildFPToSI(ctx
->ac
.builder
, src
[0], def_type
, "");
870 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
871 result
= LLVMBuildFPToUI(ctx
->ac
.builder
, src
[0], def_type
, "");
876 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
877 result
= LLVMBuildSIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
882 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
883 result
= LLVMBuildUIToFP(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
885 case nir_op_f2f16_rtz
:
886 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
887 if (LLVMTypeOf(src
[0]) == ctx
->ac
.f64
)
888 src
[0] = LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ctx
->ac
.f32
, "");
889 LLVMValueRef param
[2] = { src
[0], ctx
->ac
.f32_0
};
890 result
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, param
);
891 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
893 case nir_op_f2f16_rtne
:
897 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
898 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
899 result
= LLVMBuildFPExt(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
901 result
= LLVMBuildFPTrunc(ctx
->ac
.builder
, src
[0], ac_to_float_type(&ctx
->ac
, def_type
), "");
906 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
907 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
908 result
= LLVMBuildZExt(ctx
->ac
.builder
, src
[0], def_type
, "");
910 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
915 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
916 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
[0])) < ac_get_elem_bits(&ctx
->ac
, def_type
))
917 result
= LLVMBuildSExt(ctx
->ac
.builder
, src
[0], def_type
, "");
919 result
= LLVMBuildTrunc(ctx
->ac
.builder
, src
[0], def_type
, "");
922 result
= emit_bcsel(&ctx
->ac
, src
[0], src
[1], src
[2]);
924 case nir_op_find_lsb
:
925 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
926 result
= ac_find_lsb(&ctx
->ac
, ctx
->ac
.i32
, src
[0]);
928 case nir_op_ufind_msb
:
929 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
930 result
= ac_build_umsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
932 case nir_op_ifind_msb
:
933 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
934 result
= ac_build_imsb(&ctx
->ac
, src
[0], ctx
->ac
.i32
);
936 case nir_op_uadd_carry
:
937 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
938 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
939 result
= emit_uint_carry(&ctx
->ac
, "llvm.uadd.with.overflow.i32", src
[0], src
[1]);
941 case nir_op_usub_borrow
:
942 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
943 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
944 result
= emit_uint_carry(&ctx
->ac
, "llvm.usub.with.overflow.i32", src
[0], src
[1]);
949 result
= emit_b2f(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
952 result
= emit_f2b(&ctx
->ac
, src
[0]);
957 result
= emit_b2i(&ctx
->ac
, src
[0], instr
->dest
.dest
.ssa
.bit_size
);
960 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
961 result
= emit_i2b(&ctx
->ac
, src
[0]);
963 case nir_op_fquantize2f16
:
964 result
= emit_f2f16(&ctx
->ac
, src
[0]);
966 case nir_op_umul_high
:
967 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
968 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
969 result
= emit_umul_high(&ctx
->ac
, src
[0], src
[1]);
971 case nir_op_imul_high
:
972 src
[0] = ac_to_integer(&ctx
->ac
, src
[0]);
973 src
[1] = ac_to_integer(&ctx
->ac
, src
[1]);
974 result
= emit_imul_high(&ctx
->ac
, src
[0], src
[1]);
976 case nir_op_pack_half_2x16
:
977 result
= emit_pack_half_2x16(&ctx
->ac
, src
[0]);
979 case nir_op_unpack_half_2x16
:
980 result
= emit_unpack_half_2x16(&ctx
->ac
, src
[0]);
984 case nir_op_fddx_fine
:
985 case nir_op_fddy_fine
:
986 case nir_op_fddx_coarse
:
987 case nir_op_fddy_coarse
:
988 result
= emit_ddxy(ctx
, instr
->op
, src
[0]);
991 case nir_op_unpack_64_2x32_split_x
: {
992 assert(ac_get_llvm_num_components(src
[0]) == 1);
993 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
996 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1001 case nir_op_unpack_64_2x32_split_y
: {
1002 assert(ac_get_llvm_num_components(src
[0]) == 1);
1003 LLVMValueRef tmp
= LLVMBuildBitCast(ctx
->ac
.builder
, src
[0],
1006 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
,
1011 case nir_op_pack_64_2x32_split
: {
1012 LLVMValueRef tmp
= LLVMGetUndef(ctx
->ac
.v2i32
);
1013 tmp
= ac_build_gather_values(&ctx
->ac
, src
, 2);
1014 result
= LLVMBuildBitCast(ctx
->ac
.builder
, tmp
, ctx
->ac
.i64
, "");
1018 case nir_op_cube_face_coord
: {
1019 src
[0] = ac_to_float(&ctx
->ac
, src
[0]);
1020 LLVMValueRef results
[2];
1022 for (unsigned chan
= 0; chan
< 3; chan
++)
1023 in
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src
[0], chan
);
1024 results
[0] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubetc",
1025 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
1026 results
[1] = ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.cubesc",
1027 ctx
->ac
.f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
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
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1050 result
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, result
, src
[2]);
1053 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1054 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, 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
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1064 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, result
, src
[2]);
1067 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1068 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, result
, src
[2]);
1070 case nir_op_fmed3
: {
1071 LLVMValueRef tmp1
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1072 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1073 LLVMValueRef tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1074 ac_to_float_type(&ctx
->ac
, def_type
), src
[0], src
[1]);
1075 tmp2
= emit_intrin_2f_param(&ctx
->ac
, "llvm.minnum",
1076 ac_to_float_type(&ctx
->ac
, def_type
), tmp2
, src
[2]);
1077 result
= emit_intrin_2f_param(&ctx
->ac
, "llvm.maxnum",
1078 ac_to_float_type(&ctx
->ac
, def_type
), tmp1
, tmp2
);
1081 case nir_op_imed3
: {
1082 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, src
[0], src
[1]);
1083 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, src
[0], src
[1]);
1084 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntSLT
, tmp2
, src
[2]);
1085 result
= emit_minmax_int(&ctx
->ac
, LLVMIntSGT
, tmp1
, tmp2
);
1088 case nir_op_umed3
: {
1089 LLVMValueRef tmp1
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, src
[0], src
[1]);
1090 LLVMValueRef tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, src
[0], src
[1]);
1091 tmp2
= emit_minmax_int(&ctx
->ac
, LLVMIntULT
, tmp2
, src
[2]);
1092 result
= emit_minmax_int(&ctx
->ac
, LLVMIntUGT
, tmp1
, tmp2
);
1097 fprintf(stderr
, "Unknown NIR alu instr: ");
1098 nir_print_instr(&instr
->instr
, stderr
);
1099 fprintf(stderr
, "\n");
1104 assert(instr
->dest
.dest
.is_ssa
);
1105 result
= ac_to_integer_or_pointer(&ctx
->ac
, result
);
1106 ctx
->ssa_defs
[instr
->dest
.dest
.ssa
.index
] = result
;
1110 static void visit_load_const(struct ac_nir_context
*ctx
,
1111 const nir_load_const_instr
*instr
)
1113 LLVMValueRef values
[4], value
= NULL
;
1114 LLVMTypeRef element_type
=
1115 LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
1117 for (unsigned i
= 0; i
< instr
->def
.num_components
; ++i
) {
1118 switch (instr
->def
.bit_size
) {
1120 values
[i
] = LLVMConstInt(element_type
,
1121 instr
->value
.u8
[i
], false);
1124 values
[i
] = LLVMConstInt(element_type
,
1125 instr
->value
.u16
[i
], false);
1128 values
[i
] = LLVMConstInt(element_type
,
1129 instr
->value
.u32
[i
], false);
1132 values
[i
] = LLVMConstInt(element_type
,
1133 instr
->value
.u64
[i
], false);
1137 "unsupported nir load_const bit_size: %d\n",
1138 instr
->def
.bit_size
);
1142 if (instr
->def
.num_components
> 1) {
1143 value
= LLVMConstVector(values
, instr
->def
.num_components
);
1147 ctx
->ssa_defs
[instr
->def
.index
] = value
;
1151 get_buffer_size(struct ac_nir_context
*ctx
, LLVMValueRef descriptor
, bool in_elements
)
1154 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1155 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
1158 if (ctx
->ac
.chip_class
== VI
&& in_elements
) {
1159 /* On VI, the descriptor contains the size in bytes,
1160 * but TXQ must return the size in elements.
1161 * The stride is always non-zero for resources using TXQ.
1163 LLVMValueRef stride
=
1164 LLVMBuildExtractElement(ctx
->ac
.builder
, descriptor
,
1166 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
,
1167 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
1168 stride
= LLVMBuildAnd(ctx
->ac
.builder
, stride
,
1169 LLVMConstInt(ctx
->ac
.i32
, 0x3fff, false), "");
1171 size
= LLVMBuildUDiv(ctx
->ac
.builder
, size
, stride
, "");
1176 static LLVMValueRef
lower_gather4_integer(struct ac_llvm_context
*ctx
,
1178 struct ac_image_args
*args
,
1179 const nir_tex_instr
*instr
)
1181 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1182 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1183 LLVMValueRef half_texel
[2];
1184 LLVMValueRef compare_cube_wa
= NULL
;
1185 LLVMValueRef result
;
1189 struct ac_image_args txq_args
= { 0 };
1191 txq_args
.dim
= get_ac_sampler_dim(ctx
, instr
->sampler_dim
, instr
->is_array
);
1192 txq_args
.opcode
= ac_image_get_resinfo
;
1193 txq_args
.dmask
= 0xf;
1194 txq_args
.lod
= ctx
->i32_0
;
1195 txq_args
.resource
= args
->resource
;
1196 txq_args
.attributes
= AC_FUNC_ATTR_READNONE
;
1197 LLVMValueRef size
= ac_build_image_opcode(ctx
, &txq_args
);
1199 for (unsigned c
= 0; c
< 2; c
++) {
1200 half_texel
[c
] = LLVMBuildExtractElement(ctx
->builder
, size
,
1201 LLVMConstInt(ctx
->i32
, c
, false), "");
1202 half_texel
[c
] = LLVMBuildUIToFP(ctx
->builder
, half_texel
[c
], ctx
->f32
, "");
1203 half_texel
[c
] = ac_build_fdiv(ctx
, ctx
->f32_1
, half_texel
[c
]);
1204 half_texel
[c
] = LLVMBuildFMul(ctx
->builder
, half_texel
[c
],
1205 LLVMConstReal(ctx
->f32
, -0.5), "");
1209 LLVMValueRef orig_coords
[2] = { args
->coords
[0], args
->coords
[1] };
1211 for (unsigned c
= 0; c
< 2; c
++) {
1213 tmp
= LLVMBuildBitCast(ctx
->builder
, args
->coords
[c
], ctx
->f32
, "");
1214 args
->coords
[c
] = LLVMBuildFAdd(ctx
->builder
, tmp
, half_texel
[c
], "");
1218 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1219 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1220 * workaround by sampling using a scaled type and converting.
1221 * This is taken from amdgpu-pro shaders.
1223 /* NOTE this produces some ugly code compared to amdgpu-pro,
1224 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1225 * and then reads them back. -pro generates two selects,
1226 * one s_cmp for the descriptor rewriting
1227 * one v_cmp for the coordinate and result changes.
1229 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1230 LLVMValueRef tmp
, tmp2
;
1232 /* workaround 8/8/8/8 uint/sint cube gather bug */
1233 /* first detect it then change to a scaled read and f2i */
1234 tmp
= LLVMBuildExtractElement(ctx
->builder
, args
->resource
, ctx
->i32_1
, "");
1237 /* extract the DATA_FORMAT */
1238 tmp
= ac_build_bfe(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 20, false),
1239 LLVMConstInt(ctx
->i32
, 6, false), false);
1241 /* is the DATA_FORMAT == 8_8_8_8 */
1242 compare_cube_wa
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tmp
, LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false), "");
1244 if (stype
== GLSL_TYPE_UINT
)
1245 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1246 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0x8000000, false),
1247 LLVMConstInt(ctx
->i32
, 0x10000000, false), "");
1249 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1250 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, LLVMConstInt(ctx
->i32
, 0xc000000, false),
1251 LLVMConstInt(ctx
->i32
, 0x14000000, false), "");
1253 /* replace the NUM FORMAT in the descriptor */
1254 tmp2
= LLVMBuildAnd(ctx
->builder
, tmp2
, LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false), "");
1255 tmp2
= LLVMBuildOr(ctx
->builder
, tmp2
, tmp
, "");
1257 args
->resource
= LLVMBuildInsertElement(ctx
->builder
, args
->resource
, tmp2
, ctx
->i32_1
, "");
1259 /* don't modify the coordinates for this case */
1260 for (unsigned c
= 0; c
< 2; ++c
)
1261 args
->coords
[c
] = LLVMBuildSelect(
1262 ctx
->builder
, compare_cube_wa
,
1263 orig_coords
[c
], args
->coords
[c
], "");
1266 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1267 result
= ac_build_image_opcode(ctx
, args
);
1269 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1270 LLVMValueRef tmp
, tmp2
;
1272 /* if the cube workaround is in place, f2i the result. */
1273 for (unsigned c
= 0; c
< 4; c
++) {
1274 tmp
= LLVMBuildExtractElement(ctx
->builder
, result
, LLVMConstInt(ctx
->i32
, c
, false), "");
1275 if (stype
== GLSL_TYPE_UINT
)
1276 tmp2
= LLVMBuildFPToUI(ctx
->builder
, tmp
, ctx
->i32
, "");
1278 tmp2
= LLVMBuildFPToSI(ctx
->builder
, tmp
, ctx
->i32
, "");
1279 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->i32
, "");
1280 tmp2
= LLVMBuildBitCast(ctx
->builder
, tmp2
, ctx
->i32
, "");
1281 tmp
= LLVMBuildSelect(ctx
->builder
, compare_cube_wa
, tmp2
, tmp
, "");
1282 tmp
= LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f32
, "");
1283 result
= LLVMBuildInsertElement(ctx
->builder
, result
, tmp
, LLVMConstInt(ctx
->i32
, c
, false), "");
1289 static nir_deref_instr
*get_tex_texture_deref(const nir_tex_instr
*instr
)
1291 nir_deref_instr
*texture_deref_instr
= NULL
;
1293 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1294 switch (instr
->src
[i
].src_type
) {
1295 case nir_tex_src_texture_deref
:
1296 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
1302 return texture_deref_instr
;
1305 static LLVMValueRef
build_tex_intrinsic(struct ac_nir_context
*ctx
,
1306 const nir_tex_instr
*instr
,
1307 struct ac_image_args
*args
)
1309 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
1310 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
1312 if (ctx
->abi
->gfx9_stride_size_workaround
) {
1313 return ac_build_buffer_load_format_gfx9_safe(&ctx
->ac
,
1317 util_last_bit(mask
),
1320 return ac_build_buffer_load_format(&ctx
->ac
,
1324 util_last_bit(mask
),
1329 args
->opcode
= ac_image_sample
;
1331 switch (instr
->op
) {
1333 case nir_texop_txf_ms
:
1334 case nir_texop_samples_identical
:
1335 args
->opcode
= args
->level_zero
||
1336 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
?
1337 ac_image_load
: ac_image_load_mip
;
1338 args
->level_zero
= false;
1341 case nir_texop_query_levels
:
1342 args
->opcode
= ac_image_get_resinfo
;
1344 args
->lod
= ctx
->ac
.i32_0
;
1345 args
->level_zero
= false;
1348 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1350 args
->level_zero
= true;
1354 args
->opcode
= ac_image_gather4
;
1355 args
->level_zero
= true;
1358 args
->opcode
= ac_image_get_lod
;
1364 if (instr
->op
== nir_texop_tg4
&& ctx
->ac
.chip_class
<= VI
) {
1365 nir_deref_instr
*texture_deref_instr
= get_tex_texture_deref(instr
);
1366 nir_variable
*var
= nir_deref_instr_get_variable(texture_deref_instr
);
1367 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1368 enum glsl_base_type stype
= glsl_get_sampler_result_type(type
);
1369 if (stype
== GLSL_TYPE_UINT
|| stype
== GLSL_TYPE_INT
) {
1370 return lower_gather4_integer(&ctx
->ac
, var
, args
, instr
);
1374 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1375 if (instr
->op
== nir_texop_lod
&& ctx
->ac
.chip_class
>= GFX9
) {
1376 if ((args
->dim
== ac_image_2darray
||
1377 args
->dim
== ac_image_2d
) && !args
->coords
[1]) {
1378 args
->coords
[1] = ctx
->ac
.i32_0
;
1382 args
->attributes
= AC_FUNC_ATTR_READNONE
;
1383 return ac_build_image_opcode(&ctx
->ac
, args
);
1386 static LLVMValueRef
visit_vulkan_resource_reindex(struct ac_nir_context
*ctx
,
1387 nir_intrinsic_instr
*instr
)
1389 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
1390 LLVMValueRef index
= get_src(ctx
, instr
->src
[1]);
1392 LLVMValueRef result
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
1393 LLVMSetMetadata(result
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1397 static LLVMValueRef
visit_load_push_constant(struct ac_nir_context
*ctx
,
1398 nir_intrinsic_instr
*instr
)
1400 LLVMValueRef ptr
, addr
;
1401 LLVMValueRef src0
= get_src(ctx
, instr
->src
[0]);
1402 unsigned index
= nir_intrinsic_base(instr
);
1404 addr
= LLVMConstInt(ctx
->ac
.i32
, index
, 0);
1405 addr
= LLVMBuildAdd(ctx
->ac
.builder
, addr
, src0
, "");
1407 /* Load constant values from user SGPRS when possible, otherwise
1408 * fallback to the default path that loads directly from memory.
1410 if (LLVMIsConstant(src0
) &&
1411 instr
->dest
.ssa
.bit_size
== 32) {
1412 unsigned count
= instr
->dest
.ssa
.num_components
;
1413 unsigned offset
= index
;
1415 offset
+= LLVMConstIntGetZExtValue(src0
);
1418 offset
-= ctx
->abi
->base_inline_push_consts
;
1420 if (offset
+ count
<= ctx
->abi
->num_inline_push_consts
) {
1421 return ac_build_gather_values(&ctx
->ac
,
1422 ctx
->abi
->inline_push_consts
+ offset
,
1427 ptr
= ac_build_gep0(&ctx
->ac
, ctx
->abi
->push_constants
, addr
);
1429 if (instr
->dest
.ssa
.bit_size
== 16) {
1430 unsigned load_dwords
= instr
->dest
.ssa
.num_components
/ 2 + 1;
1431 LLVMTypeRef vec_type
= LLVMVectorType(LLVMInt16TypeInContext(ctx
->ac
.context
), 2 * load_dwords
);
1432 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, vec_type
);
1433 LLVMValueRef res
= LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1434 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res
, vec_type
, "");
1435 LLVMValueRef cond
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, ctx
->ac
.i32_1
, "");
1436 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
, ctx
->ac
.i1
, "");
1437 LLVMValueRef mask
[] = { LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
1438 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
1439 LLVMConstInt(ctx
->ac
.i32
, 4, false)};
1440 LLVMValueRef swizzle_aligned
= LLVMConstVector(&mask
[0], instr
->dest
.ssa
.num_components
);
1441 LLVMValueRef swizzle_unaligned
= LLVMConstVector(&mask
[1], instr
->dest
.ssa
.num_components
);
1442 LLVMValueRef shuffle_aligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_aligned
, "");
1443 LLVMValueRef shuffle_unaligned
= LLVMBuildShuffleVector(ctx
->ac
.builder
, res
, res
, swizzle_unaligned
, "");
1444 res
= LLVMBuildSelect(ctx
->ac
.builder
, cond
, shuffle_unaligned
, shuffle_aligned
, "");
1445 return LLVMBuildBitCast(ctx
->ac
.builder
, res
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
1448 ptr
= ac_cast_ptr(&ctx
->ac
, ptr
, get_def_type(ctx
, &instr
->dest
.ssa
));
1450 return LLVMBuildLoad(ctx
->ac
.builder
, ptr
, "");
1453 static LLVMValueRef
visit_get_buffer_size(struct ac_nir_context
*ctx
,
1454 const nir_intrinsic_instr
*instr
)
1456 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
1458 return get_buffer_size(ctx
, ctx
->abi
->load_ssbo(ctx
->abi
, index
, false), false);
1461 static uint32_t widen_mask(uint32_t mask
, unsigned multiplier
)
1463 uint32_t new_mask
= 0;
1464 for(unsigned i
= 0; i
< 32 && (1u << i
) <= mask
; ++i
)
1465 if (mask
& (1u << i
))
1466 new_mask
|= ((1u << multiplier
) - 1u) << (i
* multiplier
);
1470 static LLVMValueRef
extract_vector_range(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
1471 unsigned start
, unsigned count
)
1473 LLVMValueRef mask
[] = {
1474 ctx
->i32_0
, ctx
->i32_1
,
1475 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false) };
1477 unsigned src_elements
= ac_get_llvm_num_components(src
);
1479 if (count
== src_elements
) {
1482 } else if (count
== 1) {
1483 assert(start
< src_elements
);
1484 return LLVMBuildExtractElement(ctx
->builder
, src
, mask
[start
], "");
1486 assert(start
+ count
<= src_elements
);
1488 LLVMValueRef swizzle
= LLVMConstVector(&mask
[start
], count
);
1489 return LLVMBuildShuffleVector(ctx
->builder
, src
, src
, swizzle
, "");
1493 static unsigned get_cache_policy(struct ac_nir_context
*ctx
,
1494 enum gl_access_qualifier access
,
1495 bool may_store_unaligned
,
1496 bool writeonly_memory
)
1498 unsigned cache_policy
= 0;
1500 /* SI has a TC L1 bug causing corruption of 8bit/16bit stores. All
1501 * store opcodes not aligned to a dword are affected. The only way to
1502 * get unaligned stores is through shader images.
1504 if (((may_store_unaligned
&& ctx
->ac
.chip_class
== SI
) ||
1505 /* If this is write-only, don't keep data in L1 to prevent
1506 * evicting L1 cache lines that may be needed by other
1510 access
& (ACCESS_COHERENT
| ACCESS_VOLATILE
))) {
1511 cache_policy
|= ac_glc
;
1514 return cache_policy
;
1517 static void visit_store_ssbo(struct ac_nir_context
*ctx
,
1518 nir_intrinsic_instr
*instr
)
1520 const char *store_name
;
1521 LLVMValueRef src_data
= get_src(ctx
, instr
->src
[0]);
1522 int elem_size_bytes
= ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src_data
)) / 8;
1523 unsigned writemask
= nir_intrinsic_write_mask(instr
);
1524 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1525 bool writeonly_memory
= access
& ACCESS_NON_READABLE
;
1526 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, writeonly_memory
);
1527 LLVMValueRef glc
= (cache_policy
& ac_glc
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
1529 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1530 get_src(ctx
, instr
->src
[1]), true);
1531 LLVMValueRef base_data
= ac_to_float(&ctx
->ac
, src_data
);
1532 base_data
= ac_trim_vector(&ctx
->ac
, base_data
, instr
->num_components
);
1533 LLVMValueRef base_offset
= get_src(ctx
, instr
->src
[2]);
1537 LLVMValueRef data
, offset
;
1538 LLVMTypeRef data_type
;
1540 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
1542 /* Due to an LLVM limitation, split 3-element writes
1543 * into a 2-element and a 1-element write. */
1545 writemask
|= 1 << (start
+ 2);
1548 int num_bytes
= count
* elem_size_bytes
; /* count in bytes */
1550 /* we can only store 4 DWords at the same time.
1551 * can only happen for 64 Bit vectors. */
1552 if (num_bytes
> 16) {
1553 writemask
|= ((1u << (count
- 2)) - 1u) << (start
+ 2);
1558 /* check alignment of 16 Bit stores */
1559 if (elem_size_bytes
== 2 && num_bytes
> 2 && (start
% 2) == 1) {
1560 writemask
|= ((1u << (count
- 1)) - 1u) << (start
+ 1);
1564 data
= extract_vector_range(&ctx
->ac
, base_data
, start
, count
);
1567 offset
= base_offset
;
1569 offset
= LLVMBuildAdd(ctx
->ac
.builder
, base_offset
,
1570 LLVMConstInt(ctx
->ac
.i32
, start
* elem_size_bytes
, false), "");
1572 if (num_bytes
== 2) {
1573 store_name
= "llvm.amdgcn.tbuffer.store.i32";
1574 data_type
= ctx
->ac
.i32
;
1575 LLVMValueRef tbuffer_params
[] = {
1578 ctx
->ac
.i32_0
, /* vindex */
1579 offset
, /* voffset */
1582 LLVMConstInt(ctx
->ac
.i32
, 2, false), // dfmt (= 16bit)
1583 LLVMConstInt(ctx
->ac
.i32
, 4, false), // nfmt (= uint)
1587 ac_build_intrinsic(&ctx
->ac
, store_name
,
1588 ctx
->ac
.voidt
, tbuffer_params
, 10, 0);
1590 switch (num_bytes
) {
1591 case 16: /* v4f32 */
1592 store_name
= "llvm.amdgcn.buffer.store.v4f32";
1593 data_type
= ctx
->ac
.v4f32
;
1596 store_name
= "llvm.amdgcn.buffer.store.v2f32";
1597 data_type
= ctx
->ac
.v2f32
;
1600 store_name
= "llvm.amdgcn.buffer.store.f32";
1601 data_type
= ctx
->ac
.f32
;
1604 unreachable("Malformed vector store.");
1606 data
= LLVMBuildBitCast(ctx
->ac
.builder
, data
, data_type
, "");
1607 LLVMValueRef params
[] = {
1610 ctx
->ac
.i32_0
, /* vindex */
1613 ctx
->ac
.i1false
, /* slc */
1615 ac_build_intrinsic(&ctx
->ac
, store_name
,
1616 ctx
->ac
.voidt
, params
, 6, 0);
1621 static LLVMValueRef
visit_atomic_ssbo(struct ac_nir_context
*ctx
,
1622 const nir_intrinsic_instr
*instr
)
1625 LLVMValueRef params
[6];
1628 if (instr
->intrinsic
== nir_intrinsic_ssbo_atomic_comp_swap
) {
1629 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[3]), 0);
1631 params
[arg_count
++] = ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
1632 params
[arg_count
++] = ctx
->abi
->load_ssbo(ctx
->abi
,
1633 get_src(ctx
, instr
->src
[0]),
1635 params
[arg_count
++] = ctx
->ac
.i32_0
; /* vindex */
1636 params
[arg_count
++] = get_src(ctx
, instr
->src
[1]); /* voffset */
1637 params
[arg_count
++] = ctx
->ac
.i1false
; /* slc */
1639 switch (instr
->intrinsic
) {
1640 case nir_intrinsic_ssbo_atomic_add
:
1641 name
= "llvm.amdgcn.buffer.atomic.add";
1643 case nir_intrinsic_ssbo_atomic_imin
:
1644 name
= "llvm.amdgcn.buffer.atomic.smin";
1646 case nir_intrinsic_ssbo_atomic_umin
:
1647 name
= "llvm.amdgcn.buffer.atomic.umin";
1649 case nir_intrinsic_ssbo_atomic_imax
:
1650 name
= "llvm.amdgcn.buffer.atomic.smax";
1652 case nir_intrinsic_ssbo_atomic_umax
:
1653 name
= "llvm.amdgcn.buffer.atomic.umax";
1655 case nir_intrinsic_ssbo_atomic_and
:
1656 name
= "llvm.amdgcn.buffer.atomic.and";
1658 case nir_intrinsic_ssbo_atomic_or
:
1659 name
= "llvm.amdgcn.buffer.atomic.or";
1661 case nir_intrinsic_ssbo_atomic_xor
:
1662 name
= "llvm.amdgcn.buffer.atomic.xor";
1664 case nir_intrinsic_ssbo_atomic_exchange
:
1665 name
= "llvm.amdgcn.buffer.atomic.swap";
1667 case nir_intrinsic_ssbo_atomic_comp_swap
:
1668 name
= "llvm.amdgcn.buffer.atomic.cmpswap";
1674 return ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.i32
, params
, arg_count
, 0);
1677 static LLVMValueRef
visit_load_buffer(struct ac_nir_context
*ctx
,
1678 const nir_intrinsic_instr
*instr
)
1680 int elem_size_bytes
= instr
->dest
.ssa
.bit_size
/ 8;
1681 int num_components
= instr
->num_components
;
1682 enum gl_access_qualifier access
= nir_intrinsic_access(instr
);
1683 unsigned cache_policy
= get_cache_policy(ctx
, access
, false, false);
1684 LLVMValueRef glc
= (cache_policy
& ac_glc
) ? ctx
->ac
.i1true
: ctx
->ac
.i1false
;
1686 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1687 LLVMValueRef rsrc
= ctx
->abi
->load_ssbo(ctx
->abi
,
1688 get_src(ctx
, instr
->src
[0]), false);
1689 LLVMValueRef vindex
= ctx
->ac
.i32_0
;
1691 LLVMTypeRef def_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1692 LLVMTypeRef def_elem_type
= num_components
> 1 ? LLVMGetElementType(def_type
) : def_type
;
1694 LLVMValueRef results
[4];
1695 for (int i
= 0; i
< num_components
;) {
1696 int num_elems
= num_components
- i
;
1697 if (elem_size_bytes
< 4 && nir_intrinsic_align(instr
) % 4 != 0)
1699 if (num_elems
* elem_size_bytes
> 16)
1700 num_elems
= 16 / elem_size_bytes
;
1701 int load_bytes
= num_elems
* elem_size_bytes
;
1703 LLVMValueRef immoffset
= LLVMConstInt(ctx
->ac
.i32
, i
* elem_size_bytes
, false);
1706 if (load_bytes
== 2) {
1707 ret
= ac_build_tbuffer_load_short(&ctx
->ac
,
1715 const char *load_name
;
1716 LLVMTypeRef data_type
;
1717 switch (load_bytes
) {
1720 load_name
= "llvm.amdgcn.buffer.load.v4f32";
1721 data_type
= ctx
->ac
.v4f32
;
1725 load_name
= "llvm.amdgcn.buffer.load.v2f32";
1726 data_type
= ctx
->ac
.v2f32
;
1729 load_name
= "llvm.amdgcn.buffer.load.f32";
1730 data_type
= ctx
->ac
.f32
;
1733 unreachable("Malformed load buffer.");
1735 LLVMValueRef params
[] = {
1738 LLVMBuildAdd(ctx
->ac
.builder
, offset
, immoffset
, ""),
1742 ret
= ac_build_intrinsic(&ctx
->ac
, load_name
, data_type
, params
, 5, 0);
1745 LLVMTypeRef byte_vec
= LLVMVectorType(ctx
->ac
.i8
, ac_get_type_size(LLVMTypeOf(ret
)));
1746 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, byte_vec
, "");
1747 ret
= ac_trim_vector(&ctx
->ac
, ret
, load_bytes
);
1749 LLVMTypeRef ret_type
= LLVMVectorType(def_elem_type
, num_elems
);
1750 ret
= LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ret_type
, "");
1752 for (unsigned j
= 0; j
< num_elems
; j
++) {
1753 results
[i
+ j
] = LLVMBuildExtractElement(ctx
->ac
.builder
, ret
, LLVMConstInt(ctx
->ac
.i32
, j
, false), "");
1758 return ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1761 static LLVMValueRef
visit_load_ubo_buffer(struct ac_nir_context
*ctx
,
1762 const nir_intrinsic_instr
*instr
)
1765 LLVMValueRef rsrc
= get_src(ctx
, instr
->src
[0]);
1766 LLVMValueRef offset
= get_src(ctx
, instr
->src
[1]);
1767 int num_components
= instr
->num_components
;
1769 if (ctx
->abi
->load_ubo
)
1770 rsrc
= ctx
->abi
->load_ubo(ctx
->abi
, rsrc
);
1772 if (instr
->dest
.ssa
.bit_size
== 64)
1773 num_components
*= 2;
1775 if (instr
->dest
.ssa
.bit_size
== 16) {
1776 LLVMValueRef results
[num_components
];
1777 for (unsigned i
= 0; i
< num_components
; ++i
) {
1778 results
[i
] = ac_build_tbuffer_load_short(&ctx
->ac
,
1783 LLVMConstInt(ctx
->ac
.i32
, 2 * i
, 0),
1786 ret
= ac_build_gather_values(&ctx
->ac
, results
, num_components
);
1788 ret
= ac_build_buffer_load(&ctx
->ac
, rsrc
, num_components
, NULL
, offset
,
1789 NULL
, 0, false, false, true, true);
1791 ret
= ac_trim_vector(&ctx
->ac
, ret
, num_components
);
1794 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
,
1795 get_def_type(ctx
, &instr
->dest
.ssa
), "");
1799 get_deref_offset(struct ac_nir_context
*ctx
, nir_deref_instr
*instr
,
1800 bool vs_in
, unsigned *vertex_index_out
,
1801 LLVMValueRef
*vertex_index_ref
,
1802 unsigned *const_out
, LLVMValueRef
*indir_out
)
1804 nir_variable
*var
= nir_deref_instr_get_variable(instr
);
1805 nir_deref_path path
;
1806 unsigned idx_lvl
= 1;
1808 nir_deref_path_init(&path
, instr
, NULL
);
1810 if (vertex_index_out
!= NULL
|| vertex_index_ref
!= NULL
) {
1811 if (vertex_index_ref
) {
1812 *vertex_index_ref
= get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
);
1813 if (vertex_index_out
)
1814 *vertex_index_out
= 0;
1816 nir_const_value
*v
= nir_src_as_const_value(path
.path
[idx_lvl
]->arr
.index
);
1818 *vertex_index_out
= v
->u32
[0];
1823 uint32_t const_offset
= 0;
1824 LLVMValueRef offset
= NULL
;
1826 if (var
->data
.compact
) {
1827 assert(instr
->deref_type
== nir_deref_type_array
);
1828 nir_const_value
*v
= nir_src_as_const_value(instr
->arr
.index
);
1830 const_offset
= v
->u32
[0];
1834 for (; path
.path
[idx_lvl
]; ++idx_lvl
) {
1835 const struct glsl_type
*parent_type
= path
.path
[idx_lvl
- 1]->type
;
1836 if (path
.path
[idx_lvl
]->deref_type
== nir_deref_type_struct
) {
1837 unsigned index
= path
.path
[idx_lvl
]->strct
.index
;
1839 for (unsigned i
= 0; i
< index
; i
++) {
1840 const struct glsl_type
*ft
= glsl_get_struct_field(parent_type
, i
);
1841 const_offset
+= glsl_count_attribute_slots(ft
, vs_in
);
1843 } else if(path
.path
[idx_lvl
]->deref_type
== nir_deref_type_array
) {
1844 unsigned size
= glsl_count_attribute_slots(path
.path
[idx_lvl
]->type
, vs_in
);
1845 LLVMValueRef array_off
= LLVMBuildMul(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, size
, 0),
1846 get_src(ctx
, path
.path
[idx_lvl
]->arr
.index
), "");
1848 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, array_off
, "");
1852 unreachable("Uhandled deref type in get_deref_instr_offset");
1856 nir_deref_path_finish(&path
);
1858 if (const_offset
&& offset
)
1859 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
,
1860 LLVMConstInt(ctx
->ac
.i32
, const_offset
, 0),
1863 *const_out
= const_offset
;
1864 *indir_out
= offset
;
1867 static LLVMValueRef
load_tess_varyings(struct ac_nir_context
*ctx
,
1868 nir_intrinsic_instr
*instr
,
1871 LLVMValueRef result
;
1872 LLVMValueRef vertex_index
= NULL
;
1873 LLVMValueRef indir_index
= NULL
;
1874 unsigned const_index
= 0;
1876 nir_variable
*var
= nir_deref_instr_get_variable(nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
));
1878 unsigned location
= var
->data
.location
;
1879 unsigned driver_location
= var
->data
.driver_location
;
1880 const bool is_patch
= var
->data
.patch
;
1881 const bool is_compact
= var
->data
.compact
;
1883 get_deref_offset(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
1884 false, NULL
, is_patch
? NULL
: &vertex_index
,
1885 &const_index
, &indir_index
);
1887 LLVMTypeRef dest_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
1889 LLVMTypeRef src_component_type
;
1890 if (LLVMGetTypeKind(dest_type
) == LLVMVectorTypeKind
)
1891 src_component_type
= LLVMGetElementType(dest_type
);
1893 src_component_type
= dest_type
;
1895 result
= ctx
->abi
->load_tess_varyings(ctx
->abi
, src_component_type
,
1896 vertex_index
, indir_index
,
1897 const_index
, location
, driver_location
,
1898 var
->data
.location_frac
,
1899 instr
->num_components
,
1900 is_patch
, is_compact
, load_inputs
);
1901 if (instr
->dest
.ssa
.bit_size
== 16) {
1902 result
= ac_to_integer(&ctx
->ac
, result
);
1903 result
= LLVMBuildTrunc(ctx
->ac
.builder
, result
, dest_type
, "");
1905 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, dest_type
, "");
1909 type_scalar_size_bytes(const struct glsl_type
*type
)
1911 assert(glsl_type_is_vector_or_scalar(type
) ||
1912 glsl_type_is_matrix(type
));
1913 return glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
1916 static LLVMValueRef
visit_load_var(struct ac_nir_context
*ctx
,
1917 nir_intrinsic_instr
*instr
)
1919 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
1920 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1922 LLVMValueRef values
[8];
1924 int ve
= instr
->dest
.ssa
.num_components
;
1926 LLVMValueRef indir_index
;
1928 unsigned const_index
;
1929 unsigned stride
= 4;
1930 int mode
= deref
->mode
;
1933 bool vs_in
= ctx
->stage
== MESA_SHADER_VERTEX
&&
1934 var
->data
.mode
== nir_var_shader_in
;
1935 if (var
->data
.compact
)
1937 idx
= var
->data
.driver_location
;
1938 comp
= var
->data
.location_frac
;
1939 mode
= var
->data
.mode
;
1941 get_deref_offset(ctx
, deref
, vs_in
, NULL
, NULL
,
1942 &const_index
, &indir_index
);
1945 if (instr
->dest
.ssa
.bit_size
== 64 &&
1946 (deref
->mode
== nir_var_shader_in
||
1947 deref
->mode
== nir_var_shader_out
||
1948 deref
->mode
== nir_var_function_temp
))
1952 case nir_var_shader_in
:
1953 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
1954 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
1955 return load_tess_varyings(ctx
, instr
, true);
1958 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
1959 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->dest
.ssa
.bit_size
);
1960 LLVMValueRef indir_index
;
1961 unsigned const_index
, vertex_index
;
1962 get_deref_offset(ctx
, deref
, false, &vertex_index
, NULL
,
1963 &const_index
, &indir_index
);
1965 return ctx
->abi
->load_inputs(ctx
->abi
, var
->data
.location
,
1966 var
->data
.driver_location
,
1967 var
->data
.location_frac
,
1968 instr
->num_components
, vertex_index
, const_index
, type
);
1971 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
1973 unsigned count
= glsl_count_attribute_slots(
1975 ctx
->stage
== MESA_SHADER_VERTEX
);
1977 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1978 &ctx
->ac
, ctx
->abi
->inputs
+ idx
+ chan
, count
,
1979 stride
, false, true);
1981 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
1985 values
[chan
] = ctx
->abi
->inputs
[idx
+ chan
+ const_index
* stride
];
1988 case nir_var_function_temp
:
1989 for (unsigned chan
= 0; chan
< ve
; chan
++) {
1991 unsigned count
= glsl_count_attribute_slots(
1994 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
1995 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
1996 stride
, true, true);
1998 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2002 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, ctx
->locals
[idx
+ chan
+ const_index
* stride
], "");
2006 case nir_var_mem_shared
: {
2007 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2008 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2009 return LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2010 get_def_type(ctx
, &instr
->dest
.ssa
),
2013 case nir_var_shader_out
:
2014 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2015 return load_tess_varyings(ctx
, instr
, false);
2018 for (unsigned chan
= comp
; chan
< ve
+ comp
; chan
++) {
2020 unsigned count
= glsl_count_attribute_slots(
2023 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2024 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2025 stride
, true, true);
2027 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
2031 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
,
2032 ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
],
2037 case nir_var_mem_global
: {
2038 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2039 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2040 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2041 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2043 LLVMTypeRef result_type
= get_def_type(ctx
, &instr
->dest
.ssa
);
2044 if (stride
!= natural_stride
) {
2045 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(result_type
),
2046 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2047 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2049 for (unsigned i
= 0; i
< instr
->dest
.ssa
.num_components
; ++i
) {
2050 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, i
* stride
/ natural_stride
, 0);
2051 values
[i
] = LLVMBuildLoad(ctx
->ac
.builder
,
2052 ac_build_gep_ptr(&ctx
->ac
, address
, offset
), "");
2054 return ac_build_gather_values(&ctx
->ac
, values
, instr
->dest
.ssa
.num_components
);
2056 LLVMTypeRef ptr_type
= LLVMPointerType(result_type
,
2057 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2058 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2059 LLVMValueRef val
= LLVMBuildLoad(ctx
->ac
.builder
, address
, "");
2064 unreachable("unhandle variable mode");
2066 ret
= ac_build_varying_gather_values(&ctx
->ac
, values
, ve
, comp
);
2067 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2071 visit_store_var(struct ac_nir_context
*ctx
,
2072 nir_intrinsic_instr
*instr
)
2074 nir_deref_instr
*deref
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2075 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2077 LLVMValueRef temp_ptr
, value
;
2080 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[1]));
2081 int writemask
= instr
->const_index
[0];
2082 LLVMValueRef indir_index
;
2083 unsigned const_index
;
2086 get_deref_offset(ctx
, deref
, false,
2087 NULL
, NULL
, &const_index
, &indir_index
);
2088 idx
= var
->data
.driver_location
;
2089 comp
= var
->data
.location_frac
;
2092 if (ac_get_elem_bits(&ctx
->ac
, LLVMTypeOf(src
)) == 64 &&
2093 (deref
->mode
== nir_var_shader_out
||
2094 deref
->mode
== nir_var_function_temp
)) {
2096 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2097 LLVMVectorType(ctx
->ac
.f32
, ac_get_llvm_num_components(src
) * 2),
2100 writemask
= widen_mask(writemask
, 2);
2103 writemask
= writemask
<< comp
;
2105 switch (deref
->mode
) {
2106 case nir_var_shader_out
:
2108 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
2109 LLVMValueRef vertex_index
= NULL
;
2110 LLVMValueRef indir_index
= NULL
;
2111 unsigned const_index
= 0;
2112 const bool is_patch
= var
->data
.patch
;
2114 get_deref_offset(ctx
, deref
, false, NULL
,
2115 is_patch
? NULL
: &vertex_index
,
2116 &const_index
, &indir_index
);
2118 ctx
->abi
->store_tcs_outputs(ctx
->abi
, var
,
2119 vertex_index
, indir_index
,
2120 const_index
, src
, writemask
);
2124 for (unsigned chan
= 0; chan
< 8; chan
++) {
2126 if (!(writemask
& (1 << chan
)))
2129 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- comp
);
2131 if (var
->data
.compact
)
2134 unsigned count
= glsl_count_attribute_slots(
2137 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2138 &ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
, count
,
2139 stride
, true, true);
2141 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2142 value
, indir_index
, "");
2143 build_store_values_extended(&ctx
->ac
, ctx
->abi
->outputs
+ idx
+ chan
,
2144 count
, stride
, tmp_vec
);
2147 temp_ptr
= ctx
->abi
->outputs
[idx
+ chan
+ const_index
* stride
];
2149 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2153 case nir_var_function_temp
:
2154 for (unsigned chan
= 0; chan
< 8; chan
++) {
2155 if (!(writemask
& (1 << chan
)))
2158 value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2160 unsigned count
= glsl_count_attribute_slots(
2163 LLVMValueRef tmp_vec
= ac_build_gather_values_extended(
2164 &ctx
->ac
, ctx
->locals
+ idx
+ chan
, count
,
2167 tmp_vec
= LLVMBuildInsertElement(ctx
->ac
.builder
, tmp_vec
,
2168 value
, indir_index
, "");
2169 build_store_values_extended(&ctx
->ac
, ctx
->locals
+ idx
+ chan
,
2172 temp_ptr
= ctx
->locals
[idx
+ chan
+ const_index
* 4];
2174 LLVMBuildStore(ctx
->ac
.builder
, value
, temp_ptr
);
2179 case nir_var_mem_global
:
2180 case nir_var_mem_shared
: {
2181 int writemask
= instr
->const_index
[0];
2182 LLVMValueRef address
= get_src(ctx
, instr
->src
[0]);
2183 LLVMValueRef val
= get_src(ctx
, instr
->src
[1]);
2185 unsigned explicit_stride
= glsl_get_explicit_stride(deref
->type
);
2186 unsigned natural_stride
= type_scalar_size_bytes(deref
->type
);
2187 unsigned stride
= explicit_stride
? explicit_stride
: natural_stride
;
2189 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2190 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2191 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2193 if (writemask
== (1u << ac_get_llvm_num_components(val
)) - 1 &&
2194 stride
== natural_stride
) {
2195 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMTypeOf(val
),
2196 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2197 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2199 val
= LLVMBuildBitCast(ctx
->ac
.builder
, val
,
2200 LLVMGetElementType(LLVMTypeOf(address
)), "");
2201 LLVMBuildStore(ctx
->ac
.builder
, val
, address
);
2203 LLVMTypeRef ptr_type
= LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val
)),
2204 LLVMGetPointerAddressSpace(LLVMTypeOf(address
)));
2205 address
= LLVMBuildBitCast(ctx
->ac
.builder
, address
, ptr_type
, "");
2206 for (unsigned chan
= 0; chan
< 4; chan
++) {
2207 if (!(writemask
& (1 << chan
)))
2210 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, chan
* stride
/ natural_stride
, 0);
2212 LLVMValueRef ptr
= ac_build_gep_ptr(&ctx
->ac
, address
, offset
);
2213 LLVMValueRef src
= ac_llvm_extract_elem(&ctx
->ac
, val
,
2215 src
= LLVMBuildBitCast(ctx
->ac
.builder
, src
,
2216 LLVMGetElementType(LLVMTypeOf(ptr
)), "");
2217 LLVMBuildStore(ctx
->ac
.builder
, src
, ptr
);
2228 static int image_type_to_components_count(enum glsl_sampler_dim dim
, bool array
)
2231 case GLSL_SAMPLER_DIM_BUF
:
2233 case GLSL_SAMPLER_DIM_1D
:
2234 return array
? 2 : 1;
2235 case GLSL_SAMPLER_DIM_2D
:
2236 return array
? 3 : 2;
2237 case GLSL_SAMPLER_DIM_MS
:
2238 return array
? 4 : 3;
2239 case GLSL_SAMPLER_DIM_3D
:
2240 case GLSL_SAMPLER_DIM_CUBE
:
2242 case GLSL_SAMPLER_DIM_RECT
:
2243 case GLSL_SAMPLER_DIM_SUBPASS
:
2245 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2254 /* Adjust the sample index according to FMASK.
2256 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2257 * which is the identity mapping. Each nibble says which physical sample
2258 * should be fetched to get that sample.
2260 * For example, 0x11111100 means there are only 2 samples stored and
2261 * the second sample covers 3/4 of the pixel. When reading samples 0
2262 * and 1, return physical sample 0 (determined by the first two 0s
2263 * in FMASK), otherwise return physical sample 1.
2265 * The sample index should be adjusted as follows:
2266 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2268 static LLVMValueRef
adjust_sample_index_using_fmask(struct ac_llvm_context
*ctx
,
2269 LLVMValueRef coord_x
, LLVMValueRef coord_y
,
2270 LLVMValueRef coord_z
,
2271 LLVMValueRef sample_index
,
2272 LLVMValueRef fmask_desc_ptr
)
2274 struct ac_image_args args
= {0};
2277 args
.coords
[0] = coord_x
;
2278 args
.coords
[1] = coord_y
;
2280 args
.coords
[2] = coord_z
;
2282 args
.opcode
= ac_image_load
;
2283 args
.dim
= coord_z
? ac_image_2darray
: ac_image_2d
;
2284 args
.resource
= fmask_desc_ptr
;
2286 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2288 res
= ac_build_image_opcode(ctx
, &args
);
2290 res
= ac_to_integer(ctx
, res
);
2291 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, false);
2292 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xf, false);
2294 LLVMValueRef fmask
= LLVMBuildExtractElement(ctx
->builder
,
2298 LLVMValueRef sample_index4
=
2299 LLVMBuildMul(ctx
->builder
, sample_index
, four
, "");
2300 LLVMValueRef shifted_fmask
=
2301 LLVMBuildLShr(ctx
->builder
, fmask
, sample_index4
, "");
2302 LLVMValueRef final_sample
=
2303 LLVMBuildAnd(ctx
->builder
, shifted_fmask
, F
, "");
2305 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2306 * resource descriptor is 0 (invalid),
2308 LLVMValueRef fmask_desc
=
2309 LLVMBuildBitCast(ctx
->builder
, fmask_desc_ptr
,
2312 LLVMValueRef fmask_word1
=
2313 LLVMBuildExtractElement(ctx
->builder
, fmask_desc
,
2316 LLVMValueRef word1_is_nonzero
=
2317 LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2318 fmask_word1
, ctx
->i32_0
, "");
2320 /* Replace the MSAA sample index. */
2322 LLVMBuildSelect(ctx
->builder
, word1_is_nonzero
,
2323 final_sample
, sample_index
, "");
2324 return sample_index
;
2327 static nir_deref_instr
*get_image_deref(const nir_intrinsic_instr
*instr
)
2329 assert(instr
->src
[0].is_ssa
);
2330 return nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2333 static LLVMValueRef
get_image_descriptor(struct ac_nir_context
*ctx
,
2334 const nir_intrinsic_instr
*instr
,
2335 enum ac_descriptor_type desc_type
,
2338 return get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
), desc_type
, NULL
, true, write
);
2341 static void get_image_coords(struct ac_nir_context
*ctx
,
2342 const nir_intrinsic_instr
*instr
,
2343 struct ac_image_args
*args
)
2345 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2347 LLVMValueRef src0
= get_src(ctx
, instr
->src
[1]);
2348 LLVMValueRef masks
[] = {
2349 LLVMConstInt(ctx
->ac
.i32
, 0, false), LLVMConstInt(ctx
->ac
.i32
, 1, false),
2350 LLVMConstInt(ctx
->ac
.i32
, 2, false), LLVMConstInt(ctx
->ac
.i32
, 3, false),
2352 LLVMValueRef sample_index
= ac_llvm_extract_elem(&ctx
->ac
, get_src(ctx
, instr
->src
[2]), 0);
2355 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2356 bool is_array
= glsl_sampler_type_is_array(type
);
2357 bool add_frag_pos
= (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
2358 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2359 bool is_ms
= (dim
== GLSL_SAMPLER_DIM_MS
||
2360 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
2361 bool gfx9_1d
= ctx
->ac
.chip_class
>= GFX9
&& dim
== GLSL_SAMPLER_DIM_1D
;
2362 count
= image_type_to_components_count(dim
, is_array
);
2364 if (is_ms
&& instr
->intrinsic
== nir_intrinsic_image_deref_load
) {
2365 LLVMValueRef fmask_load_address
[3];
2368 fmask_load_address
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2369 fmask_load_address
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[1], "");
2371 fmask_load_address
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[2], "");
2373 fmask_load_address
[2] = NULL
;
2375 for (chan
= 0; chan
< 2; ++chan
)
2376 fmask_load_address
[chan
] =
2377 LLVMBuildAdd(ctx
->ac
.builder
, fmask_load_address
[chan
],
2378 LLVMBuildFPToUI(ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2379 ctx
->ac
.i32
, ""), "");
2380 fmask_load_address
[2] = ac_to_integer(&ctx
->ac
, ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2382 sample_index
= adjust_sample_index_using_fmask(&ctx
->ac
,
2383 fmask_load_address
[0],
2384 fmask_load_address
[1],
2385 fmask_load_address
[2],
2387 get_sampler_desc(ctx
, nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
),
2388 AC_DESC_FMASK
, NULL
, false, false));
2390 if (count
== 1 && !gfx9_1d
) {
2391 if (instr
->src
[1].ssa
->num_components
)
2392 args
->coords
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, masks
[0], "");
2394 args
->coords
[0] = src0
;
2399 for (chan
= 0; chan
< count
; ++chan
) {
2400 args
->coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, src0
, chan
);
2403 for (chan
= 0; chan
< 2; ++chan
) {
2404 args
->coords
[chan
] = LLVMBuildAdd(
2405 ctx
->ac
.builder
, args
->coords
[chan
],
2407 ctx
->ac
.builder
, ctx
->abi
->frag_pos
[chan
],
2408 ctx
->ac
.i32
, ""), "");
2410 args
->coords
[2] = ac_to_integer(&ctx
->ac
,
2411 ctx
->abi
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)]);
2417 args
->coords
[2] = args
->coords
[1];
2418 args
->coords
[1] = ctx
->ac
.i32_0
;
2420 args
->coords
[1] = ctx
->ac
.i32_0
;
2425 args
->coords
[count
] = sample_index
;
2431 static LLVMValueRef
get_image_buffer_descriptor(struct ac_nir_context
*ctx
,
2432 const nir_intrinsic_instr
*instr
, bool write
)
2434 LLVMValueRef rsrc
= get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, write
);
2435 if (ctx
->abi
->gfx9_stride_size_workaround
) {
2436 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2437 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, LLVMConstInt(ctx
->ac
.i32
, 1, 0), "");
2438 stride
= LLVMBuildLShr(ctx
->ac
.builder
, stride
, LLVMConstInt(ctx
->ac
.i32
, 16, 0), "");
2440 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->ac
.builder
,
2441 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGT
, elem_count
, stride
, ""),
2442 elem_count
, stride
, "");
2444 rsrc
= LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, new_elem_count
,
2445 LLVMConstInt(ctx
->ac
.i32
, 2, 0), "");
2450 static LLVMValueRef
visit_image_load(struct ac_nir_context
*ctx
,
2451 const nir_intrinsic_instr
*instr
)
2454 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2455 const struct glsl_type
*type
= image_deref
->type
;
2456 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2457 struct ac_image_args args
= {};
2460 get_cache_policy(ctx
, var
->data
.image
.access
, false, false);
2462 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2463 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2464 unsigned mask
= nir_ssa_def_components_read(&instr
->dest
.ssa
);
2465 unsigned num_channels
= util_last_bit(mask
);
2466 LLVMValueRef rsrc
, vindex
;
2468 rsrc
= get_image_buffer_descriptor(ctx
, instr
, false);
2469 vindex
= LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2472 /* TODO: set "can_speculate" when OpenGL needs it. */
2473 res
= ac_build_buffer_load_format(&ctx
->ac
, rsrc
, vindex
,
2474 ctx
->ac
.i32_0
, num_channels
,
2475 !!(args
.cache_policy
& ac_glc
),
2477 res
= ac_build_expand_to_vec4(&ctx
->ac
, res
, num_channels
);
2479 res
= ac_trim_vector(&ctx
->ac
, res
, instr
->dest
.ssa
.num_components
);
2480 res
= ac_to_integer(&ctx
->ac
, res
);
2482 args
.opcode
= ac_image_load
;
2483 get_image_coords(ctx
, instr
, &args
);
2484 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2485 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2486 glsl_sampler_type_is_array(type
));
2488 args
.attributes
= AC_FUNC_ATTR_READONLY
;
2490 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2492 return ac_to_integer(&ctx
->ac
, res
);
2495 static void visit_image_store(struct ac_nir_context
*ctx
,
2496 nir_intrinsic_instr
*instr
)
2498 LLVMValueRef params
[8];
2499 const nir_deref_instr
*image_deref
= get_image_deref(instr
);
2500 const struct glsl_type
*type
= image_deref
->type
;
2501 const nir_variable
*var
= nir_deref_instr_get_variable(image_deref
);
2502 const enum glsl_sampler_dim dim
= glsl_get_sampler_dim(type
);
2503 bool writeonly_memory
= var
->data
.image
.access
& ACCESS_NON_READABLE
;
2504 struct ac_image_args args
= {};
2506 args
.cache_policy
= get_cache_policy(ctx
, var
->data
.image
.access
, true,
2509 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
2511 const char *types
[] = { "f32", "v2f32", "v4f32" };
2512 LLVMValueRef rsrc
= get_image_buffer_descriptor(ctx
, instr
, true);
2513 LLVMValueRef src
= ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2514 unsigned src_channels
= ac_get_llvm_num_components(src
);
2516 if (src_channels
== 3)
2517 src
= ac_build_expand_to_vec4(&ctx
->ac
, src
, 3);
2519 params
[0] = src
; /* data */
2521 params
[2] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2522 ctx
->ac
.i32_0
, ""); /* vindex */
2523 params
[3] = ctx
->ac
.i32_0
; /* voffset */
2524 snprintf(name
, sizeof(name
), "%s.%s",
2525 HAVE_LLVM
>= 0x800 ? "llvm.amdgcn.struct.buffer.store.format"
2526 : "llvm.amdgcn.buffer.store.format",
2527 types
[CLAMP(src_channels
, 1, 3) - 1]);
2529 if (HAVE_LLVM
>= 0x800) {
2530 params
[4] = ctx
->ac
.i32_0
; /* soffset */
2531 params
[5] = (args
.cache_policy
& ac_glc
) ? ctx
->ac
.i32_1
: ctx
->ac
.i32_0
;
2533 params
[4] = LLVMConstInt(ctx
->ac
.i1
, !!(args
.cache_policy
& ac_glc
), 0);
2534 params
[5] = ctx
->ac
.i1false
; /* slc */
2536 ac_build_intrinsic(&ctx
->ac
, name
, ctx
->ac
.voidt
, params
, 6, 0);
2538 args
.opcode
= ac_image_store
;
2539 args
.data
[0] = ac_to_float(&ctx
->ac
, get_src(ctx
, instr
->src
[3]));
2540 get_image_coords(ctx
, instr
, &args
);
2541 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2542 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2543 glsl_sampler_type_is_array(type
));
2546 ac_build_image_opcode(&ctx
->ac
, &args
);
2551 static LLVMValueRef
visit_image_atomic(struct ac_nir_context
*ctx
,
2552 const nir_intrinsic_instr
*instr
)
2554 LLVMValueRef params
[7];
2555 int param_count
= 0;
2556 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2558 bool cmpswap
= instr
->intrinsic
== nir_intrinsic_image_deref_atomic_comp_swap
;
2559 const char *atomic_name
;
2560 char intrinsic_name
[64];
2561 enum ac_atomic_op atomic_subop
;
2562 MAYBE_UNUSED
int length
;
2564 bool is_unsigned
= glsl_get_sampler_result_type(type
) == GLSL_TYPE_UINT
;
2566 switch (instr
->intrinsic
) {
2567 case nir_intrinsic_image_deref_atomic_add
:
2568 atomic_name
= "add";
2569 atomic_subop
= ac_atomic_add
;
2571 case nir_intrinsic_image_deref_atomic_min
:
2572 atomic_name
= is_unsigned
? "umin" : "smin";
2573 atomic_subop
= is_unsigned
? ac_atomic_umin
: ac_atomic_smin
;
2575 case nir_intrinsic_image_deref_atomic_max
:
2576 atomic_name
= is_unsigned
? "umax" : "smax";
2577 atomic_subop
= is_unsigned
? ac_atomic_umax
: ac_atomic_smax
;
2579 case nir_intrinsic_image_deref_atomic_and
:
2580 atomic_name
= "and";
2581 atomic_subop
= ac_atomic_and
;
2583 case nir_intrinsic_image_deref_atomic_or
:
2585 atomic_subop
= ac_atomic_or
;
2587 case nir_intrinsic_image_deref_atomic_xor
:
2588 atomic_name
= "xor";
2589 atomic_subop
= ac_atomic_xor
;
2591 case nir_intrinsic_image_deref_atomic_exchange
:
2592 atomic_name
= "swap";
2593 atomic_subop
= ac_atomic_swap
;
2595 case nir_intrinsic_image_deref_atomic_comp_swap
:
2596 atomic_name
= "cmpswap";
2597 atomic_subop
= 0; /* not used */
2604 params
[param_count
++] = get_src(ctx
, instr
->src
[4]);
2605 params
[param_count
++] = get_src(ctx
, instr
->src
[3]);
2607 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
) {
2608 params
[param_count
++] = get_image_buffer_descriptor(ctx
, instr
, true);
2609 params
[param_count
++] = LLVMBuildExtractElement(ctx
->ac
.builder
, get_src(ctx
, instr
->src
[1]),
2610 ctx
->ac
.i32_0
, ""); /* vindex */
2611 params
[param_count
++] = ctx
->ac
.i32_0
; /* voffset */
2612 if (HAVE_LLVM
>= 0x800) {
2613 params
[param_count
++] = ctx
->ac
.i32_0
; /* soffset */
2614 params
[param_count
++] = ctx
->ac
.i32_0
; /* slc */
2616 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2617 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name
);
2619 params
[param_count
++] = ctx
->ac
.i1false
; /* slc */
2621 length
= snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2622 "llvm.amdgcn.buffer.atomic.%s", atomic_name
);
2625 assert(length
< sizeof(intrinsic_name
));
2626 return ac_build_intrinsic(&ctx
->ac
, intrinsic_name
, ctx
->ac
.i32
,
2627 params
, param_count
, 0);
2629 struct ac_image_args args
= {};
2630 args
.opcode
= cmpswap
? ac_image_atomic_cmpswap
: ac_image_atomic
;
2631 args
.atomic
= atomic_subop
;
2632 args
.data
[0] = params
[0];
2634 args
.data
[1] = params
[1];
2635 get_image_coords(ctx
, instr
, &args
);
2636 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, true);
2637 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2638 glsl_sampler_type_is_array(type
));
2640 return ac_build_image_opcode(&ctx
->ac
, &args
);
2644 static LLVMValueRef
visit_image_samples(struct ac_nir_context
*ctx
,
2645 const nir_intrinsic_instr
*instr
)
2647 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2649 struct ac_image_args args
= { 0 };
2650 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2651 glsl_sampler_type_is_array(type
));
2653 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2654 args
.opcode
= ac_image_get_resinfo
;
2655 args
.lod
= ctx
->ac
.i32_0
;
2656 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2658 return ac_build_image_opcode(&ctx
->ac
, &args
);
2661 static LLVMValueRef
visit_image_size(struct ac_nir_context
*ctx
,
2662 const nir_intrinsic_instr
*instr
)
2665 const struct glsl_type
*type
= get_image_deref(instr
)->type
;
2667 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_BUF
)
2668 return get_buffer_size(ctx
, get_image_descriptor(ctx
, instr
, AC_DESC_BUFFER
, false), true);
2670 struct ac_image_args args
= { 0 };
2672 args
.dim
= get_ac_image_dim(&ctx
->ac
, glsl_get_sampler_dim(type
),
2673 glsl_sampler_type_is_array(type
));
2675 args
.resource
= get_image_descriptor(ctx
, instr
, AC_DESC_IMAGE
, false);
2676 args
.opcode
= ac_image_get_resinfo
;
2677 args
.lod
= ctx
->ac
.i32_0
;
2678 args
.attributes
= AC_FUNC_ATTR_READNONE
;
2680 res
= ac_build_image_opcode(&ctx
->ac
, &args
);
2682 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
2684 if (glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_CUBE
&&
2685 glsl_sampler_type_is_array(type
)) {
2686 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
2687 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2688 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
2689 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, z
, two
, "");
2691 if (ctx
->ac
.chip_class
>= GFX9
&&
2692 glsl_get_sampler_dim(type
) == GLSL_SAMPLER_DIM_1D
&&
2693 glsl_sampler_type_is_array(type
)) {
2694 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
, two
, "");
2695 res
= LLVMBuildInsertElement(ctx
->ac
.builder
, res
, layers
,
2702 static void emit_membar(struct ac_llvm_context
*ac
,
2703 const nir_intrinsic_instr
*instr
)
2705 unsigned waitcnt
= NOOP_WAITCNT
;
2707 switch (instr
->intrinsic
) {
2708 case nir_intrinsic_memory_barrier
:
2709 case nir_intrinsic_group_memory_barrier
:
2710 waitcnt
&= VM_CNT
& LGKM_CNT
;
2712 case nir_intrinsic_memory_barrier_atomic_counter
:
2713 case nir_intrinsic_memory_barrier_buffer
:
2714 case nir_intrinsic_memory_barrier_image
:
2717 case nir_intrinsic_memory_barrier_shared
:
2718 waitcnt
&= LGKM_CNT
;
2723 if (waitcnt
!= NOOP_WAITCNT
)
2724 ac_build_waitcnt(ac
, waitcnt
);
2727 void ac_emit_barrier(struct ac_llvm_context
*ac
, gl_shader_stage stage
)
2729 /* SI only (thanks to a hw bug workaround):
2730 * The real barrier instruction isn’t needed, because an entire patch
2731 * always fits into a single wave.
2733 if (ac
->chip_class
== SI
&& stage
== MESA_SHADER_TESS_CTRL
) {
2734 ac_build_waitcnt(ac
, LGKM_CNT
& VM_CNT
);
2737 ac_build_s_barrier(ac
);
2740 static void emit_discard(struct ac_nir_context
*ctx
,
2741 const nir_intrinsic_instr
*instr
)
2745 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2746 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
2747 get_src(ctx
, instr
->src
[0]),
2750 assert(instr
->intrinsic
== nir_intrinsic_discard
);
2751 cond
= ctx
->ac
.i1false
;
2754 ctx
->abi
->emit_kill(ctx
->abi
, cond
);
2758 visit_load_helper_invocation(struct ac_nir_context
*ctx
)
2760 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2761 "llvm.amdgcn.ps.live",
2762 ctx
->ac
.i1
, NULL
, 0,
2763 AC_FUNC_ATTR_READNONE
);
2764 result
= LLVMBuildNot(ctx
->ac
.builder
, result
, "");
2765 return LLVMBuildSExt(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2769 visit_load_local_invocation_index(struct ac_nir_context
*ctx
)
2771 LLVMValueRef result
;
2772 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
->ac
);
2773 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2774 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2776 return LLVMBuildAdd(ctx
->ac
.builder
, result
, thread_id
, "");
2780 visit_load_subgroup_id(struct ac_nir_context
*ctx
)
2782 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2783 LLVMValueRef result
;
2784 result
= LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2785 LLVMConstInt(ctx
->ac
.i32
, 0xfc0, false), "");
2786 return LLVMBuildLShr(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 6, false), "");
2788 return LLVMConstInt(ctx
->ac
.i32
, 0, false);
2793 visit_load_num_subgroups(struct ac_nir_context
*ctx
)
2795 if (ctx
->stage
== MESA_SHADER_COMPUTE
) {
2796 return LLVMBuildAnd(ctx
->ac
.builder
, ctx
->abi
->tg_size
,
2797 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
2799 return LLVMConstInt(ctx
->ac
.i32
, 1, false);
2804 visit_first_invocation(struct ac_nir_context
*ctx
)
2806 LLVMValueRef active_set
= ac_build_ballot(&ctx
->ac
, ctx
->ac
.i32_1
);
2808 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2809 LLVMValueRef args
[] = {active_set
, ctx
->ac
.i1false
};
2810 LLVMValueRef result
= ac_build_intrinsic(&ctx
->ac
,
2812 ctx
->ac
.i64
, args
, 2,
2813 AC_FUNC_ATTR_NOUNWIND
|
2814 AC_FUNC_ATTR_READNONE
);
2816 return LLVMBuildTrunc(ctx
->ac
.builder
, result
, ctx
->ac
.i32
, "");
2820 visit_load_shared(struct ac_nir_context
*ctx
,
2821 const nir_intrinsic_instr
*instr
)
2823 LLVMValueRef values
[4], derived_ptr
, index
, ret
;
2825 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
2827 for (int chan
= 0; chan
< instr
->num_components
; chan
++) {
2828 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2829 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
2830 values
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
2833 ret
= ac_build_gather_values(&ctx
->ac
, values
, instr
->num_components
);
2834 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, get_def_type(ctx
, &instr
->dest
.ssa
), "");
2838 visit_store_shared(struct ac_nir_context
*ctx
,
2839 const nir_intrinsic_instr
*instr
)
2841 LLVMValueRef derived_ptr
, data
,index
;
2842 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2844 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[1]);
2845 LLVMValueRef src
= get_src(ctx
, instr
->src
[0]);
2847 int writemask
= nir_intrinsic_write_mask(instr
);
2848 for (int chan
= 0; chan
< 4; chan
++) {
2849 if (!(writemask
& (1 << chan
))) {
2852 data
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
);
2853 index
= LLVMConstInt(ctx
->ac
.i32
, chan
, 0);
2854 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
2855 LLVMBuildStore(builder
, data
, derived_ptr
);
2859 static LLVMValueRef
visit_var_atomic(struct ac_nir_context
*ctx
,
2860 const nir_intrinsic_instr
*instr
,
2861 LLVMValueRef ptr
, int src_idx
)
2863 LLVMValueRef result
;
2864 LLVMValueRef src
= get_src(ctx
, instr
->src
[src_idx
]);
2866 if (instr
->intrinsic
== nir_intrinsic_shared_atomic_comp_swap
||
2867 instr
->intrinsic
== nir_intrinsic_deref_atomic_comp_swap
) {
2868 LLVMValueRef src1
= get_src(ctx
, instr
->src
[src_idx
+ 1]);
2869 result
= LLVMBuildAtomicCmpXchg(ctx
->ac
.builder
,
2871 LLVMAtomicOrderingSequentiallyConsistent
,
2872 LLVMAtomicOrderingSequentiallyConsistent
,
2874 result
= LLVMBuildExtractValue(ctx
->ac
.builder
, result
, 0, "");
2876 LLVMAtomicRMWBinOp op
;
2877 switch (instr
->intrinsic
) {
2878 case nir_intrinsic_shared_atomic_add
:
2879 case nir_intrinsic_deref_atomic_add
:
2880 op
= LLVMAtomicRMWBinOpAdd
;
2882 case nir_intrinsic_shared_atomic_umin
:
2883 case nir_intrinsic_deref_atomic_umin
:
2884 op
= LLVMAtomicRMWBinOpUMin
;
2886 case nir_intrinsic_shared_atomic_umax
:
2887 case nir_intrinsic_deref_atomic_umax
:
2888 op
= LLVMAtomicRMWBinOpUMax
;
2890 case nir_intrinsic_shared_atomic_imin
:
2891 case nir_intrinsic_deref_atomic_imin
:
2892 op
= LLVMAtomicRMWBinOpMin
;
2894 case nir_intrinsic_shared_atomic_imax
:
2895 case nir_intrinsic_deref_atomic_imax
:
2896 op
= LLVMAtomicRMWBinOpMax
;
2898 case nir_intrinsic_shared_atomic_and
:
2899 case nir_intrinsic_deref_atomic_and
:
2900 op
= LLVMAtomicRMWBinOpAnd
;
2902 case nir_intrinsic_shared_atomic_or
:
2903 case nir_intrinsic_deref_atomic_or
:
2904 op
= LLVMAtomicRMWBinOpOr
;
2906 case nir_intrinsic_shared_atomic_xor
:
2907 case nir_intrinsic_deref_atomic_xor
:
2908 op
= LLVMAtomicRMWBinOpXor
;
2910 case nir_intrinsic_shared_atomic_exchange
:
2911 case nir_intrinsic_deref_atomic_exchange
:
2912 op
= LLVMAtomicRMWBinOpXchg
;
2918 result
= LLVMBuildAtomicRMW(ctx
->ac
.builder
, op
, ptr
, ac_to_integer(&ctx
->ac
, src
),
2919 LLVMAtomicOrderingSequentiallyConsistent
,
2925 static LLVMValueRef
load_sample_pos(struct ac_nir_context
*ctx
)
2927 LLVMValueRef values
[2];
2928 LLVMValueRef pos
[2];
2930 pos
[0] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[0]);
2931 pos
[1] = ac_to_float(&ctx
->ac
, ctx
->abi
->frag_pos
[1]);
2933 values
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2934 values
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2935 return ac_build_gather_values(&ctx
->ac
, values
, 2);
2938 static LLVMValueRef
visit_interp(struct ac_nir_context
*ctx
,
2939 const nir_intrinsic_instr
*instr
)
2941 LLVMValueRef result
[4];
2942 LLVMValueRef interp_param
;
2945 LLVMValueRef src_c0
= NULL
;
2946 LLVMValueRef src_c1
= NULL
;
2947 LLVMValueRef src0
= NULL
;
2949 nir_deref_instr
*deref_instr
= nir_instr_as_deref(instr
->src
[0].ssa
->parent_instr
);
2950 nir_variable
*var
= nir_deref_instr_get_variable(deref_instr
);
2951 int input_base
= ctx
->abi
->fs_input_attr_indices
[var
->data
.location
- VARYING_SLOT_VAR0
];
2952 switch (instr
->intrinsic
) {
2953 case nir_intrinsic_interp_deref_at_centroid
:
2954 location
= INTERP_CENTROID
;
2956 case nir_intrinsic_interp_deref_at_sample
:
2957 case nir_intrinsic_interp_deref_at_offset
:
2958 location
= INTERP_CENTER
;
2959 src0
= get_src(ctx
, instr
->src
[1]);
2965 if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_offset
) {
2966 src_c0
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_0
, ""));
2967 src_c1
= ac_to_float(&ctx
->ac
, LLVMBuildExtractElement(ctx
->ac
.builder
, src0
, ctx
->ac
.i32_1
, ""));
2968 } else if (instr
->intrinsic
== nir_intrinsic_interp_deref_at_sample
) {
2969 LLVMValueRef sample_position
;
2970 LLVMValueRef halfval
= LLVMConstReal(ctx
->ac
.f32
, 0.5f
);
2972 /* fetch sample ID */
2973 sample_position
= ctx
->abi
->load_sample_position(ctx
->abi
, src0
);
2975 src_c0
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_0
, "");
2976 src_c0
= LLVMBuildFSub(ctx
->ac
.builder
, src_c0
, halfval
, "");
2977 src_c1
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
, ctx
->ac
.i32_1
, "");
2978 src_c1
= LLVMBuildFSub(ctx
->ac
.builder
, src_c1
, halfval
, "");
2980 interp_param
= ctx
->abi
->lookup_interp_param(ctx
->abi
, var
->data
.interpolation
, location
);
2982 if (location
== INTERP_CENTER
) {
2983 LLVMValueRef ij_out
[2];
2984 LLVMValueRef ddxy_out
= emit_ddxy_interp(ctx
, interp_param
);
2987 * take the I then J parameters, and the DDX/Y for it, and
2988 * calculate the IJ inputs for the interpolator.
2989 * temp1 = ddx * offset/sample.x + I;
2990 * interp_param.I = ddy * offset/sample.y + temp1;
2991 * temp1 = ddx * offset/sample.x + J;
2992 * interp_param.J = ddy * offset/sample.y + temp1;
2994 for (unsigned i
= 0; i
< 2; i
++) {
2995 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->ac
.i32
, i
, false);
2996 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->ac
.i32
, i
+ 2, false);
2997 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
2998 ddxy_out
, ix_ll
, "");
2999 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3000 ddxy_out
, iy_ll
, "");
3001 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3002 interp_param
, ix_ll
, "");
3003 LLVMValueRef temp1
, temp2
;
3005 interp_el
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_el
,
3008 temp1
= ac_build_fmad(&ctx
->ac
, ddx_el
, src_c0
, interp_el
);
3009 temp2
= ac_build_fmad(&ctx
->ac
, ddy_el
, src_c1
, temp1
);
3011 ij_out
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
,
3012 temp2
, ctx
->ac
.i32
, "");
3014 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
3018 LLVMValueRef attrib_idx
= ctx
->ac
.i32_0
;
3019 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3020 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3021 unsigned array_size
= glsl_count_attribute_slots(deref_instr
->type
, false);
3023 LLVMValueRef offset
;
3024 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3026 offset
= LLVMConstInt(ctx
->ac
.i32
, array_size
* const_value
->u32
[0], false);
3028 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3030 offset
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3031 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3034 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3035 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3036 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3037 LLVMValueRef offset
;
3038 unsigned sidx
= deref_instr
->strct
.index
;
3039 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3040 offset
= LLVMConstInt(ctx
->ac
.i32
, glsl_get_record_location_offset(deref_instr
->type
, sidx
), false);
3041 attrib_idx
= LLVMBuildAdd(ctx
->ac
.builder
, attrib_idx
, offset
, "");
3043 unreachable("Unsupported deref type");
3048 unsigned attrib_size
= glsl_count_attribute_slots(var
->type
, false);
3049 for (chan
= 0; chan
< 4; chan
++) {
3050 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->ac
.f32
, attrib_size
));
3051 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
3053 for (unsigned idx
= 0; idx
< attrib_size
; ++idx
) {
3054 LLVMValueRef v
, attr_number
;
3056 attr_number
= LLVMConstInt(ctx
->ac
.i32
, input_base
+ idx
, false);
3058 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
,
3059 interp_param
, ctx
->ac
.v2f32
, "");
3060 LLVMValueRef i
= LLVMBuildExtractElement(
3061 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_0
, "");
3062 LLVMValueRef j
= LLVMBuildExtractElement(
3063 ctx
->ac
.builder
, interp_param
, ctx
->ac
.i32_1
, "");
3065 v
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
, attr_number
,
3066 ctx
->abi
->prim_mask
, i
, j
);
3068 v
= ac_build_fs_interp_mov(&ctx
->ac
, LLVMConstInt(ctx
->ac
.i32
, 2, false),
3069 llvm_chan
, attr_number
, ctx
->abi
->prim_mask
);
3072 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
, gather
, v
,
3073 LLVMConstInt(ctx
->ac
.i32
, idx
, false), "");
3076 result
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, gather
, attrib_idx
, "");
3079 return ac_build_varying_gather_values(&ctx
->ac
, result
, instr
->num_components
,
3080 var
->data
.location_frac
);
3083 static void visit_intrinsic(struct ac_nir_context
*ctx
,
3084 nir_intrinsic_instr
*instr
)
3086 LLVMValueRef result
= NULL
;
3088 switch (instr
->intrinsic
) {
3089 case nir_intrinsic_ballot
:
3090 result
= ac_build_ballot(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3092 case nir_intrinsic_read_invocation
:
3093 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3094 get_src(ctx
, instr
->src
[1]));
3096 case nir_intrinsic_read_first_invocation
:
3097 result
= ac_build_readlane(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), NULL
);
3099 case nir_intrinsic_load_subgroup_invocation
:
3100 result
= ac_get_thread_id(&ctx
->ac
);
3102 case nir_intrinsic_load_work_group_id
: {
3103 LLVMValueRef values
[3];
3105 for (int i
= 0; i
< 3; i
++) {
3106 values
[i
] = ctx
->abi
->workgroup_ids
[i
] ?
3107 ctx
->abi
->workgroup_ids
[i
] : ctx
->ac
.i32_0
;
3110 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
3113 case nir_intrinsic_load_base_vertex
:
3114 case nir_intrinsic_load_first_vertex
:
3115 result
= ctx
->abi
->load_base_vertex(ctx
->abi
);
3117 case nir_intrinsic_load_local_group_size
:
3118 result
= ctx
->abi
->load_local_group_size(ctx
->abi
);
3120 case nir_intrinsic_load_vertex_id
:
3121 result
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
->vertex_id
,
3122 ctx
->abi
->base_vertex
, "");
3124 case nir_intrinsic_load_vertex_id_zero_base
: {
3125 result
= ctx
->abi
->vertex_id
;
3128 case nir_intrinsic_load_local_invocation_id
: {
3129 result
= ctx
->abi
->local_invocation_ids
;
3132 case nir_intrinsic_load_base_instance
:
3133 result
= ctx
->abi
->start_instance
;
3135 case nir_intrinsic_load_draw_id
:
3136 result
= ctx
->abi
->draw_id
;
3138 case nir_intrinsic_load_view_index
:
3139 result
= ctx
->abi
->view_index
;
3141 case nir_intrinsic_load_invocation_id
:
3142 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
3143 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->tcs_rel_ids
, 8, 5);
3145 result
= ctx
->abi
->gs_invocation_id
;
3147 case nir_intrinsic_load_primitive_id
:
3148 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3149 result
= ctx
->abi
->gs_prim_id
;
3150 } else if (ctx
->stage
== MESA_SHADER_TESS_CTRL
) {
3151 result
= ctx
->abi
->tcs_patch_id
;
3152 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3153 result
= ctx
->abi
->tes_patch_id
;
3155 fprintf(stderr
, "Unknown primitive id intrinsic: %d", ctx
->stage
);
3157 case nir_intrinsic_load_sample_id
:
3158 result
= ac_unpack_param(&ctx
->ac
, ctx
->abi
->ancillary
, 8, 4);
3160 case nir_intrinsic_load_sample_pos
:
3161 result
= load_sample_pos(ctx
);
3163 case nir_intrinsic_load_sample_mask_in
:
3164 result
= ctx
->abi
->load_sample_mask_in(ctx
->abi
);
3166 case nir_intrinsic_load_frag_coord
: {
3167 LLVMValueRef values
[4] = {
3168 ctx
->abi
->frag_pos
[0],
3169 ctx
->abi
->frag_pos
[1],
3170 ctx
->abi
->frag_pos
[2],
3171 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, ctx
->abi
->frag_pos
[3])
3173 result
= ac_build_gather_values(&ctx
->ac
, values
, 4);
3176 case nir_intrinsic_load_front_face
:
3177 result
= ctx
->abi
->front_face
;
3179 case nir_intrinsic_load_helper_invocation
:
3180 result
= visit_load_helper_invocation(ctx
);
3182 case nir_intrinsic_load_instance_id
:
3183 result
= ctx
->abi
->instance_id
;
3185 case nir_intrinsic_load_num_work_groups
:
3186 result
= ctx
->abi
->num_work_groups
;
3188 case nir_intrinsic_load_local_invocation_index
:
3189 result
= visit_load_local_invocation_index(ctx
);
3191 case nir_intrinsic_load_subgroup_id
:
3192 result
= visit_load_subgroup_id(ctx
);
3194 case nir_intrinsic_load_num_subgroups
:
3195 result
= visit_load_num_subgroups(ctx
);
3197 case nir_intrinsic_first_invocation
:
3198 result
= visit_first_invocation(ctx
);
3200 case nir_intrinsic_load_push_constant
:
3201 result
= visit_load_push_constant(ctx
, instr
);
3203 case nir_intrinsic_vulkan_resource_index
: {
3204 LLVMValueRef index
= get_src(ctx
, instr
->src
[0]);
3205 unsigned desc_set
= nir_intrinsic_desc_set(instr
);
3206 unsigned binding
= nir_intrinsic_binding(instr
);
3208 result
= ctx
->abi
->load_resource(ctx
->abi
, index
, desc_set
,
3212 case nir_intrinsic_vulkan_resource_reindex
:
3213 result
= visit_vulkan_resource_reindex(ctx
, instr
);
3215 case nir_intrinsic_store_ssbo
:
3216 visit_store_ssbo(ctx
, instr
);
3218 case nir_intrinsic_load_ssbo
:
3219 result
= visit_load_buffer(ctx
, instr
);
3221 case nir_intrinsic_ssbo_atomic_add
:
3222 case nir_intrinsic_ssbo_atomic_imin
:
3223 case nir_intrinsic_ssbo_atomic_umin
:
3224 case nir_intrinsic_ssbo_atomic_imax
:
3225 case nir_intrinsic_ssbo_atomic_umax
:
3226 case nir_intrinsic_ssbo_atomic_and
:
3227 case nir_intrinsic_ssbo_atomic_or
:
3228 case nir_intrinsic_ssbo_atomic_xor
:
3229 case nir_intrinsic_ssbo_atomic_exchange
:
3230 case nir_intrinsic_ssbo_atomic_comp_swap
:
3231 result
= visit_atomic_ssbo(ctx
, instr
);
3233 case nir_intrinsic_load_ubo
:
3234 result
= visit_load_ubo_buffer(ctx
, instr
);
3236 case nir_intrinsic_get_buffer_size
:
3237 result
= visit_get_buffer_size(ctx
, instr
);
3239 case nir_intrinsic_load_deref
:
3240 result
= visit_load_var(ctx
, instr
);
3242 case nir_intrinsic_store_deref
:
3243 visit_store_var(ctx
, instr
);
3245 case nir_intrinsic_load_shared
:
3246 result
= visit_load_shared(ctx
, instr
);
3248 case nir_intrinsic_store_shared
:
3249 visit_store_shared(ctx
, instr
);
3251 case nir_intrinsic_image_deref_samples
:
3252 result
= visit_image_samples(ctx
, instr
);
3254 case nir_intrinsic_image_deref_load
:
3255 result
= visit_image_load(ctx
, instr
);
3257 case nir_intrinsic_image_deref_store
:
3258 visit_image_store(ctx
, instr
);
3260 case nir_intrinsic_image_deref_atomic_add
:
3261 case nir_intrinsic_image_deref_atomic_min
:
3262 case nir_intrinsic_image_deref_atomic_max
:
3263 case nir_intrinsic_image_deref_atomic_and
:
3264 case nir_intrinsic_image_deref_atomic_or
:
3265 case nir_intrinsic_image_deref_atomic_xor
:
3266 case nir_intrinsic_image_deref_atomic_exchange
:
3267 case nir_intrinsic_image_deref_atomic_comp_swap
:
3268 result
= visit_image_atomic(ctx
, instr
);
3270 case nir_intrinsic_image_deref_size
:
3271 result
= visit_image_size(ctx
, instr
);
3273 case nir_intrinsic_shader_clock
:
3274 result
= ac_build_shader_clock(&ctx
->ac
);
3276 case nir_intrinsic_discard
:
3277 case nir_intrinsic_discard_if
:
3278 emit_discard(ctx
, instr
);
3280 case nir_intrinsic_memory_barrier
:
3281 case nir_intrinsic_group_memory_barrier
:
3282 case nir_intrinsic_memory_barrier_atomic_counter
:
3283 case nir_intrinsic_memory_barrier_buffer
:
3284 case nir_intrinsic_memory_barrier_image
:
3285 case nir_intrinsic_memory_barrier_shared
:
3286 emit_membar(&ctx
->ac
, instr
);
3288 case nir_intrinsic_barrier
:
3289 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3291 case nir_intrinsic_shared_atomic_add
:
3292 case nir_intrinsic_shared_atomic_imin
:
3293 case nir_intrinsic_shared_atomic_umin
:
3294 case nir_intrinsic_shared_atomic_imax
:
3295 case nir_intrinsic_shared_atomic_umax
:
3296 case nir_intrinsic_shared_atomic_and
:
3297 case nir_intrinsic_shared_atomic_or
:
3298 case nir_intrinsic_shared_atomic_xor
:
3299 case nir_intrinsic_shared_atomic_exchange
:
3300 case nir_intrinsic_shared_atomic_comp_swap
: {
3301 LLVMValueRef ptr
= get_memory_ptr(ctx
, instr
->src
[0]);
3302 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3305 case nir_intrinsic_deref_atomic_add
:
3306 case nir_intrinsic_deref_atomic_imin
:
3307 case nir_intrinsic_deref_atomic_umin
:
3308 case nir_intrinsic_deref_atomic_imax
:
3309 case nir_intrinsic_deref_atomic_umax
:
3310 case nir_intrinsic_deref_atomic_and
:
3311 case nir_intrinsic_deref_atomic_or
:
3312 case nir_intrinsic_deref_atomic_xor
:
3313 case nir_intrinsic_deref_atomic_exchange
:
3314 case nir_intrinsic_deref_atomic_comp_swap
: {
3315 LLVMValueRef ptr
= get_src(ctx
, instr
->src
[0]);
3316 result
= visit_var_atomic(ctx
, instr
, ptr
, 1);
3319 case nir_intrinsic_interp_deref_at_centroid
:
3320 case nir_intrinsic_interp_deref_at_sample
:
3321 case nir_intrinsic_interp_deref_at_offset
:
3322 result
= visit_interp(ctx
, instr
);
3324 case nir_intrinsic_emit_vertex
:
3325 ctx
->abi
->emit_vertex(ctx
->abi
, nir_intrinsic_stream_id(instr
), ctx
->abi
->outputs
);
3327 case nir_intrinsic_end_primitive
:
3328 ctx
->abi
->emit_primitive(ctx
->abi
, nir_intrinsic_stream_id(instr
));
3330 case nir_intrinsic_load_tess_coord
:
3331 result
= ctx
->abi
->load_tess_coord(ctx
->abi
);
3333 case nir_intrinsic_load_tess_level_outer
:
3334 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_OUTER
);
3336 case nir_intrinsic_load_tess_level_inner
:
3337 result
= ctx
->abi
->load_tess_level(ctx
->abi
, VARYING_SLOT_TESS_LEVEL_INNER
);
3339 case nir_intrinsic_load_patch_vertices_in
:
3340 result
= ctx
->abi
->load_patch_vertices_in(ctx
->abi
);
3342 case nir_intrinsic_vote_all
: {
3343 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3344 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3347 case nir_intrinsic_vote_any
: {
3348 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, get_src(ctx
, instr
->src
[0]));
3349 result
= LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->ac
.i32
, "");
3352 case nir_intrinsic_shuffle
:
3353 result
= ac_build_shuffle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3354 get_src(ctx
, instr
->src
[1]));
3356 case nir_intrinsic_reduce
:
3357 result
= ac_build_reduce(&ctx
->ac
,
3358 get_src(ctx
, instr
->src
[0]),
3359 instr
->const_index
[0],
3360 instr
->const_index
[1]);
3362 case nir_intrinsic_inclusive_scan
:
3363 result
= ac_build_inclusive_scan(&ctx
->ac
,
3364 get_src(ctx
, instr
->src
[0]),
3365 instr
->const_index
[0]);
3367 case nir_intrinsic_exclusive_scan
:
3368 result
= ac_build_exclusive_scan(&ctx
->ac
,
3369 get_src(ctx
, instr
->src
[0]),
3370 instr
->const_index
[0]);
3372 case nir_intrinsic_quad_broadcast
: {
3373 unsigned lane
= nir_src_as_const_value(instr
->src
[1])->u32
[0];
3374 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]),
3375 lane
, lane
, lane
, lane
);
3378 case nir_intrinsic_quad_swap_horizontal
:
3379 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 1, 0, 3 ,2);
3381 case nir_intrinsic_quad_swap_vertical
:
3382 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 2, 3, 0 ,1);
3384 case nir_intrinsic_quad_swap_diagonal
:
3385 result
= ac_build_quad_swizzle(&ctx
->ac
, get_src(ctx
, instr
->src
[0]), 3, 2, 1 ,0);
3388 fprintf(stderr
, "Unknown intrinsic: ");
3389 nir_print_instr(&instr
->instr
, stderr
);
3390 fprintf(stderr
, "\n");
3394 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3398 static LLVMValueRef
get_bindless_index_from_uniform(struct ac_nir_context
*ctx
,
3399 unsigned base_index
,
3400 unsigned constant_index
,
3401 LLVMValueRef dynamic_index
)
3403 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, base_index
* 4, 0);
3404 LLVMValueRef index
= LLVMBuildAdd(ctx
->ac
.builder
, dynamic_index
,
3405 LLVMConstInt(ctx
->ac
.i32
, constant_index
, 0), "");
3407 /* Bindless uniforms are 64bit so multiple index by 8 */
3408 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i32
, 8, 0), "");
3409 offset
= LLVMBuildAdd(ctx
->ac
.builder
, offset
, index
, "");
3411 LLVMValueRef ubo_index
= ctx
->abi
->load_ubo(ctx
->abi
, ctx
->ac
.i32_0
);
3413 LLVMValueRef ret
= ac_build_buffer_load(&ctx
->ac
, ubo_index
, 1, NULL
, offset
,
3414 NULL
, 0, false, false, true, true);
3416 return LLVMBuildBitCast(ctx
->ac
.builder
, ret
, ctx
->ac
.i32
, "");
3419 static LLVMValueRef
get_sampler_desc(struct ac_nir_context
*ctx
,
3420 nir_deref_instr
*deref_instr
,
3421 enum ac_descriptor_type desc_type
,
3422 const nir_tex_instr
*tex_instr
,
3423 bool image
, bool write
)
3425 LLVMValueRef index
= NULL
;
3426 unsigned constant_index
= 0;
3427 unsigned descriptor_set
;
3428 unsigned base_index
;
3429 bool bindless
= false;
3432 assert(tex_instr
&& !image
);
3434 base_index
= tex_instr
->sampler_index
;
3436 while(deref_instr
->deref_type
!= nir_deref_type_var
) {
3437 if (deref_instr
->deref_type
== nir_deref_type_array
) {
3438 unsigned array_size
= glsl_get_aoa_size(deref_instr
->type
);
3442 nir_const_value
*const_value
= nir_src_as_const_value(deref_instr
->arr
.index
);
3444 constant_index
+= array_size
* const_value
->u32
[0];
3446 LLVMValueRef indirect
= get_src(ctx
, deref_instr
->arr
.index
);
3448 indirect
= LLVMBuildMul(ctx
->ac
.builder
, indirect
,
3449 LLVMConstInt(ctx
->ac
.i32
, array_size
, false), "");
3454 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, indirect
, "");
3457 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3458 } else if (deref_instr
->deref_type
== nir_deref_type_struct
) {
3459 unsigned sidx
= deref_instr
->strct
.index
;
3460 deref_instr
= nir_src_as_deref(deref_instr
->parent
);
3461 constant_index
+= glsl_get_record_location_offset(deref_instr
->type
, sidx
);
3463 unreachable("Unsupported deref type");
3466 descriptor_set
= deref_instr
->var
->data
.descriptor_set
;
3468 if (deref_instr
->var
->data
.bindless
) {
3469 /* For now just assert on unhandled variable types */
3470 assert(deref_instr
->var
->data
.mode
== nir_var_uniform
);
3472 base_index
= deref_instr
->var
->data
.driver_location
;
3475 index
= index
? index
: ctx
->ac
.i32_0
;
3476 index
= get_bindless_index_from_uniform(ctx
, base_index
,
3477 constant_index
, index
);
3479 base_index
= deref_instr
->var
->data
.binding
;
3482 return ctx
->abi
->load_sampler_desc(ctx
->abi
,
3485 constant_index
, index
,
3486 desc_type
, image
, write
, bindless
);
3489 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3492 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3493 * filtering manually. The driver sets img7 to a mask clearing
3494 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3495 * s_and_b32 samp0, samp0, img7
3498 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3500 static LLVMValueRef
sici_fix_sampler_aniso(struct ac_nir_context
*ctx
,
3501 LLVMValueRef res
, LLVMValueRef samp
)
3503 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3504 LLVMValueRef img7
, samp0
;
3506 if (ctx
->ac
.chip_class
>= VI
)
3509 img7
= LLVMBuildExtractElement(builder
, res
,
3510 LLVMConstInt(ctx
->ac
.i32
, 7, 0), "");
3511 samp0
= LLVMBuildExtractElement(builder
, samp
,
3512 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3513 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
3514 return LLVMBuildInsertElement(builder
, samp
, samp0
,
3515 LLVMConstInt(ctx
->ac
.i32
, 0, 0), "");
3518 static void tex_fetch_ptrs(struct ac_nir_context
*ctx
,
3519 nir_tex_instr
*instr
,
3520 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
3521 LLVMValueRef
*fmask_ptr
)
3523 nir_deref_instr
*texture_deref_instr
= NULL
;
3524 nir_deref_instr
*sampler_deref_instr
= NULL
;
3526 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3527 switch (instr
->src
[i
].src_type
) {
3528 case nir_tex_src_texture_deref
:
3529 texture_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3531 case nir_tex_src_sampler_deref
:
3532 sampler_deref_instr
= nir_src_as_deref(instr
->src
[i
].src
);
3539 if (!sampler_deref_instr
)
3540 sampler_deref_instr
= texture_deref_instr
;
3542 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3543 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_BUFFER
, instr
, false, false);
3545 *res_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_IMAGE
, instr
, false, false);
3547 *samp_ptr
= get_sampler_desc(ctx
, sampler_deref_instr
, AC_DESC_SAMPLER
, instr
, false, false);
3548 if (instr
->sampler_dim
< GLSL_SAMPLER_DIM_RECT
)
3549 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
3551 if (fmask_ptr
&& (instr
->op
== nir_texop_txf_ms
||
3552 instr
->op
== nir_texop_samples_identical
))
3553 *fmask_ptr
= get_sampler_desc(ctx
, texture_deref_instr
, AC_DESC_FMASK
, instr
, false, false);
3556 static LLVMValueRef
apply_round_slice(struct ac_llvm_context
*ctx
,
3559 coord
= ac_to_float(ctx
, coord
);
3560 coord
= ac_build_round(ctx
, coord
);
3561 coord
= ac_to_integer(ctx
, coord
);
3565 static void visit_tex(struct ac_nir_context
*ctx
, nir_tex_instr
*instr
)
3567 LLVMValueRef result
= NULL
;
3568 struct ac_image_args args
= { 0 };
3569 LLVMValueRef fmask_ptr
= NULL
, sample_index
= NULL
;
3570 LLVMValueRef ddx
= NULL
, ddy
= NULL
;
3571 unsigned offset_src
= 0;
3573 tex_fetch_ptrs(ctx
, instr
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
3575 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3576 switch (instr
->src
[i
].src_type
) {
3577 case nir_tex_src_coord
: {
3578 LLVMValueRef coord
= get_src(ctx
, instr
->src
[i
].src
);
3579 for (unsigned chan
= 0; chan
< instr
->coord_components
; ++chan
)
3580 args
.coords
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, coord
, chan
);
3583 case nir_tex_src_projector
:
3585 case nir_tex_src_comparator
:
3586 if (instr
->is_shadow
)
3587 args
.compare
= get_src(ctx
, instr
->src
[i
].src
);
3589 case nir_tex_src_offset
:
3590 args
.offset
= get_src(ctx
, instr
->src
[i
].src
);
3593 case nir_tex_src_bias
:
3594 if (instr
->op
== nir_texop_txb
)
3595 args
.bias
= get_src(ctx
, instr
->src
[i
].src
);
3597 case nir_tex_src_lod
: {
3598 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[i
].src
);
3600 if (val
&& val
->i32
[0] == 0)
3601 args
.level_zero
= true;
3603 args
.lod
= get_src(ctx
, instr
->src
[i
].src
);
3606 case nir_tex_src_ms_index
:
3607 sample_index
= get_src(ctx
, instr
->src
[i
].src
);
3609 case nir_tex_src_ms_mcs
:
3611 case nir_tex_src_ddx
:
3612 ddx
= get_src(ctx
, instr
->src
[i
].src
);
3614 case nir_tex_src_ddy
:
3615 ddy
= get_src(ctx
, instr
->src
[i
].src
);
3617 case nir_tex_src_texture_offset
:
3618 case nir_tex_src_sampler_offset
:
3619 case nir_tex_src_plane
:
3625 if (instr
->op
== nir_texop_txs
&& instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
) {
3626 result
= get_buffer_size(ctx
, args
.resource
, true);
3630 if (instr
->op
== nir_texop_texture_samples
) {
3631 LLVMValueRef res
, samples
, is_msaa
;
3632 res
= LLVMBuildBitCast(ctx
->ac
.builder
, args
.resource
, ctx
->ac
.v8i32
, "");
3633 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
3634 LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3635 is_msaa
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3636 LLVMConstInt(ctx
->ac
.i32
, 28, false), "");
3637 is_msaa
= LLVMBuildAnd(ctx
->ac
.builder
, is_msaa
,
3638 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3639 is_msaa
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, is_msaa
,
3640 LLVMConstInt(ctx
->ac
.i32
, 0xe, false), "");
3642 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
3643 LLVMConstInt(ctx
->ac
.i32
, 16, false), "");
3644 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
3645 LLVMConstInt(ctx
->ac
.i32
, 0xf, false), "");
3646 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->ac
.i32_1
,
3648 samples
= LLVMBuildSelect(ctx
->ac
.builder
, is_msaa
, samples
,
3654 if (args
.offset
&& instr
->op
!= nir_texop_txf
) {
3655 LLVMValueRef offset
[3], pack
;
3656 for (unsigned chan
= 0; chan
< 3; ++chan
)
3657 offset
[chan
] = ctx
->ac
.i32_0
;
3659 unsigned num_components
= ac_get_llvm_num_components(args
.offset
);
3660 for (unsigned chan
= 0; chan
< num_components
; chan
++) {
3661 offset
[chan
] = ac_llvm_extract_elem(&ctx
->ac
, args
.offset
, chan
);
3662 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
3663 LLVMConstInt(ctx
->ac
.i32
, 0x3f, false), "");
3665 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
3666 LLVMConstInt(ctx
->ac
.i32
, chan
* 8, false), "");
3668 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
3669 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
3673 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3674 * so the depth comparison value isn't clamped for Z16 and
3675 * Z24 anymore. Do it manually here.
3677 * It's unnecessary if the original texture format was
3678 * Z32_FLOAT, but we don't know that here.
3680 if (args
.compare
&& ctx
->ac
.chip_class
>= VI
&& ctx
->abi
->clamp_shadow_reference
)
3681 args
.compare
= ac_build_clamp(&ctx
->ac
, ac_to_float(&ctx
->ac
, args
.compare
));
3683 /* pack derivatives */
3685 int num_src_deriv_channels
, num_dest_deriv_channels
;
3686 switch (instr
->sampler_dim
) {
3687 case GLSL_SAMPLER_DIM_3D
:
3688 case GLSL_SAMPLER_DIM_CUBE
:
3689 num_src_deriv_channels
= 3;
3690 num_dest_deriv_channels
= 3;
3692 case GLSL_SAMPLER_DIM_2D
:
3694 num_src_deriv_channels
= 2;
3695 num_dest_deriv_channels
= 2;
3697 case GLSL_SAMPLER_DIM_1D
:
3698 num_src_deriv_channels
= 1;
3699 if (ctx
->ac
.chip_class
>= GFX9
) {
3700 num_dest_deriv_channels
= 2;
3702 num_dest_deriv_channels
= 1;
3707 for (unsigned i
= 0; i
< num_src_deriv_channels
; i
++) {
3708 args
.derivs
[i
] = ac_to_float(&ctx
->ac
,
3709 ac_llvm_extract_elem(&ctx
->ac
, ddx
, i
));
3710 args
.derivs
[num_dest_deriv_channels
+ i
] = ac_to_float(&ctx
->ac
,
3711 ac_llvm_extract_elem(&ctx
->ac
, ddy
, i
));
3713 for (unsigned i
= num_src_deriv_channels
; i
< num_dest_deriv_channels
; i
++) {
3714 args
.derivs
[i
] = ctx
->ac
.f32_0
;
3715 args
.derivs
[num_dest_deriv_channels
+ i
] = ctx
->ac
.f32_0
;
3719 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& args
.coords
[0]) {
3720 for (unsigned chan
= 0; chan
< instr
->coord_components
; chan
++)
3721 args
.coords
[chan
] = ac_to_float(&ctx
->ac
, args
.coords
[chan
]);
3722 if (instr
->coord_components
== 3)
3723 args
.coords
[3] = LLVMGetUndef(ctx
->ac
.f32
);
3724 ac_prepare_cube_coords(&ctx
->ac
,
3725 instr
->op
== nir_texop_txd
, instr
->is_array
,
3726 instr
->op
== nir_texop_lod
, args
.coords
, args
.derivs
);
3729 /* Texture coordinates fixups */
3730 if (instr
->coord_components
> 1 &&
3731 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3733 instr
->op
!= nir_texop_txf
) {
3734 args
.coords
[1] = apply_round_slice(&ctx
->ac
, args
.coords
[1]);
3737 if (instr
->coord_components
> 2 &&
3738 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
3739 instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
||
3740 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS
||
3741 instr
->sampler_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) &&
3743 instr
->op
!= nir_texop_txf
&& instr
->op
!= nir_texop_txf_ms
) {
3744 args
.coords
[2] = apply_round_slice(&ctx
->ac
, args
.coords
[2]);
3747 if (ctx
->ac
.chip_class
>= GFX9
&&
3748 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3749 instr
->op
!= nir_texop_lod
) {
3750 LLVMValueRef filler
;
3751 if (instr
->op
== nir_texop_txf
)
3752 filler
= ctx
->ac
.i32_0
;
3754 filler
= LLVMConstReal(ctx
->ac
.f32
, 0.5);
3756 if (instr
->is_array
)
3757 args
.coords
[2] = args
.coords
[1];
3758 args
.coords
[1] = filler
;
3761 /* Pack sample index */
3762 if (instr
->op
== nir_texop_txf_ms
&& sample_index
)
3763 args
.coords
[instr
->coord_components
] = sample_index
;
3765 if (instr
->op
== nir_texop_samples_identical
) {
3766 struct ac_image_args txf_args
= { 0 };
3767 memcpy(txf_args
.coords
, args
.coords
, sizeof(txf_args
.coords
));
3769 txf_args
.dmask
= 0xf;
3770 txf_args
.resource
= fmask_ptr
;
3771 txf_args
.dim
= instr
->is_array
? ac_image_2darray
: ac_image_2d
;
3772 result
= build_tex_intrinsic(ctx
, instr
, &txf_args
);
3774 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3775 result
= emit_int_cmp(&ctx
->ac
, LLVMIntEQ
, result
, ctx
->ac
.i32_0
);
3779 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_MS
&&
3780 instr
->op
!= nir_texop_txs
) {
3781 unsigned sample_chan
= instr
->is_array
? 3 : 2;
3782 args
.coords
[sample_chan
] = adjust_sample_index_using_fmask(
3783 &ctx
->ac
, args
.coords
[0], args
.coords
[1],
3784 instr
->is_array
? args
.coords
[2] : NULL
,
3785 args
.coords
[sample_chan
], fmask_ptr
);
3788 if (args
.offset
&& instr
->op
== nir_texop_txf
) {
3789 nir_const_value
*const_offset
=
3790 nir_src_as_const_value(instr
->src
[offset_src
].src
);
3791 int num_offsets
= instr
->src
[offset_src
].src
.ssa
->num_components
;
3792 assert(const_offset
);
3793 num_offsets
= MIN2(num_offsets
, instr
->coord_components
);
3794 for (unsigned i
= 0; i
< num_offsets
; ++i
) {
3795 args
.coords
[i
] = LLVMBuildAdd(
3796 ctx
->ac
.builder
, args
.coords
[i
],
3797 LLVMConstInt(ctx
->ac
.i32
, const_offset
->i32
[i
], false), "");
3802 /* TODO TG4 support */
3804 if (instr
->op
== nir_texop_tg4
) {
3805 if (instr
->is_shadow
)
3808 args
.dmask
= 1 << instr
->component
;
3811 if (instr
->sampler_dim
!= GLSL_SAMPLER_DIM_BUF
)
3812 args
.dim
= get_ac_sampler_dim(&ctx
->ac
, instr
->sampler_dim
, instr
->is_array
);
3813 result
= build_tex_intrinsic(ctx
, instr
, &args
);
3815 if (instr
->op
== nir_texop_query_levels
)
3816 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, LLVMConstInt(ctx
->ac
.i32
, 3, false), "");
3817 else if (instr
->is_shadow
&& instr
->is_new_style_shadow
&&
3818 instr
->op
!= nir_texop_txs
&& instr
->op
!= nir_texop_lod
&&
3819 instr
->op
!= nir_texop_tg4
)
3820 result
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, ctx
->ac
.i32_0
, "");
3821 else if (instr
->op
== nir_texop_txs
&&
3822 instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3824 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3825 LLVMValueRef six
= LLVMConstInt(ctx
->ac
.i32
, 6, false);
3826 LLVMValueRef z
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3827 z
= LLVMBuildSDiv(ctx
->ac
.builder
, z
, six
, "");
3828 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, z
, two
, "");
3829 } else if (ctx
->ac
.chip_class
>= GFX9
&&
3830 instr
->op
== nir_texop_txs
&&
3831 instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
&&
3833 LLVMValueRef two
= LLVMConstInt(ctx
->ac
.i32
, 2, false);
3834 LLVMValueRef layers
= LLVMBuildExtractElement(ctx
->ac
.builder
, result
, two
, "");
3835 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, layers
,
3837 } else if (instr
->dest
.ssa
.num_components
!= 4)
3838 result
= ac_trim_vector(&ctx
->ac
, result
, instr
->dest
.ssa
.num_components
);
3842 assert(instr
->dest
.is_ssa
);
3843 result
= ac_to_integer(&ctx
->ac
, result
);
3844 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3849 static void visit_phi(struct ac_nir_context
*ctx
, nir_phi_instr
*instr
)
3851 LLVMTypeRef type
= get_def_type(ctx
, &instr
->dest
.ssa
);
3852 LLVMValueRef result
= LLVMBuildPhi(ctx
->ac
.builder
, type
, "");
3854 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
3855 _mesa_hash_table_insert(ctx
->phis
, instr
, result
);
3858 static void visit_post_phi(struct ac_nir_context
*ctx
,
3859 nir_phi_instr
*instr
,
3860 LLVMValueRef llvm_phi
)
3862 nir_foreach_phi_src(src
, instr
) {
3863 LLVMBasicBlockRef block
= get_block(ctx
, src
->pred
);
3864 LLVMValueRef llvm_src
= get_src(ctx
, src
->src
);
3866 LLVMAddIncoming(llvm_phi
, &llvm_src
, &block
, 1);
3870 static void phi_post_pass(struct ac_nir_context
*ctx
)
3872 hash_table_foreach(ctx
->phis
, entry
) {
3873 visit_post_phi(ctx
, (nir_phi_instr
*)entry
->key
,
3874 (LLVMValueRef
)entry
->data
);
3879 static void visit_ssa_undef(struct ac_nir_context
*ctx
,
3880 const nir_ssa_undef_instr
*instr
)
3882 unsigned num_components
= instr
->def
.num_components
;
3883 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->ac
.context
, instr
->def
.bit_size
);
3886 if (num_components
== 1)
3887 undef
= LLVMGetUndef(type
);
3889 undef
= LLVMGetUndef(LLVMVectorType(type
, num_components
));
3891 ctx
->ssa_defs
[instr
->def
.index
] = undef
;
3894 static void visit_jump(struct ac_llvm_context
*ctx
,
3895 const nir_jump_instr
*instr
)
3897 switch (instr
->type
) {
3898 case nir_jump_break
:
3899 ac_build_break(ctx
);
3901 case nir_jump_continue
:
3902 ac_build_continue(ctx
);
3905 fprintf(stderr
, "Unknown NIR jump instr: ");
3906 nir_print_instr(&instr
->instr
, stderr
);
3907 fprintf(stderr
, "\n");
3913 glsl_base_to_llvm_type(struct ac_llvm_context
*ac
,
3914 enum glsl_base_type type
)
3918 case GLSL_TYPE_UINT
:
3919 case GLSL_TYPE_BOOL
:
3920 case GLSL_TYPE_SUBROUTINE
:
3922 case GLSL_TYPE_INT16
:
3923 case GLSL_TYPE_UINT16
:
3925 case GLSL_TYPE_FLOAT
:
3927 case GLSL_TYPE_FLOAT16
:
3929 case GLSL_TYPE_INT64
:
3930 case GLSL_TYPE_UINT64
:
3932 case GLSL_TYPE_DOUBLE
:
3935 unreachable("unknown GLSL type");
3940 glsl_to_llvm_type(struct ac_llvm_context
*ac
,
3941 const struct glsl_type
*type
)
3943 if (glsl_type_is_scalar(type
)) {
3944 return glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
));
3947 if (glsl_type_is_vector(type
)) {
3948 return LLVMVectorType(
3949 glsl_base_to_llvm_type(ac
, glsl_get_base_type(type
)),
3950 glsl_get_vector_elements(type
));
3953 if (glsl_type_is_matrix(type
)) {
3954 return LLVMArrayType(
3955 glsl_to_llvm_type(ac
, glsl_get_column_type(type
)),
3956 glsl_get_matrix_columns(type
));
3959 if (glsl_type_is_array(type
)) {
3960 return LLVMArrayType(
3961 glsl_to_llvm_type(ac
, glsl_get_array_element(type
)),
3962 glsl_get_length(type
));
3965 assert(glsl_type_is_struct(type
));
3967 LLVMTypeRef member_types
[glsl_get_length(type
)];
3969 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
3971 glsl_to_llvm_type(ac
,
3972 glsl_get_struct_field(type
, i
));
3975 return LLVMStructTypeInContext(ac
->context
, member_types
,
3976 glsl_get_length(type
), false);
3979 static void visit_deref(struct ac_nir_context
*ctx
,
3980 nir_deref_instr
*instr
)
3982 if (instr
->mode
!= nir_var_mem_shared
&&
3983 instr
->mode
!= nir_var_mem_global
)
3986 LLVMValueRef result
= NULL
;
3987 switch(instr
->deref_type
) {
3988 case nir_deref_type_var
: {
3989 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->vars
, instr
->var
);
3990 result
= entry
->data
;
3993 case nir_deref_type_struct
:
3994 if (instr
->mode
== nir_var_mem_global
) {
3995 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
3996 uint64_t offset
= glsl_get_struct_field_offset(parent
->type
,
3997 instr
->strct
.index
);
3998 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
3999 LLVMConstInt(ctx
->ac
.i32
, offset
, 0));
4001 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4002 LLVMConstInt(ctx
->ac
.i32
, instr
->strct
.index
, 0));
4005 case nir_deref_type_array
:
4006 if (instr
->mode
== nir_var_mem_global
) {
4007 nir_deref_instr
*parent
= nir_deref_instr_parent(instr
);
4008 unsigned stride
= glsl_get_explicit_stride(parent
->type
);
4010 if ((glsl_type_is_matrix(parent
->type
) &&
4011 glsl_matrix_type_is_row_major(parent
->type
)) ||
4012 (glsl_type_is_vector(parent
->type
) && stride
== 0))
4013 stride
= type_scalar_size_bytes(parent
->type
);
4016 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4017 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4018 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4020 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4022 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4024 result
= ac_build_gep0(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4025 get_src(ctx
, instr
->arr
.index
));
4028 case nir_deref_type_ptr_as_array
:
4029 if (instr
->mode
== nir_var_mem_global
) {
4030 unsigned stride
= nir_deref_instr_ptr_as_array_stride(instr
);
4032 LLVMValueRef index
= get_src(ctx
, instr
->arr
.index
);
4033 if (LLVMTypeOf(index
) != ctx
->ac
.i64
)
4034 index
= LLVMBuildZExt(ctx
->ac
.builder
, index
, ctx
->ac
.i64
, "");
4036 LLVMValueRef offset
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->ac
.i64
, stride
, 0), "");
4038 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
), offset
);
4040 result
= ac_build_gep_ptr(&ctx
->ac
, get_src(ctx
, instr
->parent
),
4041 get_src(ctx
, instr
->arr
.index
));
4044 case nir_deref_type_cast
: {
4045 result
= get_src(ctx
, instr
->parent
);
4047 /* We can't use the structs from LLVM because the shader
4048 * specifies its own offsets. */
4049 LLVMTypeRef pointee_type
= ctx
->ac
.i8
;
4050 if (instr
->mode
== nir_var_mem_shared
)
4051 pointee_type
= glsl_to_llvm_type(&ctx
->ac
, instr
->type
);
4053 unsigned address_space
;
4055 switch(instr
->mode
) {
4056 case nir_var_mem_shared
:
4057 address_space
= AC_ADDR_SPACE_LDS
;
4059 case nir_var_mem_global
:
4060 address_space
= AC_ADDR_SPACE_GLOBAL
;
4063 unreachable("Unhandled address space");
4066 LLVMTypeRef type
= LLVMPointerType(pointee_type
, address_space
);
4068 if (LLVMTypeOf(result
) != type
) {
4069 if (LLVMGetTypeKind(LLVMTypeOf(result
)) == LLVMVectorTypeKind
) {
4070 result
= LLVMBuildBitCast(ctx
->ac
.builder
, result
,
4073 result
= LLVMBuildIntToPtr(ctx
->ac
.builder
, result
,
4080 unreachable("Unhandled deref_instr deref type");
4083 ctx
->ssa_defs
[instr
->dest
.ssa
.index
] = result
;
4086 static void visit_cf_list(struct ac_nir_context
*ctx
,
4087 struct exec_list
*list
);
4089 static void visit_block(struct ac_nir_context
*ctx
, nir_block
*block
)
4091 LLVMBasicBlockRef llvm_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
4092 nir_foreach_instr(instr
, block
)
4094 switch (instr
->type
) {
4095 case nir_instr_type_alu
:
4096 visit_alu(ctx
, nir_instr_as_alu(instr
));
4098 case nir_instr_type_load_const
:
4099 visit_load_const(ctx
, nir_instr_as_load_const(instr
));
4101 case nir_instr_type_intrinsic
:
4102 visit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
4104 case nir_instr_type_tex
:
4105 visit_tex(ctx
, nir_instr_as_tex(instr
));
4107 case nir_instr_type_phi
:
4108 visit_phi(ctx
, nir_instr_as_phi(instr
));
4110 case nir_instr_type_ssa_undef
:
4111 visit_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
4113 case nir_instr_type_jump
:
4114 visit_jump(&ctx
->ac
, nir_instr_as_jump(instr
));
4116 case nir_instr_type_deref
:
4117 visit_deref(ctx
, nir_instr_as_deref(instr
));
4120 fprintf(stderr
, "Unknown NIR instr type: ");
4121 nir_print_instr(instr
, stderr
);
4122 fprintf(stderr
, "\n");
4127 _mesa_hash_table_insert(ctx
->defs
, block
, llvm_block
);
4130 static void visit_if(struct ac_nir_context
*ctx
, nir_if
*if_stmt
)
4132 LLVMValueRef value
= get_src(ctx
, if_stmt
->condition
);
4134 nir_block
*then_block
=
4135 (nir_block
*) exec_list_get_head(&if_stmt
->then_list
);
4137 ac_build_uif(&ctx
->ac
, value
, then_block
->index
);
4139 visit_cf_list(ctx
, &if_stmt
->then_list
);
4141 if (!exec_list_is_empty(&if_stmt
->else_list
)) {
4142 nir_block
*else_block
=
4143 (nir_block
*) exec_list_get_head(&if_stmt
->else_list
);
4145 ac_build_else(&ctx
->ac
, else_block
->index
);
4146 visit_cf_list(ctx
, &if_stmt
->else_list
);
4149 ac_build_endif(&ctx
->ac
, then_block
->index
);
4152 static void visit_loop(struct ac_nir_context
*ctx
, nir_loop
*loop
)
4154 nir_block
*first_loop_block
=
4155 (nir_block
*) exec_list_get_head(&loop
->body
);
4157 ac_build_bgnloop(&ctx
->ac
, first_loop_block
->index
);
4159 visit_cf_list(ctx
, &loop
->body
);
4161 ac_build_endloop(&ctx
->ac
, first_loop_block
->index
);
4164 static void visit_cf_list(struct ac_nir_context
*ctx
,
4165 struct exec_list
*list
)
4167 foreach_list_typed(nir_cf_node
, node
, node
, list
)
4169 switch (node
->type
) {
4170 case nir_cf_node_block
:
4171 visit_block(ctx
, nir_cf_node_as_block(node
));
4174 case nir_cf_node_if
:
4175 visit_if(ctx
, nir_cf_node_as_if(node
));
4178 case nir_cf_node_loop
:
4179 visit_loop(ctx
, nir_cf_node_as_loop(node
));
4189 ac_handle_shader_output_decl(struct ac_llvm_context
*ctx
,
4190 struct ac_shader_abi
*abi
,
4191 struct nir_shader
*nir
,
4192 struct nir_variable
*variable
,
4193 gl_shader_stage stage
)
4195 unsigned output_loc
= variable
->data
.driver_location
/ 4;
4196 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4198 /* tess ctrl has it's own load/store paths for outputs */
4199 if (stage
== MESA_SHADER_TESS_CTRL
)
4202 if (stage
== MESA_SHADER_VERTEX
||
4203 stage
== MESA_SHADER_TESS_EVAL
||
4204 stage
== MESA_SHADER_GEOMETRY
) {
4205 int idx
= variable
->data
.location
+ variable
->data
.index
;
4206 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
4207 int length
= nir
->info
.clip_distance_array_size
+
4208 nir
->info
.cull_distance_array_size
;
4217 bool is_16bit
= glsl_type_is_16bit(glsl_without_array(variable
->type
));
4218 LLVMTypeRef type
= is_16bit
? ctx
->f16
: ctx
->f32
;
4219 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
4220 for (unsigned chan
= 0; chan
< 4; chan
++) {
4221 abi
->outputs
[ac_llvm_reg_index_soa(output_loc
+ i
, chan
)] =
4222 ac_build_alloca_undef(ctx
, type
, "");
4228 setup_locals(struct ac_nir_context
*ctx
,
4229 struct nir_function
*func
)
4232 ctx
->num_locals
= 0;
4233 nir_foreach_variable(variable
, &func
->impl
->locals
) {
4234 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
4235 variable
->data
.driver_location
= ctx
->num_locals
* 4;
4236 variable
->data
.location_frac
= 0;
4237 ctx
->num_locals
+= attrib_count
;
4239 ctx
->locals
= malloc(4 * ctx
->num_locals
* sizeof(LLVMValueRef
));
4243 for (i
= 0; i
< ctx
->num_locals
; i
++) {
4244 for (j
= 0; j
< 4; j
++) {
4245 ctx
->locals
[i
* 4 + j
] =
4246 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "temp");
4252 setup_shared(struct ac_nir_context
*ctx
,
4253 struct nir_shader
*nir
)
4255 nir_foreach_variable(variable
, &nir
->shared
) {
4256 LLVMValueRef shared
=
4257 LLVMAddGlobalInAddressSpace(
4258 ctx
->ac
.module
, glsl_to_llvm_type(&ctx
->ac
, variable
->type
),
4259 variable
->name
? variable
->name
: "",
4261 _mesa_hash_table_insert(ctx
->vars
, variable
, shared
);
4265 void ac_nir_translate(struct ac_llvm_context
*ac
, struct ac_shader_abi
*abi
,
4266 struct nir_shader
*nir
)
4268 struct ac_nir_context ctx
= {};
4269 struct nir_function
*func
;
4274 ctx
.stage
= nir
->info
.stage
;
4276 ctx
.main_function
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
4278 nir_foreach_variable(variable
, &nir
->outputs
)
4279 ac_handle_shader_output_decl(&ctx
.ac
, ctx
.abi
, nir
, variable
,
4282 ctx
.defs
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4283 _mesa_key_pointer_equal
);
4284 ctx
.phis
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4285 _mesa_key_pointer_equal
);
4286 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
4287 _mesa_key_pointer_equal
);
4289 func
= (struct nir_function
*)exec_list_get_head(&nir
->functions
);
4291 nir_index_ssa_defs(func
->impl
);
4292 ctx
.ssa_defs
= calloc(func
->impl
->ssa_alloc
, sizeof(LLVMValueRef
));
4294 setup_locals(&ctx
, func
);
4296 if (gl_shader_stage_is_compute(nir
->info
.stage
))
4297 setup_shared(&ctx
, nir
);
4299 visit_cf_list(&ctx
, &func
->impl
->body
);
4300 phi_post_pass(&ctx
);
4302 if (!gl_shader_stage_is_compute(nir
->info
.stage
))
4303 ctx
.abi
->emit_outputs(ctx
.abi
, AC_LLVM_MAX_OUTPUTS
,
4308 ralloc_free(ctx
.defs
);
4309 ralloc_free(ctx
.phis
);
4310 ralloc_free(ctx
.vars
);
4314 ac_lower_indirect_derefs(struct nir_shader
*nir
, enum chip_class chip_class
)
4316 /* While it would be nice not to have this flag, we are constrained
4317 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4320 bool llvm_has_working_vgpr_indexing
= chip_class
<= VI
;
4322 /* TODO: Indirect indexing of GS inputs is unimplemented.
4324 * TCS and TES load inputs directly from LDS or offchip memory, so
4325 * indirect indexing is trivial.
4327 nir_variable_mode indirect_mask
= 0;
4328 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
4329 (nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
4330 nir
->info
.stage
!= MESA_SHADER_TESS_EVAL
&&
4331 !llvm_has_working_vgpr_indexing
)) {
4332 indirect_mask
|= nir_var_shader_in
;
4334 if (!llvm_has_working_vgpr_indexing
&&
4335 nir
->info
.stage
!= MESA_SHADER_TESS_CTRL
)
4336 indirect_mask
|= nir_var_shader_out
;
4338 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4339 * smart enough to handle indirects without causing excess spilling
4340 * causing the gpu to hang.
4342 * See the following thread for more details of the problem:
4343 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4345 indirect_mask
|= nir_var_function_temp
;
4347 nir_lower_indirect_derefs(nir
, indirect_mask
);
4351 get_inst_tessfactor_writemask(nir_intrinsic_instr
*intrin
)
4353 if (intrin
->intrinsic
!= nir_intrinsic_store_deref
)
4357 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
4359 if (var
->data
.mode
!= nir_var_shader_out
)
4362 unsigned writemask
= 0;
4363 const int location
= var
->data
.location
;
4364 unsigned first_component
= var
->data
.location_frac
;
4365 unsigned num_comps
= intrin
->dest
.ssa
.num_components
;
4367 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
)
4368 writemask
= ((1 << (num_comps
+ 1)) - 1) << first_component
;
4369 else if (location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
4370 writemask
= (((1 << (num_comps
+ 1)) - 1) << first_component
) << 4;
4376 scan_tess_ctrl(nir_cf_node
*cf_node
, unsigned *upper_block_tf_writemask
,
4377 unsigned *cond_block_tf_writemask
,
4378 bool *tessfactors_are_def_in_all_invocs
, bool is_nested_cf
)
4380 switch (cf_node
->type
) {
4381 case nir_cf_node_block
: {
4382 nir_block
*block
= nir_cf_node_as_block(cf_node
);
4383 nir_foreach_instr(instr
, block
) {
4384 if (instr
->type
!= nir_instr_type_intrinsic
)
4387 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
4388 if (intrin
->intrinsic
== nir_intrinsic_barrier
) {
4390 /* If we find a barrier in nested control flow put this in the
4391 * too hard basket. In GLSL this is not possible but it is in
4395 *tessfactors_are_def_in_all_invocs
= false;
4399 /* The following case must be prevented:
4400 * gl_TessLevelInner = ...;
4402 * if (gl_InvocationID == 1)
4403 * gl_TessLevelInner = ...;
4405 * If you consider disjoint code segments separated by barriers, each
4406 * such segment that writes tess factor channels should write the same
4407 * channels in all codepaths within that segment.
4409 if (upper_block_tf_writemask
|| cond_block_tf_writemask
) {
4410 /* Accumulate the result: */
4411 *tessfactors_are_def_in_all_invocs
&=
4412 !(*cond_block_tf_writemask
& ~(*upper_block_tf_writemask
));
4414 /* Analyze the next code segment from scratch. */
4415 *upper_block_tf_writemask
= 0;
4416 *cond_block_tf_writemask
= 0;
4419 *upper_block_tf_writemask
|= get_inst_tessfactor_writemask(intrin
);
4424 case nir_cf_node_if
: {
4425 unsigned then_tessfactor_writemask
= 0;
4426 unsigned else_tessfactor_writemask
= 0;
4428 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
4429 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
) {
4430 scan_tess_ctrl(nested_node
, &then_tessfactor_writemask
,
4431 cond_block_tf_writemask
,
4432 tessfactors_are_def_in_all_invocs
, true);
4435 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
) {
4436 scan_tess_ctrl(nested_node
, &else_tessfactor_writemask
,
4437 cond_block_tf_writemask
,
4438 tessfactors_are_def_in_all_invocs
, true);
4441 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
4442 /* If both statements write the same tess factor channels,
4443 * we can say that the upper block writes them too.
4445 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
4446 else_tessfactor_writemask
;
4447 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
4448 else_tessfactor_writemask
;
4453 case nir_cf_node_loop
: {
4454 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
4455 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
) {
4456 scan_tess_ctrl(nested_node
, cond_block_tf_writemask
,
4457 cond_block_tf_writemask
,
4458 tessfactors_are_def_in_all_invocs
, true);
4464 unreachable("unknown cf node type");
4469 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader
*nir
)
4471 assert(nir
->info
.stage
== MESA_SHADER_TESS_CTRL
);
4473 /* The pass works as follows:
4474 * If all codepaths write tess factors, we can say that all
4475 * invocations define tess factors.
4477 * Each tess factor channel is tracked separately.
4479 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
4480 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
4482 /* Initial value = true. Here the pass will accumulate results from
4483 * multiple segments surrounded by barriers. If tess factors aren't
4484 * written at all, it's a shader bug and we don't care if this will be
4487 bool tessfactors_are_def_in_all_invocs
= true;
4489 nir_foreach_function(function
, nir
) {
4490 if (function
->impl
) {
4491 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
4492 scan_tess_ctrl(node
, &main_block_tf_writemask
,
4493 &cond_block_tf_writemask
,
4494 &tessfactors_are_def_in_all_invocs
,
4500 /* Accumulate the result for the last code segment separated by a
4503 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
4504 tessfactors_are_def_in_all_invocs
&=
4505 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
4508 return tessfactors_are_def_in_all_invocs
;