2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
30 #include "c11/threads.h"
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "util/u_math.h"
43 #include "shader_enums.h"
45 #define AC_LLVM_INITIAL_CF_DEPTH 4
47 /* Data for if/else/endif and bgnloop/endloop control flow structures.
50 /* Loop exit or next part of if/else/endif. */
51 LLVMBasicBlockRef next_block
;
52 LLVMBasicBlockRef loop_entry_block
;
55 /* Initialize module-independent parts of the context.
57 * The caller is responsible for initializing ctx::module and ctx::builder.
60 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
61 enum chip_class chip_class
, enum radeon_family family
)
65 ctx
->context
= LLVMContextCreate();
67 ctx
->chip_class
= chip_class
;
72 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
73 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
74 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
75 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
76 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
77 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
78 ctx
->intptr
= ctx
->i32
;
79 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
80 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
81 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
82 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
83 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
84 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
85 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
86 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
87 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
88 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
90 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
91 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
92 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
93 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
94 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
95 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
96 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
97 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
98 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
99 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
101 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
102 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
104 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
107 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
108 "invariant.load", 14);
110 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
112 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
113 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
115 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
116 "amdgpu.uniform", 14);
118 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
122 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
126 ctx
->flow_depth_max
= 0;
130 ac_get_llvm_num_components(LLVMValueRef value
)
132 LLVMTypeRef type
= LLVMTypeOf(value
);
133 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
134 ? LLVMGetVectorSize(type
)
136 return num_components
;
140 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
144 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
149 return LLVMBuildExtractElement(ac
->builder
, value
,
150 LLVMConstInt(ac
->i32
, index
, false), "");
154 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
156 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
157 type
= LLVMGetElementType(type
);
159 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
160 return LLVMGetIntTypeWidth(type
);
162 if (type
== ctx
->f16
)
164 if (type
== ctx
->f32
)
166 if (type
== ctx
->f64
)
169 unreachable("Unhandled type kind in get_elem_bits");
173 ac_get_type_size(LLVMTypeRef type
)
175 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
178 case LLVMIntegerTypeKind
:
179 return LLVMGetIntTypeWidth(type
) / 8;
180 case LLVMHalfTypeKind
:
182 case LLVMFloatTypeKind
:
184 case LLVMDoubleTypeKind
:
186 case LLVMPointerTypeKind
:
187 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
190 case LLVMVectorTypeKind
:
191 return LLVMGetVectorSize(type
) *
192 ac_get_type_size(LLVMGetElementType(type
));
193 case LLVMArrayTypeKind
:
194 return LLVMGetArrayLength(type
) *
195 ac_get_type_size(LLVMGetElementType(type
));
202 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
204 if (t
== ctx
->f16
|| t
== ctx
->i16
)
206 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
208 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
211 unreachable("Unhandled integer size");
215 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
217 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
218 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
219 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
220 LLVMGetVectorSize(t
));
222 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
223 switch (LLVMGetPointerAddressSpace(t
)) {
224 case AC_ADDR_SPACE_GLOBAL
:
226 case AC_ADDR_SPACE_LDS
:
229 unreachable("unhandled address space");
232 return to_integer_type_scalar(ctx
, t
);
236 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
238 LLVMTypeRef type
= LLVMTypeOf(v
);
239 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
240 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
242 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
246 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
248 LLVMTypeRef type
= LLVMTypeOf(v
);
249 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
251 return ac_to_integer(ctx
, v
);
254 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
256 if (t
== ctx
->i16
|| t
== ctx
->f16
)
258 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
260 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
263 unreachable("Unhandled float size");
267 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
269 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
270 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
271 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
272 LLVMGetVectorSize(t
));
274 return to_float_type_scalar(ctx
, t
);
278 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
280 LLVMTypeRef type
= LLVMTypeOf(v
);
281 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
286 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
287 LLVMTypeRef return_type
, LLVMValueRef
*params
,
288 unsigned param_count
, unsigned attrib_mask
)
290 LLVMValueRef function
, call
;
291 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
293 function
= LLVMGetNamedFunction(ctx
->module
, name
);
295 LLVMTypeRef param_types
[32], function_type
;
298 assert(param_count
<= 32);
300 for (i
= 0; i
< param_count
; ++i
) {
302 param_types
[i
] = LLVMTypeOf(params
[i
]);
305 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
306 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
308 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
309 LLVMSetLinkage(function
, LLVMExternalLinkage
);
311 if (!set_callsite_attrs
)
312 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
315 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
316 if (set_callsite_attrs
)
317 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
322 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
325 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
327 LLVMTypeRef elem_type
= type
;
329 assert(bufsize
>= 8);
331 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
332 int ret
= snprintf(buf
, bufsize
, "v%u",
333 LLVMGetVectorSize(type
));
335 char *type_name
= LLVMPrintTypeToString(type
);
336 fprintf(stderr
, "Error building type name for: %s\n",
340 elem_type
= LLVMGetElementType(type
);
344 switch (LLVMGetTypeKind(elem_type
)) {
346 case LLVMIntegerTypeKind
:
347 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
349 case LLVMHalfTypeKind
:
350 snprintf(buf
, bufsize
, "f16");
352 case LLVMFloatTypeKind
:
353 snprintf(buf
, bufsize
, "f32");
355 case LLVMDoubleTypeKind
:
356 snprintf(buf
, bufsize
, "f64");
362 * Helper function that builds an LLVM IR PHI node and immediately adds
366 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
367 unsigned count_incoming
, LLVMValueRef
*values
,
368 LLVMBasicBlockRef
*blocks
)
370 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
371 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
375 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
377 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
378 0, AC_FUNC_ATTR_CONVERGENT
);
381 /* Prevent optimizations (at least of memory accesses) across the current
382 * point in the program by emitting empty inline assembly that is marked as
383 * having side effects.
385 * Optionally, a value can be passed through the inline assembly to prevent
386 * LLVM from hoisting calls to ReadNone functions.
389 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
392 static int counter
= 0;
394 LLVMBuilderRef builder
= ctx
->builder
;
397 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
400 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
401 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
402 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
404 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
405 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
406 LLVMValueRef vgpr
= *pvgpr
;
407 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
408 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
411 assert(vgpr_size
% 4 == 0);
413 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
414 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
415 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
416 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
417 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
424 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
426 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
427 ctx
->i64
, NULL
, 0, 0);
428 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
432 ac_build_ballot(struct ac_llvm_context
*ctx
,
435 LLVMValueRef args
[3] = {
438 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
441 /* We currently have no other way to prevent LLVM from lifting the icmp
442 * calls to a dominating basic block.
444 ac_build_optimization_barrier(ctx
, &args
[0]);
446 args
[0] = ac_to_integer(ctx
, args
[0]);
448 return ac_build_intrinsic(ctx
,
449 "llvm.amdgcn.icmp.i32",
451 AC_FUNC_ATTR_NOUNWIND
|
452 AC_FUNC_ATTR_READNONE
|
453 AC_FUNC_ATTR_CONVERGENT
);
457 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
459 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
460 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
461 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
465 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
467 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
468 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
469 LLVMConstInt(ctx
->i64
, 0, 0), "");
473 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
475 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
476 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
478 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
479 vote_set
, active_set
, "");
480 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
482 LLVMConstInt(ctx
->i64
, 0, 0), "");
483 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
487 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
488 unsigned value_count
, unsigned component
)
490 LLVMValueRef vec
= NULL
;
492 if (value_count
== 1) {
493 return values
[component
];
494 } else if (!value_count
)
495 unreachable("value_count is 0");
497 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
498 LLVMValueRef value
= values
[i
];
501 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
502 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
503 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
509 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
510 LLVMValueRef
*values
,
511 unsigned value_count
,
512 unsigned value_stride
,
516 LLVMBuilderRef builder
= ctx
->builder
;
517 LLVMValueRef vec
= NULL
;
520 if (value_count
== 1 && !always_vector
) {
522 return LLVMBuildLoad(builder
, values
[0], "");
524 } else if (!value_count
)
525 unreachable("value_count is 0");
527 for (i
= 0; i
< value_count
; i
++) {
528 LLVMValueRef value
= values
[i
* value_stride
];
530 value
= LLVMBuildLoad(builder
, value
, "");
533 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
534 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
535 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
541 ac_build_gather_values(struct ac_llvm_context
*ctx
,
542 LLVMValueRef
*values
,
543 unsigned value_count
)
545 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
548 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
549 * channels with undef. Extract at most src_channels components from the input.
552 ac_build_expand(struct ac_llvm_context
*ctx
,
554 unsigned src_channels
,
555 unsigned dst_channels
)
557 LLVMTypeRef elemtype
;
558 LLVMValueRef chan
[dst_channels
];
560 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
561 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
563 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
566 src_channels
= MIN2(src_channels
, vec_size
);
568 for (unsigned i
= 0; i
< src_channels
; i
++)
569 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
571 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
574 assert(src_channels
== 1);
577 elemtype
= LLVMTypeOf(value
);
580 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
581 chan
[i
] = LLVMGetUndef(elemtype
);
583 return ac_build_gather_values(ctx
, chan
, dst_channels
);
586 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
587 * with undef. Extract at most num_channels components from the input.
589 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
591 unsigned num_channels
)
593 return ac_build_expand(ctx
, value
, num_channels
, 4);
596 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
598 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
602 name
= "llvm.rint.f16";
603 else if (type_size
== 4)
604 name
= "llvm.rint.f32";
606 name
= "llvm.rint.f64";
608 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
609 AC_FUNC_ATTR_READNONE
);
613 ac_build_fdiv(struct ac_llvm_context
*ctx
,
617 /* If we do (num / den), LLVM >= 7.0 does:
618 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
620 * If we do (num * (1 / den)), LLVM does:
621 * return num * v_rcp_f32(den);
623 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
624 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
625 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
627 /* Use v_rcp_f32 instead of precise division. */
628 if (!LLVMIsConstant(ret
))
629 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
633 /* See fast_idiv_by_const.h. */
634 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
635 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
637 LLVMValueRef multiplier
,
638 LLVMValueRef pre_shift
,
639 LLVMValueRef post_shift
,
640 LLVMValueRef increment
)
642 LLVMBuilderRef builder
= ctx
->builder
;
644 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
645 num
= LLVMBuildMul(builder
,
646 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
647 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
648 num
= LLVMBuildAdd(builder
, num
,
649 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
650 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
651 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
652 return LLVMBuildLShr(builder
, num
, post_shift
, "");
655 /* See fast_idiv_by_const.h. */
656 /* If num != UINT_MAX, this more efficient version can be used. */
657 /* Set: increment = util_fast_udiv_info::increment; */
658 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
660 LLVMValueRef multiplier
,
661 LLVMValueRef pre_shift
,
662 LLVMValueRef post_shift
,
663 LLVMValueRef increment
)
665 LLVMBuilderRef builder
= ctx
->builder
;
667 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
668 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
669 num
= LLVMBuildMul(builder
,
670 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
671 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
672 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
673 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
674 return LLVMBuildLShr(builder
, num
, post_shift
, "");
677 /* See fast_idiv_by_const.h. */
678 /* Both operands must fit in 31 bits and the divisor must not be 1. */
679 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
681 LLVMValueRef multiplier
,
682 LLVMValueRef post_shift
)
684 LLVMBuilderRef builder
= ctx
->builder
;
686 num
= LLVMBuildMul(builder
,
687 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
688 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
689 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
690 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
691 return LLVMBuildLShr(builder
, num
, post_shift
, "");
694 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
695 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
696 * already multiplied by two. id is the cube face number.
698 struct cube_selection_coords
{
705 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
707 struct cube_selection_coords
*out
)
709 LLVMTypeRef f32
= ctx
->f32
;
711 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
712 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
713 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
714 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
715 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
716 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
717 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
718 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
722 * Build a manual selection sequence for cube face sc/tc coordinates and
723 * major axis vector (multiplied by 2 for consistency) for the given
724 * vec3 \p coords, for the face implied by \p selcoords.
726 * For the major axis, we always adjust the sign to be in the direction of
727 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
728 * the selcoords major axis.
730 static void build_cube_select(struct ac_llvm_context
*ctx
,
731 const struct cube_selection_coords
*selcoords
,
732 const LLVMValueRef
*coords
,
733 LLVMValueRef
*out_st
,
734 LLVMValueRef
*out_ma
)
736 LLVMBuilderRef builder
= ctx
->builder
;
737 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
738 LLVMValueRef is_ma_positive
;
740 LLVMValueRef is_ma_z
, is_not_ma_z
;
741 LLVMValueRef is_ma_y
;
742 LLVMValueRef is_ma_x
;
746 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
747 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
748 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
749 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
751 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
752 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
753 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
754 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
755 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
758 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
759 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
760 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
761 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
762 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
765 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
766 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
767 LLVMConstReal(f32
, -1.0), "");
768 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
771 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
772 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
773 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
774 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
775 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
779 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
780 bool is_deriv
, bool is_array
, bool is_lod
,
781 LLVMValueRef
*coords_arg
,
782 LLVMValueRef
*derivs_arg
)
785 LLVMBuilderRef builder
= ctx
->builder
;
786 struct cube_selection_coords selcoords
;
787 LLVMValueRef coords
[3];
790 if (is_array
&& !is_lod
) {
791 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
793 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
795 * "For Array forms, the array layer used will be
797 * max(0, min(d−1, floor(layer+0.5)))
799 * where d is the depth of the texture array and layer
800 * comes from the component indicated in the tables below.
801 * Workaroudn for an issue where the layer is taken from a
802 * helper invocation which happens to fall on a different
803 * layer due to extrapolation."
805 * VI and earlier attempt to implement this in hardware by
806 * clamping the value of coords[2] = (8 * layer) + face.
807 * Unfortunately, this means that the we end up with the wrong
808 * face when clamping occurs.
810 * Clamp the layer earlier to work around the issue.
812 if (ctx
->chip_class
<= VI
) {
814 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
815 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
821 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
823 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
824 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
825 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
827 for (int i
= 0; i
< 2; ++i
)
828 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
830 coords
[2] = selcoords
.id
;
832 if (is_deriv
&& derivs_arg
) {
833 LLVMValueRef derivs
[4];
836 /* Convert cube derivatives to 2D derivatives. */
837 for (axis
= 0; axis
< 2; axis
++) {
838 LLVMValueRef deriv_st
[2];
839 LLVMValueRef deriv_ma
;
841 /* Transform the derivative alongside the texture
842 * coordinate. Mathematically, the correct formula is
843 * as follows. Assume we're projecting onto the +Z face
844 * and denote by dx/dh the derivative of the (original)
845 * X texture coordinate with respect to horizontal
846 * window coordinates. The projection onto the +Z face
851 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
852 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
854 * This motivatives the implementation below.
856 * Whether this actually gives the expected results for
857 * apps that might feed in derivatives obtained via
858 * finite differences is anyone's guess. The OpenGL spec
859 * seems awfully quiet about how textureGrad for cube
860 * maps should be handled.
862 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
863 deriv_st
, &deriv_ma
);
865 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
867 for (int i
= 0; i
< 2; ++i
)
868 derivs
[axis
* 2 + i
] =
869 LLVMBuildFSub(builder
,
870 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
871 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
874 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
877 /* Shift the texture coordinate. This must be applied after the
878 * derivative calculation.
880 for (int i
= 0; i
< 2; ++i
)
881 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
884 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
885 /* coords_arg.w component - array_index for cube arrays */
886 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
889 memcpy(coords_arg
, coords
, sizeof(coords
));
894 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
895 LLVMValueRef llvm_chan
,
896 LLVMValueRef attr_number
,
901 LLVMValueRef args
[5];
906 args
[2] = attr_number
;
909 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
910 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
915 args
[3] = attr_number
;
918 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
919 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
923 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
924 LLVMValueRef llvm_chan
,
925 LLVMValueRef attr_number
,
930 LLVMValueRef args
[6];
935 args
[2] = attr_number
;
936 args
[3] = ctx
->i1false
;
939 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
940 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
945 args
[3] = attr_number
;
946 args
[4] = ctx
->i1false
;
949 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
950 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
954 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
955 LLVMValueRef parameter
,
956 LLVMValueRef llvm_chan
,
957 LLVMValueRef attr_number
,
960 LLVMValueRef args
[4];
964 args
[2] = attr_number
;
967 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
968 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
972 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
973 LLVMValueRef base_ptr
,
976 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
980 ac_build_gep0(struct ac_llvm_context
*ctx
,
981 LLVMValueRef base_ptr
,
984 LLVMValueRef indices
[2] = {
988 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
991 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
994 return LLVMBuildPointerCast(ctx
->builder
,
995 ac_build_gep0(ctx
, ptr
, index
),
996 LLVMTypeOf(ptr
), "");
1000 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1001 LLVMValueRef base_ptr
, LLVMValueRef index
,
1004 LLVMBuildStore(ctx
->builder
, value
,
1005 ac_build_gep0(ctx
, base_ptr
, index
));
1009 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1010 * It's equivalent to doing a load from &base_ptr[index].
1012 * \param base_ptr Where the array starts.
1013 * \param index The element index into the array.
1014 * \param uniform Whether the base_ptr and index can be assumed to be
1015 * dynamically uniform (i.e. load to an SGPR)
1016 * \param invariant Whether the load is invariant (no other opcodes affect it)
1017 * \param no_unsigned_wraparound
1018 * For all possible re-associations and re-distributions of an expression
1019 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1020 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1021 * does not result in an unsigned integer wraparound. This is used for
1022 * optimal code generation of 32-bit pointer arithmetic.
1024 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1025 * integer wraparound can't be an imm offset in s_load_dword, because
1026 * the instruction performs "addr + offset" in 64 bits.
1028 * Expected usage for bindless textures by chaining GEPs:
1029 * // possible unsigned wraparound, don't use InBounds:
1030 * ptr1 = LLVMBuildGEP(base_ptr, index);
1031 * image = load(ptr1); // becomes "s_load ptr1, 0"
1033 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1034 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1037 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1038 LLVMValueRef index
, bool uniform
, bool invariant
,
1039 bool no_unsigned_wraparound
)
1041 LLVMValueRef pointer
, result
;
1042 LLVMValueRef indices
[2] = {ctx
->i32_0
, index
};
1044 if (no_unsigned_wraparound
&&
1045 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1046 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1048 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1051 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1052 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1054 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1058 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1061 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1064 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1065 LLVMValueRef base_ptr
, LLVMValueRef index
)
1067 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1070 /* This assumes that there is no unsigned integer wraparound during the address
1071 * computation, excluding all GEPs within base_ptr. */
1072 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1073 LLVMValueRef base_ptr
, LLVMValueRef index
)
1075 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1078 /* See ac_build_load_custom() documentation. */
1079 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1080 LLVMValueRef base_ptr
, LLVMValueRef index
)
1082 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1085 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1086 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1087 * or v4i32 (num_channels=3,4).
1090 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1093 unsigned num_channels
,
1094 LLVMValueRef voffset
,
1095 LLVMValueRef soffset
,
1096 unsigned inst_offset
,
1099 bool writeonly_memory
,
1100 bool swizzle_enable_hint
)
1102 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
1104 if (num_channels
== 3) {
1105 LLVMValueRef v
[3], v01
;
1107 for (int i
= 0; i
< 3; i
++) {
1108 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1109 LLVMConstInt(ctx
->i32
, i
, 0), "");
1111 v01
= ac_build_gather_values(ctx
, v
, 2);
1113 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1114 soffset
, inst_offset
, glc
, slc
,
1115 writeonly_memory
, swizzle_enable_hint
);
1116 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1117 soffset
, inst_offset
+ 8,
1119 writeonly_memory
, swizzle_enable_hint
);
1123 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1124 * (voffset is swizzled, but soffset isn't swizzled).
1125 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1127 if (!swizzle_enable_hint
) {
1128 LLVMValueRef offset
= soffset
;
1130 static const char *types
[] = {"f32", "v2f32", "v4f32"};
1133 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1134 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1136 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1138 LLVMValueRef args
[] = {
1139 ac_to_float(ctx
, vdata
),
1140 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1143 LLVMConstInt(ctx
->i1
, glc
, 0),
1144 LLVMConstInt(ctx
->i1
, slc
, 0),
1148 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1149 types
[CLAMP(num_channels
, 1, 3) - 1]);
1151 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
1152 args
, ARRAY_SIZE(args
),
1154 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1155 AC_FUNC_ATTR_WRITEONLY
);
1159 static const unsigned dfmt
[] = {
1160 V_008F0C_BUF_DATA_FORMAT_32
,
1161 V_008F0C_BUF_DATA_FORMAT_32_32
,
1162 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1163 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1165 static const char *types
[] = {"i32", "v2i32", "v4i32"};
1166 LLVMValueRef args
[] = {
1168 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1170 voffset
? voffset
: ctx
->i32_0
,
1172 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1173 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
1174 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
1175 LLVMConstInt(ctx
->i1
, glc
, 0),
1176 LLVMConstInt(ctx
->i1
, slc
, 0),
1179 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
1180 types
[CLAMP(num_channels
, 1, 3) - 1]);
1182 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
1183 args
, ARRAY_SIZE(args
),
1185 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1186 AC_FUNC_ATTR_WRITEONLY
);
1190 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1192 LLVMValueRef vindex
,
1193 LLVMValueRef voffset
,
1194 unsigned num_channels
,
1200 LLVMValueRef args
[] = {
1201 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1202 vindex
? vindex
: ctx
->i32_0
,
1204 LLVMConstInt(ctx
->i1
, glc
, 0),
1205 LLVMConstInt(ctx
->i1
, slc
, 0)
1207 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1209 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1210 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1214 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1217 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1221 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1223 ac_get_load_intr_attribs(can_speculate
));
1227 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1229 LLVMValueRef vindex
,
1230 LLVMValueRef voffset
,
1231 LLVMValueRef soffset
,
1232 unsigned num_channels
,
1239 LLVMValueRef args
[5];
1241 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1243 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1244 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1245 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1246 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1247 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1249 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1250 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1251 const char *indexing_kind
= structurized
? "struct" : "raw";
1255 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1256 indexing_kind
, type_names
[func
]);
1258 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1259 indexing_kind
, type_names
[func
]);
1262 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1264 ac_get_load_intr_attribs(can_speculate
));
1268 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1271 LLVMValueRef vindex
,
1272 LLVMValueRef voffset
,
1273 LLVMValueRef soffset
,
1274 unsigned inst_offset
,
1280 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1282 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1284 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1286 if (allow_smem
&& !slc
&&
1287 (!glc
|| (HAVE_LLVM
>= 0x0800 && ctx
->chip_class
>= VI
))) {
1288 assert(vindex
== NULL
);
1290 LLVMValueRef result
[8];
1292 for (int i
= 0; i
< num_channels
; i
++) {
1294 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1295 LLVMConstInt(ctx
->i32
, 4, 0), "");
1297 const char *intrname
=
1298 HAVE_LLVM
>= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1299 : "llvm.SI.load.const.v4i32";
1300 unsigned num_args
= HAVE_LLVM
>= 0x0800 ? 3 : 2;
1301 LLVMValueRef args
[3] = {
1304 glc
? ctx
->i32_1
: ctx
->i32_0
,
1306 result
[i
] = ac_build_intrinsic(ctx
, intrname
,
1307 ctx
->f32
, args
, num_args
,
1308 AC_FUNC_ATTR_READNONE
|
1309 (HAVE_LLVM
< 0x0800 ? AC_FUNC_ATTR_LEGACY
: 0));
1311 if (num_channels
== 1)
1314 if (num_channels
== 3)
1315 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1316 return ac_build_gather_values(ctx
, result
, num_channels
);
1319 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1320 num_channels
, glc
, slc
,
1321 can_speculate
, false);
1324 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1326 LLVMValueRef vindex
,
1327 LLVMValueRef voffset
,
1328 unsigned num_channels
,
1332 if (HAVE_LLVM
>= 0x800) {
1333 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1334 num_channels
, glc
, false,
1335 can_speculate
, true, true);
1337 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1338 num_channels
, glc
, false,
1339 can_speculate
, true);
1342 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1344 LLVMValueRef vindex
,
1345 LLVMValueRef voffset
,
1346 unsigned num_channels
,
1350 if (HAVE_LLVM
>= 0x800) {
1351 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1352 num_channels
, glc
, false,
1353 can_speculate
, true, true);
1356 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1357 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1358 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1360 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1361 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1362 elem_count
, stride
, "");
1364 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1365 LLVMConstInt(ctx
->i32
, 2, 0), "");
1367 return ac_build_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1368 num_channels
, glc
, false,
1369 can_speculate
, true);
1373 ac_build_llvm8_tbuffer_load(struct ac_llvm_context
*ctx
,
1375 LLVMValueRef vindex
,
1376 LLVMValueRef voffset
,
1377 LLVMValueRef soffset
,
1378 unsigned num_channels
,
1386 LLVMValueRef args
[6];
1388 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1390 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1391 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1392 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1393 args
[idx
++] = LLVMConstInt(ctx
->i32
, dfmt
| (nfmt
<< 4), 0);
1394 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1395 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1397 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1398 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1399 const char *indexing_kind
= structurized
? "struct" : "raw";
1402 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1403 indexing_kind
, type_names
[func
]);
1405 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1407 ac_get_load_intr_attribs(can_speculate
));
1411 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1413 LLVMValueRef vindex
,
1414 LLVMValueRef voffset
,
1415 LLVMValueRef soffset
,
1416 LLVMValueRef immoffset
,
1417 unsigned num_channels
,
1423 bool structurized
) /* only matters for LLVM 8+ */
1425 if (HAVE_LLVM
>= 0x800) {
1426 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1428 return ac_build_llvm8_tbuffer_load(ctx
, rsrc
, vindex
, voffset
,
1429 soffset
, num_channels
,
1430 dfmt
, nfmt
, glc
, slc
,
1431 can_speculate
, structurized
);
1434 LLVMValueRef args
[] = {
1436 vindex
? vindex
: ctx
->i32_0
,
1440 LLVMConstInt(ctx
->i32
, dfmt
, false),
1441 LLVMConstInt(ctx
->i32
, nfmt
, false),
1442 LLVMConstInt(ctx
->i32
, glc
, false),
1443 LLVMConstInt(ctx
->i32
, slc
, false),
1445 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1446 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1447 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1450 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.load.%s",
1453 return ac_build_intrinsic(ctx
, name
, types
[func
], args
, 9,
1454 ac_get_load_intr_attribs(can_speculate
));
1458 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1460 LLVMValueRef vindex
,
1461 LLVMValueRef voffset
,
1462 LLVMValueRef soffset
,
1463 LLVMValueRef immoffset
,
1464 unsigned num_channels
,
1471 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1472 immoffset
, num_channels
, dfmt
, nfmt
, glc
,
1473 slc
, can_speculate
, true);
1477 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1479 LLVMValueRef voffset
,
1480 LLVMValueRef soffset
,
1481 LLVMValueRef immoffset
,
1482 unsigned num_channels
,
1489 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1490 immoffset
, num_channels
, dfmt
, nfmt
, glc
,
1491 slc
, can_speculate
, false);
1495 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1497 LLVMValueRef voffset
,
1498 LLVMValueRef soffset
,
1499 LLVMValueRef immoffset
,
1502 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1503 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1506 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1507 immoffset
, 1, dfmt
, nfmt
, glc
, false,
1510 return LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1514 * Set range metadata on an instruction. This can only be used on load and
1515 * call instructions. If you know an instruction can only produce the values
1516 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1517 * \p lo is the minimum value inclusive.
1518 * \p hi is the maximum value exclusive.
1520 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1521 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1523 LLVMValueRef range_md
, md_args
[2];
1524 LLVMTypeRef type
= LLVMTypeOf(value
);
1525 LLVMContextRef context
= LLVMGetTypeContext(type
);
1527 md_args
[0] = LLVMConstInt(type
, lo
, false);
1528 md_args
[1] = LLVMConstInt(type
, hi
, false);
1529 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1530 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1534 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1538 LLVMValueRef tid_args
[2];
1539 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1540 tid_args
[1] = ctx
->i32_0
;
1541 tid_args
[1] = ac_build_intrinsic(ctx
,
1542 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1543 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1545 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1547 2, AC_FUNC_ATTR_READNONE
);
1548 set_range_metadata(ctx
, tid
, 0, 64);
1553 * SI implements derivatives using the local data store (LDS)
1554 * All writes to the LDS happen in all executing threads at
1555 * the same time. TID is the Thread ID for the current
1556 * thread and is a value between 0 and 63, representing
1557 * the thread's position in the wavefront.
1559 * For the pixel shader threads are grouped into quads of four pixels.
1560 * The TIDs of the pixels of a quad are:
1568 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1569 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1570 * the current pixel's column, and masking with 0xfffffffe yields the TID
1571 * of the left pixel of the current pixel's row.
1573 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1574 * adding 2 yields the TID of the pixel below the top pixel.
1577 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1582 unsigned tl_lanes
[4], trbl_lanes
[4];
1583 LLVMValueRef tl
, trbl
;
1584 LLVMValueRef result
;
1586 for (unsigned i
= 0; i
< 4; ++i
) {
1587 tl_lanes
[i
] = i
& mask
;
1588 trbl_lanes
[i
] = (i
& mask
) + idx
;
1591 tl
= ac_build_quad_swizzle(ctx
, val
,
1592 tl_lanes
[0], tl_lanes
[1],
1593 tl_lanes
[2], tl_lanes
[3]);
1594 trbl
= ac_build_quad_swizzle(ctx
, val
,
1595 trbl_lanes
[0], trbl_lanes
[1],
1596 trbl_lanes
[2], trbl_lanes
[3]);
1598 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1599 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1600 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1602 result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.f32", ctx
->f32
,
1609 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1611 LLVMValueRef wave_id
)
1613 LLVMValueRef args
[2];
1614 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1616 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1620 ac_build_imsb(struct ac_llvm_context
*ctx
,
1622 LLVMTypeRef dst_type
)
1624 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1626 AC_FUNC_ATTR_READNONE
);
1628 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1629 * the index from LSB. Invert it by doing "31 - msb". */
1630 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1633 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1634 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1635 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1636 arg
, ctx
->i32_0
, ""),
1637 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1638 arg
, all_ones
, ""), "");
1640 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1644 ac_build_umsb(struct ac_llvm_context
*ctx
,
1646 LLVMTypeRef dst_type
)
1648 const char *intrin_name
;
1650 LLVMValueRef highest_bit
;
1654 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
1657 intrin_name
= "llvm.ctlz.i64";
1659 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
1663 intrin_name
= "llvm.ctlz.i32";
1665 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
1669 intrin_name
= "llvm.ctlz.i16";
1671 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
1675 unreachable(!"invalid bitsize");
1679 LLVMValueRef params
[2] = {
1684 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
1686 AC_FUNC_ATTR_READNONE
);
1688 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1689 * the index from LSB. Invert it by doing "31 - msb". */
1690 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
1691 msb
= LLVMBuildTruncOrBitCast(ctx
->builder
, msb
, ctx
->i32
, "");
1693 /* check for zero */
1694 return LLVMBuildSelect(ctx
->builder
,
1695 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
1696 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1699 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1703 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
1704 LLVMValueRef args
[2] = {a
, b
};
1705 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
1706 AC_FUNC_ATTR_READNONE
);
1709 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1713 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
1714 LLVMValueRef args
[2] = {a
, b
};
1715 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
1716 AC_FUNC_ATTR_READNONE
);
1719 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1722 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1723 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1726 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1729 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1730 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1733 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1736 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1737 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1740 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1742 LLVMTypeRef t
= LLVMTypeOf(value
);
1743 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
1744 LLVMConstReal(t
, 1.0));
1747 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1749 LLVMValueRef args
[9];
1751 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1752 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1755 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1756 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1758 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1760 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1762 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1763 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1765 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1766 ctx
->voidt
, args
, 6, 0);
1768 args
[2] = a
->out
[0];
1769 args
[3] = a
->out
[1];
1770 args
[4] = a
->out
[2];
1771 args
[5] = a
->out
[3];
1772 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1773 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1775 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1776 ctx
->voidt
, args
, 8, 0);
1780 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1782 struct ac_export_args args
;
1784 args
.enabled_channels
= 0x0; /* enabled channels */
1785 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1786 args
.done
= 1; /* DONE bit */
1787 args
.target
= V_008DFC_SQ_EXP_NULL
;
1788 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1789 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1790 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1791 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1792 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1794 ac_build_export(ctx
, &args
);
1797 static unsigned ac_num_coords(enum ac_image_dim dim
)
1803 case ac_image_1darray
:
1807 case ac_image_2darray
:
1808 case ac_image_2dmsaa
:
1810 case ac_image_2darraymsaa
:
1813 unreachable("ac_num_coords: bad dim");
1817 static unsigned ac_num_derivs(enum ac_image_dim dim
)
1821 case ac_image_1darray
:
1824 case ac_image_2darray
:
1829 case ac_image_2dmsaa
:
1830 case ac_image_2darraymsaa
:
1832 unreachable("derivatives not supported");
1836 static const char *get_atomic_name(enum ac_atomic_op op
)
1839 case ac_atomic_swap
: return "swap";
1840 case ac_atomic_add
: return "add";
1841 case ac_atomic_sub
: return "sub";
1842 case ac_atomic_smin
: return "smin";
1843 case ac_atomic_umin
: return "umin";
1844 case ac_atomic_smax
: return "smax";
1845 case ac_atomic_umax
: return "umax";
1846 case ac_atomic_and
: return "and";
1847 case ac_atomic_or
: return "or";
1848 case ac_atomic_xor
: return "xor";
1850 unreachable("bad atomic op");
1853 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1854 struct ac_image_args
*a
)
1856 const char *overload
[3] = { "", "", "" };
1857 unsigned num_overloads
= 0;
1858 LLVMValueRef args
[18];
1859 unsigned num_args
= 0;
1860 enum ac_image_dim dim
= a
->dim
;
1862 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
1864 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
1865 a
->opcode
!= ac_image_store_mip
) ||
1867 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1868 (!a
->compare
&& !a
->offset
));
1869 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1870 a
->opcode
== ac_image_get_lod
) ||
1872 assert((a
->bias
? 1 : 0) +
1874 (a
->level_zero
? 1 : 0) +
1875 (a
->derivs
[0] ? 1 : 0) <= 1);
1877 if (a
->opcode
== ac_image_get_lod
) {
1879 case ac_image_1darray
:
1882 case ac_image_2darray
:
1891 bool sample
= a
->opcode
== ac_image_sample
||
1892 a
->opcode
== ac_image_gather4
||
1893 a
->opcode
== ac_image_get_lod
;
1894 bool atomic
= a
->opcode
== ac_image_atomic
||
1895 a
->opcode
== ac_image_atomic_cmpswap
;
1896 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
1898 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
1899 args
[num_args
++] = a
->data
[0];
1900 if (a
->opcode
== ac_image_atomic_cmpswap
)
1901 args
[num_args
++] = a
->data
[1];
1905 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
1908 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
1910 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
1911 overload
[num_overloads
++] = ".f32";
1914 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
1916 unsigned count
= ac_num_derivs(dim
);
1917 for (unsigned i
= 0; i
< count
; ++i
)
1918 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
1919 overload
[num_overloads
++] = ".f32";
1921 unsigned num_coords
=
1922 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
1923 for (unsigned i
= 0; i
< num_coords
; ++i
)
1924 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
1926 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
1927 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
1929 args
[num_args
++] = a
->resource
;
1931 args
[num_args
++] = a
->sampler
;
1932 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
1935 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
1936 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->cache_policy
, false);
1939 const char *atomic_subop
= "";
1940 switch (a
->opcode
) {
1941 case ac_image_sample
: name
= "sample"; break;
1942 case ac_image_gather4
: name
= "gather4"; break;
1943 case ac_image_load
: name
= "load"; break;
1944 case ac_image_load_mip
: name
= "load.mip"; break;
1945 case ac_image_store
: name
= "store"; break;
1946 case ac_image_store_mip
: name
= "store.mip"; break;
1947 case ac_image_atomic
:
1949 atomic_subop
= get_atomic_name(a
->atomic
);
1951 case ac_image_atomic_cmpswap
:
1953 atomic_subop
= "cmpswap";
1955 case ac_image_get_lod
: name
= "getlod"; break;
1956 case ac_image_get_resinfo
: name
= "getresinfo"; break;
1957 default: unreachable("invalid image opcode");
1960 const char *dimname
;
1962 case ac_image_1d
: dimname
= "1d"; break;
1963 case ac_image_2d
: dimname
= "2d"; break;
1964 case ac_image_3d
: dimname
= "3d"; break;
1965 case ac_image_cube
: dimname
= "cube"; break;
1966 case ac_image_1darray
: dimname
= "1darray"; break;
1967 case ac_image_2darray
: dimname
= "2darray"; break;
1968 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
1969 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
1970 default: unreachable("invalid dim");
1974 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
1976 snprintf(intr_name
, sizeof(intr_name
),
1977 "llvm.amdgcn.image.%s%s" /* base name */
1978 "%s%s%s" /* sample/gather modifiers */
1979 ".%s.%s%s%s%s", /* dimension and type overloads */
1981 a
->compare
? ".c" : "",
1984 a
->derivs
[0] ? ".d" :
1985 a
->level_zero
? ".lz" : "",
1986 a
->offset
? ".o" : "",
1988 atomic
? "i32" : "v4f32",
1989 overload
[0], overload
[1], overload
[2]);
1994 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
1999 LLVMValueRef result
=
2000 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2002 if (!sample
&& retty
== ctx
->v4f32
) {
2003 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2009 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2010 LLVMValueRef args
[2])
2013 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2015 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2016 args
, 2, AC_FUNC_ATTR_READNONE
);
2019 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2020 LLVMValueRef args
[2])
2023 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2024 ctx
->v2i16
, args
, 2,
2025 AC_FUNC_ATTR_READNONE
);
2026 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2029 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2030 LLVMValueRef args
[2])
2033 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2034 ctx
->v2i16
, args
, 2,
2035 AC_FUNC_ATTR_READNONE
);
2036 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2039 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2040 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2041 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2043 assert(bits
== 8 || bits
== 10 || bits
== 16);
2045 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2046 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2047 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2048 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2049 LLVMValueRef max_alpha
=
2050 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2051 LLVMValueRef min_alpha
=
2052 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2056 for (int i
= 0; i
< 2; i
++) {
2057 bool alpha
= hi
&& i
== 1;
2058 args
[i
] = ac_build_imin(ctx
, args
[i
],
2059 alpha
? max_alpha
: max_rgb
);
2060 args
[i
] = ac_build_imax(ctx
, args
[i
],
2061 alpha
? min_alpha
: min_rgb
);
2066 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2067 ctx
->v2i16
, args
, 2,
2068 AC_FUNC_ATTR_READNONE
);
2069 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2072 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2073 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2074 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2076 assert(bits
== 8 || bits
== 10 || bits
== 16);
2078 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2079 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2080 LLVMValueRef max_alpha
=
2081 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2085 for (int i
= 0; i
< 2; i
++) {
2086 bool alpha
= hi
&& i
== 1;
2087 args
[i
] = ac_build_umin(ctx
, args
[i
],
2088 alpha
? max_alpha
: max_rgb
);
2093 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2094 ctx
->v2i16
, args
, 2,
2095 AC_FUNC_ATTR_READNONE
);
2096 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2099 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2101 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2102 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2105 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2107 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2111 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2112 LLVMValueRef offset
, LLVMValueRef width
,
2115 LLVMValueRef args
[] = {
2121 return ac_build_intrinsic(ctx
,
2122 is_signed
? "llvm.amdgcn.sbfe.i32" :
2123 "llvm.amdgcn.ubfe.i32",
2125 AC_FUNC_ATTR_READNONE
);
2128 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2129 LLVMValueRef s1
, LLVMValueRef s2
)
2131 return LLVMBuildAdd(ctx
->builder
,
2132 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2135 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2136 LLVMValueRef s1
, LLVMValueRef s2
)
2138 return LLVMBuildFAdd(ctx
->builder
,
2139 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2142 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
2144 LLVMValueRef args
[1] = {
2145 LLVMConstInt(ctx
->i32
, simm16
, false),
2147 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2148 ctx
->voidt
, args
, 1, 0);
2151 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2157 if (bitsize
== 32) {
2158 intr
= "llvm.floor.f32";
2161 intr
= "llvm.floor.f64";
2165 LLVMValueRef params
[] = {
2168 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2169 AC_FUNC_ATTR_READNONE
);
2170 return LLVMBuildFSub(ctx
->builder
, src0
, floor
, "");
2173 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2176 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2177 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2178 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2180 LLVMValueRef cmp
, val
;
2181 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2182 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2183 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2184 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2188 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2191 LLVMValueRef cmp
, val
, zero
, one
;
2194 if (bitsize
== 32) {
2204 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2205 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2206 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2207 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2211 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2213 LLVMValueRef result
;
2216 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2220 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2221 (LLVMValueRef
[]) { src0
}, 1,
2222 AC_FUNC_ATTR_READNONE
);
2224 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2227 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2228 (LLVMValueRef
[]) { src0
}, 1,
2229 AC_FUNC_ATTR_READNONE
);
2232 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2233 (LLVMValueRef
[]) { src0
}, 1,
2234 AC_FUNC_ATTR_READNONE
);
2237 unreachable(!"invalid bitsize");
2244 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2247 LLVMValueRef result
;
2250 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2254 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2255 (LLVMValueRef
[]) { src0
}, 1,
2256 AC_FUNC_ATTR_READNONE
);
2259 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2260 (LLVMValueRef
[]) { src0
}, 1,
2261 AC_FUNC_ATTR_READNONE
);
2264 unreachable(!"invalid bitsize");
2271 #define AC_EXP_TARGET 0
2272 #define AC_EXP_ENABLED_CHANNELS 1
2273 #define AC_EXP_OUT0 2
2281 struct ac_vs_exp_chan
2285 enum ac_ir_type type
;
2288 struct ac_vs_exp_inst
{
2291 struct ac_vs_exp_chan chan
[4];
2294 struct ac_vs_exports
{
2296 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2299 /* Return true if the PARAM export has been eliminated. */
2300 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2301 uint32_t num_outputs
,
2302 struct ac_vs_exp_inst
*exp
)
2304 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2305 bool is_zero
[4] = {}, is_one
[4] = {};
2307 for (i
= 0; i
< 4; i
++) {
2308 /* It's a constant expression. Undef outputs are eliminated too. */
2309 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2312 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2313 if (exp
->chan
[i
].const_float
== 0)
2315 else if (exp
->chan
[i
].const_float
== 1)
2318 return false; /* other constant */
2323 /* Only certain combinations of 0 and 1 can be eliminated. */
2324 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2325 default_val
= is_zero
[3] ? 0 : 1;
2326 else if (is_one
[0] && is_one
[1] && is_one
[2])
2327 default_val
= is_zero
[3] ? 2 : 3;
2331 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2332 LLVMInstructionEraseFromParent(exp
->inst
);
2334 /* Change OFFSET to DEFAULT_VAL. */
2335 for (i
= 0; i
< num_outputs
; i
++) {
2336 if (vs_output_param_offset
[i
] == exp
->offset
) {
2337 vs_output_param_offset
[i
] =
2338 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2345 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2346 uint8_t *vs_output_param_offset
,
2347 uint32_t num_outputs
,
2348 struct ac_vs_exports
*processed
,
2349 struct ac_vs_exp_inst
*exp
)
2351 unsigned p
, copy_back_channels
= 0;
2353 /* See if the output is already in the list of processed outputs.
2354 * The LLVMValueRef comparison relies on SSA.
2356 for (p
= 0; p
< processed
->num
; p
++) {
2357 bool different
= false;
2359 for (unsigned j
= 0; j
< 4; j
++) {
2360 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2361 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2363 /* Treat undef as a match. */
2364 if (c2
->type
== AC_IR_UNDEF
)
2367 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2368 * and consider the instruction duplicated.
2370 if (c1
->type
== AC_IR_UNDEF
) {
2371 copy_back_channels
|= 1 << j
;
2375 /* Test whether the channels are not equal. */
2376 if (c1
->type
!= c2
->type
||
2377 (c1
->type
== AC_IR_CONST
&&
2378 c1
->const_float
!= c2
->const_float
) ||
2379 (c1
->type
== AC_IR_VALUE
&&
2380 c1
->value
!= c2
->value
)) {
2388 copy_back_channels
= 0;
2390 if (p
== processed
->num
)
2393 /* If a match was found, but the matching export has undef where the new
2394 * one has a normal value, copy the normal value to the undef channel.
2396 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
2398 /* Get current enabled channels mask. */
2399 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
2400 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
2402 while (copy_back_channels
) {
2403 unsigned chan
= u_bit_scan(©_back_channels
);
2405 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
2406 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
2407 exp
->chan
[chan
].value
);
2408 match
->chan
[chan
] = exp
->chan
[chan
];
2410 /* Update number of enabled channels because the original mask
2411 * is not always 0xf.
2413 enabled_channels
|= (1 << chan
);
2414 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
2415 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
2418 /* The PARAM export is duplicated. Kill it. */
2419 LLVMInstructionEraseFromParent(exp
->inst
);
2421 /* Change OFFSET to the matching export. */
2422 for (unsigned i
= 0; i
< num_outputs
; i
++) {
2423 if (vs_output_param_offset
[i
] == exp
->offset
) {
2424 vs_output_param_offset
[i
] = match
->offset
;
2431 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
2432 LLVMValueRef main_fn
,
2433 uint8_t *vs_output_param_offset
,
2434 uint32_t num_outputs
,
2435 uint8_t *num_param_exports
)
2437 LLVMBasicBlockRef bb
;
2438 bool removed_any
= false;
2439 struct ac_vs_exports exports
;
2443 /* Process all LLVM instructions. */
2444 bb
= LLVMGetFirstBasicBlock(main_fn
);
2446 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
2449 LLVMValueRef cur
= inst
;
2450 inst
= LLVMGetNextInstruction(inst
);
2451 struct ac_vs_exp_inst exp
;
2453 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
2456 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
2458 if (!ac_llvm_is_function(callee
))
2461 const char *name
= LLVMGetValueName(callee
);
2462 unsigned num_args
= LLVMCountParams(callee
);
2464 /* Check if this is an export instruction. */
2465 if ((num_args
!= 9 && num_args
!= 8) ||
2466 (strcmp(name
, "llvm.SI.export") &&
2467 strcmp(name
, "llvm.amdgcn.exp.f32")))
2470 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
2471 unsigned target
= LLVMConstIntGetZExtValue(arg
);
2473 if (target
< V_008DFC_SQ_EXP_PARAM
)
2476 target
-= V_008DFC_SQ_EXP_PARAM
;
2478 /* Parse the instruction. */
2479 memset(&exp
, 0, sizeof(exp
));
2480 exp
.offset
= target
;
2483 for (unsigned i
= 0; i
< 4; i
++) {
2484 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
2486 exp
.chan
[i
].value
= v
;
2488 if (LLVMIsUndef(v
)) {
2489 exp
.chan
[i
].type
= AC_IR_UNDEF
;
2490 } else if (LLVMIsAConstantFP(v
)) {
2491 LLVMBool loses_info
;
2492 exp
.chan
[i
].type
= AC_IR_CONST
;
2493 exp
.chan
[i
].const_float
=
2494 LLVMConstRealGetDouble(v
, &loses_info
);
2496 exp
.chan
[i
].type
= AC_IR_VALUE
;
2500 /* Eliminate constant and duplicated PARAM exports. */
2501 if (ac_eliminate_const_output(vs_output_param_offset
,
2502 num_outputs
, &exp
) ||
2503 ac_eliminate_duplicated_output(ctx
,
2504 vs_output_param_offset
,
2505 num_outputs
, &exports
,
2509 exports
.exp
[exports
.num
++] = exp
;
2512 bb
= LLVMGetNextBasicBlock(bb
);
2515 /* Remove holes in export memory due to removed PARAM exports.
2516 * This is done by renumbering all PARAM exports.
2519 uint8_t old_offset
[VARYING_SLOT_MAX
];
2522 /* Make a copy of the offsets. We need the old version while
2523 * we are modifying some of them. */
2524 memcpy(old_offset
, vs_output_param_offset
,
2525 sizeof(old_offset
));
2527 for (i
= 0; i
< exports
.num
; i
++) {
2528 unsigned offset
= exports
.exp
[i
].offset
;
2530 /* Update vs_output_param_offset. Multiple outputs can
2531 * have the same offset.
2533 for (out
= 0; out
< num_outputs
; out
++) {
2534 if (old_offset
[out
] == offset
)
2535 vs_output_param_offset
[out
] = i
;
2538 /* Change the PARAM offset in the instruction. */
2539 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
2540 LLVMConstInt(ctx
->i32
,
2541 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
2543 *num_param_exports
= exports
.num
;
2547 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
2549 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
2550 ac_build_intrinsic(ctx
,
2551 "llvm.amdgcn.init.exec", ctx
->voidt
,
2552 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
2555 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
2557 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
2558 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
2559 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
2563 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
2564 LLVMValueRef dw_addr
)
2566 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
2569 void ac_lds_store(struct ac_llvm_context
*ctx
,
2570 LLVMValueRef dw_addr
,
2573 value
= ac_to_integer(ctx
, value
);
2574 ac_build_indexed_store(ctx
, ctx
->lds
,
2578 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
2579 LLVMTypeRef dst_type
,
2582 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2583 const char *intrin_name
;
2587 switch (src0_bitsize
) {
2589 intrin_name
= "llvm.cttz.i64";
2594 intrin_name
= "llvm.cttz.i32";
2599 intrin_name
= "llvm.cttz.i16";
2604 unreachable(!"invalid bitsize");
2607 LLVMValueRef params
[2] = {
2610 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2611 * add special code to check for x=0. The reason is that
2612 * the LLVM behavior for x=0 is different from what we
2613 * need here. However, LLVM also assumes that ffs(x) is
2614 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2615 * a conditional assignment to handle 0 is still required.
2617 * The hardware already implements the correct behavior.
2622 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2624 AC_FUNC_ATTR_READNONE
);
2626 if (src0_bitsize
== 64) {
2627 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
2630 /* TODO: We need an intrinsic to skip this conditional. */
2631 /* Check for zero: */
2632 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2635 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2638 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2640 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2641 AC_ADDR_SPACE_CONST
);
2644 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
2646 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2647 AC_ADDR_SPACE_CONST_32BIT
);
2650 static struct ac_llvm_flow
*
2651 get_current_flow(struct ac_llvm_context
*ctx
)
2653 if (ctx
->flow_depth
> 0)
2654 return &ctx
->flow
[ctx
->flow_depth
- 1];
2658 static struct ac_llvm_flow
*
2659 get_innermost_loop(struct ac_llvm_context
*ctx
)
2661 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
2662 if (ctx
->flow
[i
- 1].loop_entry_block
)
2663 return &ctx
->flow
[i
- 1];
2668 static struct ac_llvm_flow
*
2669 push_flow(struct ac_llvm_context
*ctx
)
2671 struct ac_llvm_flow
*flow
;
2673 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
2674 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
2675 AC_LLVM_INITIAL_CF_DEPTH
);
2677 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
2678 ctx
->flow_depth_max
= new_max
;
2681 flow
= &ctx
->flow
[ctx
->flow_depth
];
2684 flow
->next_block
= NULL
;
2685 flow
->loop_entry_block
= NULL
;
2689 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
2693 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
2694 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
2697 /* Append a basic block at the level of the parent flow.
2699 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
2702 assert(ctx
->flow_depth
>= 1);
2704 if (ctx
->flow_depth
>= 2) {
2705 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
2707 return LLVMInsertBasicBlockInContext(ctx
->context
,
2708 flow
->next_block
, name
);
2711 LLVMValueRef main_fn
=
2712 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
2713 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
2716 /* Emit a branch to the given default target for the current block if
2717 * applicable -- that is, if the current block does not already contain a
2718 * branch from a break or continue.
2720 static void emit_default_branch(LLVMBuilderRef builder
,
2721 LLVMBasicBlockRef target
)
2723 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
2724 LLVMBuildBr(builder
, target
);
2727 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
2729 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2730 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
2731 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
2732 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
2733 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2734 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
2737 void ac_build_break(struct ac_llvm_context
*ctx
)
2739 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2740 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
2743 void ac_build_continue(struct ac_llvm_context
*ctx
)
2745 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2746 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2749 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
2751 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2752 LLVMBasicBlockRef endif_block
;
2754 assert(!current_branch
->loop_entry_block
);
2756 endif_block
= append_basic_block(ctx
, "ENDIF");
2757 emit_default_branch(ctx
->builder
, endif_block
);
2759 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2760 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
2762 current_branch
->next_block
= endif_block
;
2765 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
2767 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2769 assert(!current_branch
->loop_entry_block
);
2771 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
2772 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2773 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
2778 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
2780 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
2782 assert(current_loop
->loop_entry_block
);
2784 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
2786 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
2787 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
2791 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
2793 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2794 LLVMBasicBlockRef if_block
;
2796 if_block
= append_basic_block(ctx
, "IF");
2797 flow
->next_block
= append_basic_block(ctx
, "ELSE");
2798 set_basicblock_name(if_block
, "if", label_id
);
2799 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
2800 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
2803 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2806 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
2807 value
, ctx
->f32_0
, "");
2808 ac_build_ifcc(ctx
, cond
, label_id
);
2811 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2814 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2815 ac_to_integer(ctx
, value
),
2817 ac_build_ifcc(ctx
, cond
, label_id
);
2820 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
2823 LLVMBuilderRef builder
= ac
->builder
;
2824 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
2825 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
2826 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
2827 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
2828 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
2832 LLVMPositionBuilderBefore(first_builder
, first_instr
);
2834 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
2837 res
= LLVMBuildAlloca(first_builder
, type
, name
);
2838 LLVMDisposeBuilder(first_builder
);
2842 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
2843 LLVMTypeRef type
, const char *name
)
2845 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
2846 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
2850 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
2853 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2854 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2855 LLVMPointerType(type
, addr_space
), "");
2858 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2861 unsigned num_components
= ac_get_llvm_num_components(value
);
2862 if (count
== num_components
)
2865 LLVMValueRef masks
[MAX2(count
, 2)];
2866 masks
[0] = ctx
->i32_0
;
2867 masks
[1] = ctx
->i32_1
;
2868 for (unsigned i
= 2; i
< count
; i
++)
2869 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
2872 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
2875 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
2876 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
2879 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
2880 unsigned rshift
, unsigned bitwidth
)
2882 LLVMValueRef value
= param
;
2884 value
= LLVMBuildLShr(ctx
->builder
, value
,
2885 LLVMConstInt(ctx
->i32
, rshift
, false), "");
2887 if (rshift
+ bitwidth
< 32) {
2888 unsigned mask
= (1 << bitwidth
) - 1;
2889 value
= LLVMBuildAnd(ctx
->builder
, value
,
2890 LLVMConstInt(ctx
->i32
, mask
, false), "");
2895 /* Adjust the sample index according to FMASK.
2897 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2898 * which is the identity mapping. Each nibble says which physical sample
2899 * should be fetched to get that sample.
2901 * For example, 0x11111100 means there are only 2 samples stored and
2902 * the second sample covers 3/4 of the pixel. When reading samples 0
2903 * and 1, return physical sample 0 (determined by the first two 0s
2904 * in FMASK), otherwise return physical sample 1.
2906 * The sample index should be adjusted as follows:
2907 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
2909 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
2910 LLVMValueRef
*addr
, bool is_array_tex
)
2912 struct ac_image_args fmask_load
= {};
2913 fmask_load
.opcode
= ac_image_load
;
2914 fmask_load
.resource
= fmask
;
2915 fmask_load
.dmask
= 0xf;
2916 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
2918 fmask_load
.coords
[0] = addr
[0];
2919 fmask_load
.coords
[1] = addr
[1];
2921 fmask_load
.coords
[2] = addr
[2];
2923 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
2924 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
2927 /* Apply the formula. */
2928 unsigned sample_chan
= is_array_tex
? 3 : 2;
2929 LLVMValueRef final_sample
;
2930 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
2931 LLVMConstInt(ac
->i32
, 4, 0), "");
2932 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
2933 /* Mask the sample index by 0x7, because 0x8 means an unknown value
2934 * with EQAA, so those will map to 0. */
2935 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
2936 LLVMConstInt(ac
->i32
, 0x7, 0), "");
2938 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2939 * resource descriptor is 0 (invalid).
2942 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
2943 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
2944 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
2946 /* Replace the MSAA sample index. */
2947 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
2948 addr
[sample_chan
], "");
2952 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2954 ac_build_optimization_barrier(ctx
, &src
);
2955 return ac_build_intrinsic(ctx
,
2956 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
2957 LLVMTypeOf(src
), (LLVMValueRef
[]) {
2959 lane
== NULL
? 1 : 2,
2960 AC_FUNC_ATTR_READNONE
|
2961 AC_FUNC_ATTR_CONVERGENT
);
2965 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
2968 * @param lane - id of the lane or NULL for the first active lane
2969 * @return value of the lane
2972 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2974 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2975 src
= ac_to_integer(ctx
, src
);
2976 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2980 ret
= _ac_build_readlane(ctx
, src
, lane
);
2982 assert(bits
% 32 == 0);
2983 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2984 LLVMValueRef src_vector
=
2985 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2986 ret
= LLVMGetUndef(vec_type
);
2987 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2988 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2989 LLVMConstInt(ctx
->i32
, i
, 0), "");
2990 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
2991 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
2992 LLVMConstInt(ctx
->i32
, i
, 0), "");
2995 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
2999 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3001 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
3003 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
3004 ac_get_thread_id(ctx
), "");
3005 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
3009 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3011 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3012 LLVMVectorType(ctx
->i32
, 2),
3014 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3016 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3019 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3020 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3021 2, AC_FUNC_ATTR_READNONE
);
3022 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3023 (LLVMValueRef
[]) { mask_hi
, val
},
3024 2, AC_FUNC_ATTR_READNONE
);
3029 _dpp_quad_perm
= 0x000,
3030 _dpp_row_sl
= 0x100,
3031 _dpp_row_sr
= 0x110,
3032 _dpp_row_rr
= 0x120,
3037 dpp_row_mirror
= 0x140,
3038 dpp_row_half_mirror
= 0x141,
3039 dpp_row_bcast15
= 0x142,
3040 dpp_row_bcast31
= 0x143
3043 static inline enum dpp_ctrl
3044 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3046 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3047 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3050 static inline enum dpp_ctrl
3051 dpp_row_sl(unsigned amount
)
3053 assert(amount
> 0 && amount
< 16);
3054 return _dpp_row_sl
| amount
;
3057 static inline enum dpp_ctrl
3058 dpp_row_sr(unsigned amount
)
3060 assert(amount
> 0 && amount
< 16);
3061 return _dpp_row_sr
| amount
;
3065 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3066 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3069 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3073 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3074 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3075 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3076 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3077 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3081 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3082 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3085 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3086 src
= ac_to_integer(ctx
, src
);
3087 old
= ac_to_integer(ctx
, old
);
3088 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3091 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3092 bank_mask
, bound_ctrl
);
3094 assert(bits
% 32 == 0);
3095 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3096 LLVMValueRef src_vector
=
3097 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3098 LLVMValueRef old_vector
=
3099 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3100 ret
= LLVMGetUndef(vec_type
);
3101 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3102 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3103 LLVMConstInt(ctx
->i32
, i
,
3105 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3106 LLVMConstInt(ctx
->i32
, i
,
3108 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3113 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3115 LLVMConstInt(ctx
->i32
, i
,
3119 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3122 static inline unsigned
3123 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3125 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3126 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3130 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3132 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3133 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3134 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3135 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3139 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3141 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3142 src
= ac_to_integer(ctx
, src
);
3143 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3146 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3148 assert(bits
% 32 == 0);
3149 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3150 LLVMValueRef src_vector
=
3151 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3152 ret
= LLVMGetUndef(vec_type
);
3153 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3154 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3155 LLVMConstInt(ctx
->i32
, i
,
3157 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3159 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3161 LLVMConstInt(ctx
->i32
, i
,
3165 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3169 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3171 char name
[32], type
[8];
3172 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3173 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3174 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3175 (LLVMValueRef
[]) { src
}, 1,
3176 AC_FUNC_ATTR_READNONE
);
3180 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3181 LLVMValueRef inactive
)
3183 char name
[33], type
[8];
3184 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3185 src
= ac_to_integer(ctx
, src
);
3186 inactive
= ac_to_integer(ctx
, inactive
);
3187 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3188 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3190 ac_build_intrinsic(ctx
, name
,
3191 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3193 AC_FUNC_ATTR_READNONE
|
3194 AC_FUNC_ATTR_CONVERGENT
);
3195 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3199 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3201 if (type_size
== 4) {
3203 case nir_op_iadd
: return ctx
->i32_0
;
3204 case nir_op_fadd
: return ctx
->f32_0
;
3205 case nir_op_imul
: return ctx
->i32_1
;
3206 case nir_op_fmul
: return ctx
->f32_1
;
3207 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3208 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3209 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3210 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3211 case nir_op_umax
: return ctx
->i32_0
;
3212 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3213 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3214 case nir_op_ior
: return ctx
->i32_0
;
3215 case nir_op_ixor
: return ctx
->i32_0
;
3217 unreachable("bad reduction intrinsic");
3219 } else { /* type_size == 64bit */
3221 case nir_op_iadd
: return ctx
->i64_0
;
3222 case nir_op_fadd
: return ctx
->f64_0
;
3223 case nir_op_imul
: return ctx
->i64_1
;
3224 case nir_op_fmul
: return ctx
->f64_1
;
3225 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3226 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3227 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3228 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3229 case nir_op_umax
: return ctx
->i64_0
;
3230 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3231 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3232 case nir_op_ior
: return ctx
->i64_0
;
3233 case nir_op_ixor
: return ctx
->i64_0
;
3235 unreachable("bad reduction intrinsic");
3241 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3243 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3245 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3246 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3247 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3248 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3249 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3250 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3252 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3253 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3255 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3256 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3257 _64bit
? ctx
->f64
: ctx
->f32
,
3258 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3259 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3260 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3262 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3263 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3265 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3266 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3267 _64bit
? ctx
->f64
: ctx
->f32
,
3268 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3269 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3270 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3271 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3273 unreachable("bad reduction intrinsic");
3278 * \param maxprefix specifies that the result only needs to be correct for a
3279 * prefix of this many threads
3281 * TODO: add inclusive and excluse scan functions for SI chip class.
3284 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
3287 LLVMValueRef result
, tmp
;
3291 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3292 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3295 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3296 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3299 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3300 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3303 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
3304 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3307 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
3308 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3309 if (maxprefix
<= 16)
3311 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3312 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3313 if (maxprefix
<= 32)
3315 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3316 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3321 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3323 LLVMValueRef result
;
3325 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
3326 LLVMBuilderRef builder
= ctx
->builder
;
3327 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
3328 result
= ac_build_ballot(ctx
, src
);
3329 result
= ac_build_mbcnt(ctx
, result
);
3330 result
= LLVMBuildAdd(builder
, result
, src
, "");
3334 ac_build_optimization_barrier(ctx
, &src
);
3336 LLVMValueRef identity
=
3337 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
3338 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
3339 LLVMTypeOf(identity
), "");
3340 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
3342 return ac_build_wwm(ctx
, result
);
3346 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3348 LLVMValueRef result
;
3350 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
3351 LLVMBuilderRef builder
= ctx
->builder
;
3352 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
3353 result
= ac_build_ballot(ctx
, src
);
3354 result
= ac_build_mbcnt(ctx
, result
);
3358 ac_build_optimization_barrier(ctx
, &src
);
3360 LLVMValueRef identity
=
3361 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
3362 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
3363 LLVMTypeOf(identity
), "");
3364 result
= ac_build_dpp(ctx
, identity
, result
, dpp_wf_sr1
, 0xf, 0xf, false);
3365 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
3367 return ac_build_wwm(ctx
, result
);
3371 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
3373 if (cluster_size
== 1) return src
;
3374 ac_build_optimization_barrier(ctx
, &src
);
3375 LLVMValueRef result
, swap
;
3376 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
3377 ac_get_type_size(LLVMTypeOf(src
)));
3378 result
= LLVMBuildBitCast(ctx
->builder
,
3379 ac_build_set_inactive(ctx
, src
, identity
),
3380 LLVMTypeOf(identity
), "");
3381 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
3382 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3383 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
3385 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
3386 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3387 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
3389 if (ctx
->chip_class
>= VI
)
3390 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
3392 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
3393 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3394 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
3396 if (ctx
->chip_class
>= VI
)
3397 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
3399 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
3400 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3401 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
3403 if (ctx
->chip_class
>= VI
&& cluster_size
!= 32)
3404 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3406 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
3407 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3408 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
3410 if (ctx
->chip_class
>= VI
) {
3411 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3412 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3413 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
3414 return ac_build_wwm(ctx
, result
);
3416 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
3417 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
3418 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3419 return ac_build_wwm(ctx
, result
);
3424 * "Top half" of a scan that reduces per-wave values across an entire
3427 * The source value must be present in the highest lane of the wave, and the
3428 * highest lane must be live.
3431 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3433 if (ws
->maxwaves
<= 1)
3436 const LLVMValueRef i32_63
= LLVMConstInt(ctx
->i32
, 63, false);
3437 LLVMBuilderRef builder
= ctx
->builder
;
3438 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3441 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, i32_63
, "");
3442 ac_build_ifcc(ctx
, tmp
, 1000);
3443 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
3444 ac_build_endif(ctx
, 1000);
3448 * "Bottom half" of a scan that reduces per-wave values across an entire
3451 * The caller must place a barrier between the top and bottom halves.
3454 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3456 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
3457 const LLVMValueRef identity
=
3458 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
3460 if (ws
->maxwaves
<= 1) {
3461 ws
->result_reduce
= ws
->src
;
3462 ws
->result_inclusive
= ws
->src
;
3463 ws
->result_exclusive
= identity
;
3466 assert(ws
->maxwaves
<= 32);
3468 LLVMBuilderRef builder
= ctx
->builder
;
3469 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3470 LLVMBasicBlockRef bbs
[2];
3471 LLVMValueRef phivalues_scan
[2];
3472 LLVMValueRef tmp
, tmp2
;
3474 bbs
[0] = LLVMGetInsertBlock(builder
);
3475 phivalues_scan
[0] = LLVMGetUndef(type
);
3477 if (ws
->enable_reduce
)
3478 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
3479 else if (ws
->enable_inclusive
)
3480 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
3482 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
3483 ac_build_ifcc(ctx
, tmp
, 1001);
3485 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
3487 ac_build_optimization_barrier(ctx
, &tmp
);
3489 bbs
[1] = LLVMGetInsertBlock(builder
);
3490 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
);
3492 ac_build_endif(ctx
, 1001);
3494 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
3496 if (ws
->enable_reduce
) {
3497 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
3498 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
3500 if (ws
->enable_inclusive
)
3501 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
3502 if (ws
->enable_exclusive
) {
3503 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
3504 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
3505 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
3506 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
3511 * Inclusive scan of a per-wave value across an entire workgroup.
3513 * This implies an s_barrier instruction.
3515 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
3516 * of the workgroup are live. (This requirement cannot easily be relaxed in a
3517 * useful manner because of the barrier in the algorithm.)
3520 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3522 ac_build_wg_wavescan_top(ctx
, ws
);
3523 ac_build_s_barrier(ctx
);
3524 ac_build_wg_wavescan_bottom(ctx
, ws
);
3528 * "Top half" of a scan that reduces per-thread values across an entire
3531 * All lanes must be active when this code runs.
3534 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3536 if (ws
->enable_exclusive
) {
3537 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
3538 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
3539 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
3540 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
3542 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
3545 bool enable_inclusive
= ws
->enable_inclusive
;
3546 bool enable_exclusive
= ws
->enable_exclusive
;
3547 ws
->enable_inclusive
= false;
3548 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
3549 ac_build_wg_wavescan_top(ctx
, ws
);
3550 ws
->enable_inclusive
= enable_inclusive
;
3551 ws
->enable_exclusive
= enable_exclusive
;
3555 * "Bottom half" of a scan that reduces per-thread values across an entire
3558 * The caller must place a barrier between the top and bottom halves.
3561 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3563 bool enable_inclusive
= ws
->enable_inclusive
;
3564 bool enable_exclusive
= ws
->enable_exclusive
;
3565 ws
->enable_inclusive
= false;
3566 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
3567 ac_build_wg_wavescan_bottom(ctx
, ws
);
3568 ws
->enable_inclusive
= enable_inclusive
;
3569 ws
->enable_exclusive
= enable_exclusive
;
3571 /* ws->result_reduce is already the correct value */
3572 if (ws
->enable_inclusive
)
3573 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->src
, ws
->op
);
3574 if (ws
->enable_exclusive
)
3575 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
3579 * A scan that reduces per-thread values across an entire workgroup.
3581 * The caller must ensure that all lanes are active when this code runs
3582 * (WWM is insufficient!), because there is an implied barrier.
3585 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3587 ac_build_wg_scan_top(ctx
, ws
);
3588 ac_build_s_barrier(ctx
);
3589 ac_build_wg_scan_bottom(ctx
, ws
);
3593 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3594 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3596 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
3597 if (ctx
->chip_class
>= VI
) {
3598 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
3600 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
3605 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
3607 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
3608 return ac_build_intrinsic(ctx
,
3609 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
3610 (LLVMValueRef
[]) {index
, src
}, 2,
3611 AC_FUNC_ATTR_READNONE
|
3612 AC_FUNC_ATTR_CONVERGENT
);